summaryrefslogtreecommitdiff
path: root/src/data.c
blob: a3a9eb9270bab1c0075483d2f99f3d9e3e2c1cce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "debug.h"
#include "e.h"
#include "data.h"
#include "file.h"
#include "util.h"

void
e_data_type_add_node(E_Data_Base_Type * base, char *prefix,
		     E_Data_Datatype type,
		     E_Data_Base_Type * list_type,
		     int offset, E_Data_Value def_val)
{
   E_Data_Node        *data_node;

   D_ENTER;

   data_node = NEW(E_Data_Node, 1);
   ZERO(data_node, E_Data_Node, 1);

   data_node->prefix = strdup(prefix);
   data_node->type = type;
   data_node->sub_type = list_type;
   data_node->offset = offset;
   switch (type)
     {
     case E_DATA_TYPE_INT:
	data_node->def_val.int_val = def_val.int_val;
	break;
     case E_DATA_TYPE_FLOAT:
	data_node->def_val.float_val = def_val.float_val;
	break;
     case E_DATA_TYPE_STR:
	if (data_node->def_val.str_val)
	  {
	     e_strdup(data_node->def_val.str_val, def_val.str_val);
	  }
	break;
     default:
	break;
     }
   base->nodes = evas_list_append(base->nodes, data_node);

   D_RETURN;
}

E_Data_Base_Type   *
e_data_type_new(void)
{
   E_Data_Base_Type   *t;

   D_ENTER;

   t = NEW(E_Data_Base_Type, 1);
   ZERO(t, E_Data_Base_Type, 1);

   D_RETURN_(t);
}

void               *
e_data_load(char *file, char *prefix, E_Data_Base_Type * type)
{
   E_DB_File          *db;
   char                buf[PATH_MAX];
   Evas_List          *l;
   char               *data;

   D_ENTER;

   if (!e_file_exists(file))
      D_RETURN_(NULL);
   db = e_db_open_read(file);

   if (!db)
      D_RETURN_(NULL);

   data = NEW(char, type->size);
   ZERO(data, char, type->size);

   for (l = type->nodes; l; l = l->next)
     {
	E_Data_Node        *node;

	node = l->data;
	switch (node->type)
	  {
	  case E_DATA_TYPE_INT:
	     {
		int                 val;

		val = 0;
		snprintf(buf, PATH_MAX, "%s/%s", prefix, node->prefix);
		if (e_db_int_get(db, buf, &val))
		   (*((int *)(&(data[node->offset])))) = val;
		else
		   (*((int *)(&(data[node->offset])))) = node->def_val.int_val;
	     }
	     break;
	  case E_DATA_TYPE_STR:
	     {
		char               *val;

		snprintf(buf, PATH_MAX, "%s/%s", prefix, node->prefix);
		if ((val = e_db_str_get(db, buf)))
		   (*((char **)(&(data[node->offset])))) = val;
		else
		   e_strdup((*((char **)(&(data[node->offset])))),
			    node->def_val.str_val);
	     }
	     break;
	  case E_DATA_TYPE_PTR:
	     {
		snprintf(buf, PATH_MAX, "%s/%s", prefix, node->prefix);
		(*((void **)(&(data[node->offset])))) = e_data_load(file, buf,
								    node->
								    sub_type);
	     }
	     break;
	  case E_DATA_TYPE_FLOAT:
	     {
		float               val;

		val = 0;
		snprintf(buf, PATH_MAX, "%s/%s", prefix, node->prefix);
		if (e_db_float_get(db, buf, &val))
		   (*((float *)(&(data[node->offset])))) = val;
		else
		   (*((float *)(&(data[node->offset])))) =
		      node->def_val.float_val;
	     }
	     break;
	  case E_DATA_TYPE_LIST:
	     {
		Evas_List          *l2;
		int                 i, count;

		l2 = NULL;
		snprintf(buf, PATH_MAX, "%s/%s/count", prefix, node->prefix);
		count = 0;
		e_db_int_get(db, buf, &count);
		for (i = 0; i < count; i++)
		  {
		     void               *data2;

		     snprintf(buf, PATH_MAX, "%s/%s/%i", prefix, node->prefix,
			      i);
		     data2 = e_data_load(file, buf, node->sub_type);
		     l2 = evas_list_append(l2, data2);
		  }
		(*((Evas_List **) (&(data[node->offset])))) = l2;
	     }
	     break;
	  case E_DATA_TYPE_KEY:
	     {
		snprintf(buf, PATH_MAX, "%s/%s", prefix, node->prefix);
		(*((char **)(&(data[node->offset])))) = strdup(buf);
	     }
	     break;
	  default:
	     break;
	  }
     }

   e_db_close(db);
   D_RETURN_(data);
}

void
e_data_free(E_Data_Base_Type * type, char *data)
{
   Evas_List          *l;

   D_ENTER;

   for (l = type->nodes; l; l = l->next)
     {
	E_Data_Node        *node;

	node = l->data;
	switch (node->type)
	  {
	  case E_DATA_TYPE_LIST:
	     {
		Evas_List          *l2;

		l2 = (*((Evas_List **) (&(data[node->offset]))));
		while (l2)
		  {
		     char               *data2;

		     data2 = l2->data;
		     l2 = evas_list_remove(l2, data2);
		     e_data_free(node->sub_type, (char *)data2);
		     FREE(data2);
		  }
	     }
	     break;
	  case E_DATA_TYPE_STR:
	  case E_DATA_TYPE_KEY:
	     {
		IF_FREE((*((char **)(&(data[node->offset])))));
	     }
	     break;
	  case E_DATA_TYPE_PTR:
	     {
		e_data_free(node->sub_type,
			    (*((void **)(&(data[node->offset])))));
		FREE((*((void **)(&(data[node->offset])))));
	     }
	     break;
	  case E_DATA_TYPE_INT:
	  case E_DATA_TYPE_FLOAT:
	     break;
	  default:
	     D("DATA WARNING: Data node %p corrupted!!!\n", node);
	     break;
	  }
     }

   D_RETURN;
}