KTH framework for Nek5000 toolboxes; testing version  0.0.1
finiparser.c
Go to the documentation of this file.
1 #include <ctype.h>
2 #include "iniparser.h"
3 
4 #ifndef FNAME_H
5 #define FNAME_H
6 
7 /*
8  FORTRAN naming convention
9  default cpgs_setup, etc.
10  -DUPCASE CPGS_SETUP, etc.
11  -DUNDERSCORE cpgs_setup_, etc.
12 */
13 
14 #ifdef UPCASE
15 # define FORTRAN_NAME(low,up) up
16 #else
17 #ifdef UNDERSCORE
18 # define FORTRAN_NAME(low,up) low##_
19 #else
20 # define FORTRAN_NAME(low,up) low
21 #endif
22 #endif
23 
24 #endif
25 
26 #define finiparser_dump FORTRAN_NAME(finiparser_dump, FINIPARSER_DUMP)
27 #define finiparser_getPair FORTRAN_NAME(finiparser_getpair, FINIPARSER_GETPAIR)
28 #define finiparser_load FORTRAN_NAME(finiparser_load, FINIPARSER_LOAD)
29 #define finiparser_free FORTRAN_NAME(finiparser_free, FINIPARSER_FREE)
30 #define finiparser_getString FORTRAN_NAME(finiparser_getstring, FINIPARSER_GETSTRING)
31 #define finiparser_getDictEntries FORTRAN_NAME(finiparser_getdictentries, FINIPARSER_GETDICTENTRIES)
32 #define finiparser_getBool FORTRAN_NAME(finiparser_getbool, FINIPARSER_GETBOOL)
33 #define finiparser_find FORTRAN_NAME(finiparser_find, FINIPARSER_FIND)
34 #define finiparser_getDbl FORTRAN_NAME(finiparser_getdbl, FINIPARSER_GETDBL)
35 #define finiparser_getToken FORTRAN_NAME(finiparser_gettoken, FINIPARSER_GETTOKEN)
36 #define finiparser_findTokens FORTRAN_NAME(finiparser_findtokens, FINIPARSER_FINDTOKENS)
37 
38 #define ntokenmax 100
39 
40 static dictionary *dic=NULL;
41 static char *token[ntokenmax];
42 
43 
44 char *addchar0(char *str,int str_len)
45 {
46  int i, real_len;
47  char *newstr;
48 
49  /* strip trailing blanks in datarep */
50  if (str <= (char *) 0) {
51  return NULL;
52  }
53  for (i=str_len-1; i>=0; i--) if (str[i] != ' ') break;
54  if (i < 0) {
55  return NULL;
56  }
57  real_len = i + 1;
58 
59  newstr = (char *) malloc((real_len+1)*sizeof(char));
60  strncpy(newstr, str, real_len);
61  newstr[real_len] = '\0';
62  return newstr;
63 }
64 
65 void finiparser_dump()
66 {
67  if(dic != NULL) iniparser_dump(dic,stdout);
68  return;
69 }
70 
71 void finiparser_find(int* out,char *key,int* ifnd,int key_len)
72 {
73  int tmp;
74  *ifnd = 0;
75  char *key_;
76 
77  key_ = addchar0(key,key_len);
78  tmp = iniparser_find_entry(dic,key_);
79  if (tmp == 1) {
80  *out = tmp;
81  *ifnd = 1;
82  }
83  free(key_);
84  return;
85 }
86 
88 {
89  *n = dic->n;
90  return;
91 }
92 
93 
94 void finiparser_getPair(char *key,char *val,int *id,int *ifnd,int key_len, int val_len)
95 {
96  *ifnd = 0;
97  int i;
98  int real_key_len = 0;
99  int real_val_len = 0;
100 
101  if (*id > dic->n) return;
102  for (i=0; i<key_len; i++) key[i] = ' ';
103  for (i=0; i<val_len; i++) val[i] = ' ';
104 
105  real_key_len = strlen(dic->key[*id-1]);
106  if(dic->val[*id-1] != NULL) real_val_len = strlen(dic->val[*id-1]);
107 
108  if(real_key_len > key_len) return;
109  if(real_val_len > val_len) return;
110 
111  strncpy(key,dic->key[*id-1],real_key_len);
112  strncpy(val,dic->val[*id-1],real_val_len);
113 
114  *ifnd = 1;
115  return;
116 }
117 
118 void finiparser_load(char * fname,int* ierr,int fname_len)
119 {
120  *ierr = 0;
121  char *fname_;
122 
123  fname_ = addchar0(fname,fname_len);
124  dic = iniparser_load(fname_);
125  if (dic == NULL) *ierr = 1;
126  free(fname_);
127  return;
128 }
129 
130 void finiparser_free()
131 {
132  dictionary_del(dic);
133  return;
134 }
135 
136 void finiparser_getString(char *out,char *key,int *ifnd,int out_len,int key_len)
137 {
138  int i;
139  const char* str;
140  int real_out_len;
141  char *key_;
142 
143  *ifnd = 0;
144  for (i=0; i<out_len; i++) out[i] = ' ';
145 
146  key_ = addchar0(key,key_len);
147  str = iniparser_getstring(dic,key_,NULL);
148  if (str != NULL) {
149  real_out_len = strlen(str);
150  if(real_out_len <= out_len) {
151  strncpy(out,str,real_out_len);
152  *ifnd = 1;
153  }
154  }
155  free(key_);
156  return;
157 }
158 
159 void finiparser_getBool(int* out,char *key,int* ifnd,int key_len)
160 {
161  int tmp;
162  char *key_;
163 
164  *ifnd = 0;
165  key_ = addchar0(key,key_len);
166  tmp = iniparser_getboolean(dic,key_,-1);
167  if (tmp != -1) {
168  *out = tmp;
169  *ifnd = 1;
170  }
171  free(key_);
172  return;
173 }
174 
175 void finiparser_getDbl(double* out,char *key,int *ifnd,int key_len)
176 {
177  const char* str;
178  char *key_;
179 
180  *ifnd = 0;
181  key_ = addchar0(key,key_len);
182  str = iniparser_getstring(dic,key_,NULL);
183  if (str != NULL) {
184  *out = atof(str);
185  *ifnd = 1;
186  }
187  free(key_);
188  return;
189 }
190 
191 void finiparser_getToken(char *out,int *id,int out_len)
192 {
193  int real_out_len, i;
194 
195  for (i=0; i<out_len; i++) out[i] = ' ';
196  if(*id > ntokenmax) return;
197  real_out_len = strlen(token[*id-1]);
198  if(real_out_len <= out_len) strncpy(out,token[*id-1],real_out_len);
199  return;
200 }
201 
202 void finiparser_findTokens(char *key, char *delim, int *icounter,int key_len,int delim_len)
203 {
204  const char *str;
205  char *newstr;
206  char *d, *key_;
207  int i;
208 
209  *icounter = 0;
210 
211  d = addchar0(delim,delim_len);
212  key_ = addchar0(key,key_len);
213  str = iniparser_getstring(dic,key_,NULL);
214  free(key_);
215  if (str == NULL) return;
216 
217  newstr = (char *) malloc((strlen(str)+1)*sizeof(char));
218  strncpy(newstr,str,strlen(str)+1);
219 
220  i = 0;
221  token[i] = strtok(newstr,d);
222  while (token[i] != NULL && i <= ntokenmax-1) {
223  strstrip(token[i]);
224  token[++i] = strtok(NULL,d);
225  }
226  *icounter = i;
227  free(d);
228 
229  return;
230 }
void dictionary_del(dictionary *d)
Delete a dictionary object.
Definition: dictionary.c:177
#define finiparser_getBool
Definition: finiparser.c:32
#define finiparser_find
Definition: finiparser.c:33
#define finiparser_load
Definition: finiparser.c:28
#define finiparser_dump
Definition: finiparser.c:26
#define finiparser_getDictEntries
Definition: finiparser.c:31
#define finiparser_getDbl
Definition: finiparser.c:34
char * addchar0(char *str, int str_len)
Definition: finiparser.c:44
#define finiparser_getString
Definition: finiparser.c:30
#define ntokenmax
Definition: finiparser.c:38
#define finiparser_getPair
Definition: finiparser.c:27
#define finiparser_getToken
Definition: finiparser.c:35
#define finiparser_free
Definition: finiparser.c:29
#define finiparser_findTokens
Definition: finiparser.c:36
void iniparser_dump(const dictionary *d, FILE *f)
Dump a dictionary to an opened file pointer.
Definition: iniparser.c:194
unsigned strstrip(char *s)
Remove blanks at the beginning and the end of a string.
Definition: iniparser.c:90
dictionary * iniparser_load(const char *ininame)
Parse an ini file and return an allocated dictionary object.
Definition: iniparser.c:642
int iniparser_find_entry(const dictionary *ini, const char *entry)
Finds out if a given entry exists in a dictionary.
Definition: iniparser.c:505
const char * iniparser_getstring(const dictionary *d, const char *key, const char *def)
Get the string associated to a key.
Definition: iniparser.c:372
int iniparser_getboolean(const dictionary *d, const char *key, int notfound)
Get the string associated to a key, convert to a boolean.
Definition: iniparser.c:476
Parser for ini files.
Dictionary object.
Definition: dictionary.h:45
char ** key
Definition: dictionary.h:49
char ** val
Definition: dictionary.h:48