Fossil SCM
Fixed a C99/C++ism. Added json_new_string_f() (printf-style).
Commit
f5cc421dc20b41a3b93fe4408a52b052742316fd
Parent
fa17e0980cd098a…
2 files changed
+14
-1
+12
+14
-1
| --- src/json.c | ||
| +++ src/json.c | ||
| @@ -396,10 +396,22 @@ | ||
| 396 | 396 | cson_value * json_new_string( char const * str ){ |
| 397 | 397 | return str |
| 398 | 398 | ? cson_value_new_string(str,strlen(str)) |
| 399 | 399 | : NULL; |
| 400 | 400 | } |
| 401 | + | |
| 402 | +cson_value * json_new_string_f( char const * fmt, ... ){ | |
| 403 | + cson_value * v; | |
| 404 | + char * zStr; | |
| 405 | + va_list vargs; | |
| 406 | + va_start(vargs,fmt); | |
| 407 | + zStr = vmprintf(fmt,vargs); | |
| 408 | + va_end(vargs); | |
| 409 | + v = cson_value_new_string(zStr, strlen(zStr)); | |
| 410 | + free(zStr); | |
| 411 | + return v; | |
| 412 | +} | |
| 401 | 413 | |
| 402 | 414 | cson_value * json_new_int( int v ){ |
| 403 | 415 | return cson_value_new_integer((cson_int_t)v); |
| 404 | 416 | } |
| 405 | 417 | |
| @@ -883,12 +895,13 @@ | ||
| 883 | 895 | if(fmt && *fmt){ |
| 884 | 896 | /* FIXME: treat NULL fmt as standard warning message for |
| 885 | 897 | the code, but we don't have those yet. |
| 886 | 898 | */ |
| 887 | 899 | va_list vargs; |
| 900 | + char * msg; | |
| 888 | 901 | va_start(vargs,fmt); |
| 889 | - char * msg = vmprintf(fmt,vargs); | |
| 902 | + msg = vmprintf(fmt,vargs); | |
| 890 | 903 | va_end(vargs); |
| 891 | 904 | cson_object_set(obj,"text", cson_value_new_string(msg,strlen(msg))); |
| 892 | 905 | free(msg); |
| 893 | 906 | } |
| 894 | 907 | } |
| 895 | 908 |
| --- src/json.c | |
| +++ src/json.c | |
| @@ -396,10 +396,22 @@ | |
| 396 | cson_value * json_new_string( char const * str ){ |
| 397 | return str |
| 398 | ? cson_value_new_string(str,strlen(str)) |
| 399 | : NULL; |
| 400 | } |
| 401 | |
| 402 | cson_value * json_new_int( int v ){ |
| 403 | return cson_value_new_integer((cson_int_t)v); |
| 404 | } |
| 405 | |
| @@ -883,12 +895,13 @@ | |
| 883 | if(fmt && *fmt){ |
| 884 | /* FIXME: treat NULL fmt as standard warning message for |
| 885 | the code, but we don't have those yet. |
| 886 | */ |
| 887 | va_list vargs; |
| 888 | va_start(vargs,fmt); |
| 889 | char * msg = vmprintf(fmt,vargs); |
| 890 | va_end(vargs); |
| 891 | cson_object_set(obj,"text", cson_value_new_string(msg,strlen(msg))); |
| 892 | free(msg); |
| 893 | } |
| 894 | } |
| 895 |
| --- src/json.c | |
| +++ src/json.c | |
| @@ -396,10 +396,22 @@ | |
| 396 | cson_value * json_new_string( char const * str ){ |
| 397 | return str |
| 398 | ? cson_value_new_string(str,strlen(str)) |
| 399 | : NULL; |
| 400 | } |
| 401 | |
| 402 | cson_value * json_new_string_f( char const * fmt, ... ){ |
| 403 | cson_value * v; |
| 404 | char * zStr; |
| 405 | va_list vargs; |
| 406 | va_start(vargs,fmt); |
| 407 | zStr = vmprintf(fmt,vargs); |
| 408 | va_end(vargs); |
| 409 | v = cson_value_new_string(zStr, strlen(zStr)); |
| 410 | free(zStr); |
| 411 | return v; |
| 412 | } |
| 413 | |
| 414 | cson_value * json_new_int( int v ){ |
| 415 | return cson_value_new_integer((cson_int_t)v); |
| 416 | } |
| 417 | |
| @@ -883,12 +895,13 @@ | |
| 895 | if(fmt && *fmt){ |
| 896 | /* FIXME: treat NULL fmt as standard warning message for |
| 897 | the code, but we don't have those yet. |
| 898 | */ |
| 899 | va_list vargs; |
| 900 | char * msg; |
| 901 | va_start(vargs,fmt); |
| 902 | msg = vmprintf(fmt,vargs); |
| 903 | va_end(vargs); |
| 904 | cson_object_set(obj,"text", cson_value_new_string(msg,strlen(msg))); |
| 905 | free(msg); |
| 906 | } |
| 907 | } |
| 908 |
+12
| --- src/json_detail.h | ||
| +++ src/json_detail.h | ||
| @@ -216,6 +216,18 @@ | ||
| 216 | 216 | ** Convenience wrapper around cson_value_new_string(). |
| 217 | 217 | ** Returns NULL if str is NULL or on allocation error. |
| 218 | 218 | */ |
| 219 | 219 | cson_value * json_new_string( char const * str ); |
| 220 | 220 | |
| 221 | +/* | |
| 222 | +** Similar to json_new_string(), but takes a printf()-style format | |
| 223 | +** specifiers. Supports the printf extensions supported by fossil's | |
| 224 | +** mprintf(). Returns NULL if str is NULL or on allocation error. | |
| 225 | +** | |
| 226 | +** Maintenance note: json_new_string() is NOT variadic because by the | |
| 227 | +** time the variadic form was introduced we already had use cases | |
| 228 | +** which segfaulted via json_new_string() because they contain printf | |
| 229 | +** markup (e.g. wiki content). Been there, debugged that. | |
| 230 | +*/ | |
| 231 | +cson_value * json_new_string_f( char const * fmt, ... ); | |
| 232 | + | |
| 221 | 233 | #endif/*FOSSIL_JSON_DETAIL_H_INCLUDED*/ |
| 222 | 234 |
| --- src/json_detail.h | |
| +++ src/json_detail.h | |
| @@ -216,6 +216,18 @@ | |
| 216 | ** Convenience wrapper around cson_value_new_string(). |
| 217 | ** Returns NULL if str is NULL or on allocation error. |
| 218 | */ |
| 219 | cson_value * json_new_string( char const * str ); |
| 220 | |
| 221 | #endif/*FOSSIL_JSON_DETAIL_H_INCLUDED*/ |
| 222 |
| --- src/json_detail.h | |
| +++ src/json_detail.h | |
| @@ -216,6 +216,18 @@ | |
| 216 | ** Convenience wrapper around cson_value_new_string(). |
| 217 | ** Returns NULL if str is NULL or on allocation error. |
| 218 | */ |
| 219 | cson_value * json_new_string( char const * str ); |
| 220 | |
| 221 | /* |
| 222 | ** Similar to json_new_string(), but takes a printf()-style format |
| 223 | ** specifiers. Supports the printf extensions supported by fossil's |
| 224 | ** mprintf(). Returns NULL if str is NULL or on allocation error. |
| 225 | ** |
| 226 | ** Maintenance note: json_new_string() is NOT variadic because by the |
| 227 | ** time the variadic form was introduced we already had use cases |
| 228 | ** which segfaulted via json_new_string() because they contain printf |
| 229 | ** markup (e.g. wiki content). Been there, debugged that. |
| 230 | */ |
| 231 | cson_value * json_new_string_f( char const * fmt, ... ); |
| 232 | |
| 233 | #endif/*FOSSIL_JSON_DETAIL_H_INCLUDED*/ |
| 234 |