D-Bus
1.10.12
|
00001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ 00002 /* dbus-string.h String utility class (internal to D-Bus implementation) 00003 * 00004 * Copyright (C) 2002, 2003 Red Hat, Inc. 00005 * Copyright (C) 2006 Ralf Habacker <ralf.habacker@freenet.de> 00006 * 00007 * Licensed under the Academic Free License version 2.1 00008 * 00009 * This program is free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00022 * 00023 */ 00024 00025 #ifndef DBUS_STRING_H 00026 #define DBUS_STRING_H 00027 00028 #include <dbus/dbus-macros.h> 00029 #include <dbus/dbus-types.h> 00030 #include <dbus/dbus-memory.h> 00031 00032 #include <stdarg.h> 00033 00034 DBUS_BEGIN_DECLS 00035 00040 typedef struct DBusString DBusString; 00041 00042 struct DBusString 00043 { 00044 #if defined(DBUS_WIN) && defined(_DEBUG) 00045 const char *dummy1; 00046 #else 00047 const void *dummy1; 00048 #endif 00049 int dummy2; 00050 int dummy3; 00051 unsigned int dummy_bit1 : 1; 00052 unsigned int dummy_bit2 : 1; 00053 unsigned int dummy_bit3 : 1; 00054 unsigned int dummy_bits : 3; 00055 }; 00056 00057 #ifdef DBUS_DISABLE_ASSERT 00058 /* Some simple inlining hacks; the current linker is not smart enough 00059 * to inline non-exported symbols across files in the library. 00060 * Note that these break type safety (due to the casts) 00061 */ 00062 #define _dbus_string_get_data(s) ((char*)(((DBusString*)(s))->dummy1)) 00063 #define _dbus_string_get_length(s) (((DBusString*)(s))->dummy2) 00064 #define _dbus_string_set_byte(s, i, b) ((((unsigned char*)(((DBusString*)(s))->dummy1))[(i)]) = (unsigned char) (b)) 00065 #define _dbus_string_get_byte(s, i) (((const unsigned char*)(((DBusString*)(s))->dummy1))[(i)]) 00066 #define _dbus_string_get_const_data(s) ((const char*)(((DBusString*)(s))->dummy1)) 00067 #define _dbus_string_get_const_data_len(s,start,len) (((const char*)(((DBusString*)(s))->dummy1)) + (start)) 00068 #endif 00069 00070 DBUS_PRIVATE_EXPORT 00071 dbus_bool_t _dbus_string_init (DBusString *str); 00072 DBUS_PRIVATE_EXPORT 00073 void _dbus_string_init_const (DBusString *str, 00074 const char *value); 00075 DBUS_PRIVATE_EXPORT 00076 void _dbus_string_init_const_len (DBusString *str, 00077 const char *value, 00078 int len); 00079 dbus_bool_t _dbus_string_init_preallocated (DBusString *str, 00080 int allocate_size); 00081 00082 DBUS_PRIVATE_EXPORT 00083 dbus_bool_t _dbus_string_init_from_string (DBusString *str, 00084 const DBusString *from); 00085 DBUS_PRIVATE_EXPORT 00086 void _dbus_string_free (DBusString *str); 00087 void _dbus_string_lock (DBusString *str); 00088 DBUS_PRIVATE_EXPORT 00089 dbus_bool_t _dbus_string_compact (DBusString *str, 00090 int max_waste); 00091 #ifndef _dbus_string_get_data 00092 DBUS_PRIVATE_EXPORT 00093 char* _dbus_string_get_data (DBusString *str); 00094 #endif /* _dbus_string_get_data */ 00095 #ifndef _dbus_string_get_const_data 00096 DBUS_PRIVATE_EXPORT 00097 const char* _dbus_string_get_const_data (const DBusString *str); 00098 #endif /* _dbus_string_get_const_data */ 00099 char* _dbus_string_get_data_len (DBusString *str, 00100 int start, 00101 int len); 00102 #ifndef _dbus_string_get_const_data_len 00103 DBUS_PRIVATE_EXPORT 00104 const char* _dbus_string_get_const_data_len (const DBusString *str, 00105 int start, 00106 int len); 00107 #endif 00108 #ifndef _dbus_string_set_byte 00109 DBUS_PRIVATE_EXPORT 00110 void _dbus_string_set_byte (DBusString *str, 00111 int i, 00112 unsigned char byte); 00113 #endif 00114 #ifndef _dbus_string_get_byte 00115 DBUS_PRIVATE_EXPORT 00116 unsigned char _dbus_string_get_byte (const DBusString *str, 00117 int start); 00118 #endif /* _dbus_string_get_byte */ 00119 DBUS_PRIVATE_EXPORT 00120 dbus_bool_t _dbus_string_insert_bytes (DBusString *str, 00121 int i, 00122 int n_bytes, 00123 unsigned char byte); 00124 DBUS_PRIVATE_EXPORT 00125 dbus_bool_t _dbus_string_insert_byte (DBusString *str, 00126 int i, 00127 unsigned char byte); 00128 DBUS_PRIVATE_EXPORT 00129 dbus_bool_t _dbus_string_steal_data (DBusString *str, 00130 char **data_return); 00131 dbus_bool_t _dbus_string_steal_data_len (DBusString *str, 00132 char **data_return, 00133 int start, 00134 int len); 00135 DBUS_PRIVATE_EXPORT 00136 dbus_bool_t _dbus_string_copy_data (const DBusString *str, 00137 char **data_return); 00138 dbus_bool_t _dbus_string_copy_data_len (const DBusString *str, 00139 char **data_return, 00140 int start, 00141 int len); 00142 void _dbus_string_copy_to_buffer (const DBusString *str, 00143 char *buffer, 00144 int len); 00145 DBUS_PRIVATE_EXPORT 00146 void _dbus_string_copy_to_buffer_with_nul (const DBusString *str, 00147 char *buffer, 00148 int avail_len); 00149 #ifndef _dbus_string_get_length 00150 DBUS_PRIVATE_EXPORT 00151 int _dbus_string_get_length (const DBusString *str); 00152 #endif /* !_dbus_string_get_length */ 00153 00164 static inline unsigned int 00165 _dbus_string_get_length_uint (const DBusString *str) 00166 { 00167 return (unsigned int) _dbus_string_get_length (str); 00168 } 00169 00170 DBUS_PRIVATE_EXPORT 00171 dbus_bool_t _dbus_string_lengthen (DBusString *str, 00172 int additional_length); 00173 DBUS_PRIVATE_EXPORT 00174 void _dbus_string_shorten (DBusString *str, 00175 int length_to_remove); 00176 DBUS_PRIVATE_EXPORT 00177 dbus_bool_t _dbus_string_set_length (DBusString *str, 00178 int length); 00179 dbus_bool_t _dbus_string_align_length (DBusString *str, 00180 int alignment); 00181 dbus_bool_t _dbus_string_alloc_space (DBusString *str, 00182 int extra_bytes); 00183 DBUS_PRIVATE_EXPORT 00184 dbus_bool_t _dbus_string_append (DBusString *str, 00185 const char *buffer); 00186 DBUS_PRIVATE_EXPORT 00187 dbus_bool_t _dbus_string_append_len (DBusString *str, 00188 const char *buffer, 00189 int len); 00190 DBUS_PRIVATE_EXPORT 00191 dbus_bool_t _dbus_string_append_int (DBusString *str, 00192 long value); 00193 DBUS_PRIVATE_EXPORT 00194 dbus_bool_t _dbus_string_append_uint (DBusString *str, 00195 unsigned long value); 00196 DBUS_PRIVATE_EXPORT 00197 dbus_bool_t _dbus_string_append_byte (DBusString *str, 00198 unsigned char byte); 00199 DBUS_PRIVATE_EXPORT 00200 dbus_bool_t _dbus_string_append_printf (DBusString *str, 00201 const char *format, 00202 ...) _DBUS_GNUC_PRINTF (2, 3); 00203 DBUS_PRIVATE_EXPORT 00204 dbus_bool_t _dbus_string_append_printf_valist (DBusString *str, 00205 const char *format, 00206 va_list args); 00207 dbus_bool_t _dbus_string_insert_2_aligned (DBusString *str, 00208 int insert_at, 00209 const unsigned char octets[2]); 00210 dbus_bool_t _dbus_string_insert_4_aligned (DBusString *str, 00211 int insert_at, 00212 const unsigned char octets[4]); 00213 dbus_bool_t _dbus_string_insert_8_aligned (DBusString *str, 00214 int insert_at, 00215 const unsigned char octets[8]); 00216 dbus_bool_t _dbus_string_insert_alignment (DBusString *str, 00217 int *insert_at, 00218 int alignment); 00219 DBUS_PRIVATE_EXPORT 00220 void _dbus_string_delete (DBusString *str, 00221 int start, 00222 int len); 00223 DBUS_PRIVATE_EXPORT 00224 dbus_bool_t _dbus_string_move (DBusString *source, 00225 int start, 00226 DBusString *dest, 00227 int insert_at); 00228 DBUS_PRIVATE_EXPORT 00229 dbus_bool_t _dbus_string_copy (const DBusString *source, 00230 int start, 00231 DBusString *dest, 00232 int insert_at); 00233 dbus_bool_t _dbus_string_move_len (DBusString *source, 00234 int start, 00235 int len, 00236 DBusString *dest, 00237 int insert_at); 00238 DBUS_PRIVATE_EXPORT 00239 dbus_bool_t _dbus_string_copy_len (const DBusString *source, 00240 int start, 00241 int len, 00242 DBusString *dest, 00243 int insert_at); 00244 DBUS_PRIVATE_EXPORT 00245 dbus_bool_t _dbus_string_replace_len (const DBusString *source, 00246 int start, 00247 int len, 00248 DBusString *dest, 00249 int replace_at, 00250 int replace_len); 00251 DBUS_PRIVATE_EXPORT 00252 dbus_bool_t _dbus_string_split_on_byte (DBusString *source, 00253 unsigned char byte, 00254 DBusString *tail); 00255 DBUS_PRIVATE_EXPORT 00256 dbus_bool_t _dbus_string_parse_int (const DBusString *str, 00257 int start, 00258 long *value_return, 00259 int *end_return); 00260 DBUS_PRIVATE_EXPORT 00261 dbus_bool_t _dbus_string_parse_uint (const DBusString *str, 00262 int start, 00263 unsigned long *value_return, 00264 int *end_return); 00265 DBUS_PRIVATE_EXPORT 00266 dbus_bool_t _dbus_string_find (const DBusString *str, 00267 int start, 00268 const char *substr, 00269 int *found); 00270 DBUS_PRIVATE_EXPORT 00271 dbus_bool_t _dbus_string_find_eol (const DBusString *str, 00272 int start, 00273 int *found, 00274 int *found_len); 00275 DBUS_PRIVATE_EXPORT 00276 dbus_bool_t _dbus_string_find_to (const DBusString *str, 00277 int start, 00278 int end, 00279 const char *substr, 00280 int *found); 00281 dbus_bool_t _dbus_string_find_byte_backward (const DBusString *str, 00282 int start, 00283 unsigned char byte, 00284 int *found); 00285 DBUS_PRIVATE_EXPORT 00286 dbus_bool_t _dbus_string_find_blank (const DBusString *str, 00287 int start, 00288 int *found); 00289 DBUS_PRIVATE_EXPORT 00290 void _dbus_string_skip_blank (const DBusString *str, 00291 int start, 00292 int *end); 00293 DBUS_PRIVATE_EXPORT 00294 void _dbus_string_skip_white (const DBusString *str, 00295 int start, 00296 int *end); 00297 void _dbus_string_skip_white_reverse (const DBusString *str, 00298 int end, 00299 int *start); 00300 DBUS_PRIVATE_EXPORT 00301 dbus_bool_t _dbus_string_equal (const DBusString *a, 00302 const DBusString *b); 00303 DBUS_PRIVATE_EXPORT 00304 dbus_bool_t _dbus_string_equal_c_str (const DBusString *a, 00305 const char *c_str); 00306 DBUS_PRIVATE_EXPORT 00307 dbus_bool_t _dbus_string_equal_len (const DBusString *a, 00308 const DBusString *b, 00309 int len); 00310 DBUS_PRIVATE_EXPORT 00311 dbus_bool_t _dbus_string_equal_substring (const DBusString *a, 00312 int a_start, 00313 int a_len, 00314 const DBusString *b, 00315 int b_start); 00316 DBUS_PRIVATE_EXPORT 00317 dbus_bool_t _dbus_string_starts_with_c_str (const DBusString *a, 00318 const char *c_str); 00319 dbus_bool_t _dbus_string_ends_with_c_str (const DBusString *a, 00320 const char *c_str); 00321 DBUS_PRIVATE_EXPORT 00322 dbus_bool_t _dbus_string_pop_line (DBusString *source, 00323 DBusString *dest); 00324 DBUS_PRIVATE_EXPORT 00325 void _dbus_string_delete_first_word (DBusString *str); 00326 DBUS_PRIVATE_EXPORT 00327 void _dbus_string_delete_leading_blanks (DBusString *str); 00328 DBUS_PRIVATE_EXPORT 00329 void _dbus_string_chop_white (DBusString *str); 00330 dbus_bool_t _dbus_string_append_byte_as_hex (DBusString *str, 00331 unsigned char byte); 00332 DBUS_PRIVATE_EXPORT 00333 dbus_bool_t _dbus_string_hex_encode (const DBusString *source, 00334 int start, 00335 DBusString *dest, 00336 int insert_at); 00337 DBUS_PRIVATE_EXPORT 00338 dbus_bool_t _dbus_string_hex_decode (const DBusString *source, 00339 int start, 00340 int *end_return, 00341 DBusString *dest, 00342 int insert_at); 00343 DBUS_PRIVATE_EXPORT 00344 void _dbus_string_tolower_ascii (const DBusString *str, 00345 int start, 00346 int len); 00347 DBUS_PRIVATE_EXPORT 00348 void _dbus_string_toupper_ascii (const DBusString *str, 00349 int start, 00350 int len); 00351 dbus_bool_t _dbus_string_validate_ascii (const DBusString *str, 00352 int start, 00353 int len); 00354 DBUS_PRIVATE_EXPORT 00355 dbus_bool_t _dbus_string_validate_utf8 (const DBusString *str, 00356 int start, 00357 int len); 00358 DBUS_PRIVATE_EXPORT 00359 dbus_bool_t _dbus_string_validate_nul (const DBusString *str, 00360 int start, 00361 int len); 00362 void _dbus_string_zero (DBusString *str); 00363 00364 00370 #define _DBUS_STRING_ALLOCATION_PADDING 8 00371 00379 #define _DBUS_STRING_DEFINE_STATIC(name, str) \ 00380 static const char _dbus_static_string_##name[] = str; \ 00381 static const DBusString name = { _dbus_static_string_##name, \ 00382 sizeof(_dbus_static_string_##name), \ 00383 sizeof(_dbus_static_string_##name) + \ 00384 _DBUS_STRING_ALLOCATION_PADDING, \ 00385 TRUE, TRUE, FALSE, 0 } 00386 00387 DBUS_END_DECLS 00388 00389 #endif /* DBUS_STRING_H */