X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=phpstr%2Fphpstr.c;h=f9ae25c41440d8817bcd01028c69999993b6cbc2;hp=acee06fde4df9f39197b3302a79c1811d307f4c5;hb=6be753a288ed7a42e0cd19551d85eb8eb14c4566;hpb=0d925a2820b9d75b8e32c451fd400863e51e22fe diff --git a/phpstr/phpstr.c b/phpstr/phpstr.c index acee06f..f9ae25c 100644 --- a/phpstr/phpstr.c +++ b/phpstr/phpstr.c @@ -4,11 +4,7 @@ #include "php.h" #include "phpstr.h" -#ifndef PHPSTR_DEFAULT_SIZE -#define PHPSTR_DEFAULT_SIZE 256 -#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 = emalloc(sizeof(phpstr)); @@ -249,6 +245,54 @@ 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(&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; + } + } +} + /* * Local variables: * tab-width: 4