#include #include #ifndef TRUE #define TRUE 1 #define FALSE 0 #endif double parseFile(char *fn, char *variableName, double defaultValue); int iparseFile(char *fn, char *variableName, int defaultValue); int lparseFile(char *fn, char *variableName, int defaultValue); char *sparseFile(char *fn, char *variableName); double parseFile(char *fn, char *variableName, double defaultValue) /*---------------------------------------------------------------------- * Takes a file with lines like name = value. If the given variable name * is found, assigns the found value, else the default value. * Aborts execution if the file is not found. * * Input parameters: * fn file name * variableName variable name left of = or : * defaultValue to be returned if no such entry in file * * Return value: * Value right of = or : if success, defaultValue if failure * * Terms of use: * You are not allowed to claim that this code is the work of anybody else * than Henning Weede. * The author is not liable for any damage caused by this code. * The author has no duty to waist his time answering questions related * to this code. * The GNU Public License applies. * * Author: Henning Weede * Year: 2009 *---------------------------------------------------------------------- */{ int i,l; FILE *fp; static char localLine[80]; l=strlen(variableName); if((fp=fopen(fn,"r"))==(FILE *)NULL) { fprintf(stderr,"Cannot read file %s.\n",fn); exit(1); } for(rewind(fp);;) { fgets(localLine,sizeof(localLine)-3,fp); if(feof(fp)) break; if(strncmp(localLine,variableName,l)==0) if((localLine[l]<=' ')||(localLine[l]=='=')||(localLine[l]==':')) for(i=l; localLine[i]; ++i) if((localLine[i]=='=')||(localLine[i]==':')) { fclose(fp); return(atof(&localLine[++i])); } } fclose(fp); return(defaultValue); } /* parseFile() */ int iparseFile(char *fn, char *variableName, int defaultValue) /*---------------------------------------------------------------------- * Takes a file with lines like name = value. If the given variable name * is found, assigns the found value, else the default value. * Aborts execution if the file is not found. * * Input parameters: * fn file name * variableName variable name left of = or : * defaultValue to be returned if no such entry in file * * Return value: * Value right of = or : if success, defaultValue if failure * * Terms of use: * You are not allowed to claim that this code is the work of anybody else * than Henning Weede. * The author is not liable for any damage caused by this code. * The author has no duty to waist his time answering questions related * to this code. * The GNU Public License applies. * * Author: Henning Weede * Year: 2009 *---------------------------------------------------------------------- */{ int i,l; FILE *fp; static char localLine[256]; l=strlen(variableName); if((fp=fopen(fn,"r"))==(FILE *)NULL) { fprintf(stderr,"Cannot read file %s.\n",fn); exit(1); } for(rewind(fp);;) { fgets(localLine,sizeof(localLine)-3,fp); if(feof(fp)) break; if(strncmp(localLine,variableName,l)==0) if((localLine[l]<=' ')||(localLine[l]=='=')||(localLine[l]==':')) for(i=l; localLine[i]; ++i) if((localLine[i]=='=')||(localLine[i]==':')) { fclose(fp); return(atoi(&localLine[++i])); } } fclose(fp); return(defaultValue); } /* iparseFile() */ int lparseFile(char *fn, char *variableName, int defaultValue) /*---------------------------------------------------------------------- * Takes a file with lines like name = value. If the given variable name * is found, check if the value is something like TRUE or FALSE * or yes or no etc. * Aborts execution if the file is not found. * * Input parameters: * fn file name * variableName variable name left of = or : * defaultValue TRUE or FALSE * * Return value: * TRUE if the value is TRUE or YES or yes or JA or Ja etc. * FALSE if the value is FALSE ir NO or no NEIN or nein etc. * defaultValue else * * Terms of use: * You are not allowed to claim that this code is the work of anybody else * than Henning Weede. * The author is not liable for any damage caused by this code. * The author has no duty to waist his time answering questions related * to this code. * The GNU Public License applies. * * Author: Henning Weede * Year: 2010 *---------------------------------------------------------------------- */{ int result; char *s; if((s = sparseFile(fn,variableName))==(char *)NULL) return(defaultValue); /* Pawel-Nabe-Gedenk-Code :-) */ switch(tolower(s[0])) { case 't' : /* true */ case 'y' : /* yes */ case 'j' : /* ja */ case 'o' : /* oui */ case 's' : /* si */ case 'd' : /* da */ result = TRUE; break; case 'f' : /* false */ case 'n' : /* no / nein / non / no / njet */ result = FALSE; break; default: result = defaultValue; } free(s); return(result); } /* lparseFile() */ char *sparseFile(char *fn, char *variableName) /*---------------------------------------------------------------------- * Takes a file with lines like name = value. If the given variable name * is found, assigns the found string, else NULL. * Aborts execution if the file is not found. * * Input parameters: * fn file name * variableName variable name left of = or : * * Return value: * String right of = or : if success,NULL if failure * * Terms of use: * You are not allowed to claim that this code is the work of anybody else * than Henning Weede. * The author is not liable for any damage caused by this code. * The author has no duty to waist his time answering questions related * to this code. * The GNU Public License applies. * * Author: Henning Weede * Year: 2009 *---------------------------------------------------------------------- */{ int i,j,l; char *result; FILE *fp; static char localLine[128]; l=strlen(variableName); if((fp=fopen(fn,"r"))==(FILE *)NULL) { fprintf(stderr,"Cannot read file %s.\n",fn); exit(1); } for(rewind(fp);;) { fgets(localLine,sizeof(localLine)-3,fp); if(feof(fp)) break; if(strncmp(localLine,variableName,l)==0) if((localLine[l]<=' ')||(localLine[l]=='=')||(localLine[l]==':')) for(i=l; localLine[i]; ++i) if((localLine[i]=='=')||(localLine[i]==':')) { for(++i; (localLine[i]<=' ')&&localLine[i]; ++i) ; for(j=strlen(localLine); (localLine[j]<' ')&&(j>0); --j) ; fclose(fp); if(j