dbus-string.h revision d012387afef0ba02185ebe27bc6bb15551912e92
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); 76#ifndef _dbus_string_get_data 77char* _dbus_string_get_data (DBusString *str); 78#endif /* _dbus_string_get_data */ 79#ifndef _dbus_string_get_const_data 80const char* _dbus_string_get_const_data (const DBusString *str); 81#endif /* _dbus_string_get_const_data */ 82char* _dbus_string_get_data_len (DBusString *str, 83 int start, 84 int len); 85#ifndef _dbus_string_get_const_data_len 86const char* _dbus_string_get_const_data_len (const DBusString *str, 87 int start, 88 int len); 89#endif 90#ifndef _dbus_string_set_byte 91void _dbus_string_set_byte (DBusString *str, 92 int i, 93 unsigned char byte); 94#endif 95#ifndef _dbus_string_get_byte 96unsigned char _dbus_string_get_byte (const DBusString *str, 97 int start); 98#endif /* _dbus_string_get_byte */ 99dbus_bool_t _dbus_string_insert_bytes (DBusString *str, 100 int i, 101 int n_bytes, 102 unsigned char byte); 103dbus_bool_t _dbus_string_insert_byte (DBusString *str, 104 int i, 105 unsigned char byte); 106dbus_bool_t _dbus_string_steal_data (DBusString *str, 107 char **data_return); 108dbus_bool_t _dbus_string_steal_data_len (DBusString *str, 109 char **data_return, 110 int start, 111 int len); 112dbus_bool_t _dbus_string_copy_data (const DBusString *str, 113 char **data_return); 114dbus_bool_t _dbus_string_copy_data_len (const DBusString *str, 115 char **data_return, 116 int start, 117 int len); 118void _dbus_string_copy_to_buffer (const DBusString *str, 119 char *buffer, 120 int len); 121#ifndef _dbus_string_get_length 122int _dbus_string_get_length (const DBusString *str); 123#endif /* !_dbus_string_get_length */ 124 125dbus_bool_t _dbus_string_lengthen (DBusString *str, 126 int additional_length); 127void _dbus_string_shorten (DBusString *str, 128 int length_to_remove); 129dbus_bool_t _dbus_string_set_length (DBusString *str, 130 int length); 131dbus_bool_t _dbus_string_align_length (DBusString *str, 132 int alignment); 133dbus_bool_t _dbus_string_alloc_space (DBusString *str, 134 int extra_bytes); 135dbus_bool_t _dbus_string_append (DBusString *str, 136 const char *buffer); 137dbus_bool_t _dbus_string_append_len (DBusString *str, 138 const char *buffer, 139 int len); 140dbus_bool_t _dbus_string_append_int (DBusString *str, 141 long value); 142dbus_bool_t _dbus_string_append_uint (DBusString *str, 143 unsigned long value); 144dbus_bool_t _dbus_string_append_double (DBusString *str, 145 double value); 146dbus_bool_t _dbus_string_append_byte (DBusString *str, 147 unsigned char byte); 148dbus_bool_t _dbus_string_append_unichar (DBusString *str, 149 dbus_unichar_t ch); 150dbus_bool_t _dbus_string_append_4_aligned (DBusString *str, 151 const unsigned char octets[4]); 152dbus_bool_t _dbus_string_append_8_aligned (DBusString *str, 153 const unsigned char octets[8]); 154dbus_bool_t _dbus_string_append_printf (DBusString *str, 155 const char *format, 156 ...) _DBUS_GNUC_PRINTF (2, 3); 157dbus_bool_t _dbus_string_append_printf_valist (DBusString *str, 158 const char *format, 159 va_list args); 160dbus_bool_t _dbus_string_insert_2_aligned (DBusString *str, 161 int insert_at, 162 const unsigned char octets[2]); 163dbus_bool_t _dbus_string_insert_4_aligned (DBusString *str, 164 int insert_at, 165 const unsigned char octets[4]); 166dbus_bool_t _dbus_string_insert_8_aligned (DBusString *str, 167 int insert_at, 168 const unsigned char octets[8]); 169dbus_bool_t _dbus_string_insert_alignment (DBusString *str, 170 int *insert_at, 171 int alignment); 172void _dbus_string_delete (DBusString *str, 173 int start, 174 int len); 175dbus_bool_t _dbus_string_move (DBusString *source, 176 int start, 177 DBusString *dest, 178 int insert_at); 179dbus_bool_t _dbus_string_copy (const DBusString *source, 180 int start, 181 DBusString *dest, 182 int insert_at); 183dbus_bool_t _dbus_string_move_len (DBusString *source, 184 int start, 185 int len, 186 DBusString *dest, 187 int insert_at); 188dbus_bool_t _dbus_string_copy_len (const DBusString *source, 189 int start, 190 int len, 191 DBusString *dest, 192 int insert_at); 193dbus_bool_t _dbus_string_replace_len (const DBusString *source, 194 int start, 195 int len, 196 DBusString *dest, 197 int replace_at, 198 int replace_len); 199void _dbus_string_get_unichar (const DBusString *str, 200 int start, 201 dbus_unichar_t *ch_return, 202 int *end_return); 203dbus_bool_t _dbus_string_parse_int (const DBusString *str, 204 int start, 205 long *value_return, 206 int *end_return); 207dbus_bool_t _dbus_string_parse_uint (const DBusString *str, 208 int start, 209 unsigned long *value_return, 210 int *end_return); 211dbus_bool_t _dbus_string_parse_double (const DBusString *str, 212 int start, 213 double *value, 214 int *end_return); 215dbus_bool_t _dbus_string_find (const DBusString *str, 216 int start, 217 const char *substr, 218 int *found); 219dbus_bool_t _dbus_string_find_eol (const DBusString *str, 220 int start, 221 int *found, 222 int *found_len); 223dbus_bool_t _dbus_string_find_to (const DBusString *str, 224 int start, 225 int end, 226 const char *substr, 227 int *found); 228dbus_bool_t _dbus_string_find_byte_backward (const DBusString *str, 229 int start, 230 unsigned char byte, 231 int *found); 232dbus_bool_t _dbus_string_find_blank (const DBusString *str, 233 int start, 234 int *found); 235void _dbus_string_skip_blank (const DBusString *str, 236 int start, 237 int *end); 238void _dbus_string_skip_white (const DBusString *str, 239 int start, 240 int *end); 241void _dbus_string_skip_white_reverse (const DBusString *str, 242 int end, 243 int *start); 244dbus_bool_t _dbus_string_equal (const DBusString *a, 245 const DBusString *b); 246dbus_bool_t _dbus_string_equal_c_str (const DBusString *a, 247 const char *c_str); 248dbus_bool_t _dbus_string_equal_len (const DBusString *a, 249 const DBusString *b, 250 int len); 251dbus_bool_t _dbus_string_equal_substring (const DBusString *a, 252 int a_start, 253 int a_len, 254 const DBusString *b, 255 int b_start); 256dbus_bool_t _dbus_string_starts_with_c_str (const DBusString *a, 257 const char *c_str); 258dbus_bool_t _dbus_string_ends_with_c_str (const DBusString *a, 259 const char *c_str); 260dbus_bool_t _dbus_string_pop_line (DBusString *source, 261 DBusString *dest); 262void _dbus_string_delete_first_word (DBusString *str); 263void _dbus_string_delete_leading_blanks (DBusString *str); 264void _dbus_string_chop_white (DBusString *str); 265dbus_bool_t _dbus_string_append_byte_as_hex (DBusString *str, 266 int byte); 267dbus_bool_t _dbus_string_hex_encode (const DBusString *source, 268 int start, 269 DBusString *dest, 270 int insert_at); 271dbus_bool_t _dbus_string_hex_decode (const DBusString *source, 272 int start, 273 int *end_return, 274 DBusString *dest, 275 int insert_at); 276dbus_bool_t _dbus_string_validate_ascii (const DBusString *str, 277 int start, 278 int len); 279dbus_bool_t _dbus_string_validate_utf8 (const DBusString *str, 280 int start, 281 int len); 282dbus_bool_t _dbus_string_validate_nul (const DBusString *str, 283 int start, 284 int len); 285void _dbus_string_zero (DBusString *str); 286 287 288/** 289 * We allocate 1 byte for nul termination, plus 7 bytes for possible 290 * align_offset, so we always need 8 bytes on top of the string's 291 * length to be in the allocated block. 292 */ 293#define _DBUS_STRING_ALLOCATION_PADDING 8 294 295/** 296 * Defines a static const variable with type #DBusString called "name" 297 * containing the given string literal. 298 * 299 * @param name the name of the variable 300 * @param str the string value 301 */ 302#define _DBUS_STRING_DEFINE_STATIC(name, str) \ 303 static const char _dbus_static_string_##name[] = str; \ 304 static const DBusString name = { _dbus_static_string_##name, \ 305 sizeof(_dbus_static_string_##name), \ 306 sizeof(_dbus_static_string_##name) + \ 307 _DBUS_STRING_ALLOCATION_PADDING, \ 308 sizeof(_dbus_static_string_##name), \ 309 TRUE, TRUE, FALSE, 0 } 310 311DBUS_END_DECLS 312 313#endif /* DBUS_STRING_H */ 314