2 Copyright (C) 2005 Georgia Public Library Service
3 Bill Erickson <highfalutin@gmail.com>
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
16 /* ---------------------------------------------------------------------------------------
17 Generic object framework for C. An object can be either a string, boolean, null,
18 number, array or hash (think Perl hash, dictionary, etc.).
19 * --------------------------------------------------------------------------------------- */
29 #define MAX_OBJECT_NODES 1000000
34 /* top level generic object structuure */
35 struct object_struct {
37 /* how many sub-objects do we contain. Note that this includes null
38 * array elements in sparse arrays */
41 /* optional class hint */
44 /* these determine how we define a given object */
53 /* attached accessor/mutator methods for the OO inclined*/
54 unsigned long (*push) (struct object_struct* src, struct object_struct*);
55 unsigned long (*set_index) (struct object_struct* src, unsigned long index, struct object_struct*);
56 unsigned long (*add_key) (struct object_struct* src, char* key, struct object_struct*);
57 struct object_struct* (*get_index) (struct object_struct*, unsigned long index);
58 struct object_struct* (*get_key) (struct object_struct*, char* key);
59 void (*set_string) (struct object_struct*, char*);
60 void (*set_number) (struct object_struct*, long number);
61 void (*set_double) (struct object_struct*, double number);
62 void (*set_class) (struct object_struct*, char* classname);
63 unsigned long (*remove_index) (struct object_struct*, unsigned long index);
64 unsigned long (*remove_key) (struct object_struct*, char* key);
65 char* (*get_string) (struct object_struct*);
66 char* (*to_json) (struct object_struct*);
67 void (*set_comment) (struct object_struct*, char* com);
69 /* our list of sub-objects */
70 struct object_node_struct* data;
72 /* if we're a string, here's our data */
75 /* if we're a boolean value, here's our value */
78 /* if we're a number, here's our value */
81 /* if we're a double, here's our value */
84 /* client may provide a comment string which will be
85 * added serialized object when applicable
89 typedef struct object_struct object;
92 /* this contains a single element of the object along with the elements
93 * index (if this object is an array) and key (if this object is a hash)
95 struct object_node_struct {
96 unsigned long index; /* our array position */
97 char* key; /* our hash key */
98 object* item; /* our object */
99 struct object_node_struct* next; /* pointer to the next object node */
101 typedef struct object_node_struct object_node;
103 /* utility object for iterating over hash objects */
104 struct object_iterator_struct {
105 object* obj; /* the topic object */
106 object_node* current; /* the current node within the object */
107 object_node* (*next) (struct object_iterator_struct*);
108 int (*has_next) (struct object_iterator_struct*);
110 typedef struct object_iterator_struct object_iterator;
112 /* allocates a new iterator */
113 object_iterator* new_iterator(object* obj);
115 /* de-allocates an iterator */
116 void free_iterator(object_iterator*);
118 /* returns the object_node currently pointed to by the iterator
119 * and increments the pointer to the next node
121 object_node* object_iterator_next(object_iterator*);
123 /* returns true if there is another node after the node
124 * currently pointed to
126 int object_iterator_has_next(object_iterator*);
129 /* allocates a new object. 'string' is the string data if this object
130 is to be a string. if not, string should be NULL */
131 object* new_object(char* string);
133 /* utility method for initing an object */
134 object* _init_object();
136 /* returns a pointer to the object at the given index */
137 object* object_get_index( object* obj, unsigned long index );
140 /* returns a pointer to the object with the given key */
141 object* object_get_key( object* obj, char* key );
143 /* de-allocates a object ( * should only be called on objects that are not
144 children of other objects ) */
145 void free_object(object*);
147 /* allocates a new object node */
148 object_node* new_object_node(object* obj);
150 /* de-allocates a object node */
151 void free_object_node(object_node*);
154 /* pushes the given object onto the end of the list,
155 * returns the size on success, -1 on error
156 * If obj is NULL, inserts a new object into the list with is_null set to true
158 unsigned long object_push(object*, object* obj);
160 /* removes (and deallocates) the object at the given index (if one exists) and inserts
161 * the new one. returns the size on success, -1 on error
162 * If obj is NULL, inserts a new object into the list with is_null set to true
164 unsigned long object_set_index(object*, unsigned long index, object* obj);
166 /* inserts the new object, overwriting (removing, deallocating) any
167 * previous object with the given key.
168 * returns the size on success, -1 on error
169 * if 'obj' is NULL, a new object is inserted at key 'key' with 'is_null'
172 unsigned long object_add_key(object*, char* key, object* obj);
174 /* removes the object at the given index and, if more items exist,
175 * re-indexes (shifts down by 1) the rest of the objects in the array
177 unsigned long object_remove_index(object*, unsigned long index);
179 /* removes (and deallocates) the object with key 'key' if it exists */
180 unsigned long object_remove_key(object*, char* key);
182 /* returns a pointer to the string data held by this object */
183 char* object_get_string(object*);
185 /* sets the string data */
186 void object_set_string(object*, char* string);
188 /* sets the number value for the object */
189 void object_set_number(object*, long num);
191 /* sets the double value for this object */
192 void object_set_double(object*, double num);
194 /* sets the class hint for this object */
195 void object_set_class(object*, char* classname);
197 /* converts an object to a json string. client is responsible for freeing the return string */
198 char* object_to_json(object*);
200 /* utility function. clears all of the is_* flags */
201 void object_clear_type(object*);
203 /* set this object's comment string */
204 void object_set_comment(object*, char*);
206 /* utility method. starting at index 'index', shifts all indices down by one and
207 * decrements the objects size by 1
209 void object_shift_index(object*, unsigned long index);