dbus-string.h revision 5baf2f856a9c6625993234855b07680da1c8916f
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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); 204dbus_bool_t _dbus_string_split_on_byte (DBusString *source, 205 unsigned char byte, 206 DBusString *tail); 207void _dbus_string_get_unichar (const DBusString *str, 208 int start, 209 dbus_unichar_t *ch_return, 210 int *end_return); 211dbus_bool_t _dbus_string_parse_int (const DBusString *str, 212 int start, 213 long *value_return, 214 int *end_return); 215dbus_bool_t _dbus_string_parse_uint (const DBusString *str, 216 int start, 217 unsigned long *value_return, 218 int *end_return); 219dbus_bool_t _dbus_string_parse_double (const DBusString *str, 220 int start, 221 double *value, 222 int *end_return); 223dbus_bool_t _dbus_string_find (const DBusString *str, 224 int start, 225 const char *substr, 226 int *found); 227dbus_bool_t _dbus_string_find_eol (const DBusString *str, 228 int start, 229 int *found, 230 int *found_len); 231dbus_bool_t _dbus_string_find_to (const DBusString *str, 232 int start, 233 int end, 234 const char *substr, 235 int *found); 236dbus_bool_t _dbus_string_find_byte_backward (const DBusString *str, 237 int start, 238 unsigned char byte, 239 int *found); 240dbus_bool_t _dbus_string_find_blank (const DBusString *str, 241 int start, 242 int *found); 243void _dbus_string_skip_blank (const DBusString *str, 244 int start, 245 int *end); 246void _dbus_string_skip_white (const DBusString *str, 247 int start, 248 int *end); 249void _dbus_string_skip_white_reverse (const DBusString *str, 250 int end, 251 int *start); 252dbus_bool_t _dbus_string_equal (const DBusString *a, 253 const DBusString *b); 254dbus_bool_t _dbus_string_equal_c_str (const DBusString *a, 255 const char *c_str); 256dbus_bool_t _dbus_string_equal_len (const DBusString *a, 257 const DBusString *b, 258 int len); 259dbus_bool_t _dbus_string_equal_substring (const DBusString *a, 260 int a_start, 261 int a_len, 262 const DBusString *b, 263 int b_start); 264dbus_bool_t _dbus_string_starts_with_c_str (const DBusString *a, 265 const char *c_str); 266dbus_bool_t _dbus_string_ends_with_c_str (const DBusString *a, 267 const char *c_str); 268dbus_bool_t _dbus_string_pop_line (DBusString *source, 269 DBusString *dest); 270void _dbus_string_delete_first_word (DBusString *str); 271void _dbus_string_delete_leading_blanks (DBusString *str); 272void _dbus_string_chop_white (DBusString *str); 273dbus_bool_t _dbus_string_append_byte_as_hex (DBusString *str, 274 int byte); 275dbus_bool_t _dbus_string_hex_encode (const DBusString *source, 276 int start, 277 DBusString *dest, 278 int insert_at); 279dbus_bool_t _dbus_string_hex_decode (const DBusString *source, 280 int start, 281 int *end_return, 282 DBusString *dest, 283 int insert_at); 284dbus_bool_t _dbus_string_validate_ascii (const DBusString *str, 285 int start, 286 int len); 287dbus_bool_t _dbus_string_validate_utf8 (const DBusString *str, 288 int start, 289 int len); 290dbus_bool_t _dbus_string_validate_nul (const DBusString *str, 291 int start, 292 int len); 293void _dbus_string_zero (DBusString *str); 294 295 296/** 297 * We allocate 1 byte for nul termination, plus 7 bytes for possible 298 * align_offset, so we always need 8 bytes on top of the string's 299 * length to be in the allocated block. 300 */ 301#define _DBUS_STRING_ALLOCATION_PADDING 8 302 303/** 304 * Defines a static const variable with type #DBusString called "name" 305 * containing the given string literal. 306 * 307 * @param name the name of the variable 308 * @param str the string value 309 */ 310#define _DBUS_STRING_DEFINE_STATIC(name, str) \ 311 static const char _dbus_static_string_##name[] = str; \ 312 static const DBusString name = { _dbus_static_string_##name, \ 313 sizeof(_dbus_static_string_##name), \ 314 sizeof(_dbus_static_string_##name) + \ 315 _DBUS_STRING_ALLOCATION_PADDING, \ 316 sizeof(_dbus_static_string_##name), \ 317 TRUE, TRUE, FALSE, 0 } 318 319DBUS_END_DECLS 320 321#endif /* DBUS_STRING_H */ 322