corrected a bug in reading matrices on MacOS (fgets) and cleaned a bit the code
[polylib.git] / source / kernel / param.c
blobca54d74a1160b165d270ccf8bb8df077ddb98cbc
1 /*
2 This file is part of PolyLib.
4 PolyLib is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 PolyLib is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with PolyLib. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
22 #include <polylib/polylib.h>
24 /****************************************************/
25 /* Read_ParamNames() : */
26 /* Reads FILE *in for the parameter names */
27 /* if in==NULL or not enough parameters on input, */
28 /* use default names */
29 /* returns an n-array of strings */
30 /****************************************************/
31 const char **Read_ParamNames(FILE *in,int m)
33 char **param_name;
34 int c, i, j, f;
35 char s[1024],param[32];
37 if(!in)
38 f = 0;
39 else
41 f = (fgets(s, 1024, in)!=NULL);
42 while (f && (*s=='#' || *s=='\n'));
44 param_name = (char **)malloc(m*sizeof(char *));
45 i = 0;
46 if(f) {
47 c = 0;
48 for(;i<m;++i) {
49 j=0;
50 for(;;++c) {
51 if(s[c]==' ') {
52 if(j==0)
53 continue;
54 else
55 break;
57 if(s[c]=='\n' || s[c]==0)
58 break;
59 param[j++] = s[c];
62 /* Not enough parameters on input, end */
63 if(j==0)
64 break;
65 param[j] = 0;
66 param_name[i] = (char *)malloc( (j+1)*sizeof(char) );
67 strcpy(param_name[i],param);
71 /* Not enough parameters on input : use default names */
72 if(!f || i!=m) {
73 for(;i<m;++i) {
74 param_name[i] = (char *) malloc(2*sizeof(char));
75 sprintf(param_name[i], "%c", PCHAR+i+1);
78 return (const char**)param_name;
79 } /* Read_ParamNames */
81 void Free_ParamNames(const char **params, int m)
83 while (--m >= 0)
84 free((char *)params[m]);
85 free(params);