File: params.c

package info (click to toggle)
simage 1.7.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,908 kB
  • sloc: ansic: 14,231; sh: 12,587; cpp: 930; perl: 854; makefile: 617; lisp: 25
file content (317 lines) | stat: -rw-r--r-- 7,597 bytes parent folder | download | duplicates (2)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#include <simage.h>
#include <string.h>
#include <stdarg.h>

struct simage_param_data {
  int type;
  char * name;
  union {
    int integerdata;
    float floatdata;
    double doubledata;
    char * stringdata;
    void * pointerdata;
    void (*functiondata)();
  } data;
  struct simage_param_data * next;
};

typedef void s_generic_func();

struct simage_parameters_s {
  struct simage_param_data * list;
};

s_params * 
s_params_create(void)
{
  s_params * par = (s_params*) malloc(sizeof(s_params));
  par->list = NULL;
  return par;
}

void 
s_params_destroy(s_params * params)
{
  struct simage_param_data * next, * ptr = params->list;
  while (ptr) {
    next = ptr->next;
    if (ptr->type == S_STRING_PARAM_TYPE &&
        ptr->data.stringdata) free((void*)ptr->data.stringdata);
    free((void*)ptr);
    ptr = next;
  }
  free((void*)params);
}

static struct simage_param_data * 
find_param(s_params * params, const char * name, int type, int allocnew)
{
  struct simage_param_data * last, * ptr;
  last = NULL;
  ptr = params->list;
  while (ptr) {
    last = ptr;
    if (strcmp(ptr->name, name) == 0 && ptr->type == type) return ptr;
    ptr = ptr->next;
  }
  if (allocnew) {
    ptr = (struct simage_param_data*) malloc(sizeof(struct simage_param_data));
    ptr->name = (char *) malloc(strlen(name)+1);
    strcpy(ptr->name, name);
    ptr->next = NULL;
    ptr->type = type;
    if (last) last->next = ptr;
    else params->list = ptr;
  }
  return ptr;
}

s_params * 
s_params_copy(s_params * params)
{
  struct simage_param_data * data;
  struct simage_param_data * src;
  s_params * par;
  
  if (params == NULL) return NULL;
  
  par = (s_params*) malloc(sizeof(s_params));
  par->list = NULL;
  
  src = params->list;
  while (src) {
    data = find_param(par, src->name, src->type, 1);
    switch (src->type) {
    case S_INTEGER_PARAM_TYPE:
      data->data.integerdata = src->data.integerdata;
      break;
    case S_FLOAT_PARAM_TYPE:
      data->data.floatdata = src->data.floatdata;
      break;
    case S_DOUBLE_PARAM_TYPE:
      data->data.doubledata = src->data.doubledata;
      break;
    case S_STRING_PARAM_TYPE:
      data->data.stringdata = src->data.stringdata;
      if (data->data.stringdata != NULL) {
        data->data.stringdata = (char*) malloc(strlen(src->data.stringdata)+1);
        strcpy(data->data.stringdata, src->data.stringdata);
      }
      break;
    case S_POINTER_PARAM_TYPE:
      data->data.pointerdata = src->data.pointerdata;
      break;
    case S_FUNCTION_PARAM_TYPE:
      data->data.functiondata = src->data.functiondata;
      break;
    default:
      assert(0);
      break;
    }
    src = src->next;
  }
  return par;
}

static void 
add_integer_param(s_params * params, const char * name, int val)
{
  struct simage_param_data * data = find_param(params, name, S_INTEGER_PARAM_TYPE, 1);
  data->data.integerdata = val;
} 

static void 
add_float_param(s_params * params, const char * name, float val)
{
  struct simage_param_data * data = find_param(params, name, S_FLOAT_PARAM_TYPE, 1);
  data->data.floatdata = val;
} 

static void 
add_double_param(s_params * params, const char * name, double val)
{
  struct simage_param_data * data = find_param(params, name, S_DOUBLE_PARAM_TYPE, 1);
  data->data.doubledata = val;
} 

static void 
add_string_param(s_params * params, const char * name,  const char * val)
{
  struct simage_param_data * data = find_param(params, name, S_STRING_PARAM_TYPE, 1);
  data->data.stringdata = NULL;
  if (val) {
    data->data.stringdata = (char*) malloc(strlen(val)+1);
    strcpy(data->data.stringdata, val);
  }
} 

