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

File: [Development] / JSOC / proj / globalhs / apps / saveparm.c (download)
Revision: 1.3, Sun Apr 28 06:58:43 2013 UTC (5 years, 5 months ago) by tplarson
Branch: MAIN
CVS Tags: globalhs_version_9, globalhs_version_8, globalhs_version_7, globalhs_version_6, globalhs_version_5, globalhs_version_4, globalhs_version_3, globalhs_version_2, globalhs_version_19, globalhs_version_18, globalhs_version_17, globalhs_version_16, globalhs_version_15, globalhs_version_14, globalhs_version_13, globalhs_version_12, globalhs_version_11, globalhs_version_10, globalhs_version_1, globalhs_version_0, Ver_LATEST, Ver_9-2, Ver_9-1, Ver_9-0, Ver_8-8, Ver_8-7, Ver_8-6, Ver_8-5, Ver_8-4, Ver_8-3, Ver_8-2, Ver_8-12, Ver_8-11, Ver_8-10, Ver_8-1, HEAD
Changes since 1.2: +1 -0 lines
added string to track cvs versions

char *cvsinfo_saveparm = "cvsinfo: $Header: /home/cvsuser/cvsroot/JSOC/proj/globalhs/apps/saveparm.c,v 1.3 2013/04/28 07:58:43 tplarson Exp $";

#include <stdlib.h>
#include <string.h>
#include "cmdparams.h"
#define CPSAVE_SUCCESS			(0)
#define CPSAVE_UNKNOWN_PARAM		(1)
#define CPSAVE_INVALID_CONVERSION	(2)
#define CPSAVE_OUTOFMEMORY		(4)
#define CPSAVE_UNKNOWN_ERROR		(8)

#define SAVESTRUNIT 256
#define PARMSEPRTR "\n"
char *savestr=NULL;
int savestrmax=0;
int savestrlen=0;

float 	cmdparams_save_float (CmdParams_t *parms, char *name, int *status);
double 	cmdparams_save_double (CmdParams_t *parms, char *name, int *status);
int 	cmdparams_save_int (CmdParams_t *parms, char *name, int *status);
const char * cmdparams_save_str (CmdParams_t *parms, char *name, int *status);
double 	cmdparams_save_time (CmdParams_t *parms, char *name, int *status);
int	cmdparams_save_flag (CmdParams_t *parms, char *name, int *status);
const char * cmdparams_save_arg (CmdParams_t *parms, int num, int *status);

void cpsave_decode_error(int status) {
  if (status == 0) return;
  if (status & CPSAVE_UNKNOWN_PARAM) fprintf(stderr, "CPSAVE: unknown parameter.\n");
  if (status & CPSAVE_INVALID_CONVERSION) fprintf(stderr, "CPSAVE: invalid conversion.\n");
  if (status & CPSAVE_OUTOFMEMORY) fprintf(stderr, "CPSAVE: out of memory.\n");
  if (status & CPSAVE_UNKNOWN_ERROR) fprintf(stderr, "CPSAVE: unknown error.\n");
}


float cmdparams_save_float (CmdParams_t *parms, char *name, int *status) {

  float retval;
  char *buf;
  const char *strval;
  int nadd, stat;
  int newstat=0;

  retval=cmdparams_get_float (parms, name, &stat);
  switch (stat) {
  case CMDPARAMS_SUCCESS:
    newstat=CPSAVE_SUCCESS;
    break;
  case CMDPARAMS_UNKNOWN_PARAM:
    newstat=CPSAVE_UNKNOWN_PARAM;
    break;
  case CMDPARAMS_INVALID_CONVERSION:
    newstat=CPSAVE_INVALID_CONVERSION;
    break;
  case CMDPARAMS_OUTOFMEMORY:
    newstat=CPSAVE_OUTOFMEMORY;
    break;
  default:
    newstat=CPSAVE_UNKNOWN_ERROR;
  }

  if (stat == CMDPARAMS_UNKNOWN_PARAM) {  // can't find the parameter so give up
    *status = *status | newstat;
    return retval;
  }

  if (savestrmax == 0) {
    savestr=calloc(SAVESTRUNIT,sizeof(char));
    if (savestr == NULL) {
      *status = *status | newstat | CPSAVE_OUTOFMEMORY;
      return retval;
    }
    savestrmax=SAVESTRUNIT;
  }

  strval=cmdparams_get_str (parms, name, NULL);  // already know the status is success
  nadd=strlen(strval)+strlen(name)+strlen(PARMSEPRTR)+2; 
  if (savestrlen+nadd > savestrmax) {
    savestrmax += (nadd > SAVESTRUNIT) ? nadd : SAVESTRUNIT;
    buf=malloc(savestrmax*sizeof(char));
    if (buf == NULL) {
      *status = *status | newstat | CPSAVE_OUTOFMEMORY;
      return retval;
    }
    else {
      strcpy(buf,savestr);
      free(savestr);
      savestr=buf;
    }
  }

  strcat(savestr,name);
  strcat(savestr,"=");
  strcat(savestr,strval);
  strcat(savestr,PARMSEPRTR);
  savestrlen+=nadd-1;

  *status = *status | newstat;
  return retval;
}


