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