dbus-string.h revision bef4260ad58bc9eb75e2e1a52ad9b49bc3c70fa5
1/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ 2/* dbus-string.h String utility class (internal to D-Bus implementation) 3 * 4 * Copyright (C) 2002, 2003 Red Hat, Inc. 5 * Copyright (C) 2006 Ralf Habacker <ralf.habacker@freenet.de> 6 * 7 * Licensed under the Academic Free License version 2.1 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 * 23 */ 24 25#ifndef DBUS_STRING_H 26#define DBUS_STRING_H 27 28#include <config.h> 29 30#include <dbus/dbus-memory.h> 31#include <dbus/dbus-types.h> 32#include <dbus/dbus-sysdeps.h> 33 34#include <stdarg.h> 35 36DBUS_BEGIN_DECLS 37 38/** 39 * DBusString object 40 */ 41struct DBusString 42{ 43 const void *dummy1; /**< placeholder */ 44 int dummy2; /**< placeholder */ 45 int dummy3; /**< placeholder */ 46 int dummy4; /**< placeholder */ 47 unsigned int dummy5 : 1; /**< placeholder */ 48 unsigned int dummy6 : 1; /**< placeholder */ 49 unsigned int dummy7 : 1; /**< placeholder */ 50 unsigned int dummy8 : 3; /**< placeholder */ 51}; 52 53#ifdef DBUS_DISABLE_ASSERT 54/* Some simple inlining hacks; the current linker is not smart enough 55 * to inline non-exported symbols across files in the library. 56 * Note that these break type safety (due to the casts) 57 */ 58#define _dbus_string_get_data(s) ((char*)(((DBusString*)(s))->dummy1)) 59#define _dbus_string_get_length(s) (((DBusString*)(s))->dummy2) 60#define _dbus_string_set_byte(s, i, b) ((((unsigned char*)(((DBusString*)(s))->dummy1))[(i)]) = (unsigned char) (b)) 61#define _dbus_string_get_byte(s, i) (((const unsigned char*)(((DBusString*)(s))->dummy1))[(i)]) 62#define _dbus_string_get_const_data(s) ((const char*)(((DBusString*)(s))->dummy1)) 63#define _dbus_string_get_const_data_len(s,start,len) (((const char*)(((DBusString*)(s))->dummy1)) + (start)) 64#endif 65 66dbus_bool_t _dbus_string_init (DBusString *str); 67void _dbus_string_init_const (DBusString *str, 68 const char *value); 69void _dbus_string_init_const_len (DBusString *str, 70 const char *value, 71 int len); 72dbus_bool_t _dbus_string_init_preallocated (DBusString *str, 73 int allocate_size); 74void _dbus_string_free (DBusString *str); 75void _dbus_string_lock (DBusString *str); 76dbus_bool_t _dbus_string_compact (DBusString *str, 77 int max_waste); 78#ifndef _dbus_string_get_data 79char* _dbus_string_get_data (DBusString *str); 80#endif /* _dbus_string_get_data */ 81#ifndef _dbus_string_get_const_data 82const char* _dbus_string_get_const_data (const DBusString *str); 83#endif /* _dbus_string_get_const_data */ 84char* _dbus_string_get_data_len (DBusString *str, 85 int start, 86 int len); 87#ifndef _dbus_string_get_const_data_len 88const char* _dbus_string_get_const_data_len (const DBusString *str, 89 int start, 90 int len); 91#endif 92#ifndef _dbus_string_set_byte 93void _dbus_string_set_byte (DBusString *str, 94 int i, 95 unsigned char byte); 96#endif 97#ifndef _dbus_string_get_byte 98unsigned char _dbus_string_get_byte (const DBusString *str, 99 int start); 100#endif /* _dbus_string_get_byte */ 101dbus_bool_t _dbus_string_insert_bytes (DBusString *str, 102 int i, 103 int n_bytes, 104 unsigned char byte); 105dbus_bool_t _dbus_string_insert_byte (DBusString *str, 106 int i, 107 unsigned char byte); 108dbus_bool_t _dbus_string_steal_data (DBusString *str, 109 char **data_return); 110dbus_bool_t _dbus_string_steal_data_len (DBusString *str, 111 char **data_return, 112 int start, 113 int len); 114dbus_bool_t _dbus_string_copy_data (const DBusString *str, 115 char **data_return); 116dbus_bool_t _dbus_string_copy_data_len (const DBusString *str, 117 char **data_return, 118 int start, 119 int len); 120void _dbus_string_copy_to_buffer (const DBusString *str, 121 char *buffer, 122 int len); 123void _dbus_string_copy_to_buffer_with_nul (const DBusString *str, 124 char *buffer, 125 int avail_len); 126#ifndef _dbus_string_get_length 127int _dbus_string_get_length (const DBusString *str); 128#endif /* !_dbus_string_get_length */ 129 130dbus_bool_t _dbus_string_lengthen (DBusString *str, 131 int additional_length); 132void _dbus_string_shorten (DBusString *str, 133 int length_to_remove); 134dbus_bool_t _dbus_string_set_length (DBusString *str, 135 int length); 136dbus_bool_t _dbus_string_align_length (DBusString *str, 137 int alignment); 138dbus_bool_t _dbus_string_alloc_space (DBusString *str, 139 int extra_bytes); 140dbus_bool_t _dbus_string_append (DBusString *str, 141 const char *buffer); 142dbus_bool_t _dbus_string_append_len (DBusString *str, 143 const char *buffer, 144 int len); 145dbus_bool_t _dbus_string_append_int (DBusString *str, 146 long value); 147dbus_bool_t _dbus_string_append_uint (DBusString *str, 148 unsigned long value); 149dbus_bool_t _dbus_string_append_double (DBusString *str, 150 double value); 151dbus_bool_t _dbus_string_append_byte (DBusString *str, 152 unsigned char byte); 153dbus_bool_t _dbus_string_append_unichar (DBusString *str, 154 dbus_unichar_t ch); 155dbus_bool_t _dbus_string_append_4_aligned (DBusString *str, 156 const unsigned char octets[4]); 157dbus_bool_t _dbus_string_append_8_aligned (DBusString *str, 158 const unsigned char octets[8]); 159dbus_bool_t _dbus_string_append_printf (DBusString *str, 160 const char *format, 161 ...) _DBUS_GNUC_PRINTF (2, 3); 162dbus_bool_t _dbus_string_append_printf_valist (DBusString *str, 163 const char *format, 164 va_list args); 165dbus_bool_t _dbus_string_insert_2_aligned (DBusString *str, 166 int insert_at, 167 const unsigned char octets[2]); 168dbus_bool_t _dbus_string_insert_4_aligned (DBusString *str, 169 int insert_at, 170 const unsigned char octets[4]); 171dbus_bool_t _dbus_string_insert_8_aligned (DBusString *str, 172 int insert_at, 173 const unsigned char octets[8]); 174dbus_bool_t _dbus_string_insert_alignment (DBusString *str, 175 int *insert_at, 176 int alignment); 177void _dbus_string_delete (DBusString *str, 178 int start, 179 int len); 180dbus_bool_t _dbus_string_move (DBusString *source, 181 int start, 182 DBusString *dest, 183 int insert_at); 184dbus_bool_t _dbus_string_copy (const DBusString *source, 185 int start, 186 DBusString *dest, 187 int insert_at); 188dbus_bool_t _dbus_string_move_len (DBusString *source, 189 int start, 190 int len, 191 DBusString *dest, 192 int insert_at); 193dbus_bool_t _dbus_string_copy_len (const DBusString *source, 194 int start, 195 int len, 196 DBusString *dest, 197 int insert_at); 198dbus_bool_t _dbus_string_replace_len (const DBusString *source, 199 int start, 200 int len, 201 DBusString *dest, 202 int replace_at, 203 int replace_len); 204void _dbus_string_get_unichar (const DBusString *str, 205 int start, 206 dbus_unichar_t *ch_return, 207 int *end_return); 208dbus_bool_t _dbus_string_parse_int (const DBusString *str, 209 int start, 210 long *value_return, 211 int *end_return); 212dbus_bool_t _dbus_string_parse_uint (const DBusString *str, 213 int start, 214 unsigned long *value_return, 215 int *end_return); 216dbus_bool_t _dbus_string_parse_double (const DBusString *str, 217 int start, 218 double *value, 219 int *end_return); 220dbus_bool_t _dbus_string_find (const DBusString *str, 221 int start, 222 const char *substr, 223 int *found); 224dbus_bool_t _dbus_string_find_eol (const DBusString *str, 225 int start, 226 int *found, 227 int *found_len); 228dbus_bool_t _dbus_string_find_to (const DBusString *str, 229 int start, 230 int end, 231 const char *substr, 232 int *found); 233dbus_bool_t _dbus_string_find_byte_backward (const DBusString *str, 234 int start, 235 unsigned char byte, 236 int *found); 237dbus_bool_t _dbus_string_find_blank (const DBusString *str, 238 int start, 239 int *found); 240void _dbus_string_skip_blank (const DBusString *str, 241 int start, 242 int *end); 243void _dbus_string_skip_white (const DBusString *str, 244 int start, 245 int *end); 246void _dbus_string_skip_white_reverse (const DBusString *str, 247 int end, 248 int *start); 249dbus_bool_t _dbus_string_equal (const DBusString *a, 250 const DBusString *b); 251dbus_bool_t _dbus_string_equal_c_str (const DBusString *a, 252 const char *c_str); 253dbus_bool_t _dbus_string_equal_len (const DBusString *a, 254 const DBusString *b, 255 int len); 256dbus_bool_t _dbus_string_equal_substring (const DBusString *a, 257 int a_start, 258 int a_len, 259 const DBusString *b, 260 int b_start); 261dbus_bool_t _dbus_string_starts_with_c_str (const DBusString *a, 262 const char *c_str); 263dbus_bool_t _dbus_string_ends_with_c_str (const DBusString *a, 264 const char *c_str); 265dbus_bool_t _dbus_string_pop_line (DBusString *source, 266 DBusString *dest); 267void _dbus_string_delete_first_word (DBusString *str); 268void _dbus_string_delete_leading_blanks (DBusString *str); 269void _dbus_string_chop_white (DBusString *str); 270dbus_bool_t _dbus_string_append_byte_as_hex (DBusString *str, 271 int byte); 272dbus_bool_t _dbus_string_hex_encode (const DBusString *source, 273 int start, 274 DBusString *dest, 275 int insert_at); 276dbus_bool_t _dbus_string_hex_decode (const DBusString *source, 277 int start, 278 int *end_return, 279 DBusString *dest, 280 int insert_at); 281dbus_bool_t _dbus_string_validate_ascii (const DBusString *str, 282 int start, 283 int len); 284dbus_bool_t _dbus_string_validate_utf8 (const DBusString *str, 285 int start, 286 int len); 287dbus_bool_t _dbus_string_validate_nul (const DBusString *str, 288 int start, 289 int len); 290void _dbus_string_zero (DBusString *str); 291 292 293/** 294 * We allocate 1 byte for nul termination, plus 7 bytes for possible 295 * align_offset, so we always need 8 bytes on top of the string's 296 * length to be in the allocated block. 297 */ 298#define _DBUS_STRING_ALLOCATION_PADDING 8 299 300/** 301 * Defines a static const variable with type #DBusString called "name" 302 * containing the given string literal. 303 * 304 * @param name the name of the variable 305 * @param str the string value 306 */ 307#define _DBUS_STRING_DEFINE_STATIC(name, str) \ 308 static const char _dbus_static_string_##name[] = str; \ 309 static const DBusString name = { _dbus_static_string_##name, \ 310 sizeof(_dbus_static_string_##name), \ 311 sizeof(_dbus_static_string_##name) + \ 312 _DBUS_STRING_ALLOCATION_PADDING, \ 313 sizeof(_dbus_static_string_##name), \ 314 TRUE, TRUE, FALSE, 0 } 315 316DBUS_END_DECLS 317 318#endif /* DBUS_STRING_H */ 319