Author: jra Date: 2007-09-14 17:40:58 +0000 (Fri, 14 Sep 2007) New Revision: 25164
WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=25164 Log: Add talloc_asprintf_append_buffer() and the docs for it. Jeremy. Modified: branches/SAMBA_3_2/source/lib/talloc/talloc.c branches/SAMBA_3_2/source/lib/talloc/talloc.h branches/SAMBA_3_2/source/lib/talloc/talloc_guide.txt branches/SAMBA_3_2_0/source/lib/talloc/talloc.c branches/SAMBA_3_2_0/source/lib/talloc/talloc.h branches/SAMBA_3_2_0/source/lib/talloc/talloc_guide.txt branches/SAMBA_4_0/source/lib/talloc/talloc.c branches/SAMBA_4_0/source/lib/talloc/talloc.h branches/SAMBA_4_0/source/lib/talloc/talloc_guide.txt Changeset: Modified: branches/SAMBA_3_2/source/lib/talloc/talloc.c =================================================================== --- branches/SAMBA_3_2/source/lib/talloc/talloc.c 2007-09-14 16:50:54 UTC (rev 25163) +++ branches/SAMBA_3_2/source/lib/talloc/talloc.c 2007-09-14 17:40:58 UTC (rev 25164) @@ -1223,7 +1223,8 @@ /** * Realloc @p s to append the formatted result of @p fmt and @p ap, * and return @p s, which may have moved. Good for gradually - * accumulating output into a string buffer. + * accumulating output into a string buffer. Appends at the end + * of the string. **/ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) { @@ -1245,7 +1246,7 @@ /* Either the vsnprintf failed or the format resulted in * no characters being formatted. In the former case, we * ought to return NULL, in the latter we ought to return - * the original string. Most current callers of this + * the original string. Most current callers of this * function expect it to never return NULL. */ return s; @@ -1262,6 +1263,51 @@ return s; } +/** + * Realloc @p s to append the formatted result of @p fmt and @p ap, + * and return @p s, which may have moved. Always appends at the + * end of the talloc'ed buffer, not the end of the string. + **/ +char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap) +{ + struct talloc_chunk *tc; + int len, s_len; + va_list ap2; + char c; + + if (s == NULL) { + return talloc_vasprintf(NULL, fmt, ap); + } + + tc = talloc_chunk_from_ptr(s); + + s_len = tc->size - 1; + + va_copy(ap2, ap); + len = vsnprintf(&c, 1, fmt, ap2); + va_end(ap2); + + if (len <= 0) { + /* Either the vsnprintf failed or the format resulted in + * no characters being formatted. In the former case, we + * ought to return NULL, in the latter we ought to return + * the original string. Most current callers of this + * function expect it to never return NULL. + */ + return s; + } + + s = talloc_realloc(NULL, s, char, s_len + len+1); + if (!s) return NULL; + + va_copy(ap2, ap); + vsnprintf(s+s_len, len+1, fmt, ap2); + va_end(ap2); + _talloc_set_name_const(s, s); + + return s; +} + /* Realloc @p s to append the formatted result of @p fmt and return @p s, which may have moved. Good for gradually accumulating output @@ -1278,6 +1324,21 @@ } /* + Realloc @p s to append the formatted result of @p fmt and return @p + s, which may have moved. Good for gradually accumulating output + into a buffer. + */ +char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + s = talloc_vasprintf_append_buffer(s, fmt, ap); + va_end(ap); + return s; +} + +/* alloc an array, checking for integer overflow in the array size */ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name) Modified: branches/SAMBA_3_2/source/lib/talloc/talloc.h =================================================================== --- branches/SAMBA_3_2/source/lib/talloc/talloc.h 2007-09-14 16:50:54 UTC (rev 25163) +++ branches/SAMBA_3_2/source/lib/talloc/talloc.h 2007-09-14 17:40:58 UTC (rev 25164) @@ -157,8 +157,10 @@ char *talloc_append_string(const void *t, char *orig, const char *append); char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0); char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0); +char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0); char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3); char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3); +char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3); void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name); void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name); void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name); Modified: branches/SAMBA_3_2/source/lib/talloc/talloc_guide.txt =================================================================== --- branches/SAMBA_3_2/source/lib/talloc/talloc_guide.txt 2007-09-14 16:50:54 UTC (rev 25163) +++ branches/SAMBA_3_2/source/lib/talloc/talloc_guide.txt 2007-09-14 17:40:58 UTC (rev 25164) @@ -568,8 +568,23 @@ =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- char *talloc_asprintf_append(char *s, const char *fmt, ...); +The talloc_asprintf_append() function appends the given formatted +string to the given string. +Use this varient when the string in the current talloc buffer may +have been truncated in length. + +This functions sets the name of the new pointer to the new +string. This is equivalent to: + talloc_set_name_const(ptr, ptr) + + +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...); + The talloc_asprintf_append() function appends the given formatted -string to the given string. +string to the end of the currently allocated talloc buffer. +Use this varient when the string in the current talloc buffer has +not been changed. This functions sets the name of the new pointer to the new string. This is equivalent to: @@ -577,7 +592,7 @@ =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -(type *)talloc_array(const void *ctx, type, uint_t count); +((type *)talloc_array(const void *ctx, type, uint_t count); The talloc_array() macro is equivalent to: Modified: branches/SAMBA_3_2_0/source/lib/talloc/talloc.c =================================================================== --- branches/SAMBA_3_2_0/source/lib/talloc/talloc.c 2007-09-14 16:50:54 UTC (rev 25163) +++ branches/SAMBA_3_2_0/source/lib/talloc/talloc.c 2007-09-14 17:40:58 UTC (rev 25164) @@ -1223,7 +1223,8 @@ /** * Realloc @p s to append the formatted result of @p fmt and @p ap, * and return @p s, which may have moved. Good for gradually - * accumulating output into a string buffer. + * accumulating output into a string buffer. Appends at the end + * of the string. **/ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) { @@ -1245,7 +1246,7 @@ /* Either the vsnprintf failed or the format resulted in * no characters being formatted. In the former case, we * ought to return NULL, in the latter we ought to return - * the original string. Most current callers of this + * the original string. Most current callers of this * function expect it to never return NULL. */ return s; @@ -1262,6 +1263,51 @@ return s; } +/** + * Realloc @p s to append the formatted result of @p fmt and @p ap, + * and return @p s, which may have moved. Always appends at the + * end of the talloc'ed buffer, not the end of the string. + **/ +char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap) +{ + struct talloc_chunk *tc; + int len, s_len; + va_list ap2; + char c; + + if (s == NULL) { + return talloc_vasprintf(NULL, fmt, ap); + } + + tc = talloc_chunk_from_ptr(s); + + s_len = tc->size - 1; + + va_copy(ap2, ap); + len = vsnprintf(&c, 1, fmt, ap2); + va_end(ap2); + + if (len <= 0) { + /* Either the vsnprintf failed or the format resulted in + * no characters being formatted. In the former case, we + * ought to return NULL, in the latter we ought to return + * the original string. Most current callers of this + * function expect it to never return NULL. + */ + return s; + } + + s = talloc_realloc(NULL, s, char, s_len + len+1); + if (!s) return NULL; + + va_copy(ap2, ap); + vsnprintf(s+s_len, len+1, fmt, ap2); + va_end(ap2); + _talloc_set_name_const(s, s); + + return s; +} + /* Realloc @p s to append the formatted result of @p fmt and return @p s, which may have moved. Good for gradually accumulating output @@ -1278,6 +1324,21 @@ } /* + Realloc @p s to append the formatted result of @p fmt and return @p + s, which may have moved. Good for gradually accumulating output + into a buffer. + */ +char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + s = talloc_vasprintf_append_buffer(s, fmt, ap); + va_end(ap); + return s; +} + +/* alloc an array, checking for integer overflow in the array size */ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name) Modified: branches/SAMBA_3_2_0/source/lib/talloc/talloc.h =================================================================== --- branches/SAMBA_3_2_0/source/lib/talloc/talloc.h 2007-09-14 16:50:54 UTC (rev 25163) +++ branches/SAMBA_3_2_0/source/lib/talloc/talloc.h 2007-09-14 17:40:58 UTC (rev 25164) @@ -157,8 +157,10 @@ char *talloc_append_string(const void *t, char *orig, const char *append); char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0); char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0); +char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0); char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3); char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3); +char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3); void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name); void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name); void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name); Modified: branches/SAMBA_3_2_0/source/lib/talloc/talloc_guide.txt =================================================================== --- branches/SAMBA_3_2_0/source/lib/talloc/talloc_guide.txt 2007-09-14 16:50:54 UTC (rev 25163) +++ branches/SAMBA_3_2_0/source/lib/talloc/talloc_guide.txt 2007-09-14 17:40:58 UTC (rev 25164) @@ -568,8 +568,23 @@ =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- char *talloc_asprintf_append(char *s, const char *fmt, ...); +The talloc_asprintf_append() function appends the given formatted +string to the given string. +Use this varient when the string in the current talloc buffer may +have been truncated in length. + +This functions sets the name of the new pointer to the new +string. This is equivalent to: + talloc_set_name_const(ptr, ptr) + + +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...); + The talloc_asprintf_append() function appends the given formatted -string to the given string. +string to the end of the currently allocated talloc buffer. +Use this varient when the string in the current talloc buffer has +not been changed. This functions sets the name of the new pointer to the new string. This is equivalent to: @@ -577,7 +592,7 @@ =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -(type *)talloc_array(const void *ctx, type, uint_t count); +((type *)talloc_array(const void *ctx, type, uint_t count); The talloc_array() macro is equivalent to: Modified: branches/SAMBA_4_0/source/lib/talloc/talloc.c =================================================================== --- branches/SAMBA_4_0/source/lib/talloc/talloc.c 2007-09-14 16:50:54 UTC (rev 25163) +++ branches/SAMBA_4_0/source/lib/talloc/talloc.c 2007-09-14 17:40:58 UTC (rev 25164) @@ -1223,7 +1223,8 @@ /** * Realloc @p s to append the formatted result of @p fmt and @p ap, * and return @p s, which may have moved. Good for gradually - * accumulating output into a string buffer. + * accumulating output into a string buffer. Appends at the end + * of the string. **/ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) { @@ -1245,7 +1246,7 @@ /* Either the vsnprintf failed or the format resulted in * no characters being formatted. In the former case, we * ought to return NULL, in the latter we ought to return - * the original string. Most current callers of this + * the original string. Most current callers of this * function expect it to never return NULL. */ return s; @@ -1262,6 +1263,51 @@ return s; } +/** + * Realloc @p s to append the formatted result of @p fmt and @p ap, + * and return @p s, which may have moved. Always appends at the + * end of the talloc'ed buffer, not the end of the string. + **/ +char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap) +{ + struct talloc_chunk *tc; + int len, s_len; + va_list ap2; + char c; + + if (s == NULL) { + return talloc_vasprintf(NULL, fmt, ap); + } + + tc = talloc_chunk_from_ptr(s); + + s_len = tc->size - 1; + + va_copy(ap2, ap); + len = vsnprintf(&c, 1, fmt, ap2); + va_end(ap2); + + if (len <= 0) { + /* Either the vsnprintf failed or the format resulted in + * no characters being formatted. In the former case, we + * ought to return NULL, in the latter we ought to return + * the original string. Most current callers of this + * function expect it to never return NULL. + */ + return s; + } + + s = talloc_realloc(NULL, s, char, s_len + len+1); + if (!s) return NULL; + + va_copy(ap2, ap); + vsnprintf(s+s_len, len+1, fmt, ap2); + va_end(ap2); + _talloc_set_name_const(s, s); + + return s; +} + /* Realloc @p s to append the formatted result of @p fmt and return @p s, which may have moved. Good for gradually accumulating output @@ -1278,6 +1324,21 @@ } /* + Realloc @p s to append the formatted result of @p fmt and return @p + s, which may have moved. Good for gradually accumulating output + into a buffer. + */ +char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + s = talloc_vasprintf_append_buffer(s, fmt, ap); + va_end(ap); + return s; +} + +/* alloc an array, checking for integer overflow in the array size */ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name) Modified: branches/SAMBA_4_0/source/lib/talloc/talloc.h =================================================================== --- branches/SAMBA_4_0/source/lib/talloc/talloc.h 2007-09-14 16:50:54 UTC (rev 25163) +++ branches/SAMBA_4_0/source/lib/talloc/talloc.h 2007-09-14 17:40:58 UTC (rev 25164) @@ -157,8 +157,10 @@ char *talloc_append_string(const void *t, char *orig, const char *append); char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0); char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0); +char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0); char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3); char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3); +char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3); void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name); void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name); void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name); Modified: branches/SAMBA_4_0/source/lib/talloc/talloc_guide.txt =================================================================== --- branches/SAMBA_4_0/source/lib/talloc/talloc_guide.txt 2007-09-14 16:50:54 UTC (rev 25163) +++ branches/SAMBA_4_0/source/lib/talloc/talloc_guide.txt 2007-09-14 17:40:58 UTC (rev 25164) @@ -568,8 +568,23 @@ =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- char *talloc_asprintf_append(char *s, const char *fmt, ...); +The talloc_asprintf_append() function appends the given formatted +string to the given string. +Use this varient when the string in the current talloc buffer may +have been truncated in length. + +This functions sets the name of the new pointer to the new +string. This is equivalent to: + talloc_set_name_const(ptr, ptr) + + +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...); + The talloc_asprintf_append() function appends the given formatted -string to the given string. +string to the end of the currently allocated talloc buffer. +Use this varient when the string in the current talloc buffer has +not been changed. This functions sets the name of the new pointer to the new string. This is equivalent to: @@ -577,7 +592,7 @@ =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -(type *)talloc_array(const void *ctx, type, uint_t count); +((type *)talloc_array(const void *ctx, type, uint_t count); The talloc_array() macro is equivalent to: