00001
00002
00003
00004
00005
00006
00007 #ifndef JX_H
00008 #define JX_H
00009
00039 #include <stdint.h>
00040 #include <assert.h>
00041
00043 typedef enum {
00044 JX_NULL = 0,
00045 JX_BOOLEAN,
00046 JX_INTEGER,
00047 JX_DOUBLE,
00048 JX_STRING,
00049 JX_SYMBOL,
00050 JX_ARRAY,
00051 JX_OBJECT,
00052 JX_OPERATOR,
00053 JX_FUNCTION,
00054 JX_ERROR,
00055 } jx_type_t;
00056
00057 typedef int64_t jx_int_t;
00058
00061 struct jx_item {
00062 struct jx *value;
00063 struct jx_item *next;
00064 };
00065
00068 struct jx_pair {
00069 struct jx *key;
00070 struct jx *value;
00071 struct jx_pair *next;
00072 };
00073
00074 typedef enum {
00075 JX_OP_EQ,
00076 JX_OP_NE,
00077 JX_OP_LE,
00078 JX_OP_LT,
00079 JX_OP_GE,
00080 JX_OP_GT,
00081 JX_OP_ADD,
00082 JX_OP_SUB,
00083 JX_OP_MUL,
00084 JX_OP_DIV,
00085 JX_OP_MOD,
00086 JX_OP_AND,
00087 JX_OP_OR,
00088 JX_OP_NOT,
00089 JX_OP_LOOKUP,
00090 JX_OP_INVALID
00091 } jx_operator_t;
00092
00093 struct jx_operator {
00094 jx_operator_t type;
00095 struct jx *left;
00096 struct jx *right;
00097 };
00098
00099 typedef enum {
00100 JX_FUNCTION_INVALID = 0,
00101 JX_FUNCTION_RANGE,
00102 JX_FUNCTION_STR,
00103 JX_FUNCTION_FOREACH,
00104 JX_FUNCTION_JOIN,
00105 JX_FUNCTION_DBG,
00106 } jx_function_t;
00107
00108 struct jx_function {
00109 jx_function_t function;
00110 struct jx *arguments;
00111 };
00112
00115 struct jx {
00116 jx_type_t type;
00117 union {
00118 int boolean_value;
00119 jx_int_t integer_value;
00120 double double_value;
00121 char * string_value;
00122 char * symbol_name;
00123 struct jx_item *items;
00124 struct jx_pair *pairs;
00125 struct jx_operator oper;
00126 struct jx_function func;
00127 struct jx *err;
00128 } u;
00129 };
00130
00132 struct jx * jx_null();
00133
00135 struct jx * jx_boolean( int boolean_value );
00136
00138 struct jx * jx_integer( jx_int_t integer_value );
00139
00141 struct jx * jx_double( double double_value );
00142
00144 struct jx * jx_string( const char *string_value );
00145
00147 struct jx * jx_format( const char *fmt, ... );
00148
00150 struct jx *jx_function( jx_function_t func, struct jx *args );
00151
00154 struct jx * jx_symbol( const char *symbol_name );
00155
00157 struct jx * jx_error( struct jx *err );
00158
00160 struct jx * jx_array( struct jx_item *items );
00161
00163 struct jx * jx_arrayv( struct jx *value, ... );
00164
00166 struct jx * jx_object( struct jx_pair *pairs );
00167
00169 struct jx * jx_operator( jx_operator_t oper, struct jx *left, struct jx *right );
00170
00172 struct jx_pair * jx_pair( struct jx *key, struct jx *value, struct jx_pair *next );
00173
00175 struct jx_item * jx_item( struct jx *value, struct jx_item *next );
00176
00178 int jx_istype( struct jx *j, jx_type_t type );
00179
00181 int jx_istrue( struct jx *j );
00182
00185 int jx_equals( struct jx *j, struct jx *k );
00186
00188 int jx_array_length( struct jx *array );
00189
00192 struct jx * jx_copy( struct jx *j );
00193
00195 void jx_delete( struct jx *j );
00196
00198 void jx_pair_delete( struct jx_pair *p );
00199
00201 void jx_item_delete( struct jx_item *i );
00202
00204 struct jx * jx_remove( struct jx *object, struct jx *key );
00205
00207 int jx_insert( struct jx *object, struct jx *key, struct jx *value );
00208
00210 int jx_insert_unless_empty( struct jx *object, struct jx *key, struct jx *value );
00211
00213 void jx_insert_integer( struct jx *object, const char *key, jx_int_t value );
00214
00216 void jx_insert_double( struct jx *object, const char *key, double value );
00217
00219 void jx_insert_string( struct jx *object, const char *key, const char *value );
00220
00222 struct jx * jx_lookup( struct jx *object, const char *key );
00223
00224
00225 struct jx * jx_lookup_guard( struct jx *j, const char *key, int *found );
00226
00228 const char * jx_lookup_string( struct jx *object, const char *key );
00229
00231 jx_int_t jx_lookup_integer( struct jx *object, const char *key );
00232
00234 int jx_lookup_boolean( struct jx *object, const char *key );
00235
00237 double jx_lookup_double( struct jx *object, const char *key );
00238
00240 void jx_array_insert( struct jx *array, struct jx *value );
00241
00243 void jx_array_append( struct jx *array, struct jx *value );
00244
00246 struct jx * jx_array_index( struct jx *j, int nth );
00247
00249 struct jx *jx_array_concat( struct jx *array, ...);
00250
00252 int jx_is_constant( struct jx *j );
00253
00255 void jx_export( struct jx *j );
00256
00274 struct jx * jx_iterate_array(struct jx *j, void **i);
00275
00294 struct jx * jx_iterate_values(struct jx *j, void **i);
00295
00314 struct jx * jx_iterate_keys(struct jx *j, void **i);
00315
00317 struct jx *jx_merge(struct jx *j, ...);
00318
00320 const char *jx_error_name(int code);
00321
00323 int jx_error_valid(struct jx *j);
00324
00325 #endif