source: mainline/uspace/app/sbi/src/rdata.c@ 4204ad9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4204ad9 was 37f527b, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Update SBI to rev. 144.

  • Property mode set to 100644
File size: 10.3 KB
Line 
1/*
2 * Copyright (c) 2010 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @file Run-time data representation. */
30
31#include <stdlib.h>
32#include <assert.h>
33#include "mytypes.h"
34#include "stree.h"
35
36#include "rdata.h"
37
38static void rdata_int_copy(rdata_int_t *src, rdata_int_t **dest);
39static void rdata_string_copy(rdata_string_t *src, rdata_string_t **dest);
40static void rdata_ref_copy(rdata_ref_t *src, rdata_ref_t **dest);
41static void rdata_deleg_copy(rdata_deleg_t *src, rdata_deleg_t **dest);
42static void rdata_array_copy(rdata_array_t *src, rdata_array_t **dest);
43static void rdata_object_copy(rdata_object_t *src, rdata_object_t **dest);
44static void rdata_resource_copy(rdata_resource_t *src,
45 rdata_resource_t **dest);
46
47static int rdata_array_get_dim(rdata_array_t *array);
48
49static void rdata_address_print(rdata_address_t *address);
50static void rdata_value_print(rdata_value_t *value);
51static void rdata_var_print(rdata_var_t *var);
52
53
54rdata_item_t *rdata_item_new(item_class_t ic)
55{
56 rdata_item_t *item;
57
58 item = calloc(1, sizeof(rdata_item_t));
59 if (item == NULL) {
60 printf("Memory allocation failed.\n");
61 exit(1);
62 }
63
64 item->ic = ic;
65 return item;
66}
67
68rdata_addr_var_t *rdata_addr_var_new(void)
69{
70 rdata_addr_var_t *addr_var;
71
72 addr_var = calloc(1, sizeof(rdata_addr_var_t));
73 if (addr_var == NULL) {
74 printf("Memory allocation failed.\n");
75 exit(1);
76 }
77
78 return addr_var;
79}
80
81rdata_aprop_named_t *rdata_aprop_named_new(void)
82{
83 rdata_aprop_named_t *aprop_named;
84
85 aprop_named = calloc(1, sizeof(rdata_aprop_named_t));
86 if (aprop_named == NULL) {
87 printf("Memory allocation failed.\n");
88 exit(1);
89 }
90
91 return aprop_named;
92}
93
94rdata_aprop_indexed_t *rdata_aprop_indexed_new(void)
95{
96 rdata_aprop_indexed_t *aprop_indexed;
97
98 aprop_indexed = calloc(1, sizeof(rdata_aprop_indexed_t));
99 if (aprop_indexed == NULL) {
100 printf("Memory allocation failed.\n");
101 exit(1);
102 }
103
104 return aprop_indexed;
105}
106
107rdata_addr_prop_t *rdata_addr_prop_new(aprop_class_t apc)
108{
109 rdata_addr_prop_t *addr_prop;
110
111 addr_prop = calloc(1, sizeof(rdata_addr_prop_t));
112 if (addr_prop == NULL) {
113 printf("Memory allocation failed.\n");
114 exit(1);
115 }
116
117 addr_prop->apc = apc;
118 return addr_prop;
119}
120
121rdata_address_t *rdata_address_new(address_class_t ac)
122{
123 rdata_address_t *address;
124
125 address = calloc(1, sizeof(rdata_address_t));
126 if (address == NULL) {
127 printf("Memory allocation failed.\n");
128 exit(1);
129 }
130
131 address->ac = ac;
132 return address;
133}
134
135rdata_value_t *rdata_value_new(void)
136{
137 rdata_value_t *value;
138
139 value = calloc(1, sizeof(rdata_value_t));
140 if (value == NULL) {
141 printf("Memory allocation failed.\n");
142 exit(1);
143 }
144
145 return value;
146}
147
148rdata_var_t *rdata_var_new(var_class_t vc)
149{
150 rdata_var_t *var;
151
152 var = calloc(1, sizeof(rdata_var_t));
153 if (var == NULL) {
154 printf("Memory allocation failed.\n");
155 exit(1);
156 }
157
158 var->vc = vc;
159 return var;
160}
161
162rdata_ref_t *rdata_ref_new(void)
163{
164 rdata_ref_t *ref;
165
166 ref = calloc(1, sizeof(rdata_ref_t));
167 if (ref == NULL) {
168 printf("Memory allocation failed.\n");
169 exit(1);
170 }
171
172 return ref;
173}
174
175rdata_deleg_t *rdata_deleg_new(void)
176{
177 rdata_deleg_t *deleg;
178
179 deleg = calloc(1, sizeof(rdata_deleg_t));
180 if (deleg == NULL) {
181 printf("Memory allocation failed.\n");
182 exit(1);
183 }
184
185 return deleg;
186}
187
188rdata_array_t *rdata_array_new(int rank)
189{
190 rdata_array_t *array;
191
192 array = calloc(1, sizeof(rdata_array_t));
193 if (array == NULL) {
194 printf("Memory allocation failed.\n");
195 exit(1);
196 }
197
198 array->rank = rank;
199 array->extent = calloc(rank, sizeof(int));
200 if (array == NULL) {
201 printf("Memory allocation failed.\n");
202 exit(1);
203 }
204
205 return array;
206}
207
208rdata_object_t *rdata_object_new(void)
209{
210 rdata_object_t *object;
211
212 object = calloc(1, sizeof(rdata_object_t));
213 if (object == NULL) {
214 printf("Memory allocation failed.\n");
215 exit(1);
216 }
217
218 return object;
219}
220
221rdata_int_t *rdata_int_new(void)
222{
223 rdata_int_t *int_v;
224
225 int_v = calloc(1, sizeof(rdata_int_t));
226 if (int_v == NULL) {
227 printf("Memory allocation failed.\n");
228 exit(1);
229 }
230
231 return int_v;
232}
233
234rdata_string_t *rdata_string_new(void)
235{
236 rdata_string_t *string_v;
237
238 string_v = calloc(1, sizeof(rdata_string_t));
239 if (string_v == NULL) {
240 printf("Memory allocation failed.\n");
241 exit(1);
242 }
243
244 return string_v;
245}
246
247rdata_resource_t *rdata_resource_new(void)
248{
249 rdata_resource_t *resource_v;
250
251 resource_v = calloc(1, sizeof(rdata_resource_t));
252 if (resource_v == NULL) {
253 printf("Memory allocation failed.\n");
254 exit(1);
255 }
256
257 return resource_v;
258}
259
260void rdata_array_alloc_element(rdata_array_t *array)
261{
262 int dim, idx;
263
264 dim = rdata_array_get_dim(array);
265
266 array->element = calloc(dim, sizeof(rdata_var_t *));
267 if (array->element == NULL) {
268 printf("Memory allocation failed.\n");
269 exit(1);
270 }
271
272 for (idx = 0; idx < dim; ++idx) {
273 array->element[idx] = calloc(1, sizeof(rdata_var_t));
274 if (array->element[idx] == NULL) {
275 printf("Memory allocation failed.\n");
276 exit(1);
277 }
278 }
279}
280
281/** Get array dimension.
282 *
283 * Dimension is the total number of elements in an array, in other words,
284 * the product of all extents.
285 */
286static int rdata_array_get_dim(rdata_array_t *array)
287{
288 int didx, dim;
289
290 dim = 1;
291 for (didx = 0; didx < array->rank; ++didx)
292 dim = dim * array->extent[didx];
293
294 return dim;
295}
296
297/** Make copy of a variable. */
298void rdata_var_copy(rdata_var_t *src, rdata_var_t **dest)
299{
300 rdata_var_t *nvar;
301
302 nvar = rdata_var_new(src->vc);
303
304 switch (src->vc) {
305 case vc_int:
306 rdata_int_copy(src->u.int_v, &nvar->u.int_v);
307 break;
308 case vc_string:
309 rdata_string_copy(src->u.string_v, &nvar->u.string_v);
310 break;
311 case vc_ref:
312 rdata_ref_copy(src->u.ref_v, &nvar->u.ref_v);
313 break;
314 case vc_deleg:
315 rdata_deleg_copy(src->u.deleg_v, &nvar->u.deleg_v);
316 break;
317 case vc_array:
318 rdata_array_copy(src->u.array_v, &nvar->u.array_v);
319 break;
320 case vc_object:
321 rdata_object_copy(src->u.object_v, &nvar->u.object_v);
322 break;
323 case vc_resource:
324 rdata_resource_copy(src->u.resource_v, &nvar->u.resource_v);
325 break;
326 }
327
328 *dest = nvar;
329}
330
331static void rdata_int_copy(rdata_int_t *src, rdata_int_t **dest)
332{
333 *dest = rdata_int_new();
334 (*dest)->value = src->value;
335}
336
337static void rdata_string_copy(rdata_string_t *src, rdata_string_t **dest)
338{
339 *dest = rdata_string_new();
340 (*dest)->value = src->value;
341}
342
343static void rdata_ref_copy(rdata_ref_t *src, rdata_ref_t **dest)
344{
345 *dest = rdata_ref_new();
346 (*dest)->vref = src->vref;
347}
348
349static void rdata_deleg_copy(rdata_deleg_t *src, rdata_deleg_t **dest)
350{
351 (void) src; (void) dest;
352 printf("Unimplemented: Copy delegate.\n");
353 exit(1);
354}
355
356static void rdata_array_copy(rdata_array_t *src, rdata_array_t **dest)
357{
358 (void) src; (void) dest;
359 printf("Unimplemented: Copy array.\n");
360 exit(1);
361}
362
363static void rdata_object_copy(rdata_object_t *src, rdata_object_t **dest)
364{
365 (void) src; (void) dest;
366 printf("Unimplemented: Copy object.\n");
367 exit(1);
368}
369
370static void rdata_resource_copy(rdata_resource_t *src, rdata_resource_t **dest)
371{
372 *dest = rdata_resource_new();
373 (*dest)->data = src->data;
374}
375
376/** Read data from a variable.
377 *
378 * Return value stored in variable @a var.
379 */
380void rdata_var_read(rdata_var_t *var, rdata_item_t **ritem)
381{
382 rdata_value_t *value;
383 rdata_var_t *rvar;
384
385 /* Perform a shallow copy of @a var. */
386 rdata_var_copy(var, &rvar);
387
388 value = rdata_value_new();
389 value->var = rvar;
390 *ritem = rdata_item_new(ic_value);
391 (*ritem)->u.value = value;
392}
393
394/** Write data to a variable.
395 *
396 * Store @a value to variable @a var.
397 */
398void rdata_var_write(rdata_var_t *var, rdata_value_t *value)
399{
400 rdata_var_t *nvar;
401
402 /* Perform a shallow copy of @c value->var. */
403 rdata_var_copy(value->var, &nvar);
404
405 /* XXX do this in a prettier way. */
406
407 var->vc = nvar->vc;
408 switch (nvar->vc) {
409 case vc_int: var->u.int_v = nvar->u.int_v; break;
410 case vc_string: var->u.string_v = nvar->u.string_v; break;
411 case vc_ref: var->u.ref_v = nvar->u.ref_v; break;
412 case vc_deleg: var->u.deleg_v = nvar->u.deleg_v; break;
413 case vc_array: var->u.array_v = nvar->u.array_v; break;
414 case vc_object: var->u.object_v = nvar->u.object_v; break;
415 case vc_resource: var->u.resource_v = nvar->u.resource_v; break;
416 }
417
418 /* XXX We should free some stuff around here. */
419}
420
421void rdata_item_print(rdata_item_t *item)
422{
423 if (item == NULL) {
424 printf("none");
425 return;
426 }
427
428 switch (item->ic) {
429 case ic_address:
430 printf("address:");
431 rdata_address_print(item->u.address);
432 break;
433 case ic_value:
434 printf("value:");
435 rdata_value_print(item->u.value);
436 break;
437 }
438}
439
440static void rdata_address_print(rdata_address_t *address)
441{
442 switch (address->ac) {
443 case ac_var:
444 rdata_var_print(address->u.var_a->vref);
445 break;
446 case ac_prop:
447 printf("Warning: Unimplemented: Print property address.\n");
448 break;
449 }
450}
451
452static void rdata_value_print(rdata_value_t *value)
453{
454 rdata_var_print(value->var);
455}
456
457static void rdata_var_print(rdata_var_t *var)
458{
459 switch (var->vc) {
460 case vc_int:
461 printf("int(%d)", var->u.int_v->value);
462 break;
463 case vc_string:
464 printf("string(\"%s\")", var->u.string_v->value);
465 break;
466 case vc_ref:
467 printf("ref");
468 break;
469 case vc_deleg:
470 printf("deleg");
471 break;
472 case vc_object:
473 printf("object");
474 break;
475 default:
476 printf("print(%d)\n", var->vc);
477 assert(b_false);
478 }
479}
Note: See TracBrowser for help on using the repository browser.