1 tplarson 1.1
2 #include <stdlib.h>
3 #include <string.h>
4 #include "cmdparams.h"
5 #define CPSAVE_SUCCESS (0)
6 #define CPSAVE_UNKNOWN_PARAM (1)
7 #define CPSAVE_INVALID_CONVERSION (2)
8 #define CPSAVE_OUTOFMEMORY (4)
9 #define CPSAVE_UNKNOWN_ERROR (8)
10
11 #define SAVESTRUNIT 256
12 #define PARMSEPRTR "\n"
13 char *savestr=NULL;
14 int savestrmax=0;
15 int savestrlen=0;
16
17 float cmdparams_save_float (CmdParams_t *parms, char *name, int *status);
18 double cmdparams_save_double (CmdParams_t *parms, char *name, int *status);
19 int cmdparams_save_int (CmdParams_t *parms, char *name, int *status);
20 char * cmdparams_save_str (CmdParams_t *parms, char *name, int *status);
21 double cmdparams_save_time (CmdParams_t *parms, char *name, int *status);
22 tplarson 1.1 int cmdparams_save_flag (CmdParams_t *parms, char *name, int *status);
23 char * cmdparams_save_arg (CmdParams_t *parms, int num, int *status);
24
25 void cpsave_decode_error(int status) {
26 if (status == 0) return;
27 if (status & CPSAVE_UNKNOWN_PARAM) fprintf(stderr, "CPSAVE: unknown parameter.\n");
28 if (status & CPSAVE_INVALID_CONVERSION) fprintf(stderr, "CPSAVE: invalid conversion.\n");
29 if (status & CPSAVE_OUTOFMEMORY) fprintf(stderr, "CPSAVE: out of memory.\n");
30 if (status & CPSAVE_UNKNOWN_ERROR) fprintf(stderr, "CPSAVE: unknown error.\n");
31 }
32
33
34 float cmdparams_save_float (CmdParams_t *parms, char *name, int *status) {
35
36 float retval;
37 char *strval, *buf;
38 int nadd, stat;
39 int newstat=0;
40
41 retval=cmdparams_get_float (parms, name, &stat);
42 switch (stat) {
43 tplarson 1.1 case CMDPARAMS_SUCCESS:
44 newstat=CPSAVE_SUCCESS;
45 break;
46 case CMDPARAMS_UNKNOWN_PARAM:
47 newstat=CPSAVE_UNKNOWN_PARAM;
48 break;
49 case CMDPARAMS_INVALID_CONVERSION:
50 newstat=CPSAVE_INVALID_CONVERSION;
51 break;
52 case CMDPARAMS_OUTOFMEMORY:
53 newstat=CPSAVE_OUTOFMEMORY;
54 break;
55 default:
56 newstat=CPSAVE_UNKNOWN_ERROR;
57 }
58
59 if (stat == CMDPARAMS_UNKNOWN_PARAM) { // can't find the parameter so give up
60 *status = *status | newstat;
61 return retval;
62 }
63
64 tplarson 1.1 if (savestrmax == 0) {
65 savestr=calloc(SAVESTRUNIT,sizeof(char));
66 if (savestr == NULL) {
67 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
68 return retval;
69 }
70 savestrmax=SAVESTRUNIT;
71 }
72
73 strval=cmdparams_get_str (parms, name, NULL); // already know the status is success
74 nadd=strlen(strval)+strlen(name)+strlen(PARMSEPRTR)+2;
75 if (savestrlen+nadd > savestrmax) {
76 savestrmax += (nadd > SAVESTRUNIT) ? nadd : SAVESTRUNIT;
77 buf=malloc(savestrmax*sizeof(char));
78 if (buf == NULL) {
79 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
80 return retval;
81 }
82 else {
83 strcpy(buf,savestr);
84 free(savestr);
85 tplarson 1.1 savestr=buf;
86 }
87 }
88
89 strcat(savestr,name);
90 strcat(savestr,"=");
91 strcat(savestr,strval);
92 strcat(savestr,PARMSEPRTR);
93 savestrlen+=nadd-1;
94
95 *status = *status | newstat;
96 return retval;
97 }
98
99
100 double cmdparams_save_double (CmdParams_t *parms, char *name, int *status) {
101
102 double retval;
103 char *strval, *buf;
104 int nadd, stat;
105 int newstat=0;
106 tplarson 1.1
107 retval=cmdparams_get_double (parms, name, &stat);
108 switch (stat) {
109 case CMDPARAMS_SUCCESS:
110 newstat=CPSAVE_SUCCESS;
111 break;
112 case CMDPARAMS_UNKNOWN_PARAM:
113 newstat=CPSAVE_UNKNOWN_PARAM;
114 break;
115 case CMDPARAMS_INVALID_CONVERSION:
116 newstat=CPSAVE_INVALID_CONVERSION;
117 break;
118 case CMDPARAMS_OUTOFMEMORY:
119 newstat=CPSAVE_OUTOFMEMORY;
120 break;
121 default:
122 newstat=CPSAVE_UNKNOWN_ERROR;
123 }
124
125 if (stat == CMDPARAMS_UNKNOWN_PARAM) {
126 *status = *status | newstat;
127 tplarson 1.1 return retval;
128 }
129
130 if (savestrmax == 0) {
131 savestr=calloc(SAVESTRUNIT,sizeof(char));
132 if (savestr == NULL) {
133 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
134 return retval;
135 }
136 savestrmax=SAVESTRUNIT;
137 }
138
139 strval=cmdparams_get_str (parms, name, NULL);
140 nadd=strlen(strval)+strlen(name)+strlen(PARMSEPRTR)+2;
141 if (savestrlen+nadd > savestrmax) {
142 savestrmax += (nadd > SAVESTRUNIT) ? nadd : SAVESTRUNIT;
143 buf=malloc(savestrmax*sizeof(char));
144 if (buf == NULL) {
145 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
146 return retval;
147 }
148 tplarson 1.1 else {
149 strcpy(buf,savestr);
150 free(savestr);
151 savestr=buf;
152 }
153 }
154
155 strcat(savestr,name);
156 strcat(savestr,"=");
157 strcat(savestr,strval);
158 strcat(savestr,PARMSEPRTR);
159 savestrlen+=nadd-1;
160
161 *status = *status | newstat;
162 return retval;
163 }
164
165
166 int cmdparams_save_int (CmdParams_t *parms, char *name, int *status) {
167
168 int retval;
169 tplarson 1.1 char *strval, *buf;
170 int nadd, stat;
171 int newstat=0;
172
173 retval=cmdparams_get_int (parms, name, &stat);
174 switch (stat) {
175 case CMDPARAMS_SUCCESS:
176 newstat=CPSAVE_SUCCESS;
177 break;
178 case CMDPARAMS_UNKNOWN_PARAM:
179 newstat=CPSAVE_UNKNOWN_PARAM;
180 break;
181 case CMDPARAMS_INVALID_CONVERSION:
182 newstat=CPSAVE_INVALID_CONVERSION;
183 break;
184 case CMDPARAMS_OUTOFMEMORY:
185 newstat=CPSAVE_OUTOFMEMORY;
186 break;
187 default:
188 newstat=CPSAVE_UNKNOWN_ERROR;
189 }
190 tplarson 1.1
191 if (stat == CMDPARAMS_UNKNOWN_PARAM) {
192 *status = *status | newstat;
193 return retval;
194 }
195
196 if (savestrmax == 0) {
197 savestr=calloc(SAVESTRUNIT,sizeof(char));
198 if (savestr == NULL) {
199 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
200 return retval;
201 }
202 savestrmax=SAVESTRUNIT;
203 }
204
205 strval=cmdparams_get_str (parms, name, NULL);
206 nadd=strlen(strval)+strlen(name)+strlen(PARMSEPRTR)+2;
207 if (savestrlen+nadd > savestrmax) {
208 savestrmax += (nadd > SAVESTRUNIT) ? nadd : SAVESTRUNIT;
209 buf=malloc(savestrmax*sizeof(char));
210 if (buf == NULL) {
211 tplarson 1.1 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
212 return retval;
213 }
214 else {
215 strcpy(buf,savestr);
216 free(savestr);
217 savestr=buf;
218 }
219 }
220
221 strcat(savestr,name);
222 strcat(savestr,"=");
223 strcat(savestr,strval);
224 strcat(savestr,PARMSEPRTR);
225 savestrlen+=nadd-1;
226
227 *status = *status | newstat;
228 return retval;
229 }
230
231
232 tplarson 1.1 double cmdparams_save_time (CmdParams_t *parms, char *name, int *status) {
233
234 double retval;
235 char *strval, *buf;
236 int nadd, stat;
237 int newstat=0;
238
239 retval=cmdparams_get_time (parms, name, &stat);
240 switch (stat) {
241 case CMDPARAMS_SUCCESS:
242 newstat=CPSAVE_SUCCESS;
243 break;
244 case CMDPARAMS_UNKNOWN_PARAM:
245 newstat=CPSAVE_UNKNOWN_PARAM;
246 break;
247 case CMDPARAMS_INVALID_CONVERSION:
248 newstat=CPSAVE_INVALID_CONVERSION;
249 break;
250 case CMDPARAMS_OUTOFMEMORY:
251 newstat=CPSAVE_OUTOFMEMORY;
252 break;
253 tplarson 1.1 default:
254 newstat=CPSAVE_UNKNOWN_ERROR;
255 }
256
257 if (stat == CMDPARAMS_UNKNOWN_PARAM) {
258 *status = *status | newstat;
259 return retval;
260 }
261
262 if (savestrmax == 0) {
263 savestr=calloc(SAVESTRUNIT,sizeof(char));
264 if (savestr == NULL) {
265 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
266 return retval;
267 }
268 savestrmax=SAVESTRUNIT;
269 }
270
271 strval=cmdparams_get_str (parms, name, NULL);
272 nadd=strlen(strval)+strlen(name)+strlen(PARMSEPRTR)+2;
273 if (savestrlen+nadd > savestrmax) {
274 tplarson 1.1 savestrmax += (nadd > SAVESTRUNIT) ? nadd : SAVESTRUNIT;
275 buf=malloc(savestrmax*sizeof(char));
276 if (buf == NULL) {
277 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
278 return retval;
279 }
280 else {
281 strcpy(buf,savestr);
282 free(savestr);
283 savestr=buf;
284 }
285 }
286
287 strcat(savestr,name);
288 strcat(savestr,"=");
289 strcat(savestr,strval);
290 strcat(savestr,PARMSEPRTR);
291 savestrlen+=nadd-1;
292
293 *status = *status | newstat;
294 return retval;
295 tplarson 1.1 }
296
297
298 int cmdparams_save_flag (CmdParams_t *parms, char *name, int *status) {
299
300 int retval;
301 char *strval, *buf;
302 int nadd, stat;
303 int newstat=0;
304
305 if (cmdparams_exists (parms, name)) {
306 retval = cmdparams_get_int (parms, name, &stat);
307 }
308 else {
309 stat=CMDPARAMS_SUCCESS;
310 retval=0;
311 }
312
313 switch (stat) {
314 case CMDPARAMS_SUCCESS:
315 newstat=CPSAVE_SUCCESS;
316 tplarson 1.1 break;
317 case CMDPARAMS_UNKNOWN_PARAM:
318 newstat=CPSAVE_UNKNOWN_PARAM;
319 break;
320 case CMDPARAMS_INVALID_CONVERSION:
321 newstat=CPSAVE_INVALID_CONVERSION;
322 break;
323 case CMDPARAMS_OUTOFMEMORY:
324 newstat=CPSAVE_OUTOFMEMORY;
325 break;
326 default:
327 newstat=CPSAVE_UNKNOWN_ERROR;
328 }
329
330 if (savestrmax == 0) {
331 savestr=calloc(SAVESTRUNIT,sizeof(char));
332 if (savestr == NULL) {
333 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
334 return retval;
335 }
336 savestrmax=SAVESTRUNIT;
337 tplarson 1.1 }
338
339 strval=cmdparams_get_str (parms, name, NULL);
340 nadd=strlen(strval)+strlen(name)+strlen(PARMSEPRTR)+2;
341 if (savestrlen+nadd > savestrmax) {
342 savestrmax += (nadd > SAVESTRUNIT) ? nadd : SAVESTRUNIT;
343 buf=malloc(savestrmax*sizeof(char));
344 if (buf == NULL) {
345 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
346 return retval;
347 }
348 else {
349 strcpy(buf,savestr);
350 free(savestr);
351 savestr=buf;
352 }
353 }
354
355 strcat(savestr,name);
356 strcat(savestr,"=");
357 strcat(savestr,strval);
358 tplarson 1.1 strcat(savestr,PARMSEPRTR);
359 savestrlen+=nadd-1;
360
361 *status = *status | newstat;
362 return retval;
363 }
364
365
366 char * cmdparams_save_arg (CmdParams_t *parms, int num, int *status) {
367
368 char *strval, *istr, *buf;
369 int nadd, i;
370 static int savenum;
371
372 strval=cmdparams_getarg(parms,num);
373 if (strval == NULL || num <= savenum) return strval;
374
375 if (savestrmax == 0) {
376 savestr=calloc(SAVESTRUNIT,sizeof(char));
377 if (savestr == NULL) {
378 *status = *status | CPSAVE_OUTOFMEMORY;
379 tplarson 1.1 return strval;
380 }
381 savestrmax=SAVESTRUNIT;
382 }
383
384 for (i=savenum+1; i<=num; i++) {
385 istr=cmdparams_getarg(parms,i);
386 nadd=strlen(istr)+strlen(PARMSEPRTR)+1;
387 if (savestrlen+nadd > savestrmax) {
388 savestrmax += (nadd > SAVESTRUNIT) ? nadd : SAVESTRUNIT;
389 buf=malloc(savestrmax*sizeof(char));
390 if (buf == NULL) {
391 *status = *status | CPSAVE_OUTOFMEMORY;
392 break;
393 }
394 else {
395 strcpy(buf,savestr);
396 free(savestr);
397 savestr=buf;
398 }
399 }
400 tplarson 1.1
401 strcat(savestr,istr);
402 strcat(savestr,PARMSEPRTR);
403 savestrlen+=nadd-1;
404 }
405 savenum=num;
406 return strval;
407 }
408
409
410 char * cmdparams_save_str (CmdParams_t *parms, char *name, int *status) {
411
412 char *strval;
413 int nadd, stat;
414 int newstat=0;
415 char *buf;
416
417 strval=cmdparams_get_str (parms, name, &stat);
418 switch (stat) {
419 case CMDPARAMS_SUCCESS:
420 newstat=CPSAVE_SUCCESS;
421 tplarson 1.1 break;
422 case CMDPARAMS_UNKNOWN_PARAM:
423 newstat=CPSAVE_UNKNOWN_PARAM;
424 break;
425 case CMDPARAMS_INVALID_CONVERSION:
426 newstat=CPSAVE_INVALID_CONVERSION;
427 break;
428 case CMDPARAMS_OUTOFMEMORY:
429 newstat=CPSAVE_OUTOFMEMORY;
430 break;
431 default:
432 newstat=CPSAVE_UNKNOWN_ERROR;
433 }
434
435 if (stat == CMDPARAMS_UNKNOWN_PARAM) {
436 *status = *status | newstat;
437 return strval;
438 }
439
440 if (savestrmax == 0) {
441 savestr=calloc(SAVESTRUNIT,sizeof(char));
442 tplarson 1.1 if (savestr == NULL) {
443 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
444 return strval;
445 }
446 savestrmax=SAVESTRUNIT;
447 }
448
449 nadd=strlen(strval)+strlen(name)+strlen(PARMSEPRTR)+4;
450 if (savestrlen+nadd > savestrmax) {
451 savestrmax += (nadd > SAVESTRUNIT) ? nadd : SAVESTRUNIT;
452 buf=malloc(savestrmax*sizeof(char));
453 if (buf == NULL) {
454 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
455 return strval;
456 }
457 else {
458 strcpy(buf,savestr);
459 free(savestr);
460 savestr=buf;
461 }
462 }
463 tplarson 1.1
464 strcat(savestr,name);
465 strcat(savestr,"=\"");
466 strcat(savestr,strval);
467 strcat(savestr,"\"");
468 strcat(savestr,PARMSEPRTR);
469 savestrlen+=nadd-1;
470
471 *status = *status | newstat;
472 return strval;
473 }
|