Fossil SCM

Changed /json/stat to use brief mode by default due to relatively high runtime cost, replaced 'brief' param with 'full'. Added json_getenv_bool().

stephan 2011-09-27 02:09 UTC json
Commit c1914eaa7944d9c06432dcbdac6f5415a0ffc023
1 file changed +42 -7
+42 -7
--- src/json.c
+++ src/json.c
@@ -288,12 +288,12 @@
288288
289289
/*
290290
** Wrapper around json_getenv() which...
291291
**
292292
** If it finds a value and that value is-a JSON number or is a string
293
-** which looks like an integer or is-a JSON bool then it is converted
294
-** to an int. If none of those apply then dflt is returned.
293
+** which looks like an integer or is-a JSON bool/null then it is
294
+** converted to an int. If none of those apply then dflt is returned.
295295
*/
296296
int json_getenv_int(char const * pKey, int dflt ){
297297
cson_value const * v = json_getenv(pKey);
298298
if(!v){
299299
return dflt;
@@ -303,16 +303,51 @@
303303
char const * sv = cson_string_cstr(cson_value_get_string(v));
304304
assert( (NULL!=sv) && "This is quite unexpected." );
305305
return sv ? atoi(sv) : dflt;
306306
}else if( cson_value_is_bool(v) ){
307307
return cson_value_get_bool(v) ? 1 : 0;
308
+ }else if( cson_value_is_null(v) ){
309
+ return 0;
308310
}else{
309311
/* we should arguably treat JSON null as 0. */
310312
return dflt;
311313
}
312314
}
313315
316
+
317
+/*
318
+** Wrapper around json_getenv() which tries to evaluate a payload/env
319
+** value as a boolean. Uses mostly the same logic as
320
+** json_getenv_int(), with the addition that string values which
321
+** either start with a digit 1..9 or the letters [tT] are considered
322
+** to be true. If this function cannot find a matching key/value then
323
+** dflt is returned. e.g. if it finds the key but the value is-a
324
+** Object then dftl is returned.
325
+*/
326
+char json_getenv_bool(char const * pKey, char dflt ){
327
+ cson_value const * v = json_getenv(pKey);
328
+ if(!v){
329
+ return dflt;
330
+ }else if( cson_value_is_number(v) ){
331
+ return cson_value_get_integer(v) ? 1 : 0;
332
+ }else if( cson_value_is_string(v) ){
333
+ char const * sv = cson_string_cstr(cson_value_get_string(v));
334
+ if(!*sv || ('0'==*sv)){
335
+ return 0;
336
+ }else{
337
+ return (('t'==*sv) || ('T'==*sv)
338
+ || (('1'<=*sv) && ('9'>=*sv)))
339
+ ? 1 : 0;
340
+ }
341
+ }else if( cson_value_is_bool(v) ){
342
+ return cson_value_get_bool(v) ? 1 : 0;
343
+ }else if( cson_value_is_null(v) ){
344
+ return 0;
345
+ }else{
346
+ return dflt;
347
+ }
348
+}
314349
315350
/*
316351
** Returns the string form of a json_getenv() value, but ONLY If that
317352
** value is-a String. Non-strings are not converted to strings for
318353
** this purpose. Returned memory is owned by g.json or fossil and is
@@ -1222,11 +1257,11 @@
12221257
**
12231258
*/
12241259
cson_value * json_page_stat(){
12251260
i64 t, fsize;
12261261
int n, m;
1227
- int brief;
1262
+ int full;
12281263
const char *zDb;
12291264
enum { BufLen = 1000 };
12301265
char zBuf[BufLen];
12311266
cson_value * jv = NULL;
12321267
cson_object * jo = NULL;
@@ -1235,13 +1270,13 @@
12351270
if( !g.perm.Read ){
12361271
g.json.resultCode = FSL_JSON_E_DENIED;
12371272
return NULL;
12381273
}
12391274
if( g.isHTTP ){
1240
- brief = json_getenv_int("brief",0);
1275
+ full = json_getenv_bool("full",0);
12411276
}else{
1242
- brief = (0!=find_option("brief","b",0));
1277
+ full = (0!=find_option("full","f",0));
12431278
}
12441279
#define SETBUF(O,K) cson_object_set(O, K, cson_value_new_string(zBuf, strlen(zBuf)));
12451280
12461281
jv = cson_value_new_object();
12471282
jo = cson_value_get_object(jv);
@@ -1253,11 +1288,11 @@
12531288
with a blob for this purpose.
12541289
*/
12551290
fsize = file_size(g.zRepositoryName);
12561291
cson_object_set(jo, "repositorySize", cson_value_new_integer((cson_int_t)fsize));
12571292
1258
- if(!brief){
1293
+ if(full){
12591294
n = db_int(0, "SELECT count(*) FROM blob");
12601295
m = db_int(0, "SELECT count(*) FROM delta");
12611296
cson_object_set(jo, "blobCount", cson_value_new_integer((cson_int_t)n));
12621297
cson_object_set(jo, "deltaCount", cson_value_new_integer((cson_int_t)m));
12631298
if( n>0 ){
@@ -1292,11 +1327,11 @@
12921327
" WHERE +tagname GLOB 'wiki-*'");
12931328
cson_object_set(jo, "wikiPageCount", cson_value_new_integer((cson_int_t)n));
12941329
n = db_int(0, "SELECT count(*) FROM tag /*scan*/"
12951330
" WHERE +tagname GLOB 'tkt-*'");
12961331
cson_object_set(jo, "ticketCount", cson_value_new_integer((cson_int_t)n));
1297
- }/*!brief*/
1332
+ }/*full*/
12981333
n = db_int(0, "SELECT julianday('now') - (SELECT min(mtime) FROM event)"
12991334
" + 0.99");
13001335
cson_object_set(jo, "ageDays", cson_value_new_integer((cson_int_t)n));
13011336
cson_object_set(jo, "ageYears", cson_value_new_double(n/365.24));
13021337
sqlite3_snprintf(BufLen, zBuf, db_get("project-code",""));
13031338
--- src/json.c
+++ src/json.c
@@ -288,12 +288,12 @@
288
289 /*
290 ** Wrapper around json_getenv() which...
291 **
292 ** If it finds a value and that value is-a JSON number or is a string
293 ** which looks like an integer or is-a JSON bool then it is converted
294 ** to an int. If none of those apply then dflt is returned.
295 */
296 int json_getenv_int(char const * pKey, int dflt ){
297 cson_value const * v = json_getenv(pKey);
298 if(!v){
299 return dflt;
@@ -303,16 +303,51 @@
303 char const * sv = cson_string_cstr(cson_value_get_string(v));
304 assert( (NULL!=sv) && "This is quite unexpected." );
305 return sv ? atoi(sv) : dflt;
306 }else if( cson_value_is_bool(v) ){
307 return cson_value_get_bool(v) ? 1 : 0;
 
 
308 }else{
309 /* we should arguably treat JSON null as 0. */
310 return dflt;
311 }
312 }
313
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
314
315 /*
316 ** Returns the string form of a json_getenv() value, but ONLY If that
317 ** value is-a String. Non-strings are not converted to strings for
318 ** this purpose. Returned memory is owned by g.json or fossil and is
@@ -1222,11 +1257,11 @@
1222 **
1223 */
1224 cson_value * json_page_stat(){
1225 i64 t, fsize;
1226 int n, m;
1227 int brief;
1228 const char *zDb;
1229 enum { BufLen = 1000 };
1230 char zBuf[BufLen];
1231 cson_value * jv = NULL;
1232 cson_object * jo = NULL;
@@ -1235,13 +1270,13 @@
1235 if( !g.perm.Read ){
1236 g.json.resultCode = FSL_JSON_E_DENIED;
1237 return NULL;
1238 }
1239 if( g.isHTTP ){
1240 brief = json_getenv_int("brief",0);
1241 }else{
1242 brief = (0!=find_option("brief","b",0));
1243 }
1244 #define SETBUF(O,K) cson_object_set(O, K, cson_value_new_string(zBuf, strlen(zBuf)));
1245
1246 jv = cson_value_new_object();
1247 jo = cson_value_get_object(jv);
@@ -1253,11 +1288,11 @@
1253 with a blob for this purpose.
1254 */
1255 fsize = file_size(g.zRepositoryName);
1256 cson_object_set(jo, "repositorySize", cson_value_new_integer((cson_int_t)fsize));
1257
1258 if(!brief){
1259 n = db_int(0, "SELECT count(*) FROM blob");
1260 m = db_int(0, "SELECT count(*) FROM delta");
1261 cson_object_set(jo, "blobCount", cson_value_new_integer((cson_int_t)n));
1262 cson_object_set(jo, "deltaCount", cson_value_new_integer((cson_int_t)m));
1263 if( n>0 ){
@@ -1292,11 +1327,11 @@
1292 " WHERE +tagname GLOB 'wiki-*'");
1293 cson_object_set(jo, "wikiPageCount", cson_value_new_integer((cson_int_t)n));
1294 n = db_int(0, "SELECT count(*) FROM tag /*scan*/"
1295 " WHERE +tagname GLOB 'tkt-*'");
1296 cson_object_set(jo, "ticketCount", cson_value_new_integer((cson_int_t)n));
1297 }/*!brief*/
1298 n = db_int(0, "SELECT julianday('now') - (SELECT min(mtime) FROM event)"
1299 " + 0.99");
1300 cson_object_set(jo, "ageDays", cson_value_new_integer((cson_int_t)n));
1301 cson_object_set(jo, "ageYears", cson_value_new_double(n/365.24));
1302 sqlite3_snprintf(BufLen, zBuf, db_get("project-code",""));
1303
--- src/json.c
+++ src/json.c
@@ -288,12 +288,12 @@
288
289 /*
290 ** Wrapper around json_getenv() which...
291 **
292 ** If it finds a value and that value is-a JSON number or is a string
293 ** which looks like an integer or is-a JSON bool/null then it is
294 ** converted to an int. If none of those apply then dflt is returned.
295 */
296 int json_getenv_int(char const * pKey, int dflt ){
297 cson_value const * v = json_getenv(pKey);
298 if(!v){
299 return dflt;
@@ -303,16 +303,51 @@
303 char const * sv = cson_string_cstr(cson_value_get_string(v));
304 assert( (NULL!=sv) && "This is quite unexpected." );
305 return sv ? atoi(sv) : dflt;
306 }else if( cson_value_is_bool(v) ){
307 return cson_value_get_bool(v) ? 1 : 0;
308 }else if( cson_value_is_null(v) ){
309 return 0;
310 }else{
311 /* we should arguably treat JSON null as 0. */
312 return dflt;
313 }
314 }
315
316
317 /*
318 ** Wrapper around json_getenv() which tries to evaluate a payload/env
319 ** value as a boolean. Uses mostly the same logic as
320 ** json_getenv_int(), with the addition that string values which
321 ** either start with a digit 1..9 or the letters [tT] are considered
322 ** to be true. If this function cannot find a matching key/value then
323 ** dflt is returned. e.g. if it finds the key but the value is-a
324 ** Object then dftl is returned.
325 */
326 char json_getenv_bool(char const * pKey, char dflt ){
327 cson_value const * v = json_getenv(pKey);
328 if(!v){
329 return dflt;
330 }else if( cson_value_is_number(v) ){
331 return cson_value_get_integer(v) ? 1 : 0;
332 }else if( cson_value_is_string(v) ){
333 char const * sv = cson_string_cstr(cson_value_get_string(v));
334 if(!*sv || ('0'==*sv)){
335 return 0;
336 }else{
337 return (('t'==*sv) || ('T'==*sv)
338 || (('1'<=*sv) && ('9'>=*sv)))
339 ? 1 : 0;
340 }
341 }else if( cson_value_is_bool(v) ){
342 return cson_value_get_bool(v) ? 1 : 0;
343 }else if( cson_value_is_null(v) ){
344 return 0;
345 }else{
346 return dflt;
347 }
348 }
349
350 /*
351 ** Returns the string form of a json_getenv() value, but ONLY If that
352 ** value is-a String. Non-strings are not converted to strings for
353 ** this purpose. Returned memory is owned by g.json or fossil and is
@@ -1222,11 +1257,11 @@
1257 **
1258 */
1259 cson_value * json_page_stat(){
1260 i64 t, fsize;
1261 int n, m;
1262 int full;
1263 const char *zDb;
1264 enum { BufLen = 1000 };
1265 char zBuf[BufLen];
1266 cson_value * jv = NULL;
1267 cson_object * jo = NULL;
@@ -1235,13 +1270,13 @@
1270 if( !g.perm.Read ){
1271 g.json.resultCode = FSL_JSON_E_DENIED;
1272 return NULL;
1273 }
1274 if( g.isHTTP ){
1275 full = json_getenv_bool("full",0);
1276 }else{
1277 full = (0!=find_option("full","f",0));
1278 }
1279 #define SETBUF(O,K) cson_object_set(O, K, cson_value_new_string(zBuf, strlen(zBuf)));
1280
1281 jv = cson_value_new_object();
1282 jo = cson_value_get_object(jv);
@@ -1253,11 +1288,11 @@
1288 with a blob for this purpose.
1289 */
1290 fsize = file_size(g.zRepositoryName);
1291 cson_object_set(jo, "repositorySize", cson_value_new_integer((cson_int_t)fsize));
1292
1293 if(full){
1294 n = db_int(0, "SELECT count(*) FROM blob");
1295 m = db_int(0, "SELECT count(*) FROM delta");
1296 cson_object_set(jo, "blobCount", cson_value_new_integer((cson_int_t)n));
1297 cson_object_set(jo, "deltaCount", cson_value_new_integer((cson_int_t)m));
1298 if( n>0 ){
@@ -1292,11 +1327,11 @@
1327 " WHERE +tagname GLOB 'wiki-*'");
1328 cson_object_set(jo, "wikiPageCount", cson_value_new_integer((cson_int_t)n));
1329 n = db_int(0, "SELECT count(*) FROM tag /*scan*/"
1330 " WHERE +tagname GLOB 'tkt-*'");
1331 cson_object_set(jo, "ticketCount", cson_value_new_integer((cson_int_t)n));
1332 }/*full*/
1333 n = db_int(0, "SELECT julianday('now') - (SELECT min(mtime) FROM event)"
1334 " + 0.99");
1335 cson_object_set(jo, "ageDays", cson_value_new_integer((cson_int_t)n));
1336 cson_object_set(jo, "ageYears", cson_value_new_double(n/365.24));
1337 sqlite3_snprintf(BufLen, zBuf, db_get("project-code",""));
1338

Keyboard Shortcuts

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