]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/objson/object.h
added license info and some additional comments
[Evergreen.git] / OpenSRF / src / objson / object.h
1 /*
2 Copyright (C) 2005  Georgia Public Library Service 
3 Bill Erickson <highfalutin@gmail.com>
4
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.
9
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.
14 */
15
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  * --------------------------------------------------------------------------------------- */
20
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <assert.h>
27 #include "utils.h"
28
29 #define MAX_OBJECT_NODES 1000000
30
31 #ifndef OBJECT_H
32 #define OBJECT_H
33
34 /* top level generic object structuure */
35 struct object_struct {
36
37         /* how many sub-objects do we contain.  Note that this includes null
38          * array elements in sparse arrays */
39         unsigned long size;
40
41         /* optional class hint */
42         char* classname;
43
44         /* these determine how we define a given object */
45         int is_array;
46         int is_hash;
47         int is_string;
48         int is_null;
49         int is_bool;
50         int is_number;
51         int is_double;
52
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);
68
69         /* our list of sub-objects */
70         struct object_node_struct* data;
71
72         /* if we're a string, here's our data */
73         char* string_data;
74
75         /* if we're a boolean value, here's our value */
76         int bool_value;
77
78         /* if we're a number, here's our value */
79         long num_value;
80
81         /* if we're a double, here's our value */
82         double double_value;
83
84         /* client may provide a comment string which will be 
85          * added serialized object when applicable
86          */
87         char* comment;
88 };
89 typedef struct object_struct object;
90
91
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)
94  */
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 */
100 };
101 typedef struct object_node_struct object_node;
102
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*);
109 };
110 typedef struct object_iterator_struct object_iterator;
111
112 /* allocates a new iterator */
113 object_iterator* new_iterator(object* obj);
114
115 /* de-allocates an iterator */
116 void free_iterator(object_iterator*);
117
118 /* returns the object_node currently pointed to by the iterator
119  * and increments the pointer to the next node
120  */
121 object_node* object_iterator_next(object_iterator*);
122
123 /* returns true if there is another node after the node 
124  * currently pointed to
125  */
126 int object_iterator_has_next(object_iterator*);
127
128
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);
132
133 /* utility method for initing an object */
134 object* _init_object();
135
136 /* returns a pointer to the object at the given index */
137 object* object_get_index( object* obj, unsigned long index );
138
139
140 /* returns a pointer to the object with the given key */
141 object* object_get_key( object* obj, char* key );
142
143 /* de-allocates a object ( * should only be called on objects that are not
144         children of other objects ) */
145 void free_object(object*);
146
147 /* allocates a new object node */
148 object_node* new_object_node(object* obj);
149
150 /* de-allocates a object node */
151 void free_object_node(object_node*);
152
153
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
157  */
158 unsigned long object_push(object*, object* obj);
159
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
163  */
164 unsigned long object_set_index(object*, unsigned long index, object* obj);
165
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' 
170  * set to true
171  */
172 unsigned long object_add_key(object*, char* key, object* obj);
173
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
176  */
177 unsigned long object_remove_index(object*, unsigned long index);
178
179 /* removes (and deallocates) the object with key 'key' if it exists */
180 unsigned long object_remove_key(object*, char* key);
181
182 /* returns a pointer to the string data held by this object */
183 char* object_get_string(object*);
184
185 /* sets the string data */
186 void object_set_string(object*, char* string);
187
188 /* sets the number value for the object */
189 void object_set_number(object*, long num);
190
191 /* sets the double value for this object */
192 void object_set_double(object*, double num);
193
194 /* sets the class hint for this object */
195 void object_set_class(object*, char* classname);
196
197 /* converts an object to a json string.  client is responsible for freeing the return string */
198 char* object_to_json(object*);
199
200 /* utility function. clears all of the is_* flags */
201 void object_clear_type(object*);
202
203 /* set this object's comment string */
204 void object_set_comment(object*, char*);
205
206 /* utility method.  starting at index 'index', shifts all indices down by one and 
207  * decrements the objects size by 1 
208  */
209 void object_shift_index(object*, unsigned long index);
210
211
212
213 #endif