00001 char *cvsinfo_saveparm = "cvsinfo: $Header: /home/cvsuser/cvsroot/JSOC/proj/mag/synop/apps/synop-saveparm.c,v 1.1 2016/05/05 19:49:28 arta Exp $";
00002
00003 #include <stdlib.h>
00004 #include <string.h>
00005 #include "cmdparams.h"
00006 #define CPSAVE_SUCCESS (0)
00007 #define CPSAVE_UNKNOWN_PARAM (1)
00008 #define CPSAVE_INVALID_CONVERSION (2)
00009 #define CPSAVE_OUTOFMEMORY (4)
00010 #define CPSAVE_UNKNOWN_ERROR (8)
00011
00012 #define SAVESTRUNIT 256
00013 #define PARMSEPRTR "\n"
00014 char *savestr=NULL;
00015 int savestrmax=0;
00016 int savestrlen=0;
00017
00018 float cmdparams_save_float (CmdParams_t *parms, char *name, int *status);
00019 double cmdparams_save_double (CmdParams_t *parms, char *name, int *status);
00020 int cmdparams_save_int (CmdParams_t *parms, char *name, int *status);
00021 const char * cmdparams_save_str (CmdParams_t *parms, char *name, int *status);
00022 double cmdparams_save_time (CmdParams_t *parms, char *name, int *status);
00023 int cmdparams_save_flag (CmdParams_t *parms, char *name, int *status);
00024 const char * cmdparams_save_arg (CmdParams_t *parms, int num, int *status);
00025
00026 void cpsave_decode_error(int status) {
00027 if (status == 0) return;
00028 if (status & CPSAVE_UNKNOWN_PARAM) fprintf(stderr, "CPSAVE: unknown parameter.\n");
00029 if (status & CPSAVE_INVALID_CONVERSION) fprintf(stderr, "CPSAVE: invalid conversion.\n");
00030 if (status & CPSAVE_OUTOFMEMORY) fprintf(stderr, "CPSAVE: out of memory.\n");
00031 if (status & CPSAVE_UNKNOWN_ERROR) fprintf(stderr, "CPSAVE: unknown error.\n");
00032 }
00033
00034
00035 float cmdparams_save_float (CmdParams_t *parms, char *name, int *status) {
00036
00037 float retval;
00038 char *buf;
00039 const char *strval;
00040 int nadd, stat;
00041 int newstat=0;
00042
00043 retval=cmdparams_get_float (parms, name, &stat);
00044 switch (stat) {
00045 case CMDPARAMS_SUCCESS:
00046 newstat=CPSAVE_SUCCESS;
00047 break;
00048 case CMDPARAMS_UNKNOWN_PARAM:
00049 newstat=CPSAVE_UNKNOWN_PARAM;
00050 break;
00051 case CMDPARAMS_INVALID_CONVERSION:
00052 newstat=CPSAVE_INVALID_CONVERSION;
00053 break;
00054 case CMDPARAMS_OUTOFMEMORY:
00055 newstat=CPSAVE_OUTOFMEMORY;
00056 break;
00057 default:
00058 newstat=CPSAVE_UNKNOWN_ERROR;
00059 }
00060
00061 if (stat == CMDPARAMS_UNKNOWN_PARAM) {
00062 *status = *status | newstat;
00063 return retval;
00064 }
00065
00066 if (savestrmax == 0) {
00067 savestr=calloc(SAVESTRUNIT,sizeof(char));
00068 if (savestr == NULL) {
00069 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
00070 return retval;
00071 }
00072 savestrmax=SAVESTRUNIT;
00073 }
00074
00075 strval=cmdparams_get_str (parms, name, NULL);
00076 nadd=strlen(strval)+strlen(name)+strlen(PARMSEPRTR)+2;
00077 if (savestrlen+nadd > savestrmax) {
00078 savestrmax += (nadd > SAVESTRUNIT) ? nadd : SAVESTRUNIT;
00079 buf=malloc(savestrmax*sizeof(char));
00080 if (buf == NULL) {
00081 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
00082 return retval;
00083 }
00084 else {
00085 strcpy(buf,savestr);
00086 free(savestr);
00087 savestr=buf;
00088 }
00089 }
00090
00091 strcat(savestr,name);
00092 strcat(savestr,"=");
00093 strcat(savestr,strval);
00094 strcat(savestr,PARMSEPRTR);
00095 savestrlen+=nadd-1;
00096
00097 *status = *status | newstat;
00098 return retval;
00099 }
00100
00101
00102 double cmdparams_save_double (CmdParams_t *parms, char *name, int *status) {
00103
00104 double retval;
00105 char *buf;
00106 const char *strval;
00107 int nadd, stat;
00108 int newstat=0;
00109
00110 retval=cmdparams_get_double (parms, name, &stat);
00111 switch (stat) {
00112 case CMDPARAMS_SUCCESS:
00113 newstat=CPSAVE_SUCCESS;
00114 break;
00115 case CMDPARAMS_UNKNOWN_PARAM:
00116 newstat=CPSAVE_UNKNOWN_PARAM;
00117 break;
00118 case CMDPARAMS_INVALID_CONVERSION:
00119 newstat=CPSAVE_INVALID_CONVERSION;
00120 break;
00121 case CMDPARAMS_OUTOFMEMORY:
00122 newstat=CPSAVE_OUTOFMEMORY;
00123 break;
00124 default:
00125 newstat=CPSAVE_UNKNOWN_ERROR;
00126 }
00127
00128 if (stat == CMDPARAMS_UNKNOWN_PARAM) {
00129 *status = *status | newstat;
00130 return retval;
00131 }
00132
00133 if (savestrmax == 0) {
00134 savestr=calloc(SAVESTRUNIT,sizeof(char));
00135 if (savestr == NULL) {
00136 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
00137 return retval;
00138 }
00139 savestrmax=SAVESTRUNIT;
00140 }
00141
00142 strval=cmdparams_get_str (parms, name, NULL);
00143 nadd=strlen(strval)+strlen(name)+strlen(PARMSEPRTR)+2;
00144 if (savestrlen+nadd > savestrmax) {
00145 savestrmax += (nadd > SAVESTRUNIT) ? nadd : SAVESTRUNIT;
00146 buf=malloc(savestrmax*sizeof(char));
00147 if (buf == NULL) {
00148 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
00149 return retval;
00150 }
00151 else {
00152 strcpy(buf,savestr);
00153 free(savestr);
00154 savestr=buf;
00155 }
00156 }
00157
00158 strcat(savestr,name);
00159 strcat(savestr,"=");
00160 strcat(savestr,strval);
00161 strcat(savestr,PARMSEPRTR);
00162 savestrlen+=nadd-1;
00163
00164 *status = *status | newstat;
00165 return retval;
00166 }
00167
00168
00169 int cmdparams_save_int (CmdParams_t *parms, char *name, int *status) {
00170
00171 int retval;
00172 char *buf;
00173 const char *strval;
00174 int nadd, stat;
00175 int newstat=0;
00176
00177 retval=cmdparams_get_int (parms, name, &stat);
00178 switch (stat) {
00179 case CMDPARAMS_SUCCESS:
00180 newstat=CPSAVE_SUCCESS;
00181 break;
00182 case CMDPARAMS_UNKNOWN_PARAM:
00183 newstat=CPSAVE_UNKNOWN_PARAM;
00184 break;
00185 case CMDPARAMS_INVALID_CONVERSION:
00186 newstat=CPSAVE_INVALID_CONVERSION;
00187 break;
00188 case CMDPARAMS_OUTOFMEMORY:
00189 newstat=CPSAVE_OUTOFMEMORY;
00190 break;
00191 default:
00192 newstat=CPSAVE_UNKNOWN_ERROR;
00193 }
00194
00195 if (stat == CMDPARAMS_UNKNOWN_PARAM) {
00196 *status = *status | newstat;
00197 return retval;
00198 }
00199
00200 if (savestrmax == 0) {
00201 savestr=calloc(SAVESTRUNIT,sizeof(char));
00202 if (savestr == NULL) {
00203 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
00204 return retval;
00205 }
00206 savestrmax=SAVESTRUNIT;
00207 }
00208
00209 strval=cmdparams_get_str (parms, name, NULL);
00210 nadd=strlen(strval)+strlen(name)+strlen(PARMSEPRTR)+2;
00211 if (savestrlen+nadd > savestrmax) {
00212 savestrmax += (nadd > SAVESTRUNIT) ? nadd : SAVESTRUNIT;
00213 buf=malloc(savestrmax*sizeof(char));
00214 if (buf == NULL) {
00215 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
00216 return retval;
00217 }
00218 else {
00219 strcpy(buf,savestr);
00220 free(savestr);
00221 savestr=buf;
00222 }
00223 }
00224
00225 strcat(savestr,name);
00226 strcat(savestr,"=");
00227 strcat(savestr,strval);
00228 strcat(savestr,PARMSEPRTR);
00229 savestrlen+=nadd-1;
00230
00231 *status = *status | newstat;
00232 return retval;
00233 }
00234
00235
00236 double cmdparams_save_time (CmdParams_t *parms, char *name, int *status) {
00237
00238 double retval;
00239 char *buf;
00240 const char *strval;
00241 int nadd, stat;
00242 int newstat=0;
00243
00244 retval=cmdparams_get_time (parms, name, &stat);
00245 switch (stat) {
00246 case CMDPARAMS_SUCCESS:
00247 newstat=CPSAVE_SUCCESS;
00248 break;
00249 case CMDPARAMS_UNKNOWN_PARAM:
00250 newstat=CPSAVE_UNKNOWN_PARAM;
00251 break;
00252 case CMDPARAMS_INVALID_CONVERSION:
00253 newstat=CPSAVE_INVALID_CONVERSION;
00254 break;
00255 case CMDPARAMS_OUTOFMEMORY:
00256 newstat=CPSAVE_OUTOFMEMORY;
00257 break;
00258 default:
00259 newstat=CPSAVE_UNKNOWN_ERROR;
00260 }
00261
00262 if (stat == CMDPARAMS_UNKNOWN_PARAM) {
00263 *status = *status | newstat;
00264 return retval;
00265 }
00266
00267 if (savestrmax == 0) {
00268 savestr=calloc(SAVESTRUNIT,sizeof(char));
00269 if (savestr == NULL) {
00270 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
00271 return retval;
00272 }
00273 savestrmax=SAVESTRUNIT;
00274 }
00275
00276 strval=cmdparams_get_str (parms, name, NULL);
00277 nadd=strlen(strval)+strlen(name)+strlen(PARMSEPRTR)+2;
00278 if (savestrlen+nadd > savestrmax) {
00279 savestrmax += (nadd > SAVESTRUNIT) ? nadd : SAVESTRUNIT;
00280 buf=malloc(savestrmax*sizeof(char));
00281 if (buf == NULL) {
00282 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
00283 return retval;
00284 }
00285 else {
00286 strcpy(buf,savestr);
00287 free(savestr);
00288 savestr=buf;
00289 }
00290 }
00291
00292 strcat(savestr,name);
00293 strcat(savestr,"=");
00294 strcat(savestr,strval);
00295 strcat(savestr,PARMSEPRTR);
00296 savestrlen+=nadd-1;
00297
00298 *status = *status | newstat;
00299 return retval;
00300 }
00301
00302
00303 int cmdparams_save_flag (CmdParams_t *parms, char *name, int *status) {
00304
00305 int retval;
00306 char *buf;
00307 const char *strval;
00308 int nadd, stat;
00309 int newstat=0;
00310
00311 if (cmdparams_exists (parms, name)) {
00312 retval = cmdparams_get_int (parms, name, &stat);
00313 }
00314 else {
00315 stat=CMDPARAMS_SUCCESS;
00316 retval=0;
00317 }
00318
00319 switch (stat) {
00320 case CMDPARAMS_SUCCESS:
00321 newstat=CPSAVE_SUCCESS;
00322 break;
00323 case CMDPARAMS_UNKNOWN_PARAM:
00324 newstat=CPSAVE_UNKNOWN_PARAM;
00325 break;
00326 case CMDPARAMS_INVALID_CONVERSION:
00327 newstat=CPSAVE_INVALID_CONVERSION;
00328 break;
00329 case CMDPARAMS_OUTOFMEMORY:
00330 newstat=CPSAVE_OUTOFMEMORY;
00331 break;
00332 default:
00333 newstat=CPSAVE_UNKNOWN_ERROR;
00334 }
00335
00336 if (savestrmax == 0) {
00337 savestr=calloc(SAVESTRUNIT,sizeof(char));
00338 if (savestr == NULL) {
00339 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
00340 return retval;
00341 }
00342 savestrmax=SAVESTRUNIT;
00343 }
00344
00345 strval=cmdparams_get_str (parms, name, NULL);
00346 nadd=strlen(strval)+strlen(name)+strlen(PARMSEPRTR)+2;
00347 if (savestrlen+nadd > savestrmax) {
00348 savestrmax += (nadd > SAVESTRUNIT) ? nadd : SAVESTRUNIT;
00349 buf=malloc(savestrmax*sizeof(char));
00350 if (buf == NULL) {
00351 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
00352 return retval;
00353 }
00354 else {
00355 strcpy(buf,savestr);
00356 free(savestr);
00357 savestr=buf;
00358 }
00359 }
00360
00361 strcat(savestr,name);
00362 strcat(savestr,"=");
00363 strcat(savestr,strval);
00364 strcat(savestr,PARMSEPRTR);
00365 savestrlen+=nadd-1;
00366
00367 *status = *status | newstat;
00368 return retval;
00369 }
00370
00371
00372 const char *cmdparams_save_arg (CmdParams_t *parms, int num, int *status) {
00373
00374 char *buf;
00375 const char *strval, *istr;
00376 int nadd, i;
00377 static int savenum;
00378
00379 strval=cmdparams_getarg(parms,num);
00380 if (strval == NULL || num <= savenum) return strval;
00381
00382 if (savestrmax == 0) {
00383 savestr=calloc(SAVESTRUNIT,sizeof(char));
00384 if (savestr == NULL) {
00385 *status = *status | CPSAVE_OUTOFMEMORY;
00386 return strval;
00387 }
00388 savestrmax=SAVESTRUNIT;
00389 }
00390
00391 for (i=savenum+1; i<=num; i++) {
00392 istr=cmdparams_getarg(parms,i);
00393 nadd=strlen(istr)+strlen(PARMSEPRTR)+1;
00394 if (savestrlen+nadd > savestrmax) {
00395 savestrmax += (nadd > SAVESTRUNIT) ? nadd : SAVESTRUNIT;
00396 buf=malloc(savestrmax*sizeof(char));
00397 if (buf == NULL) {
00398 *status = *status | CPSAVE_OUTOFMEMORY;
00399 break;
00400 }
00401 else {
00402 strcpy(buf,savestr);
00403 free(savestr);
00404 savestr=buf;
00405 }
00406 }
00407
00408 strcat(savestr,istr);
00409 strcat(savestr,PARMSEPRTR);
00410 savestrlen+=nadd-1;
00411 }
00412 savenum=num;
00413 return strval;
00414 }
00415
00416
00417 const char *cmdparams_save_str (CmdParams_t *parms, char *name, int *status) {
00418
00419 const char *strval;
00420 int nadd, stat;
00421 int newstat=0;
00422 char *buf;
00423
00424 strval=cmdparams_get_str (parms, name, &stat);
00425 switch (stat) {
00426 case CMDPARAMS_SUCCESS:
00427 newstat=CPSAVE_SUCCESS;
00428 break;
00429 case CMDPARAMS_UNKNOWN_PARAM:
00430 newstat=CPSAVE_UNKNOWN_PARAM;
00431 break;
00432 case CMDPARAMS_INVALID_CONVERSION:
00433 newstat=CPSAVE_INVALID_CONVERSION;
00434 break;
00435 case CMDPARAMS_OUTOFMEMORY:
00436 newstat=CPSAVE_OUTOFMEMORY;
00437 break;
00438 default:
00439 newstat=CPSAVE_UNKNOWN_ERROR;
00440 }
00441
00442 if (stat == CMDPARAMS_UNKNOWN_PARAM) {
00443 *status = *status | newstat;
00444 return strval;
00445 }
00446
00447 if (savestrmax == 0) {
00448 savestr=calloc(SAVESTRUNIT,sizeof(char));
00449 if (savestr == NULL) {
00450 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
00451 return strval;
00452 }
00453 savestrmax=SAVESTRUNIT;
00454 }
00455
00456 nadd=strlen(strval)+strlen(name)+strlen(PARMSEPRTR)+4;
00457 if (savestrlen+nadd > savestrmax) {
00458 savestrmax += (nadd > SAVESTRUNIT) ? nadd : SAVESTRUNIT;
00459 buf=malloc(savestrmax*sizeof(char));
00460 if (buf == NULL) {
00461 *status = *status | newstat | CPSAVE_OUTOFMEMORY;
00462 return strval;
00463 }
00464 else {
00465 strcpy(buf,savestr);
00466 free(savestr);
00467 savestr=buf;
00468 }
00469 }
00470
00471 strcat(savestr,name);
00472 strcat(savestr,"=\"");
00473 strcat(savestr,strval);
00474 strcat(savestr,"\"");
00475 strcat(savestr,PARMSEPRTR);
00476 savestrlen+=nadd-1;
00477
00478 *status = *status | newstat;
00479 return strval;
00480 }