Lines Matching refs:b
20 buffer *b; in buffer_init() local
22 b = malloc(sizeof(*b)); in buffer_init()
23 assert(b); in buffer_init()
25 b->ptr = NULL; in buffer_init()
26 b->size = 0; in buffer_init()
27 b->used = 0; in buffer_init()
29 return b; in buffer_init()
33 buffer *b = buffer_init(); in buffer_init_buffer() local
34 buffer_copy_string_buffer(b, src); in buffer_init_buffer()
35 return b; in buffer_init_buffer()
43 void buffer_free(buffer *b) { in buffer_free() argument
44 if (!b) return; in buffer_free()
46 free(b->ptr); in buffer_free()
47 free(b); in buffer_free()
50 void buffer_reset(buffer *b) { in buffer_reset() argument
51 if (!b) return; in buffer_reset()
54 if (b->size > BUFFER_MAX_REUSE_SIZE) { in buffer_reset()
55 free(b->ptr); in buffer_reset()
56 b->ptr = NULL; in buffer_reset()
57 b->size = 0; in buffer_reset()
58 } else if (b->size) { in buffer_reset()
59 b->ptr[0] = '\0'; in buffer_reset()
62 b->used = 0; in buffer_reset()
75 int buffer_prepare_copy(buffer *b, size_t size) { in buffer_prepare_copy() argument
76 if (!b) return -1; in buffer_prepare_copy()
78 if ((0 == b->size) || in buffer_prepare_copy()
79 (size > b->size)) { in buffer_prepare_copy()
80 if (b->size) free(b->ptr); in buffer_prepare_copy()
82 b->size = size; in buffer_prepare_copy()
85 b->size += BUFFER_PIECE_SIZE - (b->size % BUFFER_PIECE_SIZE); in buffer_prepare_copy()
87 b->ptr = malloc(b->size); in buffer_prepare_copy()
88 assert(b->ptr); in buffer_prepare_copy()
90 b->used = 0; in buffer_prepare_copy()
101 int buffer_prepare_append(buffer *b, size_t size) { in buffer_prepare_append() argument
102 if (!b) return -1; in buffer_prepare_append()
104 if (0 == b->size) { in buffer_prepare_append()
105 b->size = size; in buffer_prepare_append()
108 b->size += BUFFER_PIECE_SIZE - (b->size % BUFFER_PIECE_SIZE); in buffer_prepare_append()
110 b->ptr = malloc(b->size); in buffer_prepare_append()
111 b->used = 0; in buffer_prepare_append()
112 assert(b->ptr); in buffer_prepare_append()
113 } else if (b->used + size > b->size) { in buffer_prepare_append()
114 b->size += size; in buffer_prepare_append()
117 b->size += BUFFER_PIECE_SIZE - (b->size % BUFFER_PIECE_SIZE); in buffer_prepare_append()
119 b->ptr = realloc(b->ptr, b->size); in buffer_prepare_append()
120 assert(b->ptr); in buffer_prepare_append()
125 int buffer_copy_string(buffer *b, const char *s) { in buffer_copy_string() argument
128 if (!s || !b) return -1; in buffer_copy_string()
131 buffer_prepare_copy(b, s_len); in buffer_copy_string()
133 memcpy(b->ptr, s, s_len); in buffer_copy_string()
134 b->used = s_len; in buffer_copy_string()
139 int buffer_copy_string_len(buffer *b, const char *s, size_t s_len) { in buffer_copy_string_len() argument
140 if (!s || !b) return -1; in buffer_copy_string_len()
149 buffer_prepare_copy(b, s_len + 1); in buffer_copy_string_len()
151 memcpy(b->ptr, s, s_len); in buffer_copy_string_len()
152 b->ptr[s_len] = '\0'; in buffer_copy_string_len()
153 b->used = s_len + 1; in buffer_copy_string_len()
158 int buffer_copy_string_buffer(buffer *b, const buffer *src) { in buffer_copy_string_buffer() argument
162 buffer_reset(b); in buffer_copy_string_buffer()
165 return buffer_copy_string_len(b, src->ptr, src->used - 1); in buffer_copy_string_buffer()
168 int buffer_append_string(buffer *b, const char *s) { in buffer_append_string() argument
171 if (!s || !b) return -1; in buffer_append_string()
174 buffer_prepare_append(b, s_len + 1); in buffer_append_string()
175 if (b->used == 0) in buffer_append_string()
176 b->used++; in buffer_append_string()
178 memcpy(b->ptr + b->used - 1, s, s_len + 1); in buffer_append_string()
179 b->used += s_len; in buffer_append_string()
184 int buffer_append_string_rfill(buffer *b, const char *s, size_t maxlen) { in buffer_append_string_rfill() argument
187 if (!s || !b) return -1; in buffer_append_string_rfill()
191 buffer_prepare_append(b, maxlen + 1); in buffer_append_string_rfill()
192 if (b->used == 0) in buffer_append_string_rfill()
193 b->used++; in buffer_append_string_rfill()
195 memcpy(b->ptr + b->used - 1, s, s_len); in buffer_append_string_rfill()
197 memset(b->ptr + b->used - 1 + s_len, ' ', maxlen - s_len); in buffer_append_string_rfill()
200 b->used += maxlen; in buffer_append_string_rfill()
201 b->ptr[b->used - 1] = '\0'; in buffer_append_string_rfill()
216 int buffer_append_string_len(buffer *b, const char *s, size_t s_len) { in buffer_append_string_len() argument
217 if (!s || !b) return -1; in buffer_append_string_len()
220 buffer_prepare_append(b, s_len + 1); in buffer_append_string_len()
221 if (b->used == 0) in buffer_append_string_len()
222 b->used++; in buffer_append_string_len()
224 memcpy(b->ptr + b->used - 1, s, s_len); in buffer_append_string_len()
225 b->used += s_len; in buffer_append_string_len()
226 b->ptr[b->used - 1] = '\0'; in buffer_append_string_len()
231 int buffer_append_string_buffer(buffer *b, const buffer *src) { in buffer_append_string_buffer() argument
235 return buffer_append_string_len(b, src->ptr, src->used - 1); in buffer_append_string_buffer()
238 int buffer_append_memory(buffer *b, const char *s, size_t s_len) { in buffer_append_memory() argument
239 if (!s || !b) return -1; in buffer_append_memory()
242 buffer_prepare_append(b, s_len); in buffer_append_memory()
243 memcpy(b->ptr + b->used, s, s_len); in buffer_append_memory()
244 b->used += s_len; in buffer_append_memory()
249 int buffer_copy_memory(buffer *b, const char *s, size_t s_len) { in buffer_copy_memory() argument
250 if (!s || !b) return -1; in buffer_copy_memory()
252 b->used = 0; in buffer_copy_memory()
254 return buffer_append_memory(b, s, s_len); in buffer_copy_memory()
257 int buffer_append_long_hex(buffer *b, unsigned long value) { in buffer_append_long_hex() argument
271 buffer_prepare_append(b, shift + 1); in buffer_append_long_hex()
272 if (b->used == 0) in buffer_append_long_hex()
273 b->used++; in buffer_append_long_hex()
274 buf = b->ptr + (b->used - 1); in buffer_append_long_hex()
275 b->used += shift; in buffer_append_long_hex()
319 int buffer_append_long(buffer *b, long val) { in buffer_append_long() argument
320 if (!b) return -1; in buffer_append_long()
322 buffer_prepare_append(b, 32); in buffer_append_long()
323 if (b->used == 0) in buffer_append_long()
324 b->used++; in buffer_append_long()
326 b->used += LI_ltostr(b->ptr + (b->used - 1), val); in buffer_append_long()
330 int buffer_copy_long(buffer *b, long val) { in buffer_copy_long() argument
331 if (!b) return -1; in buffer_copy_long()
333 b->used = 0; in buffer_copy_long()
334 return buffer_append_long(b, val); in buffer_copy_long()
338 int buffer_append_off_t(buffer *b, off_t val) { in buffer_append_off_t() argument
344 if (!b) return -1; in buffer_append_off_t()
346 buffer_prepare_append(b, 32); in buffer_append_off_t()
347 if (b->used == 0) in buffer_append_off_t()
348 b->used++; in buffer_append_off_t()
350 start = b->ptr + (b->used - 1); in buffer_append_off_t()
375 b->used += len; in buffer_append_off_t()
379 int buffer_copy_off_t(buffer *b, off_t val) { in buffer_copy_off_t() argument
380 if (!b) return -1; in buffer_copy_off_t()
382 b->used = 0; in buffer_copy_off_t()
383 return buffer_append_off_t(b, val); in buffer_copy_off_t()
413 buffer_array *b; in buffer_array_init() local
415 b = malloc(sizeof(*b)); in buffer_array_init()
417 assert(b); in buffer_array_init()
418 b->ptr = NULL; in buffer_array_init()
419 b->size = 0; in buffer_array_init()
420 b->used = 0; in buffer_array_init()
422 return b; in buffer_array_init()
425 void buffer_array_reset(buffer_array *b) { in buffer_array_reset() argument
428 if (!b) return; in buffer_array_reset()
431 for (i = 0; i < b->used; i++) { in buffer_array_reset()
432 buffer_reset(b->ptr[i]); in buffer_array_reset()
435 b->used = 0; in buffer_array_reset()
444 void buffer_array_free(buffer_array *b) { in buffer_array_free() argument
446 if (!b) return; in buffer_array_free()
448 for (i = 0; i < b->size; i++) { in buffer_array_free()
449 if (b->ptr[i]) buffer_free(b->ptr[i]); in buffer_array_free()
451 free(b->ptr); in buffer_array_free()
452 free(b); in buffer_array_free()
455 buffer *buffer_array_append_get_buffer(buffer_array *b) { in buffer_array_append_get_buffer() argument
458 if (b->size == 0) { in buffer_array_append_get_buffer()
459 b->size = 16; in buffer_array_append_get_buffer()
460 b->ptr = malloc(sizeof(*b->ptr) * b->size); in buffer_array_append_get_buffer()
461 assert(b->ptr); in buffer_array_append_get_buffer()
462 for (i = 0; i < b->size; i++) { in buffer_array_append_get_buffer()
463 b->ptr[i] = NULL; in buffer_array_append_get_buffer()
465 } else if (b->size == b->used) { in buffer_array_append_get_buffer()
466 b->size += 16; in buffer_array_append_get_buffer()
467 b->ptr = realloc(b->ptr, sizeof(*b->ptr) * b->size); in buffer_array_append_get_buffer()
468 assert(b->ptr); in buffer_array_append_get_buffer()
469 for (i = b->used; i < b->size; i++) { in buffer_array_append_get_buffer()
470 b->ptr[i] = NULL; in buffer_array_append_get_buffer()
474 if (b->ptr[b->used] == NULL) { in buffer_array_append_get_buffer()
475 b->ptr[b->used] = buffer_init(); in buffer_array_append_get_buffer()
478 b->ptr[b->used]->used = 0; in buffer_array_append_get_buffer()
480 return b->ptr[b->used++]; in buffer_array_append_get_buffer()
484 char * buffer_search_string_len(buffer *b, const char *needle, size_t len) { in buffer_search_string_len() argument
489 if (b->used < len) return NULL; in buffer_search_string_len()
491 for(i = 0; i < b->used - len; i++) { in buffer_search_string_len()
492 if (0 == memcmp(b->ptr + i, needle, len)) { in buffer_search_string_len()
493 return b->ptr + i; in buffer_search_string_len()
501 buffer *b = buffer_init(); in buffer_init_string() local
503 buffer_copy_string(b, str); in buffer_init_string()
505 return b; in buffer_init_string()
508 int buffer_is_empty(buffer *b) { in buffer_is_empty() argument
509 if (!b) return 1; in buffer_is_empty()
510 return (b->used == 0); in buffer_is_empty()
520 int buffer_is_equal(buffer *a, buffer *b) { in buffer_is_equal() argument
521 if (a->used != b->used) return 0; in buffer_is_equal()
524 return (0 == strcmp(a->ptr, b->ptr)); in buffer_is_equal()
528 buffer b; in buffer_is_equal_string() local
530 b.ptr = (char *)s; in buffer_is_equal_string()
531 b.used = b_len + 1; in buffer_is_equal_string()
533 return buffer_is_equal(a, &b); in buffer_is_equal_string()
541 int buffer_caseless_compare(const char *a, size_t a_len, const char *b, size_t b_len) { in buffer_caseless_compare() argument
547 bl = (size_t *)b; in buffer_caseless_compare()
564 b = (char *)bl; in buffer_caseless_compare()
569 int a1 = *a++, b1 = *b++; in buffer_caseless_compare()
614 int buffer_copy_string_hex(buffer *b, const char *in, size_t in_len) { in buffer_copy_string_hex() argument
620 buffer_prepare_copy(b, in_len * 2 + 1); in buffer_copy_string_hex()
623 b->ptr[b->used++] = hex_chars[(in[i] >> 4) & 0x0F]; in buffer_copy_string_hex()
624 b->ptr[b->used++] = hex_chars[in[i] & 0x0F]; in buffer_copy_string_hex()
626 b->ptr[b->used++] = '\0'; in buffer_copy_string_hex()
767 int buffer_append_string_encoded(buffer *b, const char *s, size_t s_len, buffer_encoding_t encoding… in buffer_append_string_encoded() argument
772 if (!s || !b) return -1; in buffer_append_string_encoded()
774 if (b->ptr[b->used - 1] != '\0') { in buffer_append_string_encoded()
829 buffer_prepare_append(b, d_len); in buffer_append_string_encoded()
831 …for (ds = (unsigned char *)s, d = (unsigned char *)b->ptr + b->used - 1, d_len = 0, ndx = 0; ndx <… in buffer_append_string_encoded()
866 b->ptr[b->used + d_len - 1] = '\0'; in buffer_append_string_encoded()
868 b->used += d_len; in buffer_append_string_encoded()
1050 int buffer_to_lower(buffer *b) { in buffer_to_lower() argument
1053 if (b->used == 0) return 0; in buffer_to_lower()
1055 for (c = b->ptr; *c; c++) { in buffer_to_lower()
1065 int buffer_to_upper(buffer *b) { in buffer_to_upper() argument
1068 if (b->used == 0) return 0; in buffer_to_upper()
1070 for (c = b->ptr; *c; c++) { in buffer_to_upper()