X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=phpstr%2Fphpstr.c;h=98a8e63ebcc8bd52b83b1c93235b76a9e11074b1;hb=424eec077256edbed079988cfa4dccab5b979536;hp=69f577f2da079cf1221db19c90188b215063f7e4;hpb=41d6cf70475c5a4e12dae565ffcf51de7f6fe120;p=m6w6%2Fext-http diff --git a/phpstr/phpstr.c b/phpstr/phpstr.c index 69f577f..98a8e63 100644 --- a/phpstr/phpstr.c +++ b/phpstr/phpstr.c @@ -25,7 +25,7 @@ PHPSTR_API phpstr *phpstr_init_ex(phpstr *buf, size_t chunk_size, int flags) PHPSTR_API phpstr *phpstr_from_string_ex(phpstr *buf, const char *string, size_t length) { - if (buf = phpstr_init(buf)) { + if ((buf = phpstr_init(buf))) { if (NOMEM == phpstr_append(buf, string, length)) { pefree(buf, buf->pmem); buf = NULL; @@ -36,6 +36,9 @@ PHPSTR_API phpstr *phpstr_from_string_ex(phpstr *buf, const char *string, size_t PHPSTR_API size_t phpstr_resize_ex(phpstr *buf, size_t len, size_t override_size) { +#if 0 + fprintf(stderr, "RESIZE: size=%lu, used=%lu, free=%lu\n", buf->size, buf->used, buf->free); +#endif if (buf->free < len) { size_t size = override_size ? override_size : buf->size; @@ -63,6 +66,22 @@ PHPSTR_API size_t phpstr_resize_ex(phpstr *buf, size_t len, size_t override_size return 0; } +PHPSTR_API size_t phpstr_shrink(phpstr *buf) +{ + /* avoid another realloc on fixation */ + if (buf->free > 1) { + char *ptr = perealloc(buf->data, buf->used + 1, buf->pmem); + + if (ptr) { + buf->data = ptr; + } else { + return NOMEM; + } + buf->free = 1; + } + return buf->used; +} + PHPSTR_API size_t phpstr_append(phpstr *buf, const char *append, size_t append_len) { if (NOMEM == phpstr_resize(buf, append_len)) { @@ -346,7 +365,7 @@ PHPSTR_API void phpstr_chunked_output(phpstr **s, const char *data, size_t data_ char *chunk = NULL; size_t got = 0; - while (got = phpstr_chunk_buffer(s, data, data_len, &chunk, chunk_len)) { + 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,