Merge branch 'master' of github.com:periscop/clay
[clay.git] / source / data.c
blob025b2183f499fc590792176971d729a8c837bb2d
2 /*--------------------------------------------------------------------+
3 | Clay |
4 |--------------------------------------------------------------------|
5 | data.c |
6 |--------------------------------------------------------------------|
7 | First version: 03/04/2012 |
8 +--------------------------------------------------------------------+
10 +--------------------------------------------------------------------------+
11 | / __)( ) /__\ ( \/ ) |
12 | ( (__ )(__ /(__)\ \ / Chunky Loop Alteration wizardrY |
13 | \___)(____)(__)(__)(__) |
14 +--------------------------------------------------------------------------+
15 | Copyright (C) 2012 University of Paris-Sud |
16 | |
17 | This library is free software; you can redistribute it and/or modify it |
18 | under the terms of the GNU Lesser General Public License as published by |
19 | the Free Software Foundation; either version 2.1 of the License, or |
20 | (at your option) any later version. |
21 | |
22 | This library is distributed in the hope that it will be useful but |
23 | WITHOUT ANY WARRANTY; without even the implied warranty of |
24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser |
25 | General Public License for more details. |
26 | |
27 | You should have received a copy of the GNU Lesser General Public License |
28 | along with this software; if not, write to the Free Software Foundation, |
29 | Inc., 51 Franklin Street, Fifth Floor, |
30 | Boston, MA 02110-1301 USA |
31 | |
32 | Clay, the Chunky Loop Alteration wizardrY |
33 | Written by Joel Poudroux, joel.poudroux@u-psud.fr |
34 +--------------------------------------------------------------------------*/
37 #include <stdlib.h>
38 #include <string.h>
39 #include <clay/macros.h>
40 #include <clay/data.h>
41 #include <clay/array.h>
42 #include <clay/list.h>
44 clay_data_p clay_data_malloc(int type) {
45 clay_data_p tmp = malloc(sizeof(clay_data_t));
46 tmp->type = type;
47 return tmp;
50 void clay_data_free(clay_data_p d) {
51 if (!d)
52 return;
53 clay_data_clear(d);
54 free(d);
58 void clay_data_clear(clay_data_p d) {
59 switch (d->type) {
60 case INTEGER_T:
61 case VOID_T:
62 case REF_T:
63 case UNDEF_T:
64 break;
66 case ARRAY_T:
67 clay_array_free((clay_array_p) d->data.obj);
68 break;
70 case LIST_T:
71 clay_list_free((clay_list_p) d->data.obj);
72 break;
74 case STRING_T:
75 free(d->data.obj);
76 break;
81 void clay_data_print(FILE* f, clay_data_p d) {
82 if (d == NULL)
83 fprintf(f, "NULL\n");
85 switch (d->type) {
86 case UNDEF_T:
87 fprintf(f, "undef\n");
88 break;
90 case REF_T:
91 fprintf(f, "ref ");
92 clay_data_print(f, d->data.obj);
93 break;
95 case INTEGER_T:
96 fprintf(f, "int %d\n", d->data.integer);
97 break;
99 case VOID_T:
100 fprintf(f, "void %d\n", d->data.integer);
101 break;
103 case ARRAY_T:
104 fprintf(f, "array ");
105 clay_array_print(f, d->data.obj, 1);
106 break;
108 case LIST_T:
109 fprintf(f, "list ");
110 clay_list_print(f, d->data.obj);
111 break;
113 case STRING_T:
114 fprintf(f, "string %s\n", (char*) d->data.obj);
115 break;
120 clay_data_p clay_data_clone(clay_data_p d) {
121 if (d == NULL)
122 return NULL;
124 clay_data_p newd = clay_data_malloc(d->type);
126 switch (d->type) {
127 case UNDEF_T:
128 case VOID_T:
129 break;
131 case REF_T:
132 newd->data.obj = clay_data_clone(d->data.obj);
133 break;
135 case INTEGER_T:
136 newd->data.integer = d->data.integer;
137 break;
139 case ARRAY_T:
140 newd->data.obj = clay_array_clone(d->data.obj);
141 break;
143 case LIST_T:
144 newd->data.obj = clay_list_clone(d->data.obj);
145 break;
147 case STRING_T:
148 newd->data.obj = strdup((char*) d->data.obj);
149 break;
152 return newd;