X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=phpstr%2Fphpstr.c;h=f9ae25c41440d8817bcd01028c69999993b6cbc2;hp=999230005bdf15c94d40e84cb8f511ae40678bcf;hb=6be753a288ed7a42e0cd19551d85eb8eb14c4566;hpb=7038c951485d1b2afa79c9a84b8dcdcb57c9bc54 diff --git a/phpstr/phpstr.c b/phpstr/phpstr.c index 9992300..f9ae25c 100644 --- a/phpstr/phpstr.c +++ b/phpstr/phpstr.c @@ -4,27 +4,23 @@ #include "php.h" #include "phpstr.h" -#ifndef PHPSTR_DEFAULT_SIZE -#define PHPSTR_DEFAULT_SIZE 4096 -#endif - -PHPSTR_API phpstr *phpstr_init_ex(phpstr *buf, size_t chunk_size, zend_bool pre_alloc) +PHPSTR_API phpstr *phpstr_init_ex(phpstr *buf, size_t chunk_size, int pre_alloc) { if (!buf) { - buf = ecalloc(1, sizeof(phpstr)); + buf = emalloc(sizeof(phpstr)); } - buf->used = 0; buf->size = chunk_size > 0 ? chunk_size : PHPSTR_DEFAULT_SIZE; - buf->free = pre_alloc ? buf->size : 0; buf->data = pre_alloc ? emalloc(buf->size) : NULL; + buf->free = pre_alloc ? buf->size : 0; + buf->used = 0; return buf; } -PHPSTR_API phpstr *phpstr_from_string_ex(phpstr *buf, char *string, size_t length) +PHPSTR_API phpstr *phpstr_from_string_ex(phpstr *buf, const char *string, size_t length) { - buf = phpstr_init(buf); + buf = phpstr_init(buf); phpstr_append(buf, string, length); return buf; } @@ -45,12 +41,13 @@ PHPSTR_API void phpstr_resize_ex(phpstr *buf, size_t len, size_t override_size) } } -PHPSTR_API void phpstr_append(phpstr *buf, const char *append, size_t append_len) +PHPSTR_API size_t phpstr_append(phpstr *buf, const char *append, size_t append_len) { phpstr_resize(buf, append_len); memcpy(buf->data + buf->used, append, append_len); buf->used += append_len; buf->free -= append_len; + return append_len; } PHPSTR_API size_t phpstr_appendf(phpstr *buf, const char *format, ...) @@ -69,13 +66,14 @@ PHPSTR_API size_t phpstr_appendf(phpstr *buf, const char *format, ...) return append_len; } -PHPSTR_API void phpstr_insert(phpstr *buf, const char *insert, size_t insert_len, size_t offset) +PHPSTR_API size_t phpstr_insert(phpstr *buf, const char *insert, size_t insert_len, size_t offset) { phpstr_resize(buf, insert_len); memmove(buf->data + offset + insert_len, buf->data + offset, insert_len); memcpy(buf->data + offset, insert, insert_len); buf->used += insert_len; buf->free -= insert_len; + return insert_len; } PHPSTR_API size_t phpstr_insertf(phpstr *buf, size_t offset, const char *format, ...) @@ -94,13 +92,14 @@ PHPSTR_API size_t phpstr_insertf(phpstr *buf, size_t offset, const char *format, return insert_len; } -PHPSTR_API void phpstr_prepend(phpstr *buf, const char *prepend, size_t prepend_len) +PHPSTR_API size_t phpstr_prepend(phpstr *buf, const char *prepend, size_t prepend_len) { phpstr_resize(buf, prepend_len); memmove(buf->data + prepend_len, buf->data, buf->used); memcpy(buf->data, prepend, prepend_len); buf->used += prepend_len; buf->free -= prepend_len; + return prepend_len; } PHPSTR_API size_t phpstr_prependf(phpstr *buf, const char *format, ...) @@ -139,10 +138,10 @@ PHPSTR_API phpstr *phpstr_dup(const phpstr *buf) return dup; } -PHPSTR_API ssize_t phpstr_cut(phpstr *buf, size_t offset, size_t length) +PHPSTR_API size_t phpstr_cut(phpstr *buf, size_t offset, size_t length) { if (offset >= buf->used) { - return -1; + return 0; } if (offset + length > buf->used) { length = buf->used - offset; @@ -230,21 +229,67 @@ PHPSTR_API int phpstr_cmp(phpstr *left, phpstr *right) } } -PHPSTR_API void phpstr_free(phpstr *buf) +PHPSTR_API void phpstr_dtor(phpstr *buf) { - if (buf->data) { - efree(buf->data); - buf->data = NULL; - } + STR_SET(buf->data, NULL); buf->used = 0; buf->free = 0; } -PHPSTR_API void phpstr_dtor(phpstr *buf) +PHPSTR_API void phpstr_free(phpstr **buf) { - if (buf) { - phpstr_free(buf); - efree(buf); + if (*buf) { + phpstr_dtor(*buf); + efree(*buf); + *buf = NULL; + } +} + +PHPSTR_API size_t phpstr_chunk_buffer(phpstr **s, const char *data, size_t data_len, char **chunk, size_t chunk_size) +{ + phpstr *storage; + + *chunk = NULL; + + if (!*s) { + *s = phpstr_init_ex(NULL, chunk_size * 2, chunk_size ? 1 : 0); + } + storage = *s; + + if (data_len) { + phpstr_append(storage, data, data_len); + } + + if (!chunk_size) { + phpstr_data(storage, chunk, &chunk_size); + phpstr_free(&storage); + return chunk_size; + } + + if (storage->used >= storage->size/2) { + phpstr *avail = phpstr_left(storage, storage->size/2); + *chunk = estrndup(PHPSTR_VAL(avail), PHPSTR_LEN(avail)); + phpstr_free(&avail); + phpstr_cut(storage, 0, storage->size/2); + return storage->size/2; + } + + return 0; +} + +PHPSTR_API void phpstr_chunked_output(phpstr **s, const char *data, size_t data_len, size_t chunk_len, void (*passthru)(const char *, size_t TSRMLS_DC) TSRMLS_DC) +{ + char *chunk = NULL; + size_t got = 0; + + while (got = phpstr_chunk_buffer(s, data, data_len, &chunk, chunk_len)) { + passthru(chunk, got TSRMLS_CC); + efree(chunk); + data = NULL; + data_len = 0; + if (!chunk_len) { + break; + } } }