Merge pull request #3 from skimo-openhub/skimo/pr/distclean
[polylib.git] / source / kernel / param.c
blob841e024dd7f13dae0425c77db610284bed099814
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include <polylib/polylib.h>
7 /****************************************************/
8 /* Read_ParamNames() : */
9 /* Reads FILE *in for the parameter names */
10 /* if in==NULL or not enough parameters on input, */
11 /* use default names */
12 /* returns an n-array of strings */
13 /****************************************************/
14 const char **Read_ParamNames(FILE *in,int m)
16 char **param_name;
17 int c, i, j, f;
18 char s[1024],param[32];
20 if(!in)
21 f = 0;
22 else
24 f = (fgets(s, 1024, in)!=NULL);
25 while (f && (*s=='#' || *s=='\n'));
27 param_name = (char **)malloc(m*sizeof(char *));
28 i = 0;
29 if(f) {
30 c = 0;
31 for(;i<m;++i) {
32 j=0;
33 for(;;++c) {
34 if(s[c]==' ') {
35 if(j==0)
36 continue;
37 else
38 break;
40 if(s[c]=='\n' || s[c]==0)
41 break;
42 param[j++] = s[c];
45 /* Not enough parameters on input, end */
46 if(j==0)
47 break;
48 param[j] = 0;
49 param_name[i] = (char *)malloc( (j+1)*sizeof(char) );
50 strcpy(param_name[i],param);
54 /* Not enough parameters on input : use default names */
55 if(!f || i!=m) {
56 for(;i<m;++i) {
57 param_name[i] = (char *) malloc(2*sizeof(char));
58 sprintf(param_name[i], "%c", PCHAR+i+1);
61 return (const char**)param_name;
62 } /* Read_ParamNames */
64 void Free_ParamNames(const char **params, int m)
66 while (--m >= 0)
67 free((char *)params[m]);
68 free(params);