static void 
add_pointer_param(s_params * params, const char * name, void * val)
{
  struct simage_param_data * data = find_param(params, name, S_POINTER_PARAM_TYPE, 1);
  data->data.pointerdata = val;
} 

static void 
add_function_param(s_params * params, const char * name, void (*val)())
{
  struct simage_param_data * data = find_param(params, name, S_FUNCTION_PARAM_TYPE, 1);
  data->data.functiondata = val;
} 

void 
s_params_set(s_params * params, ...)
{
  int type;
  int argerr;
  const char * name;
  
  va_list ap;
  va_start(ap, params);
   
  name = va_arg(ap, const char*);
  while (name) {
    type = va_arg(ap, int);
    argerr = 0;
    switch (type) {

    case S_INTEGER_PARAM_TYPE:
      add_integer_param(params, name, va_arg(ap, int));
      break;

      /* FIXME: one should never use 'float' as a type for va_arg, so
         this type is bogus. see for instance

         http://c-faq.com/varargs/float.html

         -mortene
      */
    case S_FLOAT_PARAM_TYPE:
#if (__GNUC__ == 2 && __GNUC_MINOR__ == 96) || __GNUC__ >= 3
      /* fix for silly bug in gcc 2.96 */
      add_float_param(params, name, va_arg(ap, double));      
#else /* ! gcc version 2.96 */
      add_float_param(params, name, va_arg(ap, float));
#endif /* gcc version 2.96 */
      break;

    case S_DOUBLE_PARAM_TYPE:
      add_double_param(params, name, va_arg(ap, double));
      break;

    case S_STRING_PARAM_TYPE:
      add_string_param(params, name, va_arg(ap, const char*));
      break;

    case S_POINTER_PARAM_TYPE:
      add_pointer_param(params, name, va_arg(ap, void *));
      break;

    case S_FUNCTION_PARAM_TYPE:
      add_function_param(params, name, va_arg(ap, s_generic_func*));
      break;

    default:
      argerr = 1;
      break;
    }
    if (argerr) break; /* whoa, get out of here */

    /* process next token */
    name = va_arg(ap, const char*);
  }
  va_end(ap);
}

int 
s_params_get(s_params * params, ...)
{
  int numok;
  int type;
  int argerr;
  const char * name;
  struct simage_param_data * data;
  
  va_list ap;
  va_start(ap, params);
  
  numok = 0;

  name = va_arg(ap, const char*);
  while (name) {
    type = va_arg(ap, int);
    argerr = 0;
    switch (type) {
    case S_INTEGER_PARAM_TYPE:
      data = find_param(params, name, type, 0);
      if (data) {
        int * ptr = va_arg(ap, int*);
        *ptr = data->data.integerdata;
        numok++;
      }
      else argerr = 1;
      break;
    case S_FLOAT_PARAM_TYPE:
      data = find_param(params, name, type, 0);
      if (data) {
        float * ptr = va_arg(ap, float*);
        *ptr = data->data.floatdata;
        numok++;
      }
      else argerr = 1;
      break;
    case S_DOUBLE_PARAM_TYPE:
      data = find_param(params, name, type, 0);
      if (data) {
        double * ptr = va_arg(ap, double*);
        *ptr = data->data.doubledata;
        numok++;
      }
      else argerr = 1;
      break;
    case S_STRING_PARAM_TYPE:
      data = find_param(params, name, type, 0);
      if (data) {
        char ** ptr = va_arg(ap, char**);
        *ptr = data->data.stringdata;
        numok++;
      }
      else argerr = 1;
      break;
    case S_POINTER_PARAM_TYPE:
      data = find_param(params, name, type, 0);
      if (data) {
        void ** ptr = va_arg(ap, void**);
        *ptr = data->data.pointerdata;
        numok++;
      }
      else argerr = 1;
      break;
    case S_FUNCTION_PARAM_TYPE:
      data = find_param(params, name, type, 0);
      if (data) {
        s_generic_func ** ptr = va_arg(ap, s_generic_func**);
        *ptr = data->data.functiondata;
        numok++;
      }
      else argerr = 1;
      break;
    default:
      argerr = 1;
      break;
    }
    if (argerr) break;
    /* process next token */
    name = va_arg(ap, const char*);
  }
  va_end(ap);
  return numok;
}