Check-in [cde55bd7a3]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
SHA1 Hash:cde55bd7a3dc7c5eeb920ff085de1c72955aed1d
Date: 2012-07-05 19:03:18
User: stephan
Comment:Pulled in latest cson amalgamation for the cson_type_id addition and started refactoring some O(N) lookups to O(1) based on that addition.
Tags And Properties
Changes

Changes to src/cson_amalgamation.c

1420 1420 1421 #if defined(__cplusplus) 1421 #if defined(__cplusplus) 1422 extern "C" { 1422 extern "C" { 1423 #endif 1423 #endif 1424 1424 1425 1425 1426 1426 1427 /** < 1428 Type IDs corresponding to JavaScript/JSON types. < 1429 */ < 1430 enum cson_type_id { < 1431 /** < 1432 The special "undefined" value constant. < 1433 < 1434 Its value must be 0 for internal reasons. < 1435 */ < 1436 CSON_TYPE_UNDEF = 0, < 1437 /** < 1438 The special "null" value constant. < 1439 */ < 1440 CSON_TYPE_NULL = 1, < 1441 /** < 1442 The bool value type. < 1443 */ < 1444 CSON_TYPE_BOOL = 2, < 1445 /** < 1446 The integer value type, represented in this library < 1447 by cson_int_t. < 1448 */ < 1449 CSON_TYPE_INTEGER = 3, < 1450 /** < 1451 The double value type, represented in this library < 1452 by cson_double_t. < 1453 */ < 1454 CSON_TYPE_DOUBLE = 4, < 1455 /** The immutable string type. This library stores strings < 1456 as immutable UTF8. < 1457 */ < 1458 CSON_TYPE_STRING = 5, < 1459 /** The "Array" type. */ < 1460 CSON_TYPE_ARRAY = 6, < 1461 /** The "Object" type. */ < 1462 CSON_TYPE_OBJECT = 7 < 1463 }; < 1464 typedef enum cson_type_id cson_type_id; < 1465 1427 1466 /** 1428 /** 1467 This type holds the "vtbl" for type-specific operations when 1429 This type holds the "vtbl" for type-specific operations when 1468 working with cson_value objects. 1430 working with cson_value objects. 1469 1431 1470 All cson_values of a given logical type share a pointer to a single 1432 All cson_values of a given logical type share a pointer to a single 1471 library-internal instance of this class. 1433 library-internal instance of this class. ................................................................................................................................................................................ 2312 /** Returns true if v is not NULL and has the given type ID. */ 2274 /** Returns true if v is not NULL and has the given type ID. */ 2313 static char cson_value_is_a( cson_value const * v, cson_type_id is ) 2275 static char cson_value_is_a( cson_value const * v, cson_type_id is ) 2314 { 2276 { 2315 return (v && v->api && (v->api->typeID == is)) ? 1 : 0; 2277 return (v && v->api && (v->api->typeID == is)) ? 1 : 0; 2316 } 2278 } 2317 #endif 2279 #endif 2318 2280 2319 #if 0 < 2320 cson_type_id cson_value_type_id( cson_value const * v ) 2281 cson_type_id cson_value_type_id( cson_value const * v ) 2321 { 2282 { 2322 return (v && v->api) ? v->api->typeID : CSON_TYPE_UNDEF; 2283 return (v && v->api) ? v->api->typeID : CSON_TYPE_UNDEF; 2323 } 2284 } 2324 #endif < 2325 2285 2326 char cson_value_is_undef( cson_value const * v ) 2286 char cson_value_is_undef( cson_value const * v ) 2327 { 2287 { 2328 return ( !v || !v->api || (v->api==&cson_value_api_undef)) 2288 return ( !v || !v->api || (v->api==&cson_value_api_undef)) 2329 ? 1 : 0; 2289 ? 1 : 0; 2330 } 2290 } 2331 #define ISA(T,TID) char cson_value_is_##T( cson_value const * v ) { \ 2291 #define ISA(T,TID) char cson_value_is_##T( cson_value const * v ) { \ ................................................................................................................................................................................ 4470 4430 4471 - (inp) = is a pointer to the pointer to the start of the input. 4431 - (inp) = is a pointer to the pointer to the start of the input. 4472 4432 4473 - (separator) = the separator character 4433 - (separator) = the separator character 4474 4434 4475 - (end) = a pointer to NULL. i.e. (*end == NULL) 4435 - (end) = a pointer to NULL. i.e. (*end == NULL) 4476 4436 4477 This function scans *inp for the given separator char or a NULL char. | 4437 This function scans *inp for the given separator char or a NUL char. 4478 Successive separators at the start of *inp are skipped. The effect is 4438 Successive separators at the start of *inp are skipped. The effect is 4479 that, when this function is called in a loop, all neighboring 4439 that, when this function is called in a loop, all neighboring 4480 separators are ignored. e.g. the string "aa.bb...cc" will tokenize to 4440 separators are ignored. e.g. the string "aa.bb...cc" will tokenize to 4481 the list (aa,bb,cc) if the separator is '.' and to (aa.,...cc) if the 4441 the list (aa,bb,cc) if the separator is '.' and to (aa.,...cc) if the 4482 separator is 'b'. 4442 separator is 'b'. 4483 4443 4484 Returns 0 (false) if it finds no token, else non-0 (true). 4444 Returns 0 (false) if it finds no token, else non-0 (true).