double cmdparams_save_double (CmdParams_t *parms, char *name, int *status) {

  double retval;
  char *buf;
  const char *strval;
  int nadd, stat;
  int newstat=0;

  retval=cmdparams_get_double (parms, name, &stat);
  switch (stat) {
  case CMDPARAMS_SUCCESS:
    newstat=CPSAVE_SUCCESS;
    break;
  case CMDPARAMS_UNKNOWN_PARAM:
    newstat=CPSAVE_UNKNOWN_PARAM;
    break;
  case CMDPARAMS_INVALID_CONVERSION:
    newstat=CPSAVE_INVALID_CONVERSION;
    break;
  case CMDPARAMS_OUTOFMEMORY:
    newstat=CPSAVE_OUTOFMEMORY;
    break;
  default:
    newstat=CPSAVE_UNKNOWN_ERROR;
  }

  if (stat == CMDPARAMS_UNKNOWN_PARAM) {
    *status = *status | newstat;
    return retval;
  }

  if (savestrmax == 0) {
    savestr=calloc(SAVESTRUNIT,sizeof(char));
    if (savestr == NULL) {
      *status = *status | newstat | CPSAVE_OUTOFMEMORY;
      return retval;
    }
    savestrmax=SAVESTRUNIT;
  }

  strval=cmdparams_get_str (parms, name, NULL);
  nadd=strlen(strval)+strlen(name)+strlen(PARMSEPRTR)+2;
  if (savestrlen+nadd > savestrmax) {
    savestrmax += (nadd > SAVESTRUNIT) ? nadd : SAVESTRUNIT;
    buf=malloc(savestrmax*sizeof(char));
    if (buf == NULL) {
      *status = *status | newstat | CPSAVE_OUTOFMEMORY;
      return retval;
    }
    else {
      strcpy(buf,savestr);
      free(savestr);
      savestr=buf;
    }
  }

  strcat(savestr,name);
  strcat(savestr,"=");
  strcat(savestr,strval);
  strcat(savestr,PARMSEPRTR);
  savestrlen+=nadd-1;

  *status = *status | newstat;
  return retval;
}


int cmdparams_save_int (CmdParams_t *parms, char *name, int *status) {

  int retval;
  char *buf;
  const char *strval;
  int nadd, stat;
  int newstat=0;

  retval=cmdparams_get_int (parms, name, &stat);
  switch (stat) {
  case CMDPARAMS_SUCCESS:
    newstat=CPSAVE_SUCCESS;
    break;
  case CMDPARAMS_UNKNOWN_PARAM:
    newstat=CPSAVE_UNKNOWN_PARAM;
    break;
  case CMDPARAMS_INVALID_CONVERSION:
    newstat=CPSAVE_INVALID_CONVERSION;
    break;
  case CMDPARAMS_OUTOFMEMORY:
    newstat=CPSAVE_OUTOFMEMORY;
    break;
  default:
    newstat=CPSAVE_UNKNOWN_ERROR;
  }

  if (stat == CMDPARAMS_UNKNOWN_PARAM) {
    *status = *status | newstat;
    return retval;
  }

  if (savestrmax == 0) {
    savestr=calloc(SAVESTRUNIT,sizeof(char));
    if (savestr == NULL) {
      *status = *status | newstat | CPSAVE_OUTOFMEMORY;
      return retval;
    }
    savestrmax=SAVESTRUNIT;
  }

  strval=cmdparams_get_str (parms, name, NULL);
  nadd=strlen(strval)+strlen(name)+strlen(PARMSEPRTR)+2;
  if (savestrlen+nadd > savestrmax) {
    savestrmax += (nadd > SAVESTRUNIT) ? nadd : SAVESTRUNIT;
    buf=malloc(savestrmax*sizeof(char));
    if (buf == NULL) {
      *status = *status | newstat | CPSAVE_OUTOFMEMORY;
      return retval;
    }
    else {
      strcpy(buf,savestr);
      free(savestr);
      savestr=buf;
    }
  }

  strcat(savestr,name);
  strcat(savestr,"=");
  strcat(savestr,strval);
  strcat(savestr,PARMSEPRTR);
  savestrlen+=nadd-1;

  *status = *status | newstat;
  return retval;
}


