X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=phpstr%2Fphpstr.c;h=c0d9543f3d3e4a7e5b11b9d20b3aee68f3632c04;hp=70673a92ed7c4fd4c8b5f737b75442b98a4d73c9;hb=bdf32aa5fadfeedaeff828890949b4bc0f889bc8;hpb=5dca59e3a269c903b395d97abe831c0fc138b5b6 diff --git a/phpstr/phpstr.c b/phpstr/phpstr.c index 70673a9..c0d9543 100644 --- a/phpstr/phpstr.c +++ b/phpstr/phpstr.c @@ -4,7 +4,7 @@ #include "php.h" #include "phpstr.h" -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 = emalloc(sizeof(phpstr)); @@ -245,6 +245,57 @@ PHPSTR_API void phpstr_free(phpstr **buf) } } +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(s); + 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); + if (!chunk_len) { + /* we already got the last chunk, + and freed all resources */ + break; + } + data = NULL; + data_len = 0; + STR_SET(chunk, NULL); + } + STR_FREE(chunk); +} + /* * Local variables: * tab-width: 4