Fossil SCM
Latest upstream cson - increases a unicode output buffer size after reports of certain MSVC version(s) complaining about it.
Commit
e508424e7d7863e1133d9ea2aa685ab374396cda38578ae8162a3da91fda1647
Parent
754a79c137f6ede…
2 files changed
+45
-45
+74
-74
+45
-45
| --- src/cson_amalgamation.c | ||
| +++ src/cson_amalgamation.c | ||
| @@ -19,14 +19,14 @@ | ||
| 19 | 19 | # define JSON_PARSER_DLL_API __declspec(dllexport) |
| 20 | 20 | # else |
| 21 | 21 | # define JSON_PARSER_DLL_API __declspec(dllimport) |
| 22 | 22 | # endif |
| 23 | 23 | # else |
| 24 | -# define JSON_PARSER_DLL_API | |
| 24 | +# define JSON_PARSER_DLL_API | |
| 25 | 25 | # endif |
| 26 | 26 | #else |
| 27 | -# define JSON_PARSER_DLL_API | |
| 27 | +# define JSON_PARSER_DLL_API | |
| 28 | 28 | #endif |
| 29 | 29 | |
| 30 | 30 | /* Determine the integer type use to parse non-floating point numbers */ |
| 31 | 31 | #ifdef _WIN32 |
| 32 | 32 | typedef __int64 JSON_int_t; |
| @@ -34,22 +34,22 @@ | ||
| 34 | 34 | #define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%I64d" |
| 35 | 35 | #elif (__STDC_VERSION__ >= 199901L) || (HAVE_LONG_LONG == 1) |
| 36 | 36 | typedef long long JSON_int_t; |
| 37 | 37 | #define JSON_PARSER_INTEGER_SSCANF_TOKEN "%lld" |
| 38 | 38 | #define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%lld" |
| 39 | -#else | |
| 39 | +#else | |
| 40 | 40 | typedef long JSON_int_t; |
| 41 | 41 | #define JSON_PARSER_INTEGER_SSCANF_TOKEN "%ld" |
| 42 | 42 | #define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%ld" |
| 43 | 43 | #endif |
| 44 | 44 | |
| 45 | 45 | |
| 46 | 46 | #ifdef __cplusplus |
| 47 | 47 | extern "C" { |
| 48 | -#endif | |
| 48 | +#endif | |
| 49 | 49 | |
| 50 | -typedef enum | |
| 50 | +typedef enum | |
| 51 | 51 | { |
| 52 | 52 | JSON_E_NONE = 0, |
| 53 | 53 | JSON_E_INVALID_CHAR, |
| 54 | 54 | JSON_E_INVALID_KEYWORD, |
| 55 | 55 | JSON_E_INVALID_ESCAPE_SEQUENCE, |
| @@ -60,11 +60,11 @@ | ||
| 60 | 60 | JSON_E_EXPECTED_KEY, |
| 61 | 61 | JSON_E_EXPECTED_COLON, |
| 62 | 62 | JSON_E_OUT_OF_MEMORY |
| 63 | 63 | } JSON_error; |
| 64 | 64 | |
| 65 | -typedef enum | |
| 65 | +typedef enum | |
| 66 | 66 | { |
| 67 | 67 | JSON_T_NONE = 0, |
| 68 | 68 | JSON_T_ARRAY_BEGIN, |
| 69 | 69 | JSON_T_ARRAY_END, |
| 70 | 70 | JSON_T_OBJECT_BEGIN, |
| @@ -80,33 +80,33 @@ | ||
| 80 | 80 | } JSON_type; |
| 81 | 81 | |
| 82 | 82 | typedef struct JSON_value_struct { |
| 83 | 83 | union { |
| 84 | 84 | JSON_int_t integer_value; |
| 85 | - | |
| 85 | + | |
| 86 | 86 | double float_value; |
| 87 | - | |
| 87 | + | |
| 88 | 88 | struct { |
| 89 | 89 | const char* value; |
| 90 | 90 | size_t length; |
| 91 | 91 | } str; |
| 92 | 92 | } vu; |
| 93 | 93 | } JSON_value; |
| 94 | 94 | |
| 95 | 95 | typedef struct JSON_parser_struct* JSON_parser; |
| 96 | 96 | |
| 97 | -/*! \brief JSON parser callback | |
| 97 | +/*! \brief JSON parser callback | |
| 98 | 98 | |
| 99 | 99 | \param ctx The pointer passed to new_JSON_parser. |
| 100 | - \param type An element of JSON_type but not JSON_T_NONE. | |
| 100 | + \param type An element of JSON_type but not JSON_T_NONE. | |
| 101 | 101 | \param value A representation of the parsed value. This parameter is NULL for |
| 102 | 102 | JSON_T_ARRAY_BEGIN, JSON_T_ARRAY_END, JSON_T_OBJECT_BEGIN, JSON_T_OBJECT_END, |
| 103 | 103 | JSON_T_NULL, JSON_T_TRUE, and JSON_T_FALSE. String values are always returned |
| 104 | 104 | as zero-terminated C strings. |
| 105 | 105 | |
| 106 | 106 | \return Non-zero if parsing should continue, else zero. |
| 107 | -*/ | |
| 107 | +*/ | |
| 108 | 108 | typedef int (*JSON_parser_callback)(void* ctx, int type, const JSON_value* value); |
| 109 | 109 | |
| 110 | 110 | |
| 111 | 111 | /** |
| 112 | 112 | A typedef for allocator functions semantically compatible with malloc(). |
| @@ -115,11 +115,11 @@ | ||
| 115 | 115 | /** |
| 116 | 116 | A typedef for deallocator functions semantically compatible with free(). |
| 117 | 117 | */ |
| 118 | 118 | typedef void (*JSON_free_t)(void* mem); |
| 119 | 119 | |
| 120 | -/*! \brief The structure used to configure a JSON parser object | |
| 120 | +/*! \brief The structure used to configure a JSON parser object | |
| 121 | 121 | */ |
| 122 | 122 | typedef struct { |
| 123 | 123 | /** Pointer to a callback, called when the parser has something to tell |
| 124 | 124 | the user. This parameter may be NULL. In this case the input is |
| 125 | 125 | merely checked for validity. |
| @@ -176,17 +176,17 @@ | ||
| 176 | 176 | |
| 177 | 177 | \param config. Used to configure the parser. |
| 178 | 178 | */ |
| 179 | 179 | JSON_PARSER_DLL_API void init_JSON_config(JSON_config * config); |
| 180 | 180 | |
| 181 | -/*! \brief Create a JSON parser object | |
| 181 | +/*! \brief Create a JSON parser object | |
| 182 | 182 | |
| 183 | 183 | \param config. Used to configure the parser. Set to NULL to use |
| 184 | 184 | the default configuration. See init_JSON_config. Its contents are |
| 185 | 185 | copied by this function, so it need not outlive the returned |
| 186 | 186 | object. |
| 187 | - | |
| 187 | + | |
| 188 | 188 | \return The parser object, which is owned by the caller and must eventually |
| 189 | 189 | be freed by calling delete_JSON_parser(). |
| 190 | 190 | */ |
| 191 | 191 | JSON_PARSER_DLL_API JSON_parser new_JSON_parser(JSON_config const* config); |
| 192 | 192 | |
| @@ -200,16 +200,16 @@ | ||
| 200 | 200 | JSON_PARSER_DLL_API int JSON_parser_char(JSON_parser jc, int next_char); |
| 201 | 201 | |
| 202 | 202 | /*! \brief Finalize parsing. |
| 203 | 203 | |
| 204 | 204 | Call this method once after all input characters have been consumed. |
| 205 | - | |
| 205 | + | |
| 206 | 206 | \return Non-zero, if all parsed characters are valid JSON, zero otherwise. |
| 207 | 207 | */ |
| 208 | 208 | JSON_PARSER_DLL_API int JSON_parser_done(JSON_parser jc); |
| 209 | 209 | |
| 210 | -/*! \brief Determine if a given string is valid JSON white space | |
| 210 | +/*! \brief Determine if a given string is valid JSON white space | |
| 211 | 211 | |
| 212 | 212 | \return Non-zero if the string is valid, zero otherwise. |
| 213 | 213 | */ |
| 214 | 214 | JSON_PARSER_DLL_API int JSON_parser_is_legal_white_space_string(const char* s); |
| 215 | 215 | |
| @@ -226,12 +226,12 @@ | ||
| 226 | 226 | JSON_PARSER_DLL_API int JSON_parser_reset(JSON_parser jc); |
| 227 | 227 | |
| 228 | 228 | |
| 229 | 229 | #ifdef __cplusplus |
| 230 | 230 | } |
| 231 | -#endif | |
| 232 | - | |
| 231 | +#endif | |
| 232 | + | |
| 233 | 233 | |
| 234 | 234 | #endif /* JSON_PARSER_H */ |
| 235 | 235 | /* end file parser/JSON_parser.h */ |
| 236 | 236 | /* begin file parser/JSON_parser.c */ |
| 237 | 237 | /* |
| @@ -1431,11 +1431,11 @@ | ||
| 1431 | 1431 | #if defined(__cplusplus) |
| 1432 | 1432 | extern "C" { |
| 1433 | 1433 | #endif |
| 1434 | 1434 | |
| 1435 | 1435 | |
| 1436 | - | |
| 1436 | + | |
| 1437 | 1437 | /** |
| 1438 | 1438 | This type holds the "vtbl" for type-specific operations when |
| 1439 | 1439 | working with cson_value objects. |
| 1440 | 1440 | |
| 1441 | 1441 | All cson_values of a given logical type share a pointer to a single |
| @@ -1583,11 +1583,11 @@ | ||
| 1583 | 1583 | */ |
| 1584 | 1584 | #define CSON_CAST(T,V) ((T*)((V)->value)) |
| 1585 | 1585 | /** |
| 1586 | 1586 | Assumes V is a pointer to memory which is allocated as part of a |
| 1587 | 1587 | cson_value instance (the bytes immediately after that part). |
| 1588 | - Returns a pointer a cson_value by subtracting sizeof(cson_value) | |
| 1588 | + Returns a pointer a a cson_value by subtracting sizeof(cson_value) | |
| 1589 | 1589 | from that address and casting it to a (cson_value*) |
| 1590 | 1590 | */ |
| 1591 | 1591 | #define CSON_VCAST(V) ((cson_value *)(((unsigned char *)(V))-sizeof(cson_value))) |
| 1592 | 1592 | |
| 1593 | 1593 | /** |
| @@ -1607,11 +1607,11 @@ | ||
| 1607 | 1607 | #define CSON_OBJ(V) CSON_CAST(cson_object,(V)) |
| 1608 | 1608 | #define CSON_ARRAY(V) CSON_CAST(cson_array,(V)) |
| 1609 | 1609 | |
| 1610 | 1610 | /** |
| 1611 | 1611 | Holds special shared "constant" (though they are non-const) |
| 1612 | - values. | |
| 1612 | + values. | |
| 1613 | 1613 | */ |
| 1614 | 1614 | static struct CSON_EMPTY_HOLDER_ |
| 1615 | 1615 | { |
| 1616 | 1616 | char trueValue; |
| 1617 | 1617 | cson_string stringValue; |
| @@ -1620,17 +1620,17 @@ | ||
| 1620 | 1620 | cson_string_empty_m |
| 1621 | 1621 | }; |
| 1622 | 1622 | |
| 1623 | 1623 | /** |
| 1624 | 1624 | Indexes into the CSON_SPECIAL_VALUES array. |
| 1625 | - | |
| 1625 | + | |
| 1626 | 1626 | If this enum changes in any way, |
| 1627 | 1627 | makes damned sure that CSON_SPECIAL_VALUES is updated |
| 1628 | 1628 | to match!!! |
| 1629 | 1629 | */ |
| 1630 | 1630 | enum CSON_INTERNAL_VALUES { |
| 1631 | - | |
| 1631 | + | |
| 1632 | 1632 | CSON_VAL_UNDEF = 0, |
| 1633 | 1633 | CSON_VAL_NULL = 1, |
| 1634 | 1634 | CSON_VAL_TRUE = 2, |
| 1635 | 1635 | CSON_VAL_FALSE = 3, |
| 1636 | 1636 | CSON_VAL_INT_0 = 4, |
| @@ -1642,11 +1642,11 @@ | ||
| 1642 | 1642 | /** |
| 1643 | 1643 | Some "special" shared cson_value instances. |
| 1644 | 1644 | |
| 1645 | 1645 | These values MUST be initialized in the order specified |
| 1646 | 1646 | by the CSON_INTERNAL_VALUES enum. |
| 1647 | - | |
| 1647 | + | |
| 1648 | 1648 | Note that they are not const because they are used as |
| 1649 | 1649 | shared-allocation objects in non-const contexts. However, the |
| 1650 | 1650 | public API provides no way to modifying them, and clients who |
| 1651 | 1651 | modify values directly are subject to The Wrath of Undefined |
| 1652 | 1652 | Behaviour. |
| @@ -1665,11 +1665,11 @@ | ||
| 1665 | 1665 | |
| 1666 | 1666 | /** |
| 1667 | 1667 | Returns non-0 (true) if m is one of our special |
| 1668 | 1668 | "built-in" values, e.g. from CSON_SPECIAL_VALUES and some |
| 1669 | 1669 | "empty" values. |
| 1670 | - | |
| 1670 | + | |
| 1671 | 1671 | If this returns true, m MUST NOT be free()d! |
| 1672 | 1672 | */ |
| 1673 | 1673 | static char cson_value_is_builtin( void const * m ) |
| 1674 | 1674 | { |
| 1675 | 1675 | if((m >= (void const *)&CSON_EMPTY_HOLDER) |
| @@ -2183,11 +2183,11 @@ | ||
| 2183 | 2183 | static int cson_value_list_visit( cson_value_list * self, |
| 2184 | 2184 | int (*visitor)(cson_value * obj, void * visitorState ), |
| 2185 | 2185 | void * visitorState ); |
| 2186 | 2186 | #endif |
| 2187 | 2187 | #endif |
| 2188 | - | |
| 2188 | + | |
| 2189 | 2189 | #if 0 |
| 2190 | 2190 | # define LIST_T cson_value_list |
| 2191 | 2191 | # define VALUE_T cson_value * |
| 2192 | 2192 | # define VALUE_T_IS_PTR 1 |
| 2193 | 2193 | # define LIST_T cson_kvp_list |
| @@ -2362,11 +2362,11 @@ | ||
| 2362 | 2362 | return cson_value_object_alloc(); |
| 2363 | 2363 | } |
| 2364 | 2364 | |
| 2365 | 2365 | cson_object * cson_new_object() |
| 2366 | 2366 | { |
| 2367 | - | |
| 2367 | + | |
| 2368 | 2368 | return cson_value_get_object( cson_value_new_object() ); |
| 2369 | 2369 | } |
| 2370 | 2370 | |
| 2371 | 2371 | cson_value * cson_value_new_array() |
| 2372 | 2372 | { |
| @@ -2608,11 +2608,11 @@ | ||
| 2608 | 2608 | { |
| 2609 | 2609 | cson_int_t i = 0; |
| 2610 | 2610 | int rc = 0; |
| 2611 | 2611 | switch(val->api->typeID) |
| 2612 | 2612 | { |
| 2613 | - case CSON_TYPE_UNDEF: | |
| 2613 | + case CSON_TYPE_UNDEF: | |
| 2614 | 2614 | case CSON_TYPE_NULL: |
| 2615 | 2615 | i = 0; |
| 2616 | 2616 | break; |
| 2617 | 2617 | case CSON_TYPE_BOOL: { |
| 2618 | 2618 | char b = 0; |
| @@ -2661,11 +2661,11 @@ | ||
| 2661 | 2661 | { |
| 2662 | 2662 | cson_double_t d = 0.0; |
| 2663 | 2663 | int rc = 0; |
| 2664 | 2664 | switch(val->api->typeID) |
| 2665 | 2665 | { |
| 2666 | - case CSON_TYPE_UNDEF: | |
| 2666 | + case CSON_TYPE_UNDEF: | |
| 2667 | 2667 | case CSON_TYPE_NULL: |
| 2668 | 2668 | d = 0; |
| 2669 | 2669 | break; |
| 2670 | 2670 | case CSON_TYPE_BOOL: { |
| 2671 | 2671 | char b = 0; |
| @@ -2791,11 +2791,11 @@ | ||
| 2791 | 2791 | #if 0 |
| 2792 | 2792 | /** |
| 2793 | 2793 | Removes and returns the last value from the given array, |
| 2794 | 2794 | shrinking its size by 1. Returns NULL if ar is NULL, |
| 2795 | 2795 | ar->list.count is 0, or the element at that index is NULL. |
| 2796 | - | |
| 2796 | + | |
| 2797 | 2797 | |
| 2798 | 2798 | If removeRef is true then cson_value_free() is called to remove |
| 2799 | 2799 | ar's reference count for the value. In that case NULL is returned, |
| 2800 | 2800 | even if the object still has live references. If removeRef is false |
| 2801 | 2801 | then the caller takes over ownership of that reference count point. |
| @@ -2858,11 +2858,11 @@ | ||
| 2858 | 2858 | #if !defined(NDEBUG) && CSON_VOID_PTR_IS_BIG |
| 2859 | 2859 | assert( sizeof(cson_int_t) <= sizeof(void *) ); |
| 2860 | 2860 | #endif |
| 2861 | 2861 | if( c ) |
| 2862 | 2862 | { |
| 2863 | - memcpy(CSON_INT(c), &v, sizeof(v)); | |
| 2863 | + memcpy( CSON_INT(c), &v, sizeof(v) ); | |
| 2864 | 2864 | } |
| 2865 | 2865 | return c; |
| 2866 | 2866 | } |
| 2867 | 2867 | } |
| 2868 | 2868 | |
| @@ -2877,11 +2877,11 @@ | ||
| 2877 | 2877 | else |
| 2878 | 2878 | { |
| 2879 | 2879 | cson_value * c = cson_value_new(CSON_TYPE_DOUBLE,0); |
| 2880 | 2880 | if( c ) |
| 2881 | 2881 | { |
| 2882 | - memcpy(CSON_DBL(c), &v, sizeof(v)); | |
| 2882 | + memcpy( CSON_DBL(c), &v, sizeof(v) ); | |
| 2883 | 2883 | } |
| 2884 | 2884 | return c; |
| 2885 | 2885 | } |
| 2886 | 2886 | } |
| 2887 | 2887 | |
| @@ -3068,11 +3068,11 @@ | ||
| 3068 | 3068 | qsort( obj->kvp.list, obj->kvp.count, sizeof(cson_kvp*), |
| 3069 | 3069 | cson_kvp_cmp ); |
| 3070 | 3070 | } |
| 3071 | 3071 | |
| 3072 | 3072 | } |
| 3073 | -#endif | |
| 3073 | +#endif | |
| 3074 | 3074 | |
| 3075 | 3075 | int cson_object_unset( cson_object * obj, char const * key ) |
| 3076 | 3076 | { |
| 3077 | 3077 | if( ! obj || !key || !*key ) return cson_rc.ArgError; |
| 3078 | 3078 | else |
| @@ -3238,11 +3238,11 @@ | ||
| 3238 | 3238 | using p->key. In any other case cson_rc.InternalError is returned. |
| 3239 | 3239 | |
| 3240 | 3240 | Returns cson_rc.AllocError if an allocation fails. |
| 3241 | 3241 | |
| 3242 | 3242 | Returns 0 on success. On error, parsing must be ceased immediately. |
| 3243 | - | |
| 3243 | + | |
| 3244 | 3244 | Ownership of val is ALWAYS TRANSFERED to this function. If this |
| 3245 | 3245 | function fails, val will be cleaned up and destroyed. (This |
| 3246 | 3246 | simplifies error handling in the core parser.) |
| 3247 | 3247 | */ |
| 3248 | 3248 | static int cson_parser_set_key( cson_parser * p, cson_value * val ) |
| @@ -3485,11 +3485,11 @@ | ||
| 3485 | 3485 | ++p->totalKeyCount; |
| 3486 | 3486 | break; |
| 3487 | 3487 | } |
| 3488 | 3488 | case JSON_T_STRING: { |
| 3489 | 3489 | cson_value * v = cson_value_new_string( value->vu.str.value, value->vu.str.length ); |
| 3490 | - rc = ( NULL == v ) | |
| 3490 | + rc = ( NULL == v ) | |
| 3491 | 3491 | ? cson_rc.AllocError |
| 3492 | 3492 | : cson_parser_push_value( p, v ); |
| 3493 | 3493 | break; |
| 3494 | 3494 | } |
| 3495 | 3495 | default: |
| @@ -3532,11 +3532,11 @@ | ||
| 3532 | 3532 | To properly take over ownership of the parser's root node on a |
| 3533 | 3533 | successful parse: |
| 3534 | 3534 | |
| 3535 | 3535 | - Copy p->root's pointer and set p->root to NULL. |
| 3536 | 3536 | - Eventually free up p->root with cson_value_free(). |
| 3537 | - | |
| 3537 | + | |
| 3538 | 3538 | If you do not set p->root to NULL, p->root will be freed along with |
| 3539 | 3539 | any other items inserted into it (or under it) during the parsing |
| 3540 | 3540 | process. |
| 3541 | 3541 | */ |
| 3542 | 3542 | static int cson_parser_clean( cson_parser * p ) |
| @@ -3571,11 +3571,11 @@ | ||
| 3571 | 3571 | int rc = 0; |
| 3572 | 3572 | unsigned int len = 1; |
| 3573 | 3573 | cson_parse_info info = info_ ? *info_ : cson_parse_info_empty; |
| 3574 | 3574 | cson_parser p = cson_parser_empty; |
| 3575 | 3575 | if( ! tgt || ! src ) return cson_rc.ArgError; |
| 3576 | - | |
| 3576 | + | |
| 3577 | 3577 | { |
| 3578 | 3578 | JSON_config jopt = {0}; |
| 3579 | 3579 | init_JSON_config( &jopt ); |
| 3580 | 3580 | jopt.allow_comments = opt.allowComments; |
| 3581 | 3581 | jopt.depth = opt.maxDepth; |
| @@ -3780,11 +3780,11 @@ | ||
| 3780 | 3780 | unsigned char const * end = (unsigned char const *)(str ? (str + len) : NULL); |
| 3781 | 3781 | unsigned char const * next = NULL; |
| 3782 | 3782 | int ch; |
| 3783 | 3783 | unsigned char clen = 0; |
| 3784 | 3784 | char escChar[3] = {'\\',0,0}; |
| 3785 | - enum { UBLen = 13 }; | |
| 3785 | + enum { UBLen = 20 }; | |
| 3786 | 3786 | char ubuf[UBLen]; |
| 3787 | 3787 | int rc = 0; |
| 3788 | 3788 | rc = f(state, "\"", 1 ); |
| 3789 | 3789 | for( ; (pos < end) && (0 == rc); pos += clen ) |
| 3790 | 3790 | { |
| @@ -4640,11 +4640,11 @@ | ||
| 4640 | 4640 | #endif |
| 4641 | 4641 | #undef TRY_SHARING |
| 4642 | 4642 | cson_value_add_reference(rc); |
| 4643 | 4643 | return rc; |
| 4644 | 4644 | } |
| 4645 | - | |
| 4645 | + | |
| 4646 | 4646 | static cson_value * cson_value_clone_array( cson_value const * orig ) |
| 4647 | 4647 | { |
| 4648 | 4648 | unsigned int i = 0; |
| 4649 | 4649 | cson_array const * asrc = cson_value_get_array( orig ); |
| 4650 | 4650 | unsigned int alen = cson_array_length_get( asrc ); |
| @@ -4680,11 +4680,11 @@ | ||
| 4680 | 4680 | cson_value_free(cl)/*remove our artificial reference */; |
| 4681 | 4681 | } |
| 4682 | 4682 | } |
| 4683 | 4683 | return destV; |
| 4684 | 4684 | } |
| 4685 | - | |
| 4685 | + | |
| 4686 | 4686 | static cson_value * cson_value_clone_object( cson_value const * orig ) |
| 4687 | 4687 | { |
| 4688 | 4688 | cson_object const * src = cson_value_get_object( orig ); |
| 4689 | 4689 | cson_value * destV = NULL; |
| 4690 | 4690 | cson_object * dest = NULL; |
| @@ -4834,11 +4834,11 @@ | ||
| 4834 | 4834 | } |
| 4835 | 4835 | case CSON_TYPE_STRING: { |
| 4836 | 4836 | cson_string const * jstr = cson_value_get_string(orig); |
| 4837 | 4837 | unsigned const int slen = cson_string_length_bytes( jstr ); |
| 4838 | 4838 | assert( NULL != jstr ); |
| 4839 | - v = cson_strdup( cson_string_cstr( jstr ), slen ); | |
| 4839 | + v = cson_strdup( cson_string_cstr( jstr ), slen ); | |
| 4840 | 4840 | break; |
| 4841 | 4841 | } |
| 4842 | 4842 | case CSON_TYPE_INTEGER: { |
| 4843 | 4843 | char buf[BufSize] = {0}; |
| 4844 | 4844 | if( 0 < sprintf( v, "%"CSON_INT_T_PFMT, cson_value_get_integer(orig)) ) |
| @@ -4887,11 +4887,11 @@ | ||
| 4887 | 4887 | } |
| 4888 | 4888 | case CSON_TYPE_STRING: { |
| 4889 | 4889 | cson_string const * jstr = cson_value_get_string(orig); |
| 4890 | 4890 | unsigned const int slen = cson_string_length_bytes( jstr ); |
| 4891 | 4891 | assert( NULL != jstr ); |
| 4892 | - v = cson_strdup( cson_string_cstr( jstr ), slen ); | |
| 4892 | + v = cson_strdup( cson_string_cstr( jstr ), slen ); | |
| 4893 | 4893 | break; |
| 4894 | 4894 | } |
| 4895 | 4895 | case CSON_TYPE_INTEGER: { |
| 4896 | 4896 | char buf[BufSize] = {0}; |
| 4897 | 4897 | if( 0 < sprintf( v, "%"CSON_INT_T_PFMT, cson_value_get_integer(orig)) ) |
| @@ -5351,11 +5351,11 @@ | ||
| 5351 | 5351 | int rc = 0; |
| 5352 | 5352 | int colCount = 0; |
| 5353 | 5353 | assert(st); |
| 5354 | 5354 | colCount = sqlite3_column_count(st); |
| 5355 | 5355 | if( colCount <= 0 ) return NULL; |
| 5356 | - | |
| 5356 | + | |
| 5357 | 5357 | aryV = cson_value_new_array(); |
| 5358 | 5358 | if( ! aryV ) return NULL; |
| 5359 | 5359 | ary = cson_value_get_array(aryV); |
| 5360 | 5360 | assert(ary); |
| 5361 | 5361 | for( i = 0; (0 ==rc) && (i < colCount); ++i ) |
| @@ -5491,11 +5491,11 @@ | ||
| 5491 | 5491 | aryV = NULL; |
| 5492 | 5492 | end: |
| 5493 | 5493 | return aryV; |
| 5494 | 5494 | } |
| 5495 | 5495 | |
| 5496 | - | |
| 5496 | + | |
| 5497 | 5497 | /** |
| 5498 | 5498 | Internal impl of cson_sqlite3_stmt_to_json() when the 'fat' |
| 5499 | 5499 | parameter is non-0. |
| 5500 | 5500 | */ |
| 5501 | 5501 | static int cson_sqlite3_stmt_to_json_fat( sqlite3_stmt * st, cson_value ** tgt ) |
| @@ -5636,11 +5636,11 @@ | ||
| 5636 | 5636 | int rc = sqlite3_prepare_v2( db, sql, -1, &st, NULL ); |
| 5637 | 5637 | if( 0 != rc ) return cson_rc.IOError /* FIXME: Better error code? */; |
| 5638 | 5638 | rc = cson_sqlite3_stmt_to_json( st, tgt, fat ); |
| 5639 | 5639 | sqlite3_finalize( st ); |
| 5640 | 5640 | return rc; |
| 5641 | - } | |
| 5641 | + } | |
| 5642 | 5642 | } |
| 5643 | 5643 | |
| 5644 | 5644 | int cson_sqlite3_bind_value( sqlite3_stmt * st, int ndx, cson_value const * v ) |
| 5645 | 5645 | { |
| 5646 | 5646 | int rc = 0; |
| 5647 | 5647 |
| --- src/cson_amalgamation.c | |
| +++ src/cson_amalgamation.c | |
| @@ -19,14 +19,14 @@ | |
| 19 | # define JSON_PARSER_DLL_API __declspec(dllexport) |
| 20 | # else |
| 21 | # define JSON_PARSER_DLL_API __declspec(dllimport) |
| 22 | # endif |
| 23 | # else |
| 24 | # define JSON_PARSER_DLL_API |
| 25 | # endif |
| 26 | #else |
| 27 | # define JSON_PARSER_DLL_API |
| 28 | #endif |
| 29 | |
| 30 | /* Determine the integer type use to parse non-floating point numbers */ |
| 31 | #ifdef _WIN32 |
| 32 | typedef __int64 JSON_int_t; |
| @@ -34,22 +34,22 @@ | |
| 34 | #define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%I64d" |
| 35 | #elif (__STDC_VERSION__ >= 199901L) || (HAVE_LONG_LONG == 1) |
| 36 | typedef long long JSON_int_t; |
| 37 | #define JSON_PARSER_INTEGER_SSCANF_TOKEN "%lld" |
| 38 | #define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%lld" |
| 39 | #else |
| 40 | typedef long JSON_int_t; |
| 41 | #define JSON_PARSER_INTEGER_SSCANF_TOKEN "%ld" |
| 42 | #define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%ld" |
| 43 | #endif |
| 44 | |
| 45 | |
| 46 | #ifdef __cplusplus |
| 47 | extern "C" { |
| 48 | #endif |
| 49 | |
| 50 | typedef enum |
| 51 | { |
| 52 | JSON_E_NONE = 0, |
| 53 | JSON_E_INVALID_CHAR, |
| 54 | JSON_E_INVALID_KEYWORD, |
| 55 | JSON_E_INVALID_ESCAPE_SEQUENCE, |
| @@ -60,11 +60,11 @@ | |
| 60 | JSON_E_EXPECTED_KEY, |
| 61 | JSON_E_EXPECTED_COLON, |
| 62 | JSON_E_OUT_OF_MEMORY |
| 63 | } JSON_error; |
| 64 | |
| 65 | typedef enum |
| 66 | { |
| 67 | JSON_T_NONE = 0, |
| 68 | JSON_T_ARRAY_BEGIN, |
| 69 | JSON_T_ARRAY_END, |
| 70 | JSON_T_OBJECT_BEGIN, |
| @@ -80,33 +80,33 @@ | |
| 80 | } JSON_type; |
| 81 | |
| 82 | typedef struct JSON_value_struct { |
| 83 | union { |
| 84 | JSON_int_t integer_value; |
| 85 | |
| 86 | double float_value; |
| 87 | |
| 88 | struct { |
| 89 | const char* value; |
| 90 | size_t length; |
| 91 | } str; |
| 92 | } vu; |
| 93 | } JSON_value; |
| 94 | |
| 95 | typedef struct JSON_parser_struct* JSON_parser; |
| 96 | |
| 97 | /*! \brief JSON parser callback |
| 98 | |
| 99 | \param ctx The pointer passed to new_JSON_parser. |
| 100 | \param type An element of JSON_type but not JSON_T_NONE. |
| 101 | \param value A representation of the parsed value. This parameter is NULL for |
| 102 | JSON_T_ARRAY_BEGIN, JSON_T_ARRAY_END, JSON_T_OBJECT_BEGIN, JSON_T_OBJECT_END, |
| 103 | JSON_T_NULL, JSON_T_TRUE, and JSON_T_FALSE. String values are always returned |
| 104 | as zero-terminated C strings. |
| 105 | |
| 106 | \return Non-zero if parsing should continue, else zero. |
| 107 | */ |
| 108 | typedef int (*JSON_parser_callback)(void* ctx, int type, const JSON_value* value); |
| 109 | |
| 110 | |
| 111 | /** |
| 112 | A typedef for allocator functions semantically compatible with malloc(). |
| @@ -115,11 +115,11 @@ | |
| 115 | /** |
| 116 | A typedef for deallocator functions semantically compatible with free(). |
| 117 | */ |
| 118 | typedef void (*JSON_free_t)(void* mem); |
| 119 | |
| 120 | /*! \brief The structure used to configure a JSON parser object |
| 121 | */ |
| 122 | typedef struct { |
| 123 | /** Pointer to a callback, called when the parser has something to tell |
| 124 | the user. This parameter may be NULL. In this case the input is |
| 125 | merely checked for validity. |
| @@ -176,17 +176,17 @@ | |
| 176 | |
| 177 | \param config. Used to configure the parser. |
| 178 | */ |
| 179 | JSON_PARSER_DLL_API void init_JSON_config(JSON_config * config); |
| 180 | |
| 181 | /*! \brief Create a JSON parser object |
| 182 | |
| 183 | \param config. Used to configure the parser. Set to NULL to use |
| 184 | the default configuration. See init_JSON_config. Its contents are |
| 185 | copied by this function, so it need not outlive the returned |
| 186 | object. |
| 187 | |
| 188 | \return The parser object, which is owned by the caller and must eventually |
| 189 | be freed by calling delete_JSON_parser(). |
| 190 | */ |
| 191 | JSON_PARSER_DLL_API JSON_parser new_JSON_parser(JSON_config const* config); |
| 192 | |
| @@ -200,16 +200,16 @@ | |
| 200 | JSON_PARSER_DLL_API int JSON_parser_char(JSON_parser jc, int next_char); |
| 201 | |
| 202 | /*! \brief Finalize parsing. |
| 203 | |
| 204 | Call this method once after all input characters have been consumed. |
| 205 | |
| 206 | \return Non-zero, if all parsed characters are valid JSON, zero otherwise. |
| 207 | */ |
| 208 | JSON_PARSER_DLL_API int JSON_parser_done(JSON_parser jc); |
| 209 | |
| 210 | /*! \brief Determine if a given string is valid JSON white space |
| 211 | |
| 212 | \return Non-zero if the string is valid, zero otherwise. |
| 213 | */ |
| 214 | JSON_PARSER_DLL_API int JSON_parser_is_legal_white_space_string(const char* s); |
| 215 | |
| @@ -226,12 +226,12 @@ | |
| 226 | JSON_PARSER_DLL_API int JSON_parser_reset(JSON_parser jc); |
| 227 | |
| 228 | |
| 229 | #ifdef __cplusplus |
| 230 | } |
| 231 | #endif |
| 232 | |
| 233 | |
| 234 | #endif /* JSON_PARSER_H */ |
| 235 | /* end file parser/JSON_parser.h */ |
| 236 | /* begin file parser/JSON_parser.c */ |
| 237 | /* |
| @@ -1431,11 +1431,11 @@ | |
| 1431 | #if defined(__cplusplus) |
| 1432 | extern "C" { |
| 1433 | #endif |
| 1434 | |
| 1435 | |
| 1436 | |
| 1437 | /** |
| 1438 | This type holds the "vtbl" for type-specific operations when |
| 1439 | working with cson_value objects. |
| 1440 | |
| 1441 | All cson_values of a given logical type share a pointer to a single |
| @@ -1583,11 +1583,11 @@ | |
| 1583 | */ |
| 1584 | #define CSON_CAST(T,V) ((T*)((V)->value)) |
| 1585 | /** |
| 1586 | Assumes V is a pointer to memory which is allocated as part of a |
| 1587 | cson_value instance (the bytes immediately after that part). |
| 1588 | Returns a pointer a cson_value by subtracting sizeof(cson_value) |
| 1589 | from that address and casting it to a (cson_value*) |
| 1590 | */ |
| 1591 | #define CSON_VCAST(V) ((cson_value *)(((unsigned char *)(V))-sizeof(cson_value))) |
| 1592 | |
| 1593 | /** |
| @@ -1607,11 +1607,11 @@ | |
| 1607 | #define CSON_OBJ(V) CSON_CAST(cson_object,(V)) |
| 1608 | #define CSON_ARRAY(V) CSON_CAST(cson_array,(V)) |
| 1609 | |
| 1610 | /** |
| 1611 | Holds special shared "constant" (though they are non-const) |
| 1612 | values. |
| 1613 | */ |
| 1614 | static struct CSON_EMPTY_HOLDER_ |
| 1615 | { |
| 1616 | char trueValue; |
| 1617 | cson_string stringValue; |
| @@ -1620,17 +1620,17 @@ | |
| 1620 | cson_string_empty_m |
| 1621 | }; |
| 1622 | |
| 1623 | /** |
| 1624 | Indexes into the CSON_SPECIAL_VALUES array. |
| 1625 | |
| 1626 | If this enum changes in any way, |
| 1627 | makes damned sure that CSON_SPECIAL_VALUES is updated |
| 1628 | to match!!! |
| 1629 | */ |
| 1630 | enum CSON_INTERNAL_VALUES { |
| 1631 | |
| 1632 | CSON_VAL_UNDEF = 0, |
| 1633 | CSON_VAL_NULL = 1, |
| 1634 | CSON_VAL_TRUE = 2, |
| 1635 | CSON_VAL_FALSE = 3, |
| 1636 | CSON_VAL_INT_0 = 4, |
| @@ -1642,11 +1642,11 @@ | |
| 1642 | /** |
| 1643 | Some "special" shared cson_value instances. |
| 1644 | |
| 1645 | These values MUST be initialized in the order specified |
| 1646 | by the CSON_INTERNAL_VALUES enum. |
| 1647 | |
| 1648 | Note that they are not const because they are used as |
| 1649 | shared-allocation objects in non-const contexts. However, the |
| 1650 | public API provides no way to modifying them, and clients who |
| 1651 | modify values directly are subject to The Wrath of Undefined |
| 1652 | Behaviour. |
| @@ -1665,11 +1665,11 @@ | |
| 1665 | |
| 1666 | /** |
| 1667 | Returns non-0 (true) if m is one of our special |
| 1668 | "built-in" values, e.g. from CSON_SPECIAL_VALUES and some |
| 1669 | "empty" values. |
| 1670 | |
| 1671 | If this returns true, m MUST NOT be free()d! |
| 1672 | */ |
| 1673 | static char cson_value_is_builtin( void const * m ) |
| 1674 | { |
| 1675 | if((m >= (void const *)&CSON_EMPTY_HOLDER) |
| @@ -2183,11 +2183,11 @@ | |
| 2183 | static int cson_value_list_visit( cson_value_list * self, |
| 2184 | int (*visitor)(cson_value * obj, void * visitorState ), |
| 2185 | void * visitorState ); |
| 2186 | #endif |
| 2187 | #endif |
| 2188 | |
| 2189 | #if 0 |
| 2190 | # define LIST_T cson_value_list |
| 2191 | # define VALUE_T cson_value * |
| 2192 | # define VALUE_T_IS_PTR 1 |
| 2193 | # define LIST_T cson_kvp_list |
| @@ -2362,11 +2362,11 @@ | |
| 2362 | return cson_value_object_alloc(); |
| 2363 | } |
| 2364 | |
| 2365 | cson_object * cson_new_object() |
| 2366 | { |
| 2367 | |
| 2368 | return cson_value_get_object( cson_value_new_object() ); |
| 2369 | } |
| 2370 | |
| 2371 | cson_value * cson_value_new_array() |
| 2372 | { |
| @@ -2608,11 +2608,11 @@ | |
| 2608 | { |
| 2609 | cson_int_t i = 0; |
| 2610 | int rc = 0; |
| 2611 | switch(val->api->typeID) |
| 2612 | { |
| 2613 | case CSON_TYPE_UNDEF: |
| 2614 | case CSON_TYPE_NULL: |
| 2615 | i = 0; |
| 2616 | break; |
| 2617 | case CSON_TYPE_BOOL: { |
| 2618 | char b = 0; |
| @@ -2661,11 +2661,11 @@ | |
| 2661 | { |
| 2662 | cson_double_t d = 0.0; |
| 2663 | int rc = 0; |
| 2664 | switch(val->api->typeID) |
| 2665 | { |
| 2666 | case CSON_TYPE_UNDEF: |
| 2667 | case CSON_TYPE_NULL: |
| 2668 | d = 0; |
| 2669 | break; |
| 2670 | case CSON_TYPE_BOOL: { |
| 2671 | char b = 0; |
| @@ -2791,11 +2791,11 @@ | |
| 2791 | #if 0 |
| 2792 | /** |
| 2793 | Removes and returns the last value from the given array, |
| 2794 | shrinking its size by 1. Returns NULL if ar is NULL, |
| 2795 | ar->list.count is 0, or the element at that index is NULL. |
| 2796 | |
| 2797 | |
| 2798 | If removeRef is true then cson_value_free() is called to remove |
| 2799 | ar's reference count for the value. In that case NULL is returned, |
| 2800 | even if the object still has live references. If removeRef is false |
| 2801 | then the caller takes over ownership of that reference count point. |
| @@ -2858,11 +2858,11 @@ | |
| 2858 | #if !defined(NDEBUG) && CSON_VOID_PTR_IS_BIG |
| 2859 | assert( sizeof(cson_int_t) <= sizeof(void *) ); |
| 2860 | #endif |
| 2861 | if( c ) |
| 2862 | { |
| 2863 | memcpy(CSON_INT(c), &v, sizeof(v)); |
| 2864 | } |
| 2865 | return c; |
| 2866 | } |
| 2867 | } |
| 2868 | |
| @@ -2877,11 +2877,11 @@ | |
| 2877 | else |
| 2878 | { |
| 2879 | cson_value * c = cson_value_new(CSON_TYPE_DOUBLE,0); |
| 2880 | if( c ) |
| 2881 | { |
| 2882 | memcpy(CSON_DBL(c), &v, sizeof(v)); |
| 2883 | } |
| 2884 | return c; |
| 2885 | } |
| 2886 | } |
| 2887 | |
| @@ -3068,11 +3068,11 @@ | |
| 3068 | qsort( obj->kvp.list, obj->kvp.count, sizeof(cson_kvp*), |
| 3069 | cson_kvp_cmp ); |
| 3070 | } |
| 3071 | |
| 3072 | } |
| 3073 | #endif |
| 3074 | |
| 3075 | int cson_object_unset( cson_object * obj, char const * key ) |
| 3076 | { |
| 3077 | if( ! obj || !key || !*key ) return cson_rc.ArgError; |
| 3078 | else |
| @@ -3238,11 +3238,11 @@ | |
| 3238 | using p->key. In any other case cson_rc.InternalError is returned. |
| 3239 | |
| 3240 | Returns cson_rc.AllocError if an allocation fails. |
| 3241 | |
| 3242 | Returns 0 on success. On error, parsing must be ceased immediately. |
| 3243 | |
| 3244 | Ownership of val is ALWAYS TRANSFERED to this function. If this |
| 3245 | function fails, val will be cleaned up and destroyed. (This |
| 3246 | simplifies error handling in the core parser.) |
| 3247 | */ |
| 3248 | static int cson_parser_set_key( cson_parser * p, cson_value * val ) |
| @@ -3485,11 +3485,11 @@ | |
| 3485 | ++p->totalKeyCount; |
| 3486 | break; |
| 3487 | } |
| 3488 | case JSON_T_STRING: { |
| 3489 | cson_value * v = cson_value_new_string( value->vu.str.value, value->vu.str.length ); |
| 3490 | rc = ( NULL == v ) |
| 3491 | ? cson_rc.AllocError |
| 3492 | : cson_parser_push_value( p, v ); |
| 3493 | break; |
| 3494 | } |
| 3495 | default: |
| @@ -3532,11 +3532,11 @@ | |
| 3532 | To properly take over ownership of the parser's root node on a |
| 3533 | successful parse: |
| 3534 | |
| 3535 | - Copy p->root's pointer and set p->root to NULL. |
| 3536 | - Eventually free up p->root with cson_value_free(). |
| 3537 | |
| 3538 | If you do not set p->root to NULL, p->root will be freed along with |
| 3539 | any other items inserted into it (or under it) during the parsing |
| 3540 | process. |
| 3541 | */ |
| 3542 | static int cson_parser_clean( cson_parser * p ) |
| @@ -3571,11 +3571,11 @@ | |
| 3571 | int rc = 0; |
| 3572 | unsigned int len = 1; |
| 3573 | cson_parse_info info = info_ ? *info_ : cson_parse_info_empty; |
| 3574 | cson_parser p = cson_parser_empty; |
| 3575 | if( ! tgt || ! src ) return cson_rc.ArgError; |
| 3576 | |
| 3577 | { |
| 3578 | JSON_config jopt = {0}; |
| 3579 | init_JSON_config( &jopt ); |
| 3580 | jopt.allow_comments = opt.allowComments; |
| 3581 | jopt.depth = opt.maxDepth; |
| @@ -3780,11 +3780,11 @@ | |
| 3780 | unsigned char const * end = (unsigned char const *)(str ? (str + len) : NULL); |
| 3781 | unsigned char const * next = NULL; |
| 3782 | int ch; |
| 3783 | unsigned char clen = 0; |
| 3784 | char escChar[3] = {'\\',0,0}; |
| 3785 | enum { UBLen = 13 }; |
| 3786 | char ubuf[UBLen]; |
| 3787 | int rc = 0; |
| 3788 | rc = f(state, "\"", 1 ); |
| 3789 | for( ; (pos < end) && (0 == rc); pos += clen ) |
| 3790 | { |
| @@ -4640,11 +4640,11 @@ | |
| 4640 | #endif |
| 4641 | #undef TRY_SHARING |
| 4642 | cson_value_add_reference(rc); |
| 4643 | return rc; |
| 4644 | } |
| 4645 | |
| 4646 | static cson_value * cson_value_clone_array( cson_value const * orig ) |
| 4647 | { |
| 4648 | unsigned int i = 0; |
| 4649 | cson_array const * asrc = cson_value_get_array( orig ); |
| 4650 | unsigned int alen = cson_array_length_get( asrc ); |
| @@ -4680,11 +4680,11 @@ | |
| 4680 | cson_value_free(cl)/*remove our artificial reference */; |
| 4681 | } |
| 4682 | } |
| 4683 | return destV; |
| 4684 | } |
| 4685 | |
| 4686 | static cson_value * cson_value_clone_object( cson_value const * orig ) |
| 4687 | { |
| 4688 | cson_object const * src = cson_value_get_object( orig ); |
| 4689 | cson_value * destV = NULL; |
| 4690 | cson_object * dest = NULL; |
| @@ -4834,11 +4834,11 @@ | |
| 4834 | } |
| 4835 | case CSON_TYPE_STRING: { |
| 4836 | cson_string const * jstr = cson_value_get_string(orig); |
| 4837 | unsigned const int slen = cson_string_length_bytes( jstr ); |
| 4838 | assert( NULL != jstr ); |
| 4839 | v = cson_strdup( cson_string_cstr( jstr ), slen ); |
| 4840 | break; |
| 4841 | } |
| 4842 | case CSON_TYPE_INTEGER: { |
| 4843 | char buf[BufSize] = {0}; |
| 4844 | if( 0 < sprintf( v, "%"CSON_INT_T_PFMT, cson_value_get_integer(orig)) ) |
| @@ -4887,11 +4887,11 @@ | |
| 4887 | } |
| 4888 | case CSON_TYPE_STRING: { |
| 4889 | cson_string const * jstr = cson_value_get_string(orig); |
| 4890 | unsigned const int slen = cson_string_length_bytes( jstr ); |
| 4891 | assert( NULL != jstr ); |
| 4892 | v = cson_strdup( cson_string_cstr( jstr ), slen ); |
| 4893 | break; |
| 4894 | } |
| 4895 | case CSON_TYPE_INTEGER: { |
| 4896 | char buf[BufSize] = {0}; |
| 4897 | if( 0 < sprintf( v, "%"CSON_INT_T_PFMT, cson_value_get_integer(orig)) ) |
| @@ -5351,11 +5351,11 @@ | |
| 5351 | int rc = 0; |
| 5352 | int colCount = 0; |
| 5353 | assert(st); |
| 5354 | colCount = sqlite3_column_count(st); |
| 5355 | if( colCount <= 0 ) return NULL; |
| 5356 | |
| 5357 | aryV = cson_value_new_array(); |
| 5358 | if( ! aryV ) return NULL; |
| 5359 | ary = cson_value_get_array(aryV); |
| 5360 | assert(ary); |
| 5361 | for( i = 0; (0 ==rc) && (i < colCount); ++i ) |
| @@ -5491,11 +5491,11 @@ | |
| 5491 | aryV = NULL; |
| 5492 | end: |
| 5493 | return aryV; |
| 5494 | } |
| 5495 | |
| 5496 | |
| 5497 | /** |
| 5498 | Internal impl of cson_sqlite3_stmt_to_json() when the 'fat' |
| 5499 | parameter is non-0. |
| 5500 | */ |
| 5501 | static int cson_sqlite3_stmt_to_json_fat( sqlite3_stmt * st, cson_value ** tgt ) |
| @@ -5636,11 +5636,11 @@ | |
| 5636 | int rc = sqlite3_prepare_v2( db, sql, -1, &st, NULL ); |
| 5637 | if( 0 != rc ) return cson_rc.IOError /* FIXME: Better error code? */; |
| 5638 | rc = cson_sqlite3_stmt_to_json( st, tgt, fat ); |
| 5639 | sqlite3_finalize( st ); |
| 5640 | return rc; |
| 5641 | } |
| 5642 | } |
| 5643 | |
| 5644 | int cson_sqlite3_bind_value( sqlite3_stmt * st, int ndx, cson_value const * v ) |
| 5645 | { |
| 5646 | int rc = 0; |
| 5647 |
| --- src/cson_amalgamation.c | |
| +++ src/cson_amalgamation.c | |
| @@ -19,14 +19,14 @@ | |
| 19 | # define JSON_PARSER_DLL_API __declspec(dllexport) |
| 20 | # else |
| 21 | # define JSON_PARSER_DLL_API __declspec(dllimport) |
| 22 | # endif |
| 23 | # else |
| 24 | # define JSON_PARSER_DLL_API |
| 25 | # endif |
| 26 | #else |
| 27 | # define JSON_PARSER_DLL_API |
| 28 | #endif |
| 29 | |
| 30 | /* Determine the integer type use to parse non-floating point numbers */ |
| 31 | #ifdef _WIN32 |
| 32 | typedef __int64 JSON_int_t; |
| @@ -34,22 +34,22 @@ | |
| 34 | #define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%I64d" |
| 35 | #elif (__STDC_VERSION__ >= 199901L) || (HAVE_LONG_LONG == 1) |
| 36 | typedef long long JSON_int_t; |
| 37 | #define JSON_PARSER_INTEGER_SSCANF_TOKEN "%lld" |
| 38 | #define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%lld" |
| 39 | #else |
| 40 | typedef long JSON_int_t; |
| 41 | #define JSON_PARSER_INTEGER_SSCANF_TOKEN "%ld" |
| 42 | #define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%ld" |
| 43 | #endif |
| 44 | |
| 45 | |
| 46 | #ifdef __cplusplus |
| 47 | extern "C" { |
| 48 | #endif |
| 49 | |
| 50 | typedef enum |
| 51 | { |
| 52 | JSON_E_NONE = 0, |
| 53 | JSON_E_INVALID_CHAR, |
| 54 | JSON_E_INVALID_KEYWORD, |
| 55 | JSON_E_INVALID_ESCAPE_SEQUENCE, |
| @@ -60,11 +60,11 @@ | |
| 60 | JSON_E_EXPECTED_KEY, |
| 61 | JSON_E_EXPECTED_COLON, |
| 62 | JSON_E_OUT_OF_MEMORY |
| 63 | } JSON_error; |
| 64 | |
| 65 | typedef enum |
| 66 | { |
| 67 | JSON_T_NONE = 0, |
| 68 | JSON_T_ARRAY_BEGIN, |
| 69 | JSON_T_ARRAY_END, |
| 70 | JSON_T_OBJECT_BEGIN, |
| @@ -80,33 +80,33 @@ | |
| 80 | } JSON_type; |
| 81 | |
| 82 | typedef struct JSON_value_struct { |
| 83 | union { |
| 84 | JSON_int_t integer_value; |
| 85 | |
| 86 | double float_value; |
| 87 | |
| 88 | struct { |
| 89 | const char* value; |
| 90 | size_t length; |
| 91 | } str; |
| 92 | } vu; |
| 93 | } JSON_value; |
| 94 | |
| 95 | typedef struct JSON_parser_struct* JSON_parser; |
| 96 | |
| 97 | /*! \brief JSON parser callback |
| 98 | |
| 99 | \param ctx The pointer passed to new_JSON_parser. |
| 100 | \param type An element of JSON_type but not JSON_T_NONE. |
| 101 | \param value A representation of the parsed value. This parameter is NULL for |
| 102 | JSON_T_ARRAY_BEGIN, JSON_T_ARRAY_END, JSON_T_OBJECT_BEGIN, JSON_T_OBJECT_END, |
| 103 | JSON_T_NULL, JSON_T_TRUE, and JSON_T_FALSE. String values are always returned |
| 104 | as zero-terminated C strings. |
| 105 | |
| 106 | \return Non-zero if parsing should continue, else zero. |
| 107 | */ |
| 108 | typedef int (*JSON_parser_callback)(void* ctx, int type, const JSON_value* value); |
| 109 | |
| 110 | |
| 111 | /** |
| 112 | A typedef for allocator functions semantically compatible with malloc(). |
| @@ -115,11 +115,11 @@ | |
| 115 | /** |
| 116 | A typedef for deallocator functions semantically compatible with free(). |
| 117 | */ |
| 118 | typedef void (*JSON_free_t)(void* mem); |
| 119 | |
| 120 | /*! \brief The structure used to configure a JSON parser object |
| 121 | */ |
| 122 | typedef struct { |
| 123 | /** Pointer to a callback, called when the parser has something to tell |
| 124 | the user. This parameter may be NULL. In this case the input is |
| 125 | merely checked for validity. |
| @@ -176,17 +176,17 @@ | |
| 176 | |
| 177 | \param config. Used to configure the parser. |
| 178 | */ |
| 179 | JSON_PARSER_DLL_API void init_JSON_config(JSON_config * config); |
| 180 | |
| 181 | /*! \brief Create a JSON parser object |
| 182 | |
| 183 | \param config. Used to configure the parser. Set to NULL to use |
| 184 | the default configuration. See init_JSON_config. Its contents are |
| 185 | copied by this function, so it need not outlive the returned |
| 186 | object. |
| 187 | |
| 188 | \return The parser object, which is owned by the caller and must eventually |
| 189 | be freed by calling delete_JSON_parser(). |
| 190 | */ |
| 191 | JSON_PARSER_DLL_API JSON_parser new_JSON_parser(JSON_config const* config); |
| 192 | |
| @@ -200,16 +200,16 @@ | |
| 200 | JSON_PARSER_DLL_API int JSON_parser_char(JSON_parser jc, int next_char); |
| 201 | |
| 202 | /*! \brief Finalize parsing. |
| 203 | |
| 204 | Call this method once after all input characters have been consumed. |
| 205 | |
| 206 | \return Non-zero, if all parsed characters are valid JSON, zero otherwise. |
| 207 | */ |
| 208 | JSON_PARSER_DLL_API int JSON_parser_done(JSON_parser jc); |
| 209 | |
| 210 | /*! \brief Determine if a given string is valid JSON white space |
| 211 | |
| 212 | \return Non-zero if the string is valid, zero otherwise. |
| 213 | */ |
| 214 | JSON_PARSER_DLL_API int JSON_parser_is_legal_white_space_string(const char* s); |
| 215 | |
| @@ -226,12 +226,12 @@ | |
| 226 | JSON_PARSER_DLL_API int JSON_parser_reset(JSON_parser jc); |
| 227 | |
| 228 | |
| 229 | #ifdef __cplusplus |
| 230 | } |
| 231 | #endif |
| 232 | |
| 233 | |
| 234 | #endif /* JSON_PARSER_H */ |
| 235 | /* end file parser/JSON_parser.h */ |
| 236 | /* begin file parser/JSON_parser.c */ |
| 237 | /* |
| @@ -1431,11 +1431,11 @@ | |
| 1431 | #if defined(__cplusplus) |
| 1432 | extern "C" { |
| 1433 | #endif |
| 1434 | |
| 1435 | |
| 1436 | |
| 1437 | /** |
| 1438 | This type holds the "vtbl" for type-specific operations when |
| 1439 | working with cson_value objects. |
| 1440 | |
| 1441 | All cson_values of a given logical type share a pointer to a single |
| @@ -1583,11 +1583,11 @@ | |
| 1583 | */ |
| 1584 | #define CSON_CAST(T,V) ((T*)((V)->value)) |
| 1585 | /** |
| 1586 | Assumes V is a pointer to memory which is allocated as part of a |
| 1587 | cson_value instance (the bytes immediately after that part). |
| 1588 | Returns a pointer a a cson_value by subtracting sizeof(cson_value) |
| 1589 | from that address and casting it to a (cson_value*) |
| 1590 | */ |
| 1591 | #define CSON_VCAST(V) ((cson_value *)(((unsigned char *)(V))-sizeof(cson_value))) |
| 1592 | |
| 1593 | /** |
| @@ -1607,11 +1607,11 @@ | |
| 1607 | #define CSON_OBJ(V) CSON_CAST(cson_object,(V)) |
| 1608 | #define CSON_ARRAY(V) CSON_CAST(cson_array,(V)) |
| 1609 | |
| 1610 | /** |
| 1611 | Holds special shared "constant" (though they are non-const) |
| 1612 | values. |
| 1613 | */ |
| 1614 | static struct CSON_EMPTY_HOLDER_ |
| 1615 | { |
| 1616 | char trueValue; |
| 1617 | cson_string stringValue; |
| @@ -1620,17 +1620,17 @@ | |
| 1620 | cson_string_empty_m |
| 1621 | }; |
| 1622 | |
| 1623 | /** |
| 1624 | Indexes into the CSON_SPECIAL_VALUES array. |
| 1625 | |
| 1626 | If this enum changes in any way, |
| 1627 | makes damned sure that CSON_SPECIAL_VALUES is updated |
| 1628 | to match!!! |
| 1629 | */ |
| 1630 | enum CSON_INTERNAL_VALUES { |
| 1631 | |
| 1632 | CSON_VAL_UNDEF = 0, |
| 1633 | CSON_VAL_NULL = 1, |
| 1634 | CSON_VAL_TRUE = 2, |
| 1635 | CSON_VAL_FALSE = 3, |
| 1636 | CSON_VAL_INT_0 = 4, |
| @@ -1642,11 +1642,11 @@ | |
| 1642 | /** |
| 1643 | Some "special" shared cson_value instances. |
| 1644 | |
| 1645 | These values MUST be initialized in the order specified |
| 1646 | by the CSON_INTERNAL_VALUES enum. |
| 1647 | |
| 1648 | Note that they are not const because they are used as |
| 1649 | shared-allocation objects in non-const contexts. However, the |
| 1650 | public API provides no way to modifying them, and clients who |
| 1651 | modify values directly are subject to The Wrath of Undefined |
| 1652 | Behaviour. |
| @@ -1665,11 +1665,11 @@ | |
| 1665 | |
| 1666 | /** |
| 1667 | Returns non-0 (true) if m is one of our special |
| 1668 | "built-in" values, e.g. from CSON_SPECIAL_VALUES and some |
| 1669 | "empty" values. |
| 1670 | |
| 1671 | If this returns true, m MUST NOT be free()d! |
| 1672 | */ |
| 1673 | static char cson_value_is_builtin( void const * m ) |
| 1674 | { |
| 1675 | if((m >= (void const *)&CSON_EMPTY_HOLDER) |
| @@ -2183,11 +2183,11 @@ | |
| 2183 | static int cson_value_list_visit( cson_value_list * self, |
| 2184 | int (*visitor)(cson_value * obj, void * visitorState ), |
| 2185 | void * visitorState ); |
| 2186 | #endif |
| 2187 | #endif |
| 2188 | |
| 2189 | #if 0 |
| 2190 | # define LIST_T cson_value_list |
| 2191 | # define VALUE_T cson_value * |
| 2192 | # define VALUE_T_IS_PTR 1 |
| 2193 | # define LIST_T cson_kvp_list |
| @@ -2362,11 +2362,11 @@ | |
| 2362 | return cson_value_object_alloc(); |
| 2363 | } |
| 2364 | |
| 2365 | cson_object * cson_new_object() |
| 2366 | { |
| 2367 | |
| 2368 | return cson_value_get_object( cson_value_new_object() ); |
| 2369 | } |
| 2370 | |
| 2371 | cson_value * cson_value_new_array() |
| 2372 | { |
| @@ -2608,11 +2608,11 @@ | |
| 2608 | { |
| 2609 | cson_int_t i = 0; |
| 2610 | int rc = 0; |
| 2611 | switch(val->api->typeID) |
| 2612 | { |
| 2613 | case CSON_TYPE_UNDEF: |
| 2614 | case CSON_TYPE_NULL: |
| 2615 | i = 0; |
| 2616 | break; |
| 2617 | case CSON_TYPE_BOOL: { |
| 2618 | char b = 0; |
| @@ -2661,11 +2661,11 @@ | |
| 2661 | { |
| 2662 | cson_double_t d = 0.0; |
| 2663 | int rc = 0; |
| 2664 | switch(val->api->typeID) |
| 2665 | { |
| 2666 | case CSON_TYPE_UNDEF: |
| 2667 | case CSON_TYPE_NULL: |
| 2668 | d = 0; |
| 2669 | break; |
| 2670 | case CSON_TYPE_BOOL: { |
| 2671 | char b = 0; |
| @@ -2791,11 +2791,11 @@ | |
| 2791 | #if 0 |
| 2792 | /** |
| 2793 | Removes and returns the last value from the given array, |
| 2794 | shrinking its size by 1. Returns NULL if ar is NULL, |
| 2795 | ar->list.count is 0, or the element at that index is NULL. |
| 2796 | |
| 2797 | |
| 2798 | If removeRef is true then cson_value_free() is called to remove |
| 2799 | ar's reference count for the value. In that case NULL is returned, |
| 2800 | even if the object still has live references. If removeRef is false |
| 2801 | then the caller takes over ownership of that reference count point. |
| @@ -2858,11 +2858,11 @@ | |
| 2858 | #if !defined(NDEBUG) && CSON_VOID_PTR_IS_BIG |
| 2859 | assert( sizeof(cson_int_t) <= sizeof(void *) ); |
| 2860 | #endif |
| 2861 | if( c ) |
| 2862 | { |
| 2863 | memcpy( CSON_INT(c), &v, sizeof(v) ); |
| 2864 | } |
| 2865 | return c; |
| 2866 | } |
| 2867 | } |
| 2868 | |
| @@ -2877,11 +2877,11 @@ | |
| 2877 | else |
| 2878 | { |
| 2879 | cson_value * c = cson_value_new(CSON_TYPE_DOUBLE,0); |
| 2880 | if( c ) |
| 2881 | { |
| 2882 | memcpy( CSON_DBL(c), &v, sizeof(v) ); |
| 2883 | } |
| 2884 | return c; |
| 2885 | } |
| 2886 | } |
| 2887 | |
| @@ -3068,11 +3068,11 @@ | |
| 3068 | qsort( obj->kvp.list, obj->kvp.count, sizeof(cson_kvp*), |
| 3069 | cson_kvp_cmp ); |
| 3070 | } |
| 3071 | |
| 3072 | } |
| 3073 | #endif |
| 3074 | |
| 3075 | int cson_object_unset( cson_object * obj, char const * key ) |
| 3076 | { |
| 3077 | if( ! obj || !key || !*key ) return cson_rc.ArgError; |
| 3078 | else |
| @@ -3238,11 +3238,11 @@ | |
| 3238 | using p->key. In any other case cson_rc.InternalError is returned. |
| 3239 | |
| 3240 | Returns cson_rc.AllocError if an allocation fails. |
| 3241 | |
| 3242 | Returns 0 on success. On error, parsing must be ceased immediately. |
| 3243 | |
| 3244 | Ownership of val is ALWAYS TRANSFERED to this function. If this |
| 3245 | function fails, val will be cleaned up and destroyed. (This |
| 3246 | simplifies error handling in the core parser.) |
| 3247 | */ |
| 3248 | static int cson_parser_set_key( cson_parser * p, cson_value * val ) |
| @@ -3485,11 +3485,11 @@ | |
| 3485 | ++p->totalKeyCount; |
| 3486 | break; |
| 3487 | } |
| 3488 | case JSON_T_STRING: { |
| 3489 | cson_value * v = cson_value_new_string( value->vu.str.value, value->vu.str.length ); |
| 3490 | rc = ( NULL == v ) |
| 3491 | ? cson_rc.AllocError |
| 3492 | : cson_parser_push_value( p, v ); |
| 3493 | break; |
| 3494 | } |
| 3495 | default: |
| @@ -3532,11 +3532,11 @@ | |
| 3532 | To properly take over ownership of the parser's root node on a |
| 3533 | successful parse: |
| 3534 | |
| 3535 | - Copy p->root's pointer and set p->root to NULL. |
| 3536 | - Eventually free up p->root with cson_value_free(). |
| 3537 | |
| 3538 | If you do not set p->root to NULL, p->root will be freed along with |
| 3539 | any other items inserted into it (or under it) during the parsing |
| 3540 | process. |
| 3541 | */ |
| 3542 | static int cson_parser_clean( cson_parser * p ) |
| @@ -3571,11 +3571,11 @@ | |
| 3571 | int rc = 0; |
| 3572 | unsigned int len = 1; |
| 3573 | cson_parse_info info = info_ ? *info_ : cson_parse_info_empty; |
| 3574 | cson_parser p = cson_parser_empty; |
| 3575 | if( ! tgt || ! src ) return cson_rc.ArgError; |
| 3576 | |
| 3577 | { |
| 3578 | JSON_config jopt = {0}; |
| 3579 | init_JSON_config( &jopt ); |
| 3580 | jopt.allow_comments = opt.allowComments; |
| 3581 | jopt.depth = opt.maxDepth; |
| @@ -3780,11 +3780,11 @@ | |
| 3780 | unsigned char const * end = (unsigned char const *)(str ? (str + len) : NULL); |
| 3781 | unsigned char const * next = NULL; |
| 3782 | int ch; |
| 3783 | unsigned char clen = 0; |
| 3784 | char escChar[3] = {'\\',0,0}; |
| 3785 | enum { UBLen = 20 }; |
| 3786 | char ubuf[UBLen]; |
| 3787 | int rc = 0; |
| 3788 | rc = f(state, "\"", 1 ); |
| 3789 | for( ; (pos < end) && (0 == rc); pos += clen ) |
| 3790 | { |
| @@ -4640,11 +4640,11 @@ | |
| 4640 | #endif |
| 4641 | #undef TRY_SHARING |
| 4642 | cson_value_add_reference(rc); |
| 4643 | return rc; |
| 4644 | } |
| 4645 | |
| 4646 | static cson_value * cson_value_clone_array( cson_value const * orig ) |
| 4647 | { |
| 4648 | unsigned int i = 0; |
| 4649 | cson_array const * asrc = cson_value_get_array( orig ); |
| 4650 | unsigned int alen = cson_array_length_get( asrc ); |
| @@ -4680,11 +4680,11 @@ | |
| 4680 | cson_value_free(cl)/*remove our artificial reference */; |
| 4681 | } |
| 4682 | } |
| 4683 | return destV; |
| 4684 | } |
| 4685 | |
| 4686 | static cson_value * cson_value_clone_object( cson_value const * orig ) |
| 4687 | { |
| 4688 | cson_object const * src = cson_value_get_object( orig ); |
| 4689 | cson_value * destV = NULL; |
| 4690 | cson_object * dest = NULL; |
| @@ -4834,11 +4834,11 @@ | |
| 4834 | } |
| 4835 | case CSON_TYPE_STRING: { |
| 4836 | cson_string const * jstr = cson_value_get_string(orig); |
| 4837 | unsigned const int slen = cson_string_length_bytes( jstr ); |
| 4838 | assert( NULL != jstr ); |
| 4839 | v = cson_strdup( cson_string_cstr( jstr ), slen ); |
| 4840 | break; |
| 4841 | } |
| 4842 | case CSON_TYPE_INTEGER: { |
| 4843 | char buf[BufSize] = {0}; |
| 4844 | if( 0 < sprintf( v, "%"CSON_INT_T_PFMT, cson_value_get_integer(orig)) ) |
| @@ -4887,11 +4887,11 @@ | |
| 4887 | } |
| 4888 | case CSON_TYPE_STRING: { |
| 4889 | cson_string const * jstr = cson_value_get_string(orig); |
| 4890 | unsigned const int slen = cson_string_length_bytes( jstr ); |
| 4891 | assert( NULL != jstr ); |
| 4892 | v = cson_strdup( cson_string_cstr( jstr ), slen ); |
| 4893 | break; |
| 4894 | } |
| 4895 | case CSON_TYPE_INTEGER: { |
| 4896 | char buf[BufSize] = {0}; |
| 4897 | if( 0 < sprintf( v, "%"CSON_INT_T_PFMT, cson_value_get_integer(orig)) ) |
| @@ -5351,11 +5351,11 @@ | |
| 5351 | int rc = 0; |
| 5352 | int colCount = 0; |
| 5353 | assert(st); |
| 5354 | colCount = sqlite3_column_count(st); |
| 5355 | if( colCount <= 0 ) return NULL; |
| 5356 | |
| 5357 | aryV = cson_value_new_array(); |
| 5358 | if( ! aryV ) return NULL; |
| 5359 | ary = cson_value_get_array(aryV); |
| 5360 | assert(ary); |
| 5361 | for( i = 0; (0 ==rc) && (i < colCount); ++i ) |
| @@ -5491,11 +5491,11 @@ | |
| 5491 | aryV = NULL; |
| 5492 | end: |
| 5493 | return aryV; |
| 5494 | } |
| 5495 | |
| 5496 | |
| 5497 | /** |
| 5498 | Internal impl of cson_sqlite3_stmt_to_json() when the 'fat' |
| 5499 | parameter is non-0. |
| 5500 | */ |
| 5501 | static int cson_sqlite3_stmt_to_json_fat( sqlite3_stmt * st, cson_value ** tgt ) |
| @@ -5636,11 +5636,11 @@ | |
| 5636 | int rc = sqlite3_prepare_v2( db, sql, -1, &st, NULL ); |
| 5637 | if( 0 != rc ) return cson_rc.IOError /* FIXME: Better error code? */; |
| 5638 | rc = cson_sqlite3_stmt_to_json( st, tgt, fat ); |
| 5639 | sqlite3_finalize( st ); |
| 5640 | return rc; |
| 5641 | } |
| 5642 | } |
| 5643 | |
| 5644 | int cson_sqlite3_bind_value( sqlite3_stmt * st, int ndx, cson_value const * v ) |
| 5645 | { |
| 5646 | int rc = 0; |
| 5647 |
+74
-74
| --- src/cson_amalgamation.h | ||
| +++ src/cson_amalgamation.h | ||
| @@ -59,11 +59,11 @@ | ||
| 59 | 59 | #define CSON_INT_T_PFMT "I64d" |
| 60 | 60 | #elif (__STDC_VERSION__ >= 199901L) || (HAVE_LONG_LONG == 1) |
| 61 | 61 | typedef long long cson_int_t; |
| 62 | 62 | #define CSON_INT_T_SFMT "lld" |
| 63 | 63 | #define CSON_INT_T_PFMT "lld" |
| 64 | -#else | |
| 64 | +#else | |
| 65 | 65 | typedef long cson_int_t; |
| 66 | 66 | #define CSON_INT_T_SFMT "ld" |
| 67 | 67 | #define CSON_INT_T_PFMT "ld" |
| 68 | 68 | #endif |
| 69 | 69 | |
| @@ -215,11 +215,11 @@ | ||
| 215 | 215 | Convenience typedef. |
| 216 | 216 | */ |
| 217 | 217 | typedef struct cson_value cson_value; |
| 218 | 218 | |
| 219 | 219 | /** @struct cson_value |
| 220 | - | |
| 220 | + | |
| 221 | 221 | The core value type of this API. It is opaque to clients, and |
| 222 | 222 | only the cson public API should be used for setting or |
| 223 | 223 | inspecting their values. |
| 224 | 224 | |
| 225 | 225 | This class is opaque because stack-based usage can easily cause |
| @@ -231,11 +231,11 @@ | ||
| 231 | 231 | counting) as long as those insertions do not cause cycles. However, |
| 232 | 232 | be very aware that such value re-use uses a reference to the |
| 233 | 233 | original copy, meaning that if its value is changed once, it is |
| 234 | 234 | changed everywhere. Also beware that multi-threaded write |
| 235 | 235 | operations on such references leads to undefined behaviour. |
| 236 | - | |
| 236 | + | |
| 237 | 237 | PLEASE read the ACHTUNGEN below... |
| 238 | 238 | |
| 239 | 239 | ACHTUNG #1: |
| 240 | 240 | |
| 241 | 241 | cson_values MUST NOT form cycles (e.g. via object or array |
| @@ -244,33 +244,33 @@ | ||
| 244 | 244 | Not abiding th Holy Law Of No Cycles will lead to double-frees and |
| 245 | 245 | the like (i.e. undefined behaviour, likely crashes due to infinite |
| 246 | 246 | recursion or stepping on invalid (freed) pointers). |
| 247 | 247 | |
| 248 | 248 | ACHTUNG #2: |
| 249 | - | |
| 249 | + | |
| 250 | 250 | ALL cson_values returned as non-const cson_value pointers from any |
| 251 | 251 | public functions in the cson API are to be treated as if they are |
| 252 | 252 | heap-allocated, and MUST be freed by client by doing ONE of: |
| 253 | - | |
| 253 | + | |
| 254 | 254 | - Passing it to cson_value_free(). |
| 255 | - | |
| 255 | + | |
| 256 | 256 | - Adding it to an Object or Array, in which case the object/array |
| 257 | 257 | takes over ownership. As of 20110323, a value may be inserted into |
| 258 | 258 | a single container multiple times, or into multiple containers, |
| 259 | 259 | in which case they all share ownership (via reference counting) |
| 260 | 260 | of the original value (meaning any changes to it are visible in |
| 261 | 261 | all references to it). |
| 262 | - | |
| 262 | + | |
| 263 | 263 | Each call to cson_value_new_xxx() MUST eventually be followed up |
| 264 | 264 | by one of those options. |
| 265 | - | |
| 265 | + | |
| 266 | 266 | Some cson_value_new_XXX() implementations do not actually allocate |
| 267 | 267 | memory, but this is an internal implementation detail. Client code |
| 268 | 268 | MUST NOT rely on this behaviour and MUST treat each object |
| 269 | 269 | returned by such a function as if it was a freshly-allocated copy |
| 270 | 270 | (even if their pointer addresses are the same). |
| 271 | - | |
| 271 | + | |
| 272 | 272 | ACHTUNG #3: |
| 273 | 273 | |
| 274 | 274 | Note that ACHTUNG #2 tells us that we must always free (or transfer |
| 275 | 275 | ownership of) all pointers returned bycson_value_new_xxx(), but |
| 276 | 276 | that two calls to (e.g.) cson_value_new_bool(1) will (or might) |
| @@ -315,11 +315,11 @@ | ||
| 315 | 315 | if( 0 == rc ) {...success...} |
| 316 | 316 | else if( cson_rc.ArgError == rc ) { ... some argument was wrong ... } |
| 317 | 317 | else if( cson_rc.AllocError == rc ) { ... allocation error ... } |
| 318 | 318 | ... |
| 319 | 319 | @endcode |
| 320 | - | |
| 320 | + | |
| 321 | 321 | The entries named Parse_XXX are generally only returned by |
| 322 | 322 | cson_parse() and friends. |
| 323 | 323 | */ |
| 324 | 324 | |
| 325 | 325 | /** @struct cson_rc_ |
| @@ -472,11 +472,11 @@ | ||
| 472 | 472 | |
| 473 | 473 | /** |
| 474 | 474 | Length, in bytes. |
| 475 | 475 | */ |
| 476 | 476 | unsigned int length; |
| 477 | - | |
| 477 | + | |
| 478 | 478 | /** |
| 479 | 479 | Error code of the parse run (0 for no error). |
| 480 | 480 | */ |
| 481 | 481 | int errorCode; |
| 482 | 482 | |
| @@ -523,11 +523,11 @@ | ||
| 523 | 523 | /** |
| 524 | 524 | Specifies how to indent (or not) output. The values |
| 525 | 525 | are: |
| 526 | 526 | |
| 527 | 527 | (0) == no extra indentation. |
| 528 | - | |
| 528 | + | |
| 529 | 529 | (1) == 1 TAB character for each level. |
| 530 | 530 | |
| 531 | 531 | (>1) == that number of SPACES for each level. |
| 532 | 532 | */ |
| 533 | 533 | unsigned char indentation; |
| @@ -536,11 +536,11 @@ | ||
| 536 | 536 | Maximum object/array depth to traverse. Traversing deeply can |
| 537 | 537 | be indicative of cycles in the object/array tree, and this |
| 538 | 538 | value is used to figure out when to abort the traversal. |
| 539 | 539 | */ |
| 540 | 540 | unsigned short maxDepth; |
| 541 | - | |
| 541 | + | |
| 542 | 542 | /** |
| 543 | 543 | If true, a newline will be added to generated output, |
| 544 | 544 | else not. |
| 545 | 545 | */ |
| 546 | 546 | char addNewline; |
| @@ -636,11 +636,11 @@ | ||
| 636 | 636 | |
| 637 | 637 | Must return 0 on success, non-0 on error (preferably a value from |
| 638 | 638 | cson_rc). |
| 639 | 639 | |
| 640 | 640 | These functions are called relatively often during the JSON-output |
| 641 | - process, and should try to be fast. | |
| 641 | + process, and should try to be fast. | |
| 642 | 642 | */ |
| 643 | 643 | typedef int (*cson_data_dest_f)( void * state, void const * src, unsigned int n ); |
| 644 | 644 | |
| 645 | 645 | /** |
| 646 | 646 | Reads JSON-formatted string data (in ASCII, UTF8, or UTF16), using the |
| @@ -664,15 +664,15 @@ | ||
| 664 | 664 | used. |
| 665 | 665 | |
| 666 | 666 | The info argument may be NULL. If it is not NULL then the parser |
| 667 | 667 | populates it with information which is useful in error |
| 668 | 668 | reporting. Namely, it contains the line/column of parse errors. |
| 669 | - | |
| 669 | + | |
| 670 | 670 | The srcState argument is ignored by this function but is passed on to src, |
| 671 | 671 | so any output-destination-specific state can be stored there and accessed |
| 672 | 672 | via the src callback. |
| 673 | - | |
| 673 | + | |
| 674 | 674 | Non-parse error conditions include: |
| 675 | 675 | |
| 676 | 676 | - (!tgt) or !src: cson_rc.ArgError |
| 677 | 677 | - cson_rc.AllocError can happen at any time during the input phase |
| 678 | 678 | |
| @@ -725,11 +725,11 @@ | ||
| 725 | 725 | TODOs: |
| 726 | 726 | |
| 727 | 727 | - Buffer the input in larger chunks. We currently read |
| 728 | 728 | byte-by-byte, but i'm too tired to write/test the looping code for |
| 729 | 729 | the buffering. |
| 730 | - | |
| 730 | + | |
| 731 | 731 | @see cson_parse_FILE() |
| 732 | 732 | @see cson_parse_string() |
| 733 | 733 | */ |
| 734 | 734 | int cson_parse( cson_value ** tgt, cson_data_source_f src, void * srcState, |
| 735 | 735 | cson_parse_opt const * opt, cson_parse_info * info ); |
| @@ -786,11 +786,11 @@ | ||
| 786 | 786 | The destState parameter is ignored by this function and is passed |
| 787 | 787 | on to the dest function. |
| 788 | 788 | |
| 789 | 789 | Returns 0 on success. On error, any amount of output might have been |
| 790 | 790 | generated before the error was triggered. |
| 791 | - | |
| 791 | + | |
| 792 | 792 | Example: |
| 793 | 793 | |
| 794 | 794 | @code |
| 795 | 795 | int rc = cson_output( myValue, cson_data_dest_FILE, stdout, NULL ); |
| 796 | 796 | // basically equivalent to: cson_output_FILE( myValue, stdout, NULL ); |
| @@ -925,15 +925,15 @@ | ||
| 925 | 925 | /** |
| 926 | 926 | Converts the given value to a boolean, using JavaScript semantics depending |
| 927 | 927 | on the concrete type of val: |
| 928 | 928 | |
| 929 | 929 | undef or null: false |
| 930 | - | |
| 930 | + | |
| 931 | 931 | boolean: same |
| 932 | - | |
| 932 | + | |
| 933 | 933 | integer, double: 0 or 0.0 == false, else true |
| 934 | - | |
| 934 | + | |
| 935 | 935 | object, array: true |
| 936 | 936 | |
| 937 | 937 | string: length-0 string is false, else true. |
| 938 | 938 | |
| 939 | 939 | Returns 0 on success and assigns *v (if v is not NULL) to either 0 or 1. |
| @@ -945,18 +945,18 @@ | ||
| 945 | 945 | Similar to cson_value_fetch_bool(), but fetches an integer value. |
| 946 | 946 | |
| 947 | 947 | The conversion, if any, depends on the concrete type of val: |
| 948 | 948 | |
| 949 | 949 | NULL, null, undefined: *v is set to 0 and 0 is returned. |
| 950 | - | |
| 950 | + | |
| 951 | 951 | string, object, array: *v is set to 0 and |
| 952 | 952 | cson_rc.TypeError is returned. The error may normally be safely |
| 953 | 953 | ignored, but it is provided for those wanted to know whether a direct |
| 954 | 954 | conversion was possible. |
| 955 | 955 | |
| 956 | 956 | integer: *v is set to the int value and 0 is returned. |
| 957 | - | |
| 957 | + | |
| 958 | 958 | double: *v is set to the value truncated to int and 0 is returned. |
| 959 | 959 | */ |
| 960 | 960 | int cson_value_fetch_integer( cson_value const * val, cson_int_t * v ); |
| 961 | 961 | |
| 962 | 962 | /** |
| @@ -1084,11 +1084,11 @@ | ||
| 1084 | 1084 | |
| 1085 | 1085 | - The maximum number of bytes compared is the lesser of rhsLen and |
| 1086 | 1086 | the length of lhs. If the strings do not match, but compare equal |
| 1087 | 1087 | up to the just-described comparison length, the shorter string is |
| 1088 | 1088 | considered to be less-than the longer one. |
| 1089 | - | |
| 1089 | + | |
| 1090 | 1090 | - If lhs and rhs are both NULL, or both have a length of 0 then they will |
| 1091 | 1091 | compare equal. |
| 1092 | 1092 | |
| 1093 | 1093 | - If lhs is null/length-0 but rhs is not then lhs is considered to be less-than |
| 1094 | 1094 | rhs. |
| @@ -1110,11 +1110,11 @@ | ||
| 1110 | 1110 | Returns the length, in bytes, of str, or 0 if str is NULL. This is |
| 1111 | 1111 | an O(1) operation. |
| 1112 | 1112 | |
| 1113 | 1113 | TODO: add cson_string_length_chars() (is O(N) unless we add another |
| 1114 | 1114 | member to store the char length). |
| 1115 | - | |
| 1115 | + | |
| 1116 | 1116 | @see cson_string_cstr() |
| 1117 | 1117 | */ |
| 1118 | 1118 | unsigned int cson_string_length_bytes( cson_string const * str ); |
| 1119 | 1119 | |
| 1120 | 1120 | /** |
| @@ -1207,11 +1207,11 @@ | ||
| 1207 | 1207 | ar is expanded, if needed, to be able to hold at least (ndx+1) |
| 1208 | 1208 | items, and any new entries created by that expansion are empty |
| 1209 | 1209 | (NULL values). |
| 1210 | 1210 | |
| 1211 | 1211 | On success, 0 is returned and ownership of v is transfered to ar. |
| 1212 | - | |
| 1212 | + | |
| 1213 | 1213 | On error ownership of v is NOT modified, and the caller may still |
| 1214 | 1214 | need to clean it up. For example, the following code will introduce |
| 1215 | 1215 | a leak if this function fails: |
| 1216 | 1216 | |
| 1217 | 1217 | @code |
| @@ -1240,11 +1240,11 @@ | ||
| 1240 | 1240 | |
| 1241 | 1241 | This is functionally equivalent to |
| 1242 | 1242 | cson_array_set(ar,cson_array_length_get(ar),v), but this |
| 1243 | 1243 | implementation has slightly different array-preallocation policy |
| 1244 | 1244 | (it grows more eagerly). |
| 1245 | - | |
| 1245 | + | |
| 1246 | 1246 | Returns 0 on success, non-zero on error. Error cases include: |
| 1247 | 1247 | |
| 1248 | 1248 | - ar or v are NULL: cson_rc.ArgError |
| 1249 | 1249 | |
| 1250 | 1250 | - Array cannot be expanded to hold enough elements: cson_rc.AllocError. |
| @@ -1283,11 +1283,11 @@ | ||
| 1283 | 1283 | cson_value * cson_new_bool(char v); |
| 1284 | 1284 | |
| 1285 | 1285 | /** |
| 1286 | 1286 | Returns the special JSON "null" value. When outputing JSON, |
| 1287 | 1287 | its string representation is "null" (without the quotes). |
| 1288 | - | |
| 1288 | + | |
| 1289 | 1289 | See cson_value_new_bool() for notes regarding the returned |
| 1290 | 1290 | value's memory. |
| 1291 | 1291 | */ |
| 1292 | 1292 | cson_value * cson_value_null(); |
| 1293 | 1293 | |
| @@ -1323,16 +1323,16 @@ | ||
| 1323 | 1323 | |
| 1324 | 1324 | /** |
| 1325 | 1325 | Semantically the same as cson_value_new_bool(), but for strings. |
| 1326 | 1326 | This creates a JSON value which copies the first n bytes of str. |
| 1327 | 1327 | The string will automatically be NUL-terminated. |
| 1328 | - | |
| 1328 | + | |
| 1329 | 1329 | Note that if str is NULL or n is 0, this function still |
| 1330 | 1330 | returns non-NULL value representing that empty string. |
| 1331 | - | |
| 1331 | + | |
| 1332 | 1332 | Returns NULL on allocation error. |
| 1333 | - | |
| 1333 | + | |
| 1334 | 1334 | See cson_value_new_bool() for important information about the |
| 1335 | 1335 | returned memory. |
| 1336 | 1336 | */ |
| 1337 | 1337 | cson_value * cson_value_new_string( char const * str, unsigned int n ); |
| 1338 | 1338 | |
| @@ -1354,11 +1354,11 @@ | ||
| 1354 | 1354 | This works like cson_value_new_object() but returns an Object |
| 1355 | 1355 | handle directly. |
| 1356 | 1356 | |
| 1357 | 1357 | The value handle for the returned object can be fetched with |
| 1358 | 1358 | cson_object_value(theObject). |
| 1359 | - | |
| 1359 | + | |
| 1360 | 1360 | Ownership is transfered to the caller, who must eventually free it |
| 1361 | 1361 | by passing the Value handle (NOT the Object handle) to |
| 1362 | 1362 | cson_value_free() or passing ownership to a parent container. |
| 1363 | 1363 | |
| 1364 | 1364 | Returns NULL on error (out of memory). |
| @@ -1419,11 +1419,11 @@ | ||
| 1419 | 1419 | cson_value_add_reference(). Even if this function does not |
| 1420 | 1420 | immediately destroy the value, the value must be considered, from |
| 1421 | 1421 | the perspective of that client code, to have been |
| 1422 | 1422 | destroyed/invalidated by this call. |
| 1423 | 1423 | |
| 1424 | - | |
| 1424 | + | |
| 1425 | 1425 | @see cson_value_new_object() |
| 1426 | 1426 | @see cson_value_new_array() |
| 1427 | 1427 | @see cson_value_add_reference() |
| 1428 | 1428 | */ |
| 1429 | 1429 | void cson_value_free(cson_value * v); |
| @@ -1445,11 +1445,11 @@ | ||
| 1445 | 1445 | case, the key is set to the special null value. |
| 1446 | 1446 | |
| 1447 | 1447 | The key may be encoded as ASCII or UTF8. Results are undefined |
| 1448 | 1448 | with other encodings, and the errors won't show up here, but may |
| 1449 | 1449 | show up later, e.g. during output. |
| 1450 | - | |
| 1450 | + | |
| 1451 | 1451 | Returns 0 on success, non-0 on error. It has the following error |
| 1452 | 1452 | cases: |
| 1453 | 1453 | |
| 1454 | 1454 | - cson_rc.ArgError: obj or key are NULL or strlen(key) is 0. |
| 1455 | 1455 | |
| @@ -1498,11 +1498,11 @@ | ||
| 1498 | 1498 | */ |
| 1499 | 1499 | int cson_object_set_s( cson_object * obj, cson_string * key, cson_value * v ); |
| 1500 | 1500 | |
| 1501 | 1501 | /** |
| 1502 | 1502 | Removes a property from an object. |
| 1503 | - | |
| 1503 | + | |
| 1504 | 1504 | If obj contains the given key, it is removed and 0 is returned. If |
| 1505 | 1505 | it is not found, cson_rc.NotFoundError is returned (which can |
| 1506 | 1506 | normally be ignored by client code). |
| 1507 | 1507 | |
| 1508 | 1508 | cson_rc.ArgError is returned if obj or key are NULL or key has |
| @@ -1569,33 +1569,33 @@ | ||
| 1569 | 1569 | |
| 1570 | 1570 | If it finds the given path, it returns the value by assiging *tgt |
| 1571 | 1571 | to it. If tgt is NULL then this function has no side-effects but |
| 1572 | 1572 | will return 0 if the given path is found within the object, so it can be used |
| 1573 | 1573 | to test for existence without fetching it. |
| 1574 | - | |
| 1574 | + | |
| 1575 | 1575 | Returns 0 if it finds an entry, cson_rc.NotFoundError if it finds |
| 1576 | 1576 | no item, and any other non-zero error code on a "real" error. Errors include: |
| 1577 | 1577 | |
| 1578 | 1578 | - obj or path are NULL: cson_rc.ArgError |
| 1579 | - | |
| 1579 | + | |
| 1580 | 1580 | - separator is 0, or path is an empty string or contains only |
| 1581 | 1581 | separator characters: cson_rc.RangeError |
| 1582 | 1582 | |
| 1583 | 1583 | - There is an upper limit on how long a single path component may |
| 1584 | 1584 | be (some "reasonable" internal size), and cson_rc.RangeError is |
| 1585 | 1585 | returned if that length is violated. |
| 1586 | 1586 | |
| 1587 | - | |
| 1587 | + | |
| 1588 | 1588 | Limitations: |
| 1589 | 1589 | |
| 1590 | 1590 | - It has no way to fetch data from arrays this way. i could |
| 1591 | 1591 | imagine, e.g., a path of "subobj.subArray.0" for |
| 1592 | 1592 | subobj.subArray[0], or "0.3.1" for [0][3][1]. But i'm too |
| 1593 | 1593 | lazy/tired to add this. |
| 1594 | 1594 | |
| 1595 | 1595 | Example usage: |
| 1596 | - | |
| 1596 | + | |
| 1597 | 1597 | |
| 1598 | 1598 | Assume we have a JSON structure which abstractly looks like: |
| 1599 | 1599 | |
| 1600 | 1600 | @code |
| 1601 | 1601 | {"subobj":{"subsubobj":{"myValue":[1,2,3]}}} |
| @@ -1613,11 +1613,11 @@ | ||
| 1613 | 1613 | "subobj/subsubobj/myValue" with separator='/' is equivalent the |
| 1614 | 1614 | path "subobj.subsubobj.myValue" with separator='.'. The value of 0 |
| 1615 | 1615 | is not legal as a separator character because we cannot |
| 1616 | 1616 | distinguish that use from the real end-of-string without requiring |
| 1617 | 1617 | the caller to also pass in the length of the string. |
| 1618 | - | |
| 1618 | + | |
| 1619 | 1619 | Multiple successive separators in the list are collapsed into a |
| 1620 | 1620 | single separator for parsing purposes. e.g. the path "a...b...c" |
| 1621 | 1621 | (separator='.') is equivalent to "a.b.c". |
| 1622 | 1622 | |
| 1623 | 1623 | @see cson_object_get_sub() |
| @@ -1700,11 +1700,11 @@ | ||
| 1700 | 1700 | @see cson_object_iter_init() |
| 1701 | 1701 | @see cson_object_iter_next() |
| 1702 | 1702 | */ |
| 1703 | 1703 | struct cson_object_iterator |
| 1704 | 1704 | { |
| 1705 | - | |
| 1705 | + | |
| 1706 | 1706 | /** @internal |
| 1707 | 1707 | The underlying object. |
| 1708 | 1708 | */ |
| 1709 | 1709 | cson_object const * obj; |
| 1710 | 1710 | /** @internal |
| @@ -1771,11 +1771,11 @@ | ||
| 1771 | 1771 | ... |
| 1772 | 1772 | } |
| 1773 | 1773 | @endcode |
| 1774 | 1774 | |
| 1775 | 1775 | There is no need to clean up an iterator, as it holds no dynamic resources. |
| 1776 | - | |
| 1776 | + | |
| 1777 | 1777 | @see cson_kvp_key() |
| 1778 | 1778 | @see cson_kvp_value() |
| 1779 | 1779 | */ |
| 1780 | 1780 | cson_kvp * cson_object_iter_next( cson_object_iterator * iter ); |
| 1781 | 1781 | |
| @@ -1863,11 +1863,11 @@ | ||
| 1863 | 1863 | buf = cson_buffer_empty; |
| 1864 | 1864 | @endcode |
| 1865 | 1865 | |
| 1866 | 1866 | (You might also need to store buf.used and buf.capacity, |
| 1867 | 1867 | depending on what you want to do with the memory.) |
| 1868 | - | |
| 1868 | + | |
| 1869 | 1869 | When doing so, the memory must eventually be passed to free() |
| 1870 | 1870 | to deallocate it. |
| 1871 | 1871 | */ |
| 1872 | 1872 | unsigned char * mem; |
| 1873 | 1873 | }; |
| @@ -1894,11 +1894,11 @@ | ||
| 1894 | 1894 | On error non-zero is returned. Errors include: |
| 1895 | 1895 | |
| 1896 | 1896 | - Invalid arguments: cson_rc.ArgError |
| 1897 | 1897 | |
| 1898 | 1898 | - Buffer cannot be expanded (runs out of memory): cson_rc.AllocError |
| 1899 | - | |
| 1899 | + | |
| 1900 | 1900 | Example usage: |
| 1901 | 1901 | |
| 1902 | 1902 | @code |
| 1903 | 1903 | cson_buffer buf = cson_buffer_empty; |
| 1904 | 1904 | // optional: cson_buffer_reserve(&buf, 1024 * 10); |
| @@ -1918,13 +1918,13 @@ | ||
| 1918 | 1918 | buf = cson_buffer_empty; |
| 1919 | 1919 | ... |
| 1920 | 1920 | free(mem); |
| 1921 | 1921 | } |
| 1922 | 1922 | @endcode |
| 1923 | - | |
| 1923 | + | |
| 1924 | 1924 | @see cson_output() |
| 1925 | - | |
| 1925 | + | |
| 1926 | 1926 | */ |
| 1927 | 1927 | int cson_output_buffer( cson_value const * v, cson_buffer * buf, |
| 1928 | 1928 | cson_output_opt const * opt ); |
| 1929 | 1929 | |
| 1930 | 1930 | /** |
| @@ -1993,11 +1993,11 @@ | ||
| 1993 | 1993 | calling cson_buffer_reserve(dest,0). |
| 1994 | 1994 | |
| 1995 | 1995 | dest->mem might (and possibly will) be (re)allocated by this |
| 1996 | 1996 | function, so any pointers to it held from before this call might be |
| 1997 | 1997 | invalidated by this call. |
| 1998 | - | |
| 1998 | + | |
| 1999 | 1999 | On error non-0 is returned and dest has almost certainly been |
| 2000 | 2000 | modified but its state must be considered incomplete. |
| 2001 | 2001 | |
| 2002 | 2002 | Errors include: |
| 2003 | 2003 | |
| @@ -2042,11 +2042,11 @@ | ||
| 2042 | 2042 | void * mem = buf.mem; |
| 2043 | 2043 | buf = cson_buffer_empty; |
| 2044 | 2044 | @endcode |
| 2045 | 2045 | |
| 2046 | 2046 | In which case the memory must eventually be passed to free() to |
| 2047 | - free it. | |
| 2047 | + free it. | |
| 2048 | 2048 | */ |
| 2049 | 2049 | int cson_buffer_fill_from( cson_buffer * dest, cson_data_source_f src, void * state ); |
| 2050 | 2050 | |
| 2051 | 2051 | |
| 2052 | 2052 | /** |
| @@ -2072,11 +2072,11 @@ | ||
| 2072 | 2072 | for other sharing points which added a reference. |
| 2073 | 2073 | |
| 2074 | 2074 | Normally any such value handles would be invalidated when the |
| 2075 | 2075 | parent container(s) is/are cleaned up, but this function can be |
| 2076 | 2076 | used to effectively delay the cleanup. |
| 2077 | - | |
| 2077 | + | |
| 2078 | 2078 | This function, at its lowest level, increments the value's |
| 2079 | 2079 | reference count by 1. |
| 2080 | 2080 | |
| 2081 | 2081 | To decrement the reference count, pass the value to |
| 2082 | 2082 | cson_value_free(), after which the value must be considered, from |
| @@ -2091,11 +2091,11 @@ | ||
| 2091 | 2091 | errors long before the reference count could overflow (assuming |
| 2092 | 2092 | those reference counts come from container insertions, as opposed |
| 2093 | 2093 | to via this function). |
| 2094 | 2094 | |
| 2095 | 2095 | Insider notes which clients really need to know: |
| 2096 | - | |
| 2096 | + | |
| 2097 | 2097 | For shared/constant value instances, such as those returned by |
| 2098 | 2098 | cson_value_true() and cson_value_null(), this function has no side |
| 2099 | 2099 | effects - it does not actually modify the reference count because |
| 2100 | 2100 | (A) those instances are shared across all client code and (B) those |
| 2101 | 2101 | objects are static and never get cleaned up. However, that is an |
| @@ -2173,11 +2173,11 @@ | ||
| 2173 | 2173 | returned object will be of the same logical type as orig. |
| 2174 | 2174 | |
| 2175 | 2175 | ACHTUNG: if orig contains any cyclic references at any depth level |
| 2176 | 2176 | this function will endlessly recurse. (Having _any_ cyclic |
| 2177 | 2177 | references violates this library's requirements.) |
| 2178 | - | |
| 2178 | + | |
| 2179 | 2179 | Returns NULL if orig is NULL or if cloning fails. Assuming that |
| 2180 | 2180 | orig is in a valid state, the only "likely" error case is that an |
| 2181 | 2181 | allocation fails while constructing the clone. In other words, if |
| 2182 | 2182 | cloning fails due to something other than an allocation error then |
| 2183 | 2183 | either orig is in an invalid state or there is a bug. |
| @@ -2278,11 +2278,11 @@ | ||
| 2278 | 2278 | --key=VAL : Treats VAL as either a double, integer, or string. |
| 2279 | 2279 | |
| 2280 | 2280 | --key= : Treats key as a JSON null (not literal NULL) value. |
| 2281 | 2281 | |
| 2282 | 2282 | Arguments not starting with a dash are skipped. |
| 2283 | - | |
| 2283 | + | |
| 2284 | 2284 | Each key/value pair is inserted into an object. If a given key |
| 2285 | 2285 | appears more than once then only the final entry is actually |
| 2286 | 2286 | stored. |
| 2287 | 2287 | |
| 2288 | 2288 | argc and argv are expected to be values from main() (or similar, |
| @@ -2434,22 +2434,22 @@ | ||
| 2434 | 2434 | Integer, double, null, or string (TEXT and BLOB data, though not |
| 2435 | 2435 | all blob data is legal for a JSON string). |
| 2436 | 2436 | |
| 2437 | 2437 | st must be a sqlite3_step()'d row and col must be a 0-based column |
| 2438 | 2438 | index within that result row. |
| 2439 | - */ | |
| 2439 | + */ | |
| 2440 | 2440 | cson_value * cson_sqlite3_column_to_value( sqlite3_stmt * st, int col ); |
| 2441 | 2441 | |
| 2442 | 2442 | /** |
| 2443 | 2443 | Creates a JSON Array object containing the names of all columns |
| 2444 | - of the given prepared statement handle. | |
| 2445 | - | |
| 2444 | + of the given prepared statement handle. | |
| 2445 | + | |
| 2446 | 2446 | Returns a new array value on success, which the caller owns. Its elements |
| 2447 | 2447 | are in the same order as in the underlying query. |
| 2448 | 2448 | |
| 2449 | 2449 | On error NULL is returned. |
| 2450 | - | |
| 2450 | + | |
| 2451 | 2451 | st is not traversed or freed by this function - only the column |
| 2452 | 2452 | count and names are read. |
| 2453 | 2453 | */ |
| 2454 | 2454 | cson_value * cson_sqlite3_column_names( sqlite3_stmt * st ); |
| 2455 | 2455 | |
| @@ -2494,22 +2494,22 @@ | ||
| 2494 | 2494 | */ |
| 2495 | 2495 | cson_value * cson_sqlite3_row_to_array( sqlite3_stmt * st ); |
| 2496 | 2496 | /** |
| 2497 | 2497 | Converts the results of an sqlite3 SELECT statement to JSON, |
| 2498 | 2498 | in the form of a cson_value object tree. |
| 2499 | - | |
| 2499 | + | |
| 2500 | 2500 | st must be a prepared, but not yet traversed, SELECT query. |
| 2501 | 2501 | tgt must be a pointer to NULL (see the example below). If |
| 2502 | 2502 | either of those arguments are NULL, cson_rc.ArgError is returned. |
| 2503 | - | |
| 2503 | + | |
| 2504 | 2504 | This walks the query results and returns a JSON object which |
| 2505 | 2505 | has a different structure depending on the value of the 'fat' |
| 2506 | 2506 | argument. |
| 2507 | - | |
| 2508 | - | |
| 2507 | + | |
| 2508 | + | |
| 2509 | 2509 | If 'fat' is 0 then the structure is: |
| 2510 | - | |
| 2510 | + | |
| 2511 | 2511 | @code |
| 2512 | 2512 | { |
| 2513 | 2513 | "columns":["colName1",..."colNameN"], |
| 2514 | 2514 | "rows":[ |
| 2515 | 2515 | [colVal0, ... colValN], |
| @@ -2516,16 +2516,16 @@ | ||
| 2516 | 2516 | [colVal0, ... colValN], |
| 2517 | 2517 | ... |
| 2518 | 2518 | ] |
| 2519 | 2519 | } |
| 2520 | 2520 | @endcode |
| 2521 | - | |
| 2521 | + | |
| 2522 | 2522 | In the "non-fat" format the order of the columns and row values is |
| 2523 | 2523 | guaranteed to be the same as that of the underlying query. |
| 2524 | - | |
| 2524 | + | |
| 2525 | 2525 | If 'fat' is not 0 then the structure is: |
| 2526 | - | |
| 2526 | + | |
| 2527 | 2527 | @code |
| 2528 | 2528 | { |
| 2529 | 2529 | "columns":["colName1",..."colNameN"], |
| 2530 | 2530 | "rows":[ |
| 2531 | 2531 | {"colName1":value1,..."colNameN":valueN}, |
| @@ -2543,33 +2543,33 @@ | ||
| 2543 | 2543 | |
| 2544 | 2544 | On success it returns 0 and assigns *tgt to a newly-allocated |
| 2545 | 2545 | JSON object tree (using the above structure), which the caller owns. |
| 2546 | 2546 | If the query returns no rows, the "rows" value will be an empty |
| 2547 | 2547 | array, as opposed to null. |
| 2548 | - | |
| 2548 | + | |
| 2549 | 2549 | On error non-0 is returned and *tgt is not modified. |
| 2550 | - | |
| 2550 | + | |
| 2551 | 2551 | The error code cson_rc.IOError is used to indicate a db-level |
| 2552 | 2552 | error, and cson_rc.TypeError is returned if sqlite3_column_count(st) |
| 2553 | 2553 | returns 0 or less (indicating an invalid or non-SELECT statement). |
| 2554 | - | |
| 2554 | + | |
| 2555 | 2555 | The JSON data types are determined by the column type as reported |
| 2556 | 2556 | by sqlite3_column_type(): |
| 2557 | - | |
| 2557 | + | |
| 2558 | 2558 | SQLITE_INTEGER: integer |
| 2559 | - | |
| 2559 | + | |
| 2560 | 2560 | SQLITE_FLOAT: double |
| 2561 | - | |
| 2561 | + | |
| 2562 | 2562 | SQLITE_TEXT or SQLITE_BLOB: string, and this will only work if |
| 2563 | 2563 | the data is UTF8 compatible. |
| 2564 | - | |
| 2564 | + | |
| 2565 | 2565 | If the db returns a literal or SQL NULL for a value it is converted |
| 2566 | 2566 | to a JSON null. If it somehow finds a column type it cannot handle, |
| 2567 | 2567 | the value is also converted to a NULL in the output. |
| 2568 | 2568 | |
| 2569 | 2569 | Example |
| 2570 | - | |
| 2570 | + | |
| 2571 | 2571 | @code |
| 2572 | 2572 | cson_value * json = NULL; |
| 2573 | 2573 | int rc = cson_sqlite3_stmt_to_json( myStatement, &json, 1 ); |
| 2574 | 2574 | if( 0 != rc ) { ... error ... } |
| 2575 | 2575 | else { |
| @@ -2603,14 +2603,14 @@ | ||
| 2603 | 2603 | TODO: add Object support for named parameters. |
| 2604 | 2604 | |
| 2605 | 2605 | Returns 0 on success, non-0 on error. |
| 2606 | 2606 | */ |
| 2607 | 2607 | int cson_sqlite3_bind_value( sqlite3_stmt * st, int ndx, cson_value const * v ); |
| 2608 | - | |
| 2608 | + | |
| 2609 | 2609 | #if defined(__cplusplus) |
| 2610 | 2610 | } /*extern "C"*/ |
| 2611 | 2611 | #endif |
| 2612 | - | |
| 2612 | + | |
| 2613 | 2613 | #endif /* CSON_ENABLE_SQLITE3 */ |
| 2614 | 2614 | #endif /* WANDERINGHORSE_NET_CSON_SQLITE3_H_INCLUDED */ |
| 2615 | 2615 | /* end file include/wh/cson/cson_sqlite3.h */ |
| 2616 | 2616 | #endif /* FOSSIL_ENABLE_JSON */ |
| 2617 | 2617 |
| --- src/cson_amalgamation.h | |
| +++ src/cson_amalgamation.h | |
| @@ -59,11 +59,11 @@ | |
| 59 | #define CSON_INT_T_PFMT "I64d" |
| 60 | #elif (__STDC_VERSION__ >= 199901L) || (HAVE_LONG_LONG == 1) |
| 61 | typedef long long cson_int_t; |
| 62 | #define CSON_INT_T_SFMT "lld" |
| 63 | #define CSON_INT_T_PFMT "lld" |
| 64 | #else |
| 65 | typedef long cson_int_t; |
| 66 | #define CSON_INT_T_SFMT "ld" |
| 67 | #define CSON_INT_T_PFMT "ld" |
| 68 | #endif |
| 69 | |
| @@ -215,11 +215,11 @@ | |
| 215 | Convenience typedef. |
| 216 | */ |
| 217 | typedef struct cson_value cson_value; |
| 218 | |
| 219 | /** @struct cson_value |
| 220 | |
| 221 | The core value type of this API. It is opaque to clients, and |
| 222 | only the cson public API should be used for setting or |
| 223 | inspecting their values. |
| 224 | |
| 225 | This class is opaque because stack-based usage can easily cause |
| @@ -231,11 +231,11 @@ | |
| 231 | counting) as long as those insertions do not cause cycles. However, |
| 232 | be very aware that such value re-use uses a reference to the |
| 233 | original copy, meaning that if its value is changed once, it is |
| 234 | changed everywhere. Also beware that multi-threaded write |
| 235 | operations on such references leads to undefined behaviour. |
| 236 | |
| 237 | PLEASE read the ACHTUNGEN below... |
| 238 | |
| 239 | ACHTUNG #1: |
| 240 | |
| 241 | cson_values MUST NOT form cycles (e.g. via object or array |
| @@ -244,33 +244,33 @@ | |
| 244 | Not abiding th Holy Law Of No Cycles will lead to double-frees and |
| 245 | the like (i.e. undefined behaviour, likely crashes due to infinite |
| 246 | recursion or stepping on invalid (freed) pointers). |
| 247 | |
| 248 | ACHTUNG #2: |
| 249 | |
| 250 | ALL cson_values returned as non-const cson_value pointers from any |
| 251 | public functions in the cson API are to be treated as if they are |
| 252 | heap-allocated, and MUST be freed by client by doing ONE of: |
| 253 | |
| 254 | - Passing it to cson_value_free(). |
| 255 | |
| 256 | - Adding it to an Object or Array, in which case the object/array |
| 257 | takes over ownership. As of 20110323, a value may be inserted into |
| 258 | a single container multiple times, or into multiple containers, |
| 259 | in which case they all share ownership (via reference counting) |
| 260 | of the original value (meaning any changes to it are visible in |
| 261 | all references to it). |
| 262 | |
| 263 | Each call to cson_value_new_xxx() MUST eventually be followed up |
| 264 | by one of those options. |
| 265 | |
| 266 | Some cson_value_new_XXX() implementations do not actually allocate |
| 267 | memory, but this is an internal implementation detail. Client code |
| 268 | MUST NOT rely on this behaviour and MUST treat each object |
| 269 | returned by such a function as if it was a freshly-allocated copy |
| 270 | (even if their pointer addresses are the same). |
| 271 | |
| 272 | ACHTUNG #3: |
| 273 | |
| 274 | Note that ACHTUNG #2 tells us that we must always free (or transfer |
| 275 | ownership of) all pointers returned bycson_value_new_xxx(), but |
| 276 | that two calls to (e.g.) cson_value_new_bool(1) will (or might) |
| @@ -315,11 +315,11 @@ | |
| 315 | if( 0 == rc ) {...success...} |
| 316 | else if( cson_rc.ArgError == rc ) { ... some argument was wrong ... } |
| 317 | else if( cson_rc.AllocError == rc ) { ... allocation error ... } |
| 318 | ... |
| 319 | @endcode |
| 320 | |
| 321 | The entries named Parse_XXX are generally only returned by |
| 322 | cson_parse() and friends. |
| 323 | */ |
| 324 | |
| 325 | /** @struct cson_rc_ |
| @@ -472,11 +472,11 @@ | |
| 472 | |
| 473 | /** |
| 474 | Length, in bytes. |
| 475 | */ |
| 476 | unsigned int length; |
| 477 | |
| 478 | /** |
| 479 | Error code of the parse run (0 for no error). |
| 480 | */ |
| 481 | int errorCode; |
| 482 | |
| @@ -523,11 +523,11 @@ | |
| 523 | /** |
| 524 | Specifies how to indent (or not) output. The values |
| 525 | are: |
| 526 | |
| 527 | (0) == no extra indentation. |
| 528 | |
| 529 | (1) == 1 TAB character for each level. |
| 530 | |
| 531 | (>1) == that number of SPACES for each level. |
| 532 | */ |
| 533 | unsigned char indentation; |
| @@ -536,11 +536,11 @@ | |
| 536 | Maximum object/array depth to traverse. Traversing deeply can |
| 537 | be indicative of cycles in the object/array tree, and this |
| 538 | value is used to figure out when to abort the traversal. |
| 539 | */ |
| 540 | unsigned short maxDepth; |
| 541 | |
| 542 | /** |
| 543 | If true, a newline will be added to generated output, |
| 544 | else not. |
| 545 | */ |
| 546 | char addNewline; |
| @@ -636,11 +636,11 @@ | |
| 636 | |
| 637 | Must return 0 on success, non-0 on error (preferably a value from |
| 638 | cson_rc). |
| 639 | |
| 640 | These functions are called relatively often during the JSON-output |
| 641 | process, and should try to be fast. |
| 642 | */ |
| 643 | typedef int (*cson_data_dest_f)( void * state, void const * src, unsigned int n ); |
| 644 | |
| 645 | /** |
| 646 | Reads JSON-formatted string data (in ASCII, UTF8, or UTF16), using the |
| @@ -664,15 +664,15 @@ | |
| 664 | used. |
| 665 | |
| 666 | The info argument may be NULL. If it is not NULL then the parser |
| 667 | populates it with information which is useful in error |
| 668 | reporting. Namely, it contains the line/column of parse errors. |
| 669 | |
| 670 | The srcState argument is ignored by this function but is passed on to src, |
| 671 | so any output-destination-specific state can be stored there and accessed |
| 672 | via the src callback. |
| 673 | |
| 674 | Non-parse error conditions include: |
| 675 | |
| 676 | - (!tgt) or !src: cson_rc.ArgError |
| 677 | - cson_rc.AllocError can happen at any time during the input phase |
| 678 | |
| @@ -725,11 +725,11 @@ | |
| 725 | TODOs: |
| 726 | |
| 727 | - Buffer the input in larger chunks. We currently read |
| 728 | byte-by-byte, but i'm too tired to write/test the looping code for |
| 729 | the buffering. |
| 730 | |
| 731 | @see cson_parse_FILE() |
| 732 | @see cson_parse_string() |
| 733 | */ |
| 734 | int cson_parse( cson_value ** tgt, cson_data_source_f src, void * srcState, |
| 735 | cson_parse_opt const * opt, cson_parse_info * info ); |
| @@ -786,11 +786,11 @@ | |
| 786 | The destState parameter is ignored by this function and is passed |
| 787 | on to the dest function. |
| 788 | |
| 789 | Returns 0 on success. On error, any amount of output might have been |
| 790 | generated before the error was triggered. |
| 791 | |
| 792 | Example: |
| 793 | |
| 794 | @code |
| 795 | int rc = cson_output( myValue, cson_data_dest_FILE, stdout, NULL ); |
| 796 | // basically equivalent to: cson_output_FILE( myValue, stdout, NULL ); |
| @@ -925,15 +925,15 @@ | |
| 925 | /** |
| 926 | Converts the given value to a boolean, using JavaScript semantics depending |
| 927 | on the concrete type of val: |
| 928 | |
| 929 | undef or null: false |
| 930 | |
| 931 | boolean: same |
| 932 | |
| 933 | integer, double: 0 or 0.0 == false, else true |
| 934 | |
| 935 | object, array: true |
| 936 | |
| 937 | string: length-0 string is false, else true. |
| 938 | |
| 939 | Returns 0 on success and assigns *v (if v is not NULL) to either 0 or 1. |
| @@ -945,18 +945,18 @@ | |
| 945 | Similar to cson_value_fetch_bool(), but fetches an integer value. |
| 946 | |
| 947 | The conversion, if any, depends on the concrete type of val: |
| 948 | |
| 949 | NULL, null, undefined: *v is set to 0 and 0 is returned. |
| 950 | |
| 951 | string, object, array: *v is set to 0 and |
| 952 | cson_rc.TypeError is returned. The error may normally be safely |
| 953 | ignored, but it is provided for those wanted to know whether a direct |
| 954 | conversion was possible. |
| 955 | |
| 956 | integer: *v is set to the int value and 0 is returned. |
| 957 | |
| 958 | double: *v is set to the value truncated to int and 0 is returned. |
| 959 | */ |
| 960 | int cson_value_fetch_integer( cson_value const * val, cson_int_t * v ); |
| 961 | |
| 962 | /** |
| @@ -1084,11 +1084,11 @@ | |
| 1084 | |
| 1085 | - The maximum number of bytes compared is the lesser of rhsLen and |
| 1086 | the length of lhs. If the strings do not match, but compare equal |
| 1087 | up to the just-described comparison length, the shorter string is |
| 1088 | considered to be less-than the longer one. |
| 1089 | |
| 1090 | - If lhs and rhs are both NULL, or both have a length of 0 then they will |
| 1091 | compare equal. |
| 1092 | |
| 1093 | - If lhs is null/length-0 but rhs is not then lhs is considered to be less-than |
| 1094 | rhs. |
| @@ -1110,11 +1110,11 @@ | |
| 1110 | Returns the length, in bytes, of str, or 0 if str is NULL. This is |
| 1111 | an O(1) operation. |
| 1112 | |
| 1113 | TODO: add cson_string_length_chars() (is O(N) unless we add another |
| 1114 | member to store the char length). |
| 1115 | |
| 1116 | @see cson_string_cstr() |
| 1117 | */ |
| 1118 | unsigned int cson_string_length_bytes( cson_string const * str ); |
| 1119 | |
| 1120 | /** |
| @@ -1207,11 +1207,11 @@ | |
| 1207 | ar is expanded, if needed, to be able to hold at least (ndx+1) |
| 1208 | items, and any new entries created by that expansion are empty |
| 1209 | (NULL values). |
| 1210 | |
| 1211 | On success, 0 is returned and ownership of v is transfered to ar. |
| 1212 | |
| 1213 | On error ownership of v is NOT modified, and the caller may still |
| 1214 | need to clean it up. For example, the following code will introduce |
| 1215 | a leak if this function fails: |
| 1216 | |
| 1217 | @code |
| @@ -1240,11 +1240,11 @@ | |
| 1240 | |
| 1241 | This is functionally equivalent to |
| 1242 | cson_array_set(ar,cson_array_length_get(ar),v), but this |
| 1243 | implementation has slightly different array-preallocation policy |
| 1244 | (it grows more eagerly). |
| 1245 | |
| 1246 | Returns 0 on success, non-zero on error. Error cases include: |
| 1247 | |
| 1248 | - ar or v are NULL: cson_rc.ArgError |
| 1249 | |
| 1250 | - Array cannot be expanded to hold enough elements: cson_rc.AllocError. |
| @@ -1283,11 +1283,11 @@ | |
| 1283 | cson_value * cson_new_bool(char v); |
| 1284 | |
| 1285 | /** |
| 1286 | Returns the special JSON "null" value. When outputing JSON, |
| 1287 | its string representation is "null" (without the quotes). |
| 1288 | |
| 1289 | See cson_value_new_bool() for notes regarding the returned |
| 1290 | value's memory. |
| 1291 | */ |
| 1292 | cson_value * cson_value_null(); |
| 1293 | |
| @@ -1323,16 +1323,16 @@ | |
| 1323 | |
| 1324 | /** |
| 1325 | Semantically the same as cson_value_new_bool(), but for strings. |
| 1326 | This creates a JSON value which copies the first n bytes of str. |
| 1327 | The string will automatically be NUL-terminated. |
| 1328 | |
| 1329 | Note that if str is NULL or n is 0, this function still |
| 1330 | returns non-NULL value representing that empty string. |
| 1331 | |
| 1332 | Returns NULL on allocation error. |
| 1333 | |
| 1334 | See cson_value_new_bool() for important information about the |
| 1335 | returned memory. |
| 1336 | */ |
| 1337 | cson_value * cson_value_new_string( char const * str, unsigned int n ); |
| 1338 | |
| @@ -1354,11 +1354,11 @@ | |
| 1354 | This works like cson_value_new_object() but returns an Object |
| 1355 | handle directly. |
| 1356 | |
| 1357 | The value handle for the returned object can be fetched with |
| 1358 | cson_object_value(theObject). |
| 1359 | |
| 1360 | Ownership is transfered to the caller, who must eventually free it |
| 1361 | by passing the Value handle (NOT the Object handle) to |
| 1362 | cson_value_free() or passing ownership to a parent container. |
| 1363 | |
| 1364 | Returns NULL on error (out of memory). |
| @@ -1419,11 +1419,11 @@ | |
| 1419 | cson_value_add_reference(). Even if this function does not |
| 1420 | immediately destroy the value, the value must be considered, from |
| 1421 | the perspective of that client code, to have been |
| 1422 | destroyed/invalidated by this call. |
| 1423 | |
| 1424 | |
| 1425 | @see cson_value_new_object() |
| 1426 | @see cson_value_new_array() |
| 1427 | @see cson_value_add_reference() |
| 1428 | */ |
| 1429 | void cson_value_free(cson_value * v); |
| @@ -1445,11 +1445,11 @@ | |
| 1445 | case, the key is set to the special null value. |
| 1446 | |
| 1447 | The key may be encoded as ASCII or UTF8. Results are undefined |
| 1448 | with other encodings, and the errors won't show up here, but may |
| 1449 | show up later, e.g. during output. |
| 1450 | |
| 1451 | Returns 0 on success, non-0 on error. It has the following error |
| 1452 | cases: |
| 1453 | |
| 1454 | - cson_rc.ArgError: obj or key are NULL or strlen(key) is 0. |
| 1455 | |
| @@ -1498,11 +1498,11 @@ | |
| 1498 | */ |
| 1499 | int cson_object_set_s( cson_object * obj, cson_string * key, cson_value * v ); |
| 1500 | |
| 1501 | /** |
| 1502 | Removes a property from an object. |
| 1503 | |
| 1504 | If obj contains the given key, it is removed and 0 is returned. If |
| 1505 | it is not found, cson_rc.NotFoundError is returned (which can |
| 1506 | normally be ignored by client code). |
| 1507 | |
| 1508 | cson_rc.ArgError is returned if obj or key are NULL or key has |
| @@ -1569,33 +1569,33 @@ | |
| 1569 | |
| 1570 | If it finds the given path, it returns the value by assiging *tgt |
| 1571 | to it. If tgt is NULL then this function has no side-effects but |
| 1572 | will return 0 if the given path is found within the object, so it can be used |
| 1573 | to test for existence without fetching it. |
| 1574 | |
| 1575 | Returns 0 if it finds an entry, cson_rc.NotFoundError if it finds |
| 1576 | no item, and any other non-zero error code on a "real" error. Errors include: |
| 1577 | |
| 1578 | - obj or path are NULL: cson_rc.ArgError |
| 1579 | |
| 1580 | - separator is 0, or path is an empty string or contains only |
| 1581 | separator characters: cson_rc.RangeError |
| 1582 | |
| 1583 | - There is an upper limit on how long a single path component may |
| 1584 | be (some "reasonable" internal size), and cson_rc.RangeError is |
| 1585 | returned if that length is violated. |
| 1586 | |
| 1587 | |
| 1588 | Limitations: |
| 1589 | |
| 1590 | - It has no way to fetch data from arrays this way. i could |
| 1591 | imagine, e.g., a path of "subobj.subArray.0" for |
| 1592 | subobj.subArray[0], or "0.3.1" for [0][3][1]. But i'm too |
| 1593 | lazy/tired to add this. |
| 1594 | |
| 1595 | Example usage: |
| 1596 | |
| 1597 | |
| 1598 | Assume we have a JSON structure which abstractly looks like: |
| 1599 | |
| 1600 | @code |
| 1601 | {"subobj":{"subsubobj":{"myValue":[1,2,3]}}} |
| @@ -1613,11 +1613,11 @@ | |
| 1613 | "subobj/subsubobj/myValue" with separator='/' is equivalent the |
| 1614 | path "subobj.subsubobj.myValue" with separator='.'. The value of 0 |
| 1615 | is not legal as a separator character because we cannot |
| 1616 | distinguish that use from the real end-of-string without requiring |
| 1617 | the caller to also pass in the length of the string. |
| 1618 | |
| 1619 | Multiple successive separators in the list are collapsed into a |
| 1620 | single separator for parsing purposes. e.g. the path "a...b...c" |
| 1621 | (separator='.') is equivalent to "a.b.c". |
| 1622 | |
| 1623 | @see cson_object_get_sub() |
| @@ -1700,11 +1700,11 @@ | |
| 1700 | @see cson_object_iter_init() |
| 1701 | @see cson_object_iter_next() |
| 1702 | */ |
| 1703 | struct cson_object_iterator |
| 1704 | { |
| 1705 | |
| 1706 | /** @internal |
| 1707 | The underlying object. |
| 1708 | */ |
| 1709 | cson_object const * obj; |
| 1710 | /** @internal |
| @@ -1771,11 +1771,11 @@ | |
| 1771 | ... |
| 1772 | } |
| 1773 | @endcode |
| 1774 | |
| 1775 | There is no need to clean up an iterator, as it holds no dynamic resources. |
| 1776 | |
| 1777 | @see cson_kvp_key() |
| 1778 | @see cson_kvp_value() |
| 1779 | */ |
| 1780 | cson_kvp * cson_object_iter_next( cson_object_iterator * iter ); |
| 1781 | |
| @@ -1863,11 +1863,11 @@ | |
| 1863 | buf = cson_buffer_empty; |
| 1864 | @endcode |
| 1865 | |
| 1866 | (You might also need to store buf.used and buf.capacity, |
| 1867 | depending on what you want to do with the memory.) |
| 1868 | |
| 1869 | When doing so, the memory must eventually be passed to free() |
| 1870 | to deallocate it. |
| 1871 | */ |
| 1872 | unsigned char * mem; |
| 1873 | }; |
| @@ -1894,11 +1894,11 @@ | |
| 1894 | On error non-zero is returned. Errors include: |
| 1895 | |
| 1896 | - Invalid arguments: cson_rc.ArgError |
| 1897 | |
| 1898 | - Buffer cannot be expanded (runs out of memory): cson_rc.AllocError |
| 1899 | |
| 1900 | Example usage: |
| 1901 | |
| 1902 | @code |
| 1903 | cson_buffer buf = cson_buffer_empty; |
| 1904 | // optional: cson_buffer_reserve(&buf, 1024 * 10); |
| @@ -1918,13 +1918,13 @@ | |
| 1918 | buf = cson_buffer_empty; |
| 1919 | ... |
| 1920 | free(mem); |
| 1921 | } |
| 1922 | @endcode |
| 1923 | |
| 1924 | @see cson_output() |
| 1925 | |
| 1926 | */ |
| 1927 | int cson_output_buffer( cson_value const * v, cson_buffer * buf, |
| 1928 | cson_output_opt const * opt ); |
| 1929 | |
| 1930 | /** |
| @@ -1993,11 +1993,11 @@ | |
| 1993 | calling cson_buffer_reserve(dest,0). |
| 1994 | |
| 1995 | dest->mem might (and possibly will) be (re)allocated by this |
| 1996 | function, so any pointers to it held from before this call might be |
| 1997 | invalidated by this call. |
| 1998 | |
| 1999 | On error non-0 is returned and dest has almost certainly been |
| 2000 | modified but its state must be considered incomplete. |
| 2001 | |
| 2002 | Errors include: |
| 2003 | |
| @@ -2042,11 +2042,11 @@ | |
| 2042 | void * mem = buf.mem; |
| 2043 | buf = cson_buffer_empty; |
| 2044 | @endcode |
| 2045 | |
| 2046 | In which case the memory must eventually be passed to free() to |
| 2047 | free it. |
| 2048 | */ |
| 2049 | int cson_buffer_fill_from( cson_buffer * dest, cson_data_source_f src, void * state ); |
| 2050 | |
| 2051 | |
| 2052 | /** |
| @@ -2072,11 +2072,11 @@ | |
| 2072 | for other sharing points which added a reference. |
| 2073 | |
| 2074 | Normally any such value handles would be invalidated when the |
| 2075 | parent container(s) is/are cleaned up, but this function can be |
| 2076 | used to effectively delay the cleanup. |
| 2077 | |
| 2078 | This function, at its lowest level, increments the value's |
| 2079 | reference count by 1. |
| 2080 | |
| 2081 | To decrement the reference count, pass the value to |
| 2082 | cson_value_free(), after which the value must be considered, from |
| @@ -2091,11 +2091,11 @@ | |
| 2091 | errors long before the reference count could overflow (assuming |
| 2092 | those reference counts come from container insertions, as opposed |
| 2093 | to via this function). |
| 2094 | |
| 2095 | Insider notes which clients really need to know: |
| 2096 | |
| 2097 | For shared/constant value instances, such as those returned by |
| 2098 | cson_value_true() and cson_value_null(), this function has no side |
| 2099 | effects - it does not actually modify the reference count because |
| 2100 | (A) those instances are shared across all client code and (B) those |
| 2101 | objects are static and never get cleaned up. However, that is an |
| @@ -2173,11 +2173,11 @@ | |
| 2173 | returned object will be of the same logical type as orig. |
| 2174 | |
| 2175 | ACHTUNG: if orig contains any cyclic references at any depth level |
| 2176 | this function will endlessly recurse. (Having _any_ cyclic |
| 2177 | references violates this library's requirements.) |
| 2178 | |
| 2179 | Returns NULL if orig is NULL or if cloning fails. Assuming that |
| 2180 | orig is in a valid state, the only "likely" error case is that an |
| 2181 | allocation fails while constructing the clone. In other words, if |
| 2182 | cloning fails due to something other than an allocation error then |
| 2183 | either orig is in an invalid state or there is a bug. |
| @@ -2278,11 +2278,11 @@ | |
| 2278 | --key=VAL : Treats VAL as either a double, integer, or string. |
| 2279 | |
| 2280 | --key= : Treats key as a JSON null (not literal NULL) value. |
| 2281 | |
| 2282 | Arguments not starting with a dash are skipped. |
| 2283 | |
| 2284 | Each key/value pair is inserted into an object. If a given key |
| 2285 | appears more than once then only the final entry is actually |
| 2286 | stored. |
| 2287 | |
| 2288 | argc and argv are expected to be values from main() (or similar, |
| @@ -2434,22 +2434,22 @@ | |
| 2434 | Integer, double, null, or string (TEXT and BLOB data, though not |
| 2435 | all blob data is legal for a JSON string). |
| 2436 | |
| 2437 | st must be a sqlite3_step()'d row and col must be a 0-based column |
| 2438 | index within that result row. |
| 2439 | */ |
| 2440 | cson_value * cson_sqlite3_column_to_value( sqlite3_stmt * st, int col ); |
| 2441 | |
| 2442 | /** |
| 2443 | Creates a JSON Array object containing the names of all columns |
| 2444 | of the given prepared statement handle. |
| 2445 | |
| 2446 | Returns a new array value on success, which the caller owns. Its elements |
| 2447 | are in the same order as in the underlying query. |
| 2448 | |
| 2449 | On error NULL is returned. |
| 2450 | |
| 2451 | st is not traversed or freed by this function - only the column |
| 2452 | count and names are read. |
| 2453 | */ |
| 2454 | cson_value * cson_sqlite3_column_names( sqlite3_stmt * st ); |
| 2455 | |
| @@ -2494,22 +2494,22 @@ | |
| 2494 | */ |
| 2495 | cson_value * cson_sqlite3_row_to_array( sqlite3_stmt * st ); |
| 2496 | /** |
| 2497 | Converts the results of an sqlite3 SELECT statement to JSON, |
| 2498 | in the form of a cson_value object tree. |
| 2499 | |
| 2500 | st must be a prepared, but not yet traversed, SELECT query. |
| 2501 | tgt must be a pointer to NULL (see the example below). If |
| 2502 | either of those arguments are NULL, cson_rc.ArgError is returned. |
| 2503 | |
| 2504 | This walks the query results and returns a JSON object which |
| 2505 | has a different structure depending on the value of the 'fat' |
| 2506 | argument. |
| 2507 | |
| 2508 | |
| 2509 | If 'fat' is 0 then the structure is: |
| 2510 | |
| 2511 | @code |
| 2512 | { |
| 2513 | "columns":["colName1",..."colNameN"], |
| 2514 | "rows":[ |
| 2515 | [colVal0, ... colValN], |
| @@ -2516,16 +2516,16 @@ | |
| 2516 | [colVal0, ... colValN], |
| 2517 | ... |
| 2518 | ] |
| 2519 | } |
| 2520 | @endcode |
| 2521 | |
| 2522 | In the "non-fat" format the order of the columns and row values is |
| 2523 | guaranteed to be the same as that of the underlying query. |
| 2524 | |
| 2525 | If 'fat' is not 0 then the structure is: |
| 2526 | |
| 2527 | @code |
| 2528 | { |
| 2529 | "columns":["colName1",..."colNameN"], |
| 2530 | "rows":[ |
| 2531 | {"colName1":value1,..."colNameN":valueN}, |
| @@ -2543,33 +2543,33 @@ | |
| 2543 | |
| 2544 | On success it returns 0 and assigns *tgt to a newly-allocated |
| 2545 | JSON object tree (using the above structure), which the caller owns. |
| 2546 | If the query returns no rows, the "rows" value will be an empty |
| 2547 | array, as opposed to null. |
| 2548 | |
| 2549 | On error non-0 is returned and *tgt is not modified. |
| 2550 | |
| 2551 | The error code cson_rc.IOError is used to indicate a db-level |
| 2552 | error, and cson_rc.TypeError is returned if sqlite3_column_count(st) |
| 2553 | returns 0 or less (indicating an invalid or non-SELECT statement). |
| 2554 | |
| 2555 | The JSON data types are determined by the column type as reported |
| 2556 | by sqlite3_column_type(): |
| 2557 | |
| 2558 | SQLITE_INTEGER: integer |
| 2559 | |
| 2560 | SQLITE_FLOAT: double |
| 2561 | |
| 2562 | SQLITE_TEXT or SQLITE_BLOB: string, and this will only work if |
| 2563 | the data is UTF8 compatible. |
| 2564 | |
| 2565 | If the db returns a literal or SQL NULL for a value it is converted |
| 2566 | to a JSON null. If it somehow finds a column type it cannot handle, |
| 2567 | the value is also converted to a NULL in the output. |
| 2568 | |
| 2569 | Example |
| 2570 | |
| 2571 | @code |
| 2572 | cson_value * json = NULL; |
| 2573 | int rc = cson_sqlite3_stmt_to_json( myStatement, &json, 1 ); |
| 2574 | if( 0 != rc ) { ... error ... } |
| 2575 | else { |
| @@ -2603,14 +2603,14 @@ | |
| 2603 | TODO: add Object support for named parameters. |
| 2604 | |
| 2605 | Returns 0 on success, non-0 on error. |
| 2606 | */ |
| 2607 | int cson_sqlite3_bind_value( sqlite3_stmt * st, int ndx, cson_value const * v ); |
| 2608 | |
| 2609 | #if defined(__cplusplus) |
| 2610 | } /*extern "C"*/ |
| 2611 | #endif |
| 2612 | |
| 2613 | #endif /* CSON_ENABLE_SQLITE3 */ |
| 2614 | #endif /* WANDERINGHORSE_NET_CSON_SQLITE3_H_INCLUDED */ |
| 2615 | /* end file include/wh/cson/cson_sqlite3.h */ |
| 2616 | #endif /* FOSSIL_ENABLE_JSON */ |
| 2617 |
| --- src/cson_amalgamation.h | |
| +++ src/cson_amalgamation.h | |
| @@ -59,11 +59,11 @@ | |
| 59 | #define CSON_INT_T_PFMT "I64d" |
| 60 | #elif (__STDC_VERSION__ >= 199901L) || (HAVE_LONG_LONG == 1) |
| 61 | typedef long long cson_int_t; |
| 62 | #define CSON_INT_T_SFMT "lld" |
| 63 | #define CSON_INT_T_PFMT "lld" |
| 64 | #else |
| 65 | typedef long cson_int_t; |
| 66 | #define CSON_INT_T_SFMT "ld" |
| 67 | #define CSON_INT_T_PFMT "ld" |
| 68 | #endif |
| 69 | |
| @@ -215,11 +215,11 @@ | |
| 215 | Convenience typedef. |
| 216 | */ |
| 217 | typedef struct cson_value cson_value; |
| 218 | |
| 219 | /** @struct cson_value |
| 220 | |
| 221 | The core value type of this API. It is opaque to clients, and |
| 222 | only the cson public API should be used for setting or |
| 223 | inspecting their values. |
| 224 | |
| 225 | This class is opaque because stack-based usage can easily cause |
| @@ -231,11 +231,11 @@ | |
| 231 | counting) as long as those insertions do not cause cycles. However, |
| 232 | be very aware that such value re-use uses a reference to the |
| 233 | original copy, meaning that if its value is changed once, it is |
| 234 | changed everywhere. Also beware that multi-threaded write |
| 235 | operations on such references leads to undefined behaviour. |
| 236 | |
| 237 | PLEASE read the ACHTUNGEN below... |
| 238 | |
| 239 | ACHTUNG #1: |
| 240 | |
| 241 | cson_values MUST NOT form cycles (e.g. via object or array |
| @@ -244,33 +244,33 @@ | |
| 244 | Not abiding th Holy Law Of No Cycles will lead to double-frees and |
| 245 | the like (i.e. undefined behaviour, likely crashes due to infinite |
| 246 | recursion or stepping on invalid (freed) pointers). |
| 247 | |
| 248 | ACHTUNG #2: |
| 249 | |
| 250 | ALL cson_values returned as non-const cson_value pointers from any |
| 251 | public functions in the cson API are to be treated as if they are |
| 252 | heap-allocated, and MUST be freed by client by doing ONE of: |
| 253 | |
| 254 | - Passing it to cson_value_free(). |
| 255 | |
| 256 | - Adding it to an Object or Array, in which case the object/array |
| 257 | takes over ownership. As of 20110323, a value may be inserted into |
| 258 | a single container multiple times, or into multiple containers, |
| 259 | in which case they all share ownership (via reference counting) |
| 260 | of the original value (meaning any changes to it are visible in |
| 261 | all references to it). |
| 262 | |
| 263 | Each call to cson_value_new_xxx() MUST eventually be followed up |
| 264 | by one of those options. |
| 265 | |
| 266 | Some cson_value_new_XXX() implementations do not actually allocate |
| 267 | memory, but this is an internal implementation detail. Client code |
| 268 | MUST NOT rely on this behaviour and MUST treat each object |
| 269 | returned by such a function as if it was a freshly-allocated copy |
| 270 | (even if their pointer addresses are the same). |
| 271 | |
| 272 | ACHTUNG #3: |
| 273 | |
| 274 | Note that ACHTUNG #2 tells us that we must always free (or transfer |
| 275 | ownership of) all pointers returned bycson_value_new_xxx(), but |
| 276 | that two calls to (e.g.) cson_value_new_bool(1) will (or might) |
| @@ -315,11 +315,11 @@ | |
| 315 | if( 0 == rc ) {...success...} |
| 316 | else if( cson_rc.ArgError == rc ) { ... some argument was wrong ... } |
| 317 | else if( cson_rc.AllocError == rc ) { ... allocation error ... } |
| 318 | ... |
| 319 | @endcode |
| 320 | |
| 321 | The entries named Parse_XXX are generally only returned by |
| 322 | cson_parse() and friends. |
| 323 | */ |
| 324 | |
| 325 | /** @struct cson_rc_ |
| @@ -472,11 +472,11 @@ | |
| 472 | |
| 473 | /** |
| 474 | Length, in bytes. |
| 475 | */ |
| 476 | unsigned int length; |
| 477 | |
| 478 | /** |
| 479 | Error code of the parse run (0 for no error). |
| 480 | */ |
| 481 | int errorCode; |
| 482 | |
| @@ -523,11 +523,11 @@ | |
| 523 | /** |
| 524 | Specifies how to indent (or not) output. The values |
| 525 | are: |
| 526 | |
| 527 | (0) == no extra indentation. |
| 528 | |
| 529 | (1) == 1 TAB character for each level. |
| 530 | |
| 531 | (>1) == that number of SPACES for each level. |
| 532 | */ |
| 533 | unsigned char indentation; |
| @@ -536,11 +536,11 @@ | |
| 536 | Maximum object/array depth to traverse. Traversing deeply can |
| 537 | be indicative of cycles in the object/array tree, and this |
| 538 | value is used to figure out when to abort the traversal. |
| 539 | */ |
| 540 | unsigned short maxDepth; |
| 541 | |
| 542 | /** |
| 543 | If true, a newline will be added to generated output, |
| 544 | else not. |
| 545 | */ |
| 546 | char addNewline; |
| @@ -636,11 +636,11 @@ | |
| 636 | |
| 637 | Must return 0 on success, non-0 on error (preferably a value from |
| 638 | cson_rc). |
| 639 | |
| 640 | These functions are called relatively often during the JSON-output |
| 641 | process, and should try to be fast. |
| 642 | */ |
| 643 | typedef int (*cson_data_dest_f)( void * state, void const * src, unsigned int n ); |
| 644 | |
| 645 | /** |
| 646 | Reads JSON-formatted string data (in ASCII, UTF8, or UTF16), using the |
| @@ -664,15 +664,15 @@ | |
| 664 | used. |
| 665 | |
| 666 | The info argument may be NULL. If it is not NULL then the parser |
| 667 | populates it with information which is useful in error |
| 668 | reporting. Namely, it contains the line/column of parse errors. |
| 669 | |
| 670 | The srcState argument is ignored by this function but is passed on to src, |
| 671 | so any output-destination-specific state can be stored there and accessed |
| 672 | via the src callback. |
| 673 | |
| 674 | Non-parse error conditions include: |
| 675 | |
| 676 | - (!tgt) or !src: cson_rc.ArgError |
| 677 | - cson_rc.AllocError can happen at any time during the input phase |
| 678 | |
| @@ -725,11 +725,11 @@ | |
| 725 | TODOs: |
| 726 | |
| 727 | - Buffer the input in larger chunks. We currently read |
| 728 | byte-by-byte, but i'm too tired to write/test the looping code for |
| 729 | the buffering. |
| 730 | |
| 731 | @see cson_parse_FILE() |
| 732 | @see cson_parse_string() |
| 733 | */ |
| 734 | int cson_parse( cson_value ** tgt, cson_data_source_f src, void * srcState, |
| 735 | cson_parse_opt const * opt, cson_parse_info * info ); |
| @@ -786,11 +786,11 @@ | |
| 786 | The destState parameter is ignored by this function and is passed |
| 787 | on to the dest function. |
| 788 | |
| 789 | Returns 0 on success. On error, any amount of output might have been |
| 790 | generated before the error was triggered. |
| 791 | |
| 792 | Example: |
| 793 | |
| 794 | @code |
| 795 | int rc = cson_output( myValue, cson_data_dest_FILE, stdout, NULL ); |
| 796 | // basically equivalent to: cson_output_FILE( myValue, stdout, NULL ); |
| @@ -925,15 +925,15 @@ | |
| 925 | /** |
| 926 | Converts the given value to a boolean, using JavaScript semantics depending |
| 927 | on the concrete type of val: |
| 928 | |
| 929 | undef or null: false |
| 930 | |
| 931 | boolean: same |
| 932 | |
| 933 | integer, double: 0 or 0.0 == false, else true |
| 934 | |
| 935 | object, array: true |
| 936 | |
| 937 | string: length-0 string is false, else true. |
| 938 | |
| 939 | Returns 0 on success and assigns *v (if v is not NULL) to either 0 or 1. |
| @@ -945,18 +945,18 @@ | |
| 945 | Similar to cson_value_fetch_bool(), but fetches an integer value. |
| 946 | |
| 947 | The conversion, if any, depends on the concrete type of val: |
| 948 | |
| 949 | NULL, null, undefined: *v is set to 0 and 0 is returned. |
| 950 | |
| 951 | string, object, array: *v is set to 0 and |
| 952 | cson_rc.TypeError is returned. The error may normally be safely |
| 953 | ignored, but it is provided for those wanted to know whether a direct |
| 954 | conversion was possible. |
| 955 | |
| 956 | integer: *v is set to the int value and 0 is returned. |
| 957 | |
| 958 | double: *v is set to the value truncated to int and 0 is returned. |
| 959 | */ |
| 960 | int cson_value_fetch_integer( cson_value const * val, cson_int_t * v ); |
| 961 | |
| 962 | /** |
| @@ -1084,11 +1084,11 @@ | |
| 1084 | |
| 1085 | - The maximum number of bytes compared is the lesser of rhsLen and |
| 1086 | the length of lhs. If the strings do not match, but compare equal |
| 1087 | up to the just-described comparison length, the shorter string is |
| 1088 | considered to be less-than the longer one. |
| 1089 | |
| 1090 | - If lhs and rhs are both NULL, or both have a length of 0 then they will |
| 1091 | compare equal. |
| 1092 | |
| 1093 | - If lhs is null/length-0 but rhs is not then lhs is considered to be less-than |
| 1094 | rhs. |
| @@ -1110,11 +1110,11 @@ | |
| 1110 | Returns the length, in bytes, of str, or 0 if str is NULL. This is |
| 1111 | an O(1) operation. |
| 1112 | |
| 1113 | TODO: add cson_string_length_chars() (is O(N) unless we add another |
| 1114 | member to store the char length). |
| 1115 | |
| 1116 | @see cson_string_cstr() |
| 1117 | */ |
| 1118 | unsigned int cson_string_length_bytes( cson_string const * str ); |
| 1119 | |
| 1120 | /** |
| @@ -1207,11 +1207,11 @@ | |
| 1207 | ar is expanded, if needed, to be able to hold at least (ndx+1) |
| 1208 | items, and any new entries created by that expansion are empty |
| 1209 | (NULL values). |
| 1210 | |
| 1211 | On success, 0 is returned and ownership of v is transfered to ar. |
| 1212 | |
| 1213 | On error ownership of v is NOT modified, and the caller may still |
| 1214 | need to clean it up. For example, the following code will introduce |
| 1215 | a leak if this function fails: |
| 1216 | |
| 1217 | @code |
| @@ -1240,11 +1240,11 @@ | |
| 1240 | |
| 1241 | This is functionally equivalent to |
| 1242 | cson_array_set(ar,cson_array_length_get(ar),v), but this |
| 1243 | implementation has slightly different array-preallocation policy |
| 1244 | (it grows more eagerly). |
| 1245 | |
| 1246 | Returns 0 on success, non-zero on error. Error cases include: |
| 1247 | |
| 1248 | - ar or v are NULL: cson_rc.ArgError |
| 1249 | |
| 1250 | - Array cannot be expanded to hold enough elements: cson_rc.AllocError. |
| @@ -1283,11 +1283,11 @@ | |
| 1283 | cson_value * cson_new_bool(char v); |
| 1284 | |
| 1285 | /** |
| 1286 | Returns the special JSON "null" value. When outputing JSON, |
| 1287 | its string representation is "null" (without the quotes). |
| 1288 | |
| 1289 | See cson_value_new_bool() for notes regarding the returned |
| 1290 | value's memory. |
| 1291 | */ |
| 1292 | cson_value * cson_value_null(); |
| 1293 | |
| @@ -1323,16 +1323,16 @@ | |
| 1323 | |
| 1324 | /** |
| 1325 | Semantically the same as cson_value_new_bool(), but for strings. |
| 1326 | This creates a JSON value which copies the first n bytes of str. |
| 1327 | The string will automatically be NUL-terminated. |
| 1328 | |
| 1329 | Note that if str is NULL or n is 0, this function still |
| 1330 | returns non-NULL value representing that empty string. |
| 1331 | |
| 1332 | Returns NULL on allocation error. |
| 1333 | |
| 1334 | See cson_value_new_bool() for important information about the |
| 1335 | returned memory. |
| 1336 | */ |
| 1337 | cson_value * cson_value_new_string( char const * str, unsigned int n ); |
| 1338 | |
| @@ -1354,11 +1354,11 @@ | |
| 1354 | This works like cson_value_new_object() but returns an Object |
| 1355 | handle directly. |
| 1356 | |
| 1357 | The value handle for the returned object can be fetched with |
| 1358 | cson_object_value(theObject). |
| 1359 | |
| 1360 | Ownership is transfered to the caller, who must eventually free it |
| 1361 | by passing the Value handle (NOT the Object handle) to |
| 1362 | cson_value_free() or passing ownership to a parent container. |
| 1363 | |
| 1364 | Returns NULL on error (out of memory). |
| @@ -1419,11 +1419,11 @@ | |
| 1419 | cson_value_add_reference(). Even if this function does not |
| 1420 | immediately destroy the value, the value must be considered, from |
| 1421 | the perspective of that client code, to have been |
| 1422 | destroyed/invalidated by this call. |
| 1423 | |
| 1424 | |
| 1425 | @see cson_value_new_object() |
| 1426 | @see cson_value_new_array() |
| 1427 | @see cson_value_add_reference() |
| 1428 | */ |
| 1429 | void cson_value_free(cson_value * v); |
| @@ -1445,11 +1445,11 @@ | |
| 1445 | case, the key is set to the special null value. |
| 1446 | |
| 1447 | The key may be encoded as ASCII or UTF8. Results are undefined |
| 1448 | with other encodings, and the errors won't show up here, but may |
| 1449 | show up later, e.g. during output. |
| 1450 | |
| 1451 | Returns 0 on success, non-0 on error. It has the following error |
| 1452 | cases: |
| 1453 | |
| 1454 | - cson_rc.ArgError: obj or key are NULL or strlen(key) is 0. |
| 1455 | |
| @@ -1498,11 +1498,11 @@ | |
| 1498 | */ |
| 1499 | int cson_object_set_s( cson_object * obj, cson_string * key, cson_value * v ); |
| 1500 | |
| 1501 | /** |
| 1502 | Removes a property from an object. |
| 1503 | |
| 1504 | If obj contains the given key, it is removed and 0 is returned. If |
| 1505 | it is not found, cson_rc.NotFoundError is returned (which can |
| 1506 | normally be ignored by client code). |
| 1507 | |
| 1508 | cson_rc.ArgError is returned if obj or key are NULL or key has |
| @@ -1569,33 +1569,33 @@ | |
| 1569 | |
| 1570 | If it finds the given path, it returns the value by assiging *tgt |
| 1571 | to it. If tgt is NULL then this function has no side-effects but |
| 1572 | will return 0 if the given path is found within the object, so it can be used |
| 1573 | to test for existence without fetching it. |
| 1574 | |
| 1575 | Returns 0 if it finds an entry, cson_rc.NotFoundError if it finds |
| 1576 | no item, and any other non-zero error code on a "real" error. Errors include: |
| 1577 | |
| 1578 | - obj or path are NULL: cson_rc.ArgError |
| 1579 | |
| 1580 | - separator is 0, or path is an empty string or contains only |
| 1581 | separator characters: cson_rc.RangeError |
| 1582 | |
| 1583 | - There is an upper limit on how long a single path component may |
| 1584 | be (some "reasonable" internal size), and cson_rc.RangeError is |
| 1585 | returned if that length is violated. |
| 1586 | |
| 1587 | |
| 1588 | Limitations: |
| 1589 | |
| 1590 | - It has no way to fetch data from arrays this way. i could |
| 1591 | imagine, e.g., a path of "subobj.subArray.0" for |
| 1592 | subobj.subArray[0], or "0.3.1" for [0][3][1]. But i'm too |
| 1593 | lazy/tired to add this. |
| 1594 | |
| 1595 | Example usage: |
| 1596 | |
| 1597 | |
| 1598 | Assume we have a JSON structure which abstractly looks like: |
| 1599 | |
| 1600 | @code |
| 1601 | {"subobj":{"subsubobj":{"myValue":[1,2,3]}}} |
| @@ -1613,11 +1613,11 @@ | |
| 1613 | "subobj/subsubobj/myValue" with separator='/' is equivalent the |
| 1614 | path "subobj.subsubobj.myValue" with separator='.'. The value of 0 |
| 1615 | is not legal as a separator character because we cannot |
| 1616 | distinguish that use from the real end-of-string without requiring |
| 1617 | the caller to also pass in the length of the string. |
| 1618 | |
| 1619 | Multiple successive separators in the list are collapsed into a |
| 1620 | single separator for parsing purposes. e.g. the path "a...b...c" |
| 1621 | (separator='.') is equivalent to "a.b.c". |
| 1622 | |
| 1623 | @see cson_object_get_sub() |
| @@ -1700,11 +1700,11 @@ | |
| 1700 | @see cson_object_iter_init() |
| 1701 | @see cson_object_iter_next() |
| 1702 | */ |
| 1703 | struct cson_object_iterator |
| 1704 | { |
| 1705 | |
| 1706 | /** @internal |
| 1707 | The underlying object. |
| 1708 | */ |
| 1709 | cson_object const * obj; |
| 1710 | /** @internal |
| @@ -1771,11 +1771,11 @@ | |
| 1771 | ... |
| 1772 | } |
| 1773 | @endcode |
| 1774 | |
| 1775 | There is no need to clean up an iterator, as it holds no dynamic resources. |
| 1776 | |
| 1777 | @see cson_kvp_key() |
| 1778 | @see cson_kvp_value() |
| 1779 | */ |
| 1780 | cson_kvp * cson_object_iter_next( cson_object_iterator * iter ); |
| 1781 | |
| @@ -1863,11 +1863,11 @@ | |
| 1863 | buf = cson_buffer_empty; |
| 1864 | @endcode |
| 1865 | |
| 1866 | (You might also need to store buf.used and buf.capacity, |
| 1867 | depending on what you want to do with the memory.) |
| 1868 | |
| 1869 | When doing so, the memory must eventually be passed to free() |
| 1870 | to deallocate it. |
| 1871 | */ |
| 1872 | unsigned char * mem; |
| 1873 | }; |
| @@ -1894,11 +1894,11 @@ | |
| 1894 | On error non-zero is returned. Errors include: |
| 1895 | |
| 1896 | - Invalid arguments: cson_rc.ArgError |
| 1897 | |
| 1898 | - Buffer cannot be expanded (runs out of memory): cson_rc.AllocError |
| 1899 | |
| 1900 | Example usage: |
| 1901 | |
| 1902 | @code |
| 1903 | cson_buffer buf = cson_buffer_empty; |
| 1904 | // optional: cson_buffer_reserve(&buf, 1024 * 10); |
| @@ -1918,13 +1918,13 @@ | |
| 1918 | buf = cson_buffer_empty; |
| 1919 | ... |
| 1920 | free(mem); |
| 1921 | } |
| 1922 | @endcode |
| 1923 | |
| 1924 | @see cson_output() |
| 1925 | |
| 1926 | */ |
| 1927 | int cson_output_buffer( cson_value const * v, cson_buffer * buf, |
| 1928 | cson_output_opt const * opt ); |
| 1929 | |
| 1930 | /** |
| @@ -1993,11 +1993,11 @@ | |
| 1993 | calling cson_buffer_reserve(dest,0). |
| 1994 | |
| 1995 | dest->mem might (and possibly will) be (re)allocated by this |
| 1996 | function, so any pointers to it held from before this call might be |
| 1997 | invalidated by this call. |
| 1998 | |
| 1999 | On error non-0 is returned and dest has almost certainly been |
| 2000 | modified but its state must be considered incomplete. |
| 2001 | |
| 2002 | Errors include: |
| 2003 | |
| @@ -2042,11 +2042,11 @@ | |
| 2042 | void * mem = buf.mem; |
| 2043 | buf = cson_buffer_empty; |
| 2044 | @endcode |
| 2045 | |
| 2046 | In which case the memory must eventually be passed to free() to |
| 2047 | free it. |
| 2048 | */ |
| 2049 | int cson_buffer_fill_from( cson_buffer * dest, cson_data_source_f src, void * state ); |
| 2050 | |
| 2051 | |
| 2052 | /** |
| @@ -2072,11 +2072,11 @@ | |
| 2072 | for other sharing points which added a reference. |
| 2073 | |
| 2074 | Normally any such value handles would be invalidated when the |
| 2075 | parent container(s) is/are cleaned up, but this function can be |
| 2076 | used to effectively delay the cleanup. |
| 2077 | |
| 2078 | This function, at its lowest level, increments the value's |
| 2079 | reference count by 1. |
| 2080 | |
| 2081 | To decrement the reference count, pass the value to |
| 2082 | cson_value_free(), after which the value must be considered, from |
| @@ -2091,11 +2091,11 @@ | |
| 2091 | errors long before the reference count could overflow (assuming |
| 2092 | those reference counts come from container insertions, as opposed |
| 2093 | to via this function). |
| 2094 | |
| 2095 | Insider notes which clients really need to know: |
| 2096 | |
| 2097 | For shared/constant value instances, such as those returned by |
| 2098 | cson_value_true() and cson_value_null(), this function has no side |
| 2099 | effects - it does not actually modify the reference count because |
| 2100 | (A) those instances are shared across all client code and (B) those |
| 2101 | objects are static and never get cleaned up. However, that is an |
| @@ -2173,11 +2173,11 @@ | |
| 2173 | returned object will be of the same logical type as orig. |
| 2174 | |
| 2175 | ACHTUNG: if orig contains any cyclic references at any depth level |
| 2176 | this function will endlessly recurse. (Having _any_ cyclic |
| 2177 | references violates this library's requirements.) |
| 2178 | |
| 2179 | Returns NULL if orig is NULL or if cloning fails. Assuming that |
| 2180 | orig is in a valid state, the only "likely" error case is that an |
| 2181 | allocation fails while constructing the clone. In other words, if |
| 2182 | cloning fails due to something other than an allocation error then |
| 2183 | either orig is in an invalid state or there is a bug. |
| @@ -2278,11 +2278,11 @@ | |
| 2278 | --key=VAL : Treats VAL as either a double, integer, or string. |
| 2279 | |
| 2280 | --key= : Treats key as a JSON null (not literal NULL) value. |
| 2281 | |
| 2282 | Arguments not starting with a dash are skipped. |
| 2283 | |
| 2284 | Each key/value pair is inserted into an object. If a given key |
| 2285 | appears more than once then only the final entry is actually |
| 2286 | stored. |
| 2287 | |
| 2288 | argc and argv are expected to be values from main() (or similar, |
| @@ -2434,22 +2434,22 @@ | |
| 2434 | Integer, double, null, or string (TEXT and BLOB data, though not |
| 2435 | all blob data is legal for a JSON string). |
| 2436 | |
| 2437 | st must be a sqlite3_step()'d row and col must be a 0-based column |
| 2438 | index within that result row. |
| 2439 | */ |
| 2440 | cson_value * cson_sqlite3_column_to_value( sqlite3_stmt * st, int col ); |
| 2441 | |
| 2442 | /** |
| 2443 | Creates a JSON Array object containing the names of all columns |
| 2444 | of the given prepared statement handle. |
| 2445 | |
| 2446 | Returns a new array value on success, which the caller owns. Its elements |
| 2447 | are in the same order as in the underlying query. |
| 2448 | |
| 2449 | On error NULL is returned. |
| 2450 | |
| 2451 | st is not traversed or freed by this function - only the column |
| 2452 | count and names are read. |
| 2453 | */ |
| 2454 | cson_value * cson_sqlite3_column_names( sqlite3_stmt * st ); |
| 2455 | |
| @@ -2494,22 +2494,22 @@ | |
| 2494 | */ |
| 2495 | cson_value * cson_sqlite3_row_to_array( sqlite3_stmt * st ); |
| 2496 | /** |
| 2497 | Converts the results of an sqlite3 SELECT statement to JSON, |
| 2498 | in the form of a cson_value object tree. |
| 2499 | |
| 2500 | st must be a prepared, but not yet traversed, SELECT query. |
| 2501 | tgt must be a pointer to NULL (see the example below). If |
| 2502 | either of those arguments are NULL, cson_rc.ArgError is returned. |
| 2503 | |
| 2504 | This walks the query results and returns a JSON object which |
| 2505 | has a different structure depending on the value of the 'fat' |
| 2506 | argument. |
| 2507 | |
| 2508 | |
| 2509 | If 'fat' is 0 then the structure is: |
| 2510 | |
| 2511 | @code |
| 2512 | { |
| 2513 | "columns":["colName1",..."colNameN"], |
| 2514 | "rows":[ |
| 2515 | [colVal0, ... colValN], |
| @@ -2516,16 +2516,16 @@ | |
| 2516 | [colVal0, ... colValN], |
| 2517 | ... |
| 2518 | ] |
| 2519 | } |
| 2520 | @endcode |
| 2521 | |
| 2522 | In the "non-fat" format the order of the columns and row values is |
| 2523 | guaranteed to be the same as that of the underlying query. |
| 2524 | |
| 2525 | If 'fat' is not 0 then the structure is: |
| 2526 | |
| 2527 | @code |
| 2528 | { |
| 2529 | "columns":["colName1",..."colNameN"], |
| 2530 | "rows":[ |
| 2531 | {"colName1":value1,..."colNameN":valueN}, |
| @@ -2543,33 +2543,33 @@ | |
| 2543 | |
| 2544 | On success it returns 0 and assigns *tgt to a newly-allocated |
| 2545 | JSON object tree (using the above structure), which the caller owns. |
| 2546 | If the query returns no rows, the "rows" value will be an empty |
| 2547 | array, as opposed to null. |
| 2548 | |
| 2549 | On error non-0 is returned and *tgt is not modified. |
| 2550 | |
| 2551 | The error code cson_rc.IOError is used to indicate a db-level |
| 2552 | error, and cson_rc.TypeError is returned if sqlite3_column_count(st) |
| 2553 | returns 0 or less (indicating an invalid or non-SELECT statement). |
| 2554 | |
| 2555 | The JSON data types are determined by the column type as reported |
| 2556 | by sqlite3_column_type(): |
| 2557 | |
| 2558 | SQLITE_INTEGER: integer |
| 2559 | |
| 2560 | SQLITE_FLOAT: double |
| 2561 | |
| 2562 | SQLITE_TEXT or SQLITE_BLOB: string, and this will only work if |
| 2563 | the data is UTF8 compatible. |
| 2564 | |
| 2565 | If the db returns a literal or SQL NULL for a value it is converted |
| 2566 | to a JSON null. If it somehow finds a column type it cannot handle, |
| 2567 | the value is also converted to a NULL in the output. |
| 2568 | |
| 2569 | Example |
| 2570 | |
| 2571 | @code |
| 2572 | cson_value * json = NULL; |
| 2573 | int rc = cson_sqlite3_stmt_to_json( myStatement, &json, 1 ); |
| 2574 | if( 0 != rc ) { ... error ... } |
| 2575 | else { |
| @@ -2603,14 +2603,14 @@ | |
| 2603 | TODO: add Object support for named parameters. |
| 2604 | |
| 2605 | Returns 0 on success, non-0 on error. |
| 2606 | */ |
| 2607 | int cson_sqlite3_bind_value( sqlite3_stmt * st, int ndx, cson_value const * v ); |
| 2608 | |
| 2609 | #if defined(__cplusplus) |
| 2610 | } /*extern "C"*/ |
| 2611 | #endif |
| 2612 | |
| 2613 | #endif /* CSON_ENABLE_SQLITE3 */ |
| 2614 | #endif /* WANDERINGHORSE_NET_CSON_SQLITE3_H_INCLUDED */ |
| 2615 | /* end file include/wh/cson/cson_sqlite3.h */ |
| 2616 | #endif /* FOSSIL_ENABLE_JSON */ |
| 2617 |