1 | /* |
---|
2 | * ---------------------------------------------------------------------- |
---|
3 | * RpObject.cc |
---|
4 | * |
---|
5 | * Rappture 2.0 Object member functions |
---|
6 | * |
---|
7 | * ====================================================================== |
---|
8 | * AUTHOR: Derrick Kearney, Purdue University |
---|
9 | * Copyright (c) 2005-2009 Purdue Research Foundation |
---|
10 | * |
---|
11 | * See the file "license.terms" for information on usage and |
---|
12 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
13 | * ====================================================================== |
---|
14 | */ |
---|
15 | |
---|
16 | #include "RpObject.h" |
---|
17 | #include "RpHashHelper.h" |
---|
18 | |
---|
19 | using namespace Rappture; |
---|
20 | |
---|
21 | Object::Object() |
---|
22 | { |
---|
23 | __init(); |
---|
24 | } |
---|
25 | |
---|
26 | Object::Object ( |
---|
27 | const char *name, |
---|
28 | const char *path, |
---|
29 | const char *label, |
---|
30 | const char *desc, |
---|
31 | const char *hints, |
---|
32 | const char *color ) |
---|
33 | { |
---|
34 | this->name(name); |
---|
35 | this->path(path); |
---|
36 | this->label(label); |
---|
37 | this->desc(desc); |
---|
38 | this->hints(hints); |
---|
39 | this->color(color); |
---|
40 | } |
---|
41 | |
---|
42 | Object::Object(const Object& o) |
---|
43 | { |
---|
44 | name(o.name()); |
---|
45 | path(o.path()); |
---|
46 | label(o.label()); |
---|
47 | desc(o.desc()); |
---|
48 | hints(o.hints()); |
---|
49 | color(o.color()); |
---|
50 | // icon(o.icon()); |
---|
51 | |
---|
52 | if (o._h != NULL) { |
---|
53 | Rp_HashCopy(_h,o._h,charCpyFxn); |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | Object::~Object() |
---|
58 | { |
---|
59 | __clear(); |
---|
60 | } |
---|
61 | |
---|
62 | const char* |
---|
63 | Object::name() const |
---|
64 | { |
---|
65 | return propstr("name"); |
---|
66 | } |
---|
67 | |
---|
68 | void |
---|
69 | Object::name(const char *p) |
---|
70 | { |
---|
71 | propstr("name",p); |
---|
72 | } |
---|
73 | |
---|
74 | const char* |
---|
75 | Object::path() const |
---|
76 | { |
---|
77 | return propstr("path"); |
---|
78 | } |
---|
79 | |
---|
80 | void |
---|
81 | Object::path(const char *p) |
---|
82 | { |
---|
83 | propstr("path",p); |
---|
84 | } |
---|
85 | |
---|
86 | const char* |
---|
87 | Object::label() const |
---|
88 | { |
---|
89 | return propstr("label"); |
---|
90 | } |
---|
91 | |
---|
92 | void |
---|
93 | Object::label( |
---|
94 | const char *p) |
---|
95 | { |
---|
96 | propstr("label",p); |
---|
97 | } |
---|
98 | |
---|
99 | const char* |
---|
100 | Object::desc() const |
---|
101 | { |
---|
102 | return propstr("desc"); |
---|
103 | } |
---|
104 | |
---|
105 | void |
---|
106 | Object::desc(const char *p) |
---|
107 | { |
---|
108 | propstr("desc",p); |
---|
109 | } |
---|
110 | |
---|
111 | const char* |
---|
112 | Object::hints() const |
---|
113 | { |
---|
114 | return propstr("hints"); |
---|
115 | } |
---|
116 | |
---|
117 | void |
---|
118 | Object::hints(const char *p) |
---|
119 | { |
---|
120 | propstr("hints",p); |
---|
121 | } |
---|
122 | |
---|
123 | const char* |
---|
124 | Object::color() const |
---|
125 | { |
---|
126 | return propstr("color"); |
---|
127 | } |
---|
128 | |
---|
129 | void |
---|
130 | Object::color(const char *p) |
---|
131 | { |
---|
132 | propstr("color",p); |
---|
133 | } |
---|
134 | |
---|
135 | // get a general property |
---|
136 | const void * |
---|
137 | Object::property(const char *key) const |
---|
138 | { |
---|
139 | if (key == NULL) { |
---|
140 | // no key to search for |
---|
141 | return NULL; |
---|
142 | } |
---|
143 | |
---|
144 | if (_h == NULL) { |
---|
145 | // hash table does not exist, value is not in table |
---|
146 | return NULL; |
---|
147 | } |
---|
148 | |
---|
149 | // get the value |
---|
150 | return Rp_HashSearchNode(_h,key); |
---|
151 | } |
---|
152 | |
---|
153 | // set a general property |
---|
154 | void |
---|
155 | Object::property(const char *key, const void *val, size_t nbytes) |
---|
156 | { |
---|
157 | if (key == NULL) { |
---|
158 | // no key for value |
---|
159 | return; |
---|
160 | } |
---|
161 | |
---|
162 | if (_h == NULL) { |
---|
163 | // hash table does not exist, create it |
---|
164 | _h = (Rp_HashTable*) malloc(sizeof(Rp_HashTable)); |
---|
165 | Rp_InitHashTable(_h,RP_STRING_KEYS); |
---|
166 | } |
---|
167 | |
---|
168 | // FIXME: this is suspect, |
---|
169 | // want to use void's and void*'s but c++ wont let me |
---|
170 | // g++ says there is no delete[] for void* |
---|
171 | void *tmp = new char[nbytes]; |
---|
172 | memcpy(tmp,val,nbytes); |
---|
173 | |
---|
174 | // set the value |
---|
175 | char *old = (char *) Rp_HashRemoveNode(_h,key); |
---|
176 | delete[] old; |
---|
177 | old = NULL; |
---|
178 | Rp_HashAddNode(_h,key,tmp); |
---|
179 | |
---|
180 | return; |
---|
181 | } |
---|
182 | |
---|
183 | // get a const char* property |
---|
184 | const char * |
---|
185 | Object::propstr(const char *key) const |
---|
186 | { |
---|
187 | if (key == NULL) { |
---|
188 | // no key for value |
---|
189 | return NULL; |
---|
190 | } |
---|
191 | |
---|
192 | if (_h == NULL) { |
---|
193 | // hash table does not exist, value does not exist in table |
---|
194 | return NULL; |
---|
195 | } |
---|
196 | |
---|
197 | // get the value |
---|
198 | return (const char *) Rp_HashSearchNode(_h,key); |
---|
199 | } |
---|
200 | |
---|
201 | // set a const char* property |
---|
202 | void |
---|
203 | Object::propstr(const char *key, const char *val) |
---|
204 | { |
---|
205 | char *str = NULL; |
---|
206 | |
---|
207 | if (key == NULL) { |
---|
208 | // no key for value |
---|
209 | return; |
---|
210 | } |
---|
211 | |
---|
212 | if (_h == NULL) { |
---|
213 | // hash table does not exist, create it |
---|
214 | _h = (Rp_HashTable*) malloc(sizeof(Rp_HashTable)); |
---|
215 | Rp_InitHashTable(_h,RP_STRING_KEYS); |
---|
216 | } |
---|
217 | |
---|
218 | if (val == NULL) { |
---|
219 | // no value provided, no value stored. |
---|
220 | // FIXME: raise error. |
---|
221 | return; |
---|
222 | } |
---|
223 | |
---|
224 | // set the value |
---|
225 | // FIXME: this is suspect, |
---|
226 | // want to use void's and void*'s but c++ wont let me |
---|
227 | // g++ says there is no delete[] for void* |
---|
228 | // FIXME: can probably just get the length and use property() fxn |
---|
229 | size_t l = strlen(val); |
---|
230 | str = new char[l+1]; |
---|
231 | strcpy(str,val); |
---|
232 | |
---|
233 | char *old = (char *) Rp_HashRemoveNode(_h,key); |
---|
234 | delete[] old; |
---|
235 | old = NULL; |
---|
236 | Rp_HashAddNode(_h,key,str); |
---|
237 | |
---|
238 | return; |
---|
239 | } |
---|
240 | |
---|
241 | // remove a property from the hash table |
---|
242 | void |
---|
243 | Object::propremove(const char *key) |
---|
244 | { |
---|
245 | if ((key == NULL) || (_h == NULL)) { |
---|
246 | // key or hash table does not exist |
---|
247 | return; |
---|
248 | } |
---|
249 | |
---|
250 | char *tmp = (char *) Rp_HashRemoveNode(_h,key); |
---|
251 | delete[] tmp; |
---|
252 | tmp = NULL; |
---|
253 | return; |
---|
254 | } |
---|
255 | |
---|
256 | void |
---|
257 | Object::__init() |
---|
258 | { |
---|
259 | _h = NULL; |
---|
260 | label(""); |
---|
261 | desc(""); |
---|
262 | hints(""); |
---|
263 | color(""); |
---|
264 | // icon(NULL); |
---|
265 | path(""); |
---|
266 | |
---|
267 | _tmpBuf.clear(); |
---|
268 | |
---|
269 | _h = (Rp_HashTable*) malloc(sizeof(Rp_HashTable)); |
---|
270 | Rp_InitHashTable(_h,RP_STRING_KEYS); |
---|
271 | } |
---|
272 | |
---|
273 | void |
---|
274 | Object::__clear() |
---|
275 | { |
---|
276 | _tmpBuf.clear(); |
---|
277 | |
---|
278 | // traverse the hash table to delete values, then delete hash table |
---|
279 | if (_h != NULL) { |
---|
280 | Rp_HashEntry *hEntry = NULL; |
---|
281 | Rp_HashSearch hSearch; |
---|
282 | |
---|
283 | hEntry = Rp_FirstHashEntry(_h,&hSearch); |
---|
284 | while (hEntry != NULL) { |
---|
285 | // FIXME: this is suspect, |
---|
286 | // want to use void's and void*'s but c++ wont let me |
---|
287 | // g++ says there is no delete[] for void* |
---|
288 | char *v = (char *) Rp_GetHashValue(hEntry); |
---|
289 | delete[] v; |
---|
290 | v = NULL; |
---|
291 | hEntry = Rp_NextHashEntry(&hSearch); |
---|
292 | } |
---|
293 | |
---|
294 | Rp_DeleteHashTable(_h); |
---|
295 | _h = NULL; |
---|
296 | } |
---|
297 | } |
---|
298 | |
---|
299 | const char * |
---|
300 | Object::xml(size_t indent, size_t tabstop) const |
---|
301 | { |
---|
302 | return NULL; |
---|
303 | } |
---|
304 | |
---|
305 | /* |
---|
306 | void |
---|
307 | Object::xml(const char *xmltext) |
---|
308 | { |
---|
309 | return; |
---|
310 | } |
---|
311 | */ |
---|
312 | |
---|
313 | void |
---|
314 | Object::configure(size_t as, void *p) |
---|
315 | { |
---|
316 | return; |
---|
317 | } |
---|
318 | |
---|
319 | void |
---|
320 | Object::dump(size_t as, void *p) |
---|
321 | { |
---|
322 | return; |
---|
323 | } |
---|
324 | |
---|
325 | const int |
---|
326 | Object::is() const |
---|
327 | { |
---|
328 | // return "var" in hex |
---|
329 | return 0x766172; |
---|
330 | } |
---|
331 | |
---|
332 | // -------------------------------------------------------------------- // |
---|
333 | |
---|