(file) Return to saveparm.c CVS log (file) (dir) Up to [Development] / JSOC / proj / globalhs / apps

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

Karen Tian
Powered by
ViewCVS 0.9.4