double cmdparams_save_time (CmdParams_t *parms, char *name, int *status) {

  double retval;
  char *buf;
  const char *strval;
  int nadd, stat;
  int newstat=0;

  retval=cmdparams_get_time (parms, name, &stat);
  switch (stat) {
  case CMDPARAMS_SUCCESS:
    newstat=CPSAVE_SUCCESS;
    break;
  case CMDPARAMS_UNKNOWN_PARAM:
    newstat=CPSAVE_UNKNOWN_PARAM;
    break;
  case CMDPARAMS_INVALID_CONVERSION:
    newstat=CPSAVE_INVALID_CONVERSION;
    break;
  case CMDPARAMS_OUTOFMEMORY:
    newstat=CPSAVE_OUTOFMEMORY;
    break;
  default:
    newstat=CPSAVE_UNKNOWN_ERROR;
  }

  if (stat == CMDPARAMS_UNKNOWN_PARAM) {
    *status = *status | newstat;
    return retval;
  }

  if (savestrmax == 0) {
    savestr=calloc(SAVESTRUNIT,sizeof(char));
    if (savestr == NULL) {
      *status = *status | newstat | CPSAVE_OUTOFMEMORY;
      return retval;
    }
    savestrmax=SAVESTRUNIT;
  }

  strval=cmdparams_get_str (parms, name, NULL);
  nadd=strlen(strval)+strlen(name)+strlen(PARMSEPRTR)+2;
  if (savestrlen+nadd > savestrmax) {
    savestrmax += (nadd > SAVESTRUNIT) ? nadd : SAVESTRUNIT;
    buf=malloc(savestrmax*sizeof(char));
    if (buf == NULL) {
      *status = *status | newstat | CPSAVE_OUTOFMEMORY;
      return retval;
    }
    else {
      strcpy(buf,savestr);
      free(savestr);
      savestr=buf;
    }
  }

  strcat(savestr,name);
  strcat(savestr,"=");
  strcat(savestr,strval);
  strcat(savestr,PARMSEPRTR);
  savestrlen+=nadd-1;

  *status = *status | newstat;
  return retval;
}


int cmdparams_save_flag (CmdParams_t *parms, char *name, int *status) {

  int retval;
  char *buf;
  const char *strval;
  int nadd, stat;
  int newstat=0;

  if (cmdparams_exists (parms, name)) {
     retval = cmdparams_get_int (parms, name, &stat);
  } 
  else {
    stat=CMDPARAMS_SUCCESS;
    retval=0;
  }

  switch (stat) {
  case CMDPARAMS_SUCCESS:
    newstat=CPSAVE_SUCCESS;
    break;
  case CMDPARAMS_UNKNOWN_PARAM:
    newstat=CPSAVE_UNKNOWN_PARAM;
    break;
  case CMDPARAMS_INVALID_CONVERSION:
    newstat=CPSAVE_INVALID_CONVERSION;
    break;
  case CMDPARAMS_OUTOFMEMORY:
    newstat=CPSAVE_OUTOFMEMORY;
    break;
  default:
    newstat=CPSAVE_UNKNOWN_ERROR;
  }

  if (savestrmax == 0) {
    savestr=calloc(SAVESTRUNIT,sizeof(char));
    if (savestr == NULL) {
      *status = *status | newstat | CPSAVE_OUTOFMEMORY;
      return retval;
    }
    savestrmax=SAVESTRUNIT;
  }

  strval=cmdparams_get_str (parms, name, NULL);
  nadd=strlen(strval)+strlen(name)+strlen(PARMSEPRTR)+2;
  if (savestrlen+nadd > savestrmax) {
    savestrmax += (nadd > SAVESTRUNIT) ? nadd : SAVESTRUNIT;
    buf=malloc(savestrmax*sizeof(char));
    if (buf == NULL) {
      *status = *status | newstat | CPSAVE_OUTOFMEMORY;
      return retval;
    }
    else {
      strcpy(buf,savestr);
      free(savestr);
      savestr=buf;
    }
  }

  strcat(savestr,name);
  strcat(savestr,"=");
  strcat(savestr,strval);
  strcat(savestr,PARMSEPRTR);
  savestrlen+=nadd-1;

  *status = *status | newstat;
  return retval;
}


