/[gxemul]/upstream/0.4.0.1/src/settings.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /upstream/0.4.0.1/src/settings.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 27 - (show annotations)
Mon Oct 8 16:20:18 2007 UTC (16 years, 7 months ago) by dpavlin
File MIME type: text/plain
File size: 7105 byte(s)
0.4.0.1
1 /*
2 * Copyright (C) 2006 Anders Gavare. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 *
28 * $Id: settings.c,v 1.5 2006/05/16 03:15:14 debug Exp $
29 *
30 * A generic settings object. (This module should be 100% indepedent of GXemul
31 * and hence easily reusable.) It is basically a tree structure of nodes,
32 * where each node has a name and a few properties. The main property is
33 * a pointer, which can either point to other settings ("sub-settings"),
34 * or to a variable in memory.
35 *
36 * Appart from the pointer, the other properties are a definition of the
37 * type being pointed to (int, int32_t, int64_t, etc), how it should be
38 * presented (e.g. it may be an int value in memory, but it should be
39 * presented as a boolean "true/false" value), and a flag which tells us
40 * whether the setting is directly writable or not.
41 */
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46
47 /* Including misc.h should ONLY be necessary to work around the fact that
48 many systems don't have PRIx64 etc defined. */
49 #include "misc.h"
50
51 #include "settings.h"
52
53
54 struct settings {
55 int n_settings;
56
57 /*
58 * Each setting has a name, a writable flag, a storage type, a
59 * presentation format, and a pointer.
60 *
61 * For subsettings, the pointer points to the subsettings object;
62 * for other settings, the pointer points to a variable.
63 *
64 * These pointers point to simple linear arrays, containing n_settings
65 * entries each.
66 */
67
68 char **name;
69 int *writable;
70 int *storage_type;
71 int *presentation_format;
72 void **ptr;
73 };
74
75
76 /*
77 * settings_new():
78 *
79 * Create a new settings object. Return value is a pointer to the newly
80 * created object. The function does not return on failure.
81 */
82 struct settings *settings_new(void)
83 {
84 struct settings *settings = malloc(sizeof(struct settings));
85
86 if (settings == NULL) {
87 fprintf(stderr, "settings_new(): out of memory\n");
88 exit(1);
89 }
90
91 /* No settings. */
92 memset(settings, 0, sizeof(struct settings));
93
94 return settings;
95 }
96
97
98 /*
99 * settings_destroy():
100 *
101 * Frees all resources occupied by a settings object.
102 */
103 void settings_destroy(struct settings *settings)
104 {
105 int i;
106
107 if (settings == NULL) {
108 fprintf(stderr, "settings_destroy(): internal error, "
109 "settings = NULL!\n");
110 exit(1);
111 }
112
113 if (settings->name != NULL) {
114 for (i=0; i<settings->n_settings; i++) {
115 if (settings->name[i] != NULL)
116 free(settings->name[i]);
117 }
118
119 free(settings->name);
120 }
121
122 if (settings->writable != NULL)
123 free(settings->writable);
124
125 if (settings->storage_type != NULL)
126 free(settings->storage_type);
127
128 if (settings->presentation_format != NULL)
129 free(settings->presentation_format);
130
131 if (settings->ptr != NULL)
132 free(settings->ptr);
133
134 free(settings);
135 }
136
137
138 /*
139 * settings_debugdump():
140 *
141 * Dump settings in a settings object to stdout.
142 * If recurse is non-zero, all subsetting objects are also dumped.
143 */
144 void settings_debugdump(struct settings *settings, const char *prefix,
145 int recurse)
146 {
147 size_t name_buflen = strlen(prefix) + 100;
148 char *name = malloc(name_buflen);
149 int i;
150 uint64_t value;
151
152 for (i=0; i<settings->n_settings; i++) {
153 snprintf(name, name_buflen, "%s.%s", prefix, settings->name[i]);
154
155 if (settings->storage_type[i] == SETTINGS_TYPE_SUBSETTINGS) {
156 /* Subsettings: */
157 if (recurse)
158 settings_debugdump(settings->ptr[i], name, 1);
159 } else {
160 /* Normal value: */
161 printf("%s = ", name);
162
163 switch (settings->storage_type[i]) {
164 case SETTINGS_TYPE_INT:
165 value = *((int *) settings->ptr[i]);
166 break;
167 case SETTINGS_TYPE_INT32:
168 value = *((int32_t *) settings->ptr[i]);
169 break;
170 case SETTINGS_TYPE_INT64:
171 value = *((int64_t *) settings->ptr[i]);
172 break;
173 default:printf("FATAL ERROR! Unknown storage type"
174 ": %i\n", settings->storage_type[i]);
175 exit(1);
176 }
177
178 switch (settings->presentation_format[i]) {
179 case SETTINGS_FORMAT_DECIMAL:
180 printf("%"PRIi64, value);
181 break;
182 case SETTINGS_FORMAT_HEX:
183 printf("0x%"PRIx64, value);
184 break;
185 case SETTINGS_FORMAT_BOOL:
186 printf(value? "true" : "false");
187 break;
188 case SETTINGS_FORMAT_YESNO:
189 printf(value? "yes" : "no");
190 break;
191 default:printf("FATAL ERROR! Unknown presentation "
192 "format: %i\n",
193 settings->presentation_format[i]);
194 exit(1);
195 }
196
197 if (!settings->writable[i])
198 printf(" (Read-only)");
199
200 printf("\n");
201 }
202 }
203
204 free(name);
205 }
206
207
208 /*
209 * settings_add():
210 *
211 * Add a setting to a settings object.
212 */
213 void settings_add(struct settings *settings, const char *name, int writable,
214 int type, int format, void *ptr)
215 {
216 settings->n_settings ++;
217
218 if ((settings->name = realloc(settings->name, settings->n_settings
219 * sizeof(char *))) == NULL)
220 goto out_of_mem;
221 if ((settings->writable = realloc(settings->writable,
222 settings->n_settings * sizeof(int))) == NULL)
223 goto out_of_mem;
224 if ((settings->storage_type = realloc(settings->storage_type,
225 settings->n_settings * sizeof(int))) == NULL)
226 goto out_of_mem;
227 if ((settings->presentation_format = realloc(settings->
228 presentation_format, settings->n_settings * sizeof(int))) == NULL)
229 goto out_of_mem;
230 if ((settings->ptr = realloc(settings->ptr, settings->n_settings
231 * sizeof(void *))) == NULL)
232 goto out_of_mem;
233
234 settings->name[settings->n_settings - 1] = strdup(name);
235 settings->writable[settings->n_settings - 1] = writable;
236 settings->storage_type[settings->n_settings - 1] = type;
237 settings->presentation_format[settings->n_settings - 1] = format;
238 settings->ptr[settings->n_settings - 1] = ptr;
239
240 return;
241
242 out_of_mem:
243 fprintf(stderr, "settings_add(): fatal error: out of memory\n");
244 exit(1);
245 }
246
247

  ViewVC Help
Powered by ViewVC 1.1.26