Changes to src/cson_amalgamation.h

130 */ 130 */ 131 131 132 /** @def CSON_DOUBLE_T_PFMT 132 /** @def CSON_DOUBLE_T_PFMT 133 133 134 printf()-compatible format token for cson_double_t. 134 printf()-compatible format token for cson_double_t. 135 */ 135 */ 136 136 > 137 /** > 138 Type IDs corresponding to JavaScript/JSON types. > 139 > 140 These are only in the public API to allow O(1) client-side > 141 dispatching based on cson_value types. > 142 */ > 143 enum cson_type_id { > 144 /** > 145 The special "undefined" value constant. > 146 > 147 Its value must be 0 for internal reasons. > 148 */ > 149 CSON_TYPE_UNDEF = 0, > 150 /** > 151 The special "null" value constant. > 152 */ > 153 CSON_TYPE_NULL = 1, > 154 /** > 155 The bool value type. > 156 */ > 157 CSON_TYPE_BOOL = 2, > 158 /** > 159 The integer value type, represented in this library > 160 by cson_int_t. > 161 */ > 162 CSON_TYPE_INTEGER = 3, > 163 /** > 164 The double value type, represented in this library > 165 by cson_double_t. > 166 */ > 167 CSON_TYPE_DOUBLE = 4, > 168 /** The immutable string type. This library stores strings > 169 as immutable UTF8. > 170 */ > 171 CSON_TYPE_STRING = 5, > 172 /** The "Array" type. */ > 173 CSON_TYPE_ARRAY = 6, > 174 /** The "Object" type. */ > 175 CSON_TYPE_OBJECT = 7 > 176 }; > 177 /** > 178 Convenience typedef. > 179 */ > 180 typedef enum cson_type_id cson_type_id; > 181 > 182 > 183 /** > 184 Convenience typedef. > 185 */ 137 typedef struct cson_value cson_value; 186 typedef struct cson_value cson_value; 138 187 139 /** @struct cson_value 188 /** @struct cson_value 140 189 141 The core value type of this API. It is opaque to clients, and 190 The core value type of this API. It is opaque to clients, and 142 only the cson public API should be used for setting or 191 only the cson public API should be used for setting or 143 inspecting their values. 192 inspecting their values. ................................................................................................................................................................................ 209 @see cson_value_new_integer() 258 @see cson_value_new_integer() 210 @see cson_value_new_double() 259 @see cson_value_new_double() 211 @see cson_value_new_bool() 260 @see cson_value_new_bool() 212 @see cson_value_true() 261 @see cson_value_true() 213 @see cson_value_false() 262 @see cson_value_false() 214 @see cson_value_null() 263 @see cson_value_null() 215 @see cson_value_free() 264 @see cson_value_free() > 265 @see cson_value_type_id() 216 */ 266 */ 217 267 218 /** @var cson_rc 268 /** @var cson_rc 219 269 220 This object defines the error codes used by cson. 270 This object defines the error codes used by cson. 221 271 222 Library routines which return int values almost always return a 272 Library routines which return int values almost always return a ................................................................................................................................................................................ 738 /** 788 /** 739 Convenience wrapper around cson_output_FILE() which writes to the given filen 789 Convenience wrapper around cson_output_FILE() which writes to the given filen 740 any existing contents. Returns cson_rc.IOError if the file cannot be opened. 790 any existing contents. Returns cson_rc.IOError if the file cannot be opened. 741 791 742 @see cson_output_FILE() 792 @see cson_output_FILE() 743 */ 793 */ 744 int cson_output_filename( cson_value const * src, char const * dest, cson_output 794 int cson_output_filename( cson_value const * src, char const * dest, cson_output > 795 > 796 /** > 797 Returns the virtual type of v, or CSON_TYPE_UNDEF if !v. > 798 */ > 799 cson_type_id cson_value_type_id( cson_value const * v ); > 800 745 801 746 /** Returns true if v is null, v->api is NULL, or v holds the special undefined 802 /** Returns true if v is null, v->api is NULL, or v holds the special undefined 747 char cson_value_is_undef( cson_value const * v ); 803 char cson_value_is_undef( cson_value const * v ); 748 /** Returns true if v contains a null value. */ 804 /** Returns true if v contains a null value. */ 749 char cson_value_is_null( cson_value const * v ); 805 char cson_value_is_null( cson_value const * v ); 750 /** Returns true if v contains a bool value. */ 806 /** Returns true if v contains a bool value. */ 751 char cson_value_is_bool( cson_value const * v ); 807 char cson_value_is_bool( cson_value const * v );

Changes to src/json.c

173 173 174 #else 174 #else 175 #define BEGIN_TIMER 175 #define BEGIN_TIMER 176 #define END_TIMER 0.0 176 #define END_TIMER 0.0 177 #define HAS_TIMER 0 177 #define HAS_TIMER 0 178 #endif 178 #endif 179 179 180 | 180 /* > 181 ** Returns true (non-0) if fossil appears to be running in JSON mode. > 182 */ 181 char fossil_has_json(){ 183 char fossil_has_json(){ 182 return g.json.isJsonMode && (g.isHTTP || g.json.post.o); 184 return g.json.isJsonMode && (g.isHTTP || g.json.post.o); 183 } 185 } 184 186 185 /* 187 /* 186 ** Placeholder /json/XXX page impl for NYI (Not Yet Implemented) 188 ** Placeholder /json/XXX page impl for NYI (Not Yet Implemented) 187 ** (but planned) pages/commands. 189 ** (but planned) pages/commands. ................................................................................................................................................................................ 465 ** 467 ** 466 ** If it finds a value and that value is-a JSON number or is a string 468 ** If it finds a value and that value is-a JSON number or is a string 467 ** which looks like an integer or is-a JSON bool/null then it is 469 ** which looks like an integer or is-a JSON bool/null then it is 468 ** converted to an int. If none of those apply then dflt is returned. 470 ** converted to an int. If none of those apply then dflt is returned. 469 */ 471 */ 470 int json_getenv_int(char const * pKey, int dflt ){ 472 int json_getenv_int(char const * pKey, int dflt ){ 471 cson_value const * v = json_getenv(pKey); 473 cson_value const * v = json_getenv(pKey); 472 if(!v){ | 474 const cson_type_id type = v ? cson_value_type_id(v) : CSON_TYPE_UNDEF; 473 return dflt; | 475 switch(type){ 474 }else if( cson_value_is_number(v) ){ | 476 case CSON_TYPE_INTEGER: > 477 case CSON_TYPE_DOUBLE: 475 return (int)cson_value_get_integer(v); | 478 return (int)cson_value_get_integer(v); 476 }else if( cson_value_is_string(v) ){ < > 479 case CSON_TYPE_STRING: { 477 char const * sv = cson_string_cstr(cson_value_get_string(v)); | 480 char const * sv = cson_string_cstr(cson_value_get_string(v)); 478 assert( (NULL!=sv) && "This is quite unexpected." ); | 481 assert( (NULL!=sv) && "This is quite unexpected." ); 479 return sv ? atoi(sv) : dflt; | 482 return sv ? atoi(sv) : dflt; 480 }else if( cson_value_is_bool(v) ){ < > 483 } > 484 case CSON_TYPE_BOOL: 481 return cson_value_get_bool(v) ? 1 : 0; | 485 return cson_value_get_bool(v) ? 1 : 0; 482 }else if( cson_value_is_null(v) ){ < > 486 case CSON_TYPE_NULL: 483 return 0; | 487 return 0; 484 }else{ < 485 /* we should arguably treat JSON null as 0. */ < > 488 default: 486 return dflt; | 489 return dflt; 487 } 490 } 488 } 491 } 489 492 490 493 491 /* 494 /* 492 ** Wrapper around json_getenv() which tries to evaluate a payload/env 495 ** Wrapper around json_getenv() which tries to evaluate a payload/env 493 ** value as a boolean. Uses mostly the same logic as 496 ** value as a boolean. Uses mostly the same logic as ................................................................................................................................................................................ 501 ** either 0 or 1, as opposed to "0 or non-zero", so that clients can 504 ** either 0 or 1, as opposed to "0 or non-zero", so that clients can 502 ** pass a different value as dflt. Thus they can use, e.g. -1 to know 505 ** pass a different value as dflt. Thus they can use, e.g. -1 to know 503 ** whether or not this function found a match (it will return -1 in 506 ** whether or not this function found a match (it will return -1 in 504 ** that case). 507 ** that case). 505 */ 508 */ 506 char json_getenv_bool(char const * pKey, char dflt ){ 509 char json_getenv_bool(char const * pKey, char dflt ){ 507 cson_value const * v = json_getenv(pKey); 510 cson_value const * v = json_getenv(pKey); 508 if(!v){ | 511 const cson_type_id type = v ? cson_value_type_id(v) : CSON_TYPE_UNDEF; 509 return dflt; | 512 switch(type){ 510 }else if( cson_value_is_number(v) ){ | 513 case CSON_TYPE_INTEGER: > 514 case CSON_TYPE_DOUBLE: 511 return cson_value_get_integer(v) ? 1 : 0; | 515 return cson_value_get_integer(v) ? 1 : 0; 512 }else if( cson_value_is_string(v) ){ < > 516 case CSON_TYPE_STRING: { 513 char const * sv = cson_string_cstr(cson_value_get_string(v)); | 517 char const * sv = cson_string_cstr(cson_value_get_string(v)); > 518 assert( (NULL!=sv) && "This is quite unexpected." ); 514 if(!*sv || ('0'==*sv)){ | 519 if(!*sv || ('0'==*sv)){ 515 return 0; | 520 return 0; 516 }else{ | 521 }else{ 517 return ((('1'<=*sv) && ('9'>=*sv)) | 522 return ((('1'<=*sv) && ('9'>=*sv)) 518 || ('t'==*sv) || ('T'==*sv) | 523 || ('t'==*sv) || ('T'==*sv) 519 || ('y'==*sv) || ('Y'==*sv) | 524 || ('y'==*sv) || ('Y'==*sv) 520 ) | 525 ) 521 ? 1 : 0; | 526 ? 1 : 0; 522 } | 527 } 523 }else if( cson_value_is_bool(v) ){ < > 528 } > 529 case CSON_TYPE_BOOL: 524 return cson_value_get_bool(v) ? 1 : 0; | 530 return cson_value_get_bool(v) ? 1 : 0; 525 }else if( cson_value_is_null(v) ){ < > 531 case CSON_TYPE_NULL: 526 return 0; | 532 return 0; 527 }else{ < > 533 default: 528 return dflt; | 534 return dflt; 529 } 535 } 530 } 536 } 531 537 532 /* 538 /* 533 ** Returns the string form of a json_getenv() value, but ONLY If that 539 ** Returns the string form of a json_getenv() value, but ONLY If that 534 ** value is-a String. Non-strings are not converted to strings for 540 ** value is-a String. Non-strings are not converted to strings for 535 ** this purpose. Returned memory is owned by g.json or fossil and is 541 ** this purpose. Returned memory is owned by g.json or fossil and is ................................................................................................................................................................................ 753 g.json.authToken = json_getenv(FossilJsonKeys.authToken); 759 g.json.authToken = json_getenv(FossilJsonKeys.authToken); 754 if(g.json.authToken 760 if(g.json.authToken 755 && cson_value_is_string(g.json.authToken) 761 && cson_value_is_string(g.json.authToken) 756 && !PD(login_cookie_name(),NULL)){ 762 && !PD(login_cookie_name(),NULL)){ 757 /* tell fossil to use this login info. 763 /* tell fossil to use this login info. 758 764 759 FIXME?: because the JSON bits don't carry around 765 FIXME?: because the JSON bits don't carry around 760 login_cookie_name(), there is a potential login hijacking | 766 login_cookie_name(), there is(?) a potential(?) login hijacking 761 window here. We may need to change the JSON auth token to be | 767 window here. We may need to change the JSON auth token to be in 762 in the form: login_cookie_name()=... | 768 the form: login_cookie_name()=... 763 769 764 Then again, the hardened cookie value helps ensure that 770 Then again, the hardened cookie value helps ensure that 765 only a proper key/value match is valid. 771 only a proper key/value match is valid. 766 */ 772 */ 767 cgi_replace_parameter( login_cookie_name(), cson_value_get_cstr(g.json.aut 773 cgi_replace_parameter( login_cookie_name(), cson_value_get_cstr(g.json.aut 768 }else if( g.isHTTP ){ 774 }else if( g.isHTTP ){ 769 /* try fossil's conventional cookie. */ 775 /* try fossil's conventional cookie. */