Fossil SCM

Fixed a C99/C++ism. Added json_new_string_f() (printf-style).

stephan 2011-10-19 20:54 json-multitag-test
Commit f5cc421dc20b41a3b93fe4408a52b052742316fd
2 files changed +14 -1 +12
+14 -1
--- src/json.c
+++ src/json.c
@@ -396,10 +396,22 @@
396396
cson_value * json_new_string( char const * str ){
397397
return str
398398
? cson_value_new_string(str,strlen(str))
399399
: NULL;
400400
}
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
+}
401413
402414
cson_value * json_new_int( int v ){
403415
return cson_value_new_integer((cson_int_t)v);
404416
}
405417
@@ -883,12 +895,13 @@
883895
if(fmt && *fmt){
884896
/* FIXME: treat NULL fmt as standard warning message for
885897
the code, but we don't have those yet.
886898
*/
887899
va_list vargs;
900
+ char * msg;
888901
va_start(vargs,fmt);
889
- char * msg = vmprintf(fmt,vargs);
902
+ msg = vmprintf(fmt,vargs);
890903
va_end(vargs);
891904
cson_object_set(obj,"text", cson_value_new_string(msg,strlen(msg)));
892905
free(msg);
893906
}
894907
}
895908
--- 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
--- src/json_detail.h
+++ src/json_detail.h
@@ -216,6 +216,18 @@
216216
** Convenience wrapper around cson_value_new_string().
217217
** Returns NULL if str is NULL or on allocation error.
218218
*/
219219
cson_value * json_new_string( char const * str );
220220
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
+
221233
#endif/*FOSSIL_JSON_DETAIL_H_INCLUDED*/
222234
--- 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

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button