polylib: param.c Source File

- Main Page
- Classes
- Files

- File List
- File Members

# param.c

Go to the documentation of this file.

    00001 /*
    00002     This file is part of PolyLib.
    00003
    00004     PolyLib is free software: you can redistribute it and/or modify
    00005     it under the terms of the GNU General Public License as published by
    00006     the Free Software Foundation, either version 3 of the License, or
    00007     (at your option) any later version.
    00008
    00009     PolyLib is distributed in the hope that it will be useful,
    00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00012     GNU General Public License for more details.
    00013
    00014     You should have received a copy of the GNU General Public License
    00015     along with PolyLib.  If not, see <http://www.gnu.org/licenses/>.
    00016 */
    00017
    00018 #include <stdio.h>
    00019 #include <stdlib.h>
    00020 #include <string.h>
    00021
    00022 #include <polylib/polylib.h>
    00023
    00024 /****************************************************/
    00025 /* Read_ParamNames() :                              */
    00026 /* Reads FILE *in for the parameter names           */
    00027 /* if in==NULL or not enough parameters on input,   */
    00028 /*  use default names                               */
    00029 /* returns an n-array of strings                    */
    00030 /****************************************************/
    00031 const char **Read_ParamNames(FILE *in,int m)
    00032 {
    00033   char **param_name;
    00034   int c, i, j, f;
    00035   char s[1024],param[32];
    00036
    00037   if(!in)
    00038     f = 0;
    00039   else
    00040     do
    00041       f = (fgets(s, 1024, in)!=NULL);
    00042     while (f && (*s=='#' || *s=='\n'));
    00043
    00044   param_name = (char **)malloc(m*sizeof(char *));
    00045   i = 0;
    00046   if(f) {
    00047     c = 0;
    00048     for(;i<m;++i) {
    00049       j=0;
    00050       for(;;++c) {
    00051         if(s[c]==' ') {
    00052           if(j==0)
    00053             continue;
    00054           else
    00055             break;
    00056         }
    00057         if(s[c]=='\n' || s[c]==0)
    00058           break;
    00059         param[j++] = s[c];
    00060       }
    00061
    00062       /* Not enough parameters on input, end */
    00063       if(j==0)
    00064         break;
    00065       param[j] = 0;
    00066       param_name[i] = (char *)malloc( (j+1)*sizeof(char) );
    00067       strcpy(param_name[i],param);
    00068     }
    00069   }
    00070
    00071   /* Not enough parameters on input : use default names */
    00072   if(!f || i!=m) {
    00073     for(;i<m;++i) {
    00074       param_name[i] = (char *) malloc(2*sizeof(char));
    00075       sprintf(param_name[i], "%c", PCHAR+i+1);
    00076     }
    00077   }
    00078   return (const char**)param_name;
    00079 } /* Read_ParamNames */
    00080
    00081 void Free_ParamNames(const char **params, int m)
    00082 {
    00083     while (--m >= 0)
    00084         free((char *)params[m]);
    00085     free(params);
    00086 }