1 /* ---------------------------------------------------------------------------------------
2 Generic object framework for C. An object can be either a string, boolean, null,
3 number, array or hash (think Perl hash, dictionary, etc.).
4 * --------------------------------------------------------------------------------------- */
14 /* does we need this? */
15 #define MAX_OBJECT_NODES 1000000
20 /* top leve generic object structuure */
21 struct object_struct {
23 /* how many sub-objects do we contain. Note that this includes null
24 * array elements in sparse arrays */
27 /* optional class hint */
30 /* these determine how we define a given object */
39 /* attached accessor/mutator methods for the OO inclined*/
40 unsigned long (*push) (struct object_struct* src, struct object_struct*);
41 unsigned long (*add_index) (struct object_struct* src, unsigned long index, struct object_struct*);
42 unsigned long (*add_key) (struct object_struct* src, char* key, struct object_struct*);
43 struct object_struct* (*get_index) (struct object_struct*, unsigned long index);
44 struct object_struct* (*get_key) (struct object_struct*, char* key);
45 void (*set_string) (struct object_struct*, char*);
46 void (*set_number) (struct object_struct*, long number);
47 void (*set_double) (struct object_struct*, double number);
48 void (*set_class) (struct object_struct*, char* classname);
49 unsigned long (*remove_index) (struct object_struct*, unsigned long index);
50 unsigned long (*remove_key) (struct object_struct*, char* key);
51 char* (*get_string) (struct object_struct*);
52 char* (*to_json) (struct object_struct*);
53 void (*set_comment) (struct object_struct*, char* com);
55 /* our list of sub-objects */
56 struct object_node_struct* data;
58 /* if we're a string, here's our data */
61 /* if we're a boolean value, here's our value */
64 /* if we're a number, here's our value */
67 /* if we're a double, here's our value */
70 /* client may provide a comment string which will be
71 * added serialized object when applicable
75 typedef struct object_struct object;
78 /* this contains a single element of the object along with the elements
79 * index (if this object is an array) and key (if this object is a hash)
81 struct object_node_struct {
82 unsigned long index; /* our array position */
83 char* key; /* our hash key */
84 object* item; /* our object */
85 struct object_node_struct* next; /* pointer to the next object node */
87 typedef struct object_node_struct object_node;
89 /* utility object for iterating over hash objects */
90 struct object_iterator_struct {
91 object* obj; /* the topic object */
92 object_node* current; /* the current node within the object */
93 object_node* (*next) (struct object_iterator_struct*);
94 int (*has_next) (struct object_iterator_struct*);
96 typedef struct object_iterator_struct object_iterator;
98 /* allocates a new iterator */
99 object_iterator* new_iterator(object* obj);
101 /* de-allocates an iterator */
102 void free_iterator(object_iterator*);
104 /* returns the object_node currently pointed to by the iterator
105 * and increments the pointer to the next node
107 object_node* object_iterator_next(object_iterator*);
109 /* returns true if there is another node after the node
110 * currently pointed to
112 int object_iterator_has_next(object_iterator*);
115 /* allocates a new object. classname is optional */
116 object* new_object(char* string);
118 /* utility method for initing an object */
119 object* _init_object();
121 /* returns a pointer to the object at the given index */
122 object* object_get_index( object* obj, unsigned long index );
125 /* returns a pointer to the object with the given key */
126 object* object_get_key( object* obj, char* key );
128 /* de-allocates a object */
129 void free_object(object*);
131 /* allocates a new object node */
132 object_node* new_object_node(object* obj);
134 /* de-allocates a object node */
135 void free_object_node(object_node*);
137 /* pushes the given object onto the end of the list,
138 * returns the size on success, -1 on error
140 unsigned long object_push(object*, object* obj);
142 /* removes the object at the given index (if one exists) and inserts
143 * the new one. returns the size on success, -1 on error
145 unsigned long object_add_index(object*, unsigned long index, object* obj);
147 /* inserts the new object, overwriting any previous object with the given key
148 * returns the size on success, -1 on error
149 * if 'obj' is NULL, a new object is inserted at key 'key' with 'is_null'
152 unsigned long object_add_key(object*, char* key, object* obj);
154 /* removes the object at the given index and, if more items exist,
155 * re-indexes (shifts down by 1) the rest of the objects in the array
157 unsigned long object_remove_index(object*, unsigned long index);
159 /* de-allocates the object at index 'index' and sets the field to null.
160 * this will *not* change the object size */
161 //unsigned long object_clear_index(object*, unsigned long index);
163 /* removes the object with key 'key' if it exists */
164 unsigned long object_remove_key(object*, char* key);
166 /* returns a pointer to the string data held by this object */
167 char* object_get_string(object*);
169 /* sets the string data */
170 void object_set_string(object*, char* string);
172 /* sets the number value for the object */
173 void object_set_number(object*, long num);
175 /* sets the double value for this object */
176 void object_set_double(object*, double num);
178 /* sets the class hint for this object */
179 void object_set_class(object*, char* classname);
181 /* converts an object to a json string. client is responsible for freeing the return string */
182 char* object_to_json(object*);
184 /* utility function. clears all of the is_* flags */
185 void object_clear_type(object*);
187 /* set this object's comment string */
188 void object_set_comment(object*, char*);
190 /* starting at index 'index', shifts all indices down by one and
191 * decrements the objects size by 1
193 void object_shift_index(object*, unsigned long index);