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