Navit SVN

/work/compile/navit/src/navit/vehicle/webos/cJSON.h

00001 /*
00002   Copyright (c) 2009 Dave Gamble
00003  
00004   Permission is hereby granted, free of charge, to any person obtaining a copy
00005   of this software and associated documentation files (the "Software"), to deal
00006   in the Software without restriction, including without limitation the rights
00007   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008   copies of the Software, and to permit persons to whom the Software is
00009   furnished to do so, subject to the following conditions:
00010  
00011   The above copyright notice and this permission notice shall be included in
00012   all copies or substantial portions of the Software.
00013  
00014   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020   THE SOFTWARE.
00021 */
00022 
00023 #ifndef cJSON__h
00024 #define cJSON__h
00025 
00026 #ifdef __cplusplus
00027 extern "C"
00028 {
00029 #endif
00030 
00031 /* cJSON Types: */
00032 #define cJSON_False 0
00033 #define cJSON_True 1
00034 #define cJSON_NULL 2
00035 #define cJSON_Number 3
00036 #define cJSON_String 4
00037 #define cJSON_Array 5
00038 #define cJSON_Object 6
00039         
00040 #define cJSON_IsReference 256
00041 
00042 /* The cJSON structure: */
00043 typedef struct cJSON {
00044         struct cJSON *next,*prev;       /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
00045         struct cJSON *child;            /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
00046 
00047         int type;                                       /* The type of the item, as above. */
00048 
00049         char *valuestring;                      /* The item's string, if type==cJSON_String */
00050         int valueint;                           /* The item's number, if type==cJSON_Number */
00051         double valuedouble;                     /* The item's number, if type==cJSON_Number */
00052 
00053         char *string;                           /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
00054 } cJSON;
00055 
00056 typedef struct cJSON_Hooks {
00057       void *(*malloc_fn)(size_t sz);
00058       void (*free_fn)(void *ptr);
00059 } cJSON_Hooks;
00060 
00061 /* Supply malloc, realloc and free functions to cJSON */
00062 extern void cJSON_InitHooks(cJSON_Hooks* hooks);
00063 
00064 
00065 /* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */
00066 extern cJSON *cJSON_Parse(const char *value);
00067 /* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */
00068 extern char  *cJSON_Print(cJSON *item);
00069 /* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
00070 extern char  *cJSON_PrintUnformatted(cJSON *item);
00071 /* Delete a cJSON entity and all subentities. */
00072 extern void   cJSON_Delete(cJSON *c);
00073 
00074 /* Returns the number of items in an array (or object). */
00075 extern int        cJSON_GetArraySize(cJSON *array);
00076 /* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
00077 extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);
00078 /* Get item "string" from object. Case insensitive. */
00079 extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
00080 
00081 /* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
00082 extern const char *cJSON_GetErrorPtr(void);
00083         
00084 /* These calls create a cJSON item of the appropriate type. */
00085 extern cJSON *cJSON_CreateNull(void);
00086 extern cJSON *cJSON_CreateTrue(void);
00087 extern cJSON *cJSON_CreateFalse(void);
00088 extern cJSON *cJSON_CreateBool(int b);
00089 extern cJSON *cJSON_CreateNumber(double num);
00090 extern cJSON *cJSON_CreateString(const char *string);
00091 extern cJSON *cJSON_CreateArray(void);
00092 extern cJSON *cJSON_CreateObject(void);
00093 
00094 /* These utilities create an Array of count items. */
00095 extern cJSON *cJSON_CreateIntArray(int *numbers,int count);
00096 extern cJSON *cJSON_CreateFloatArray(float *numbers,int count);
00097 extern cJSON *cJSON_CreateDoubleArray(double *numbers,int count);
00098 extern cJSON *cJSON_CreateStringArray(const char **strings,int count);
00099 
00100 /* Append item to the specified array/object. */
00101 extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);
00102 extern void     cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
00103 /* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
00104 extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
00105 extern void     cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item);
00106 
00107 /* Remove/Detatch items from Arrays/Objects. */
00108 extern cJSON *cJSON_DetachItemFromArray(cJSON *array,int which);
00109 extern void   cJSON_DeleteItemFromArray(cJSON *array,int which);
00110 extern cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string);
00111 extern void   cJSON_DeleteItemFromObject(cJSON *object,const char *string);
00112         
00113 /* Update array items. */
00114 extern void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem);
00115 extern void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
00116 
00117 #define cJSON_AddNullToObject(object,name)      cJSON_AddItemToObject(object, name, cJSON_CreateNull())
00118 #define cJSON_AddTrueToObject(object,name)      cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
00119 #define cJSON_AddFalseToObject(object,name)             cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
00120 #define cJSON_AddNumberToObject(object,name,n)  cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
00121 #define cJSON_AddStringToObject(object,name,s)  cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
00122 
00123 #ifdef __cplusplus
00124 }
00125 #endif
00126 
00127 #endif