const char *cmdparams_save_arg (CmdParams_t *parms, int num, int *status) {

  char *buf;
  const char *strval, *istr;
  int nadd, i;
  static int savenum;

  strval=cmdparams_getarg(parms,num);
  if (strval == NULL || num <= savenum) return strval;

  if (savestrmax == 0) {
    savestr=calloc(SAVESTRUNIT,sizeof(char));
    if (savestr == NULL) {
      *status = *status | CPSAVE_OUTOFMEMORY;
      return strval;
    }
    savestrmax=SAVESTRUNIT;
  }

  for (i=savenum+1; i<=num; i++) {
    istr=cmdparams_getarg(parms,i);
    nadd=strlen(istr)+strlen(PARMSEPRTR)+1;
    if (savestrlen+nadd > savestrmax) {
      savestrmax += (nadd > SAVESTRUNIT) ? nadd : SAVESTRUNIT;
      buf=malloc(savestrmax*sizeof(char));
      if (buf == NULL) {
        *status = *status | CPSAVE_OUTOFMEMORY;
        break;
      }
      else {
        strcpy(buf,savestr);
        free(savestr);
        savestr=buf;
      }
    }

    strcat(savestr,istr);
    strcat(savestr,PARMSEPRTR);
    savestrlen+=nadd-1;
  }
  savenum=num;
  return strval;
}


const char *cmdparams_save_str (CmdParams_t *parms, char *name, int *status) {

  const char *strval;
  int nadd, stat;
  int newstat=0;
  char *buf;

  strval=cmdparams_get_str (parms, name, &stat);
  switch (stat) {
  case CMDPARAMS_SUCCESS:
    newstat=CPSAVE_SUCCESS;
    break;
  case CMDPARAMS_UNKNOWN_PARAM:
    newstat=CPSAVE_UNKNOWN_PARAM;
    break;
  case CMDPARAMS_INVALID_CONVERSION:
    newstat=CPSAVE_INVALID_CONVERSION;
    break;
  case CMDPARAMS_OUTOFMEMORY:
    newstat=CPSAVE_OUTOFMEMORY;
    break;
  default:
    newstat=CPSAVE_UNKNOWN_ERROR;
  }

  if (stat == CMDPARAMS_UNKNOWN_PARAM) {
    *status = *status | newstat;
    return strval;
  }

  if (savestrmax == 0) {
    savestr=calloc(SAVESTRUNIT,sizeof(char));
    if (savestr == NULL) {
      *status = *status | newstat | CPSAVE_OUTOFMEMORY;
      return strval;
    }
    savestrmax=SAVESTRUNIT;
  }

  nadd=strlen(strval)+strlen(name)+strlen(PARMSEPRTR)+4;
  if (savestrlen+nadd > savestrmax) {
    savestrmax += (nadd > SAVESTRUNIT) ? nadd : SAVESTRUNIT;
    buf=malloc(savestrmax*sizeof(char));
    if (buf == NULL) {
      *status = *status | newstat | CPSAVE_OUTOFMEMORY;
      return strval;
    }
    else {
      strcpy(buf,savestr);
      free(savestr);
      savestr=buf;
    }
  }

  strcat(savestr,name);
  strcat(savestr,"=\"");
  strcat(savestr,strval);
  strcat(savestr,"\"");
  strcat(savestr,PARMSEPRTR);
  savestrlen+=nadd-1;

  *status = *status | newstat;
  return strval;
}

Karen Tian
Powered by
ViewCVS 0.9.4