Octopus
varinfo_low.c
Go to the documentation of this file.
1/*
2 Copyright (C) 2002 M. Marques, A. Castro, A. Rubio, G. Bertsch
3
4 This program 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 2, or (at your option)
7 any later version.
8
9 This program 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.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18
19*/
20
21#include <config.h>
22
23#define _GNU_SOURCE
24#include <ctype.h>
25#include <fortran_types.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30typedef struct opt_type {
31 char *name;
32 char *value;
33 char *desc;
34 struct opt_type *next;
35} opt_type;
36
37typedef struct var_type {
38 char *name;
39 char *type;
40 char *default_str; /* default is a reserved keyword */
41 char *section;
42 char *desc;
44 struct var_type *next;
45} var_type;
46
47static var_type *vars = NULL;
48
49/* --------------------------------------------------------- */
50char *get_token(char *s, char **dest) {
51 char *s1;
52 size_t len;
53 /* get rid of initial whitespace */
54 for (; *s != '\0' && isspace(*s); s++)
55 ;
56 if (!isalnum(*s) && *s != '-') {
57 *dest = NULL;
58 return s;
59 }
61 for (s1 = s; isalnum(*s1) || *s1 == '_' || *s1 == '-' || *s1 == '('; s1++)
62 ;
63 len = s1 - s;
65 *dest = (char *)strndup(s, len);
66
67 return s1;
69
70/* --------------------------------------------------------- */
71void get_text(FILE *in, char **dest) {
72 char c, line[256];
73 int b;
74
75 for (;;) {
76 /* check if the next line starts by a space */
77 if ((b = getc(in)) == EOF)
78 return;
79 c = (char)b;
80 ungetc(c, in);
82 if (!isspace(c))
83 return;
84
85 fgets(line, 256, in);
86 if (c == '\n') {
87 line[0] = ' ';
88 line[1] = '\n';
89 line[2] = '\0';
90 }
91
92 if (!*dest)
93 *dest = strdup(line + 1);
94 else {
95 *dest = realloc(*dest, strlen(*dest) + strlen(line + 1) + 1);
96 strcat(*dest, line + 1);
97 }
98 }
99}
100
101/* --------------------------------------------------------- */
102void varinfo_init(const char* fname_c) {
103 char line[256];
105 var_type *lvar = NULL;
106 opt_type *lopt;
108 in = fopen(fname_c, "r");
109 if (!in) {
110 return;
113 while (fgets(line, 256, in)) {
114
115 if (strncasecmp("Variable", line, 8) == 0) {
116 char *s;
118 get_token(line + 9, &s);
119 if (s) { /* found a token */
120 if (!lvar) {
121 lvar = (var_type *)malloc(sizeof(var_type));
122 vars = lvar;
123 } else {
124 lvar->next = (var_type *)malloc(sizeof(var_type));
125 lvar = lvar->next;
126 }
127 lvar->name = s;
128 lvar->desc = NULL;
129 lvar->type = NULL;
130 lvar->default_str = NULL;
131 lvar->section = NULL;
132 lvar->opt = NULL;
133 lvar->next = NULL;
135 lopt = NULL;
136 }
137 continue;
138 }
140 /* if no variable was found continue */
141 if (!lvar)
142 continue;
144 if (strncasecmp("Type", line, 4) == 0)
145 get_token(line + 5, &(lvar->type));
146
147 if (strncasecmp("Default", line, 7) == 0)
148 get_token(line + 8, &(lvar->default_str));
149
150 if (strncasecmp("Section", line, 7) == 0) {
151 char *s = line + 7;
152 for (; *s != '\0' && isspace(*s); s++)
154 lvar->section = strdup(s);
155 }
157 if (strncasecmp("Description", line, 11) == 0) {
158 if (lvar->desc) { /* if repeated delete old description */
159 free(lvar->desc);
160 lvar->desc = NULL;
161 }
162 get_text(in, &(lvar->desc));
164
165 if (strncasecmp("Option", line, 6) == 0) {
166 char *name, *value, *s;
167 s = get_token(line + 6, &name);
168 if (name)
169 get_token(s, &value);
170
171 if (name) { /* found an option */
172 if (!lopt) {
173 lopt = (opt_type *)malloc(sizeof(opt_type));
174 lvar->opt = lopt;
175 } else {
176 lopt->next = (opt_type *)malloc(sizeof(var_type));
177 lopt = lopt->next;
178 }
179 lopt->name = name;
180 lopt->value = value;
181 lopt->desc = NULL;
182 get_text(in, &(lopt->desc));
183 lopt->next = NULL;
184 }
185 }
186 }
187 fclose(in);
188}
189
190/* --------------------------------------------------------- */
191void FC_FUNC_(varinfo_end, VARINFO_END)() {
193 for (; v;) {
194 var_type *v1 = v->next;
195 opt_type *o = v->opt;
197 if (v->name)
198 free(v->name);
199 if (v->type)
200 free(v->type);
202 free(v->default_str);
203 if (v->section)
204 free(v->section);
205 if (v->desc)
206 free(v->desc);
207 for (; o;) {
208 opt_type *o1 = o->next;
209 if (o->name)
210 free(o->name);
211 if (o->value)
212 free(o->value);
213 if (o->desc)
214 free(o->desc);
215
216 free(o);
217 o = o1;
218 }
219
220 free(v);
221 v = v1;
222 }
223}
224
225/* --------------------------------------------------------- */
226void varinfo_getvar(const char * name_c, var_type **var) {
227 var_type *lvar;
228
229 for (lvar = vars; (lvar != NULL) && (strcasecmp(name_c, lvar->name) != 0);
230 lvar = lvar->next)
231 ;
232
233 *var = lvar;
234}
235
236/* --------------------------------------------------------- */
237void FC_FUNC_(varinfo_getinfo, VARINFO_GETINFO)(const var_type **var,
238 char **name, char **type,
239 char **default_str,
240 char **section, char **desc) {
241 if (var == NULL) {
242 *name = NULL;
243 *type = NULL;
244 *default_str = NULL;
245 *section = NULL;
246 *desc = NULL;
247 } else {
248 *name = (*var)->name;
249 *type = (*var)->type;
250 *default_str = (*var)->default_str;
251 *section = (*var)->section;
252 *desc = (*var)->desc;
253 }
254}
255
256/* --------------------------------------------------------- */
257void FC_FUNC_(varinfo_getopt, VARINFO_GETOPT)(const var_type **var,
258 opt_type **opt) {
259 if (*var == NULL)
260 *opt = NULL;
261 else if (*opt == NULL)
262 *opt = (*var)->opt;
263 else
264 *opt = (*opt)->next;
265}
266
267/* --------------------------------------------------------- */
268
269void FC_FUNC_(varinfo_opt_getinfo,
270 VARINFO_OPT_GETINFO)(const opt_type **opt, char **name,
271 fint8 *value, char **desc) {
272 if (opt == NULL) {
273 *name = NULL;
274 *desc = NULL;
275 *value = 0;
276 } else {
277 *name = (*opt)->name;
278 *desc = (*opt)->desc;
279 if ((*opt)->value) {
280 if (strncmp("bit", (*opt)->value, 3) == 0) {
281 *value = ((int64_t)1) << strtoll((*opt)->value + 4, NULL, 10);
282 } else {
283 *value = strtoll((*opt)->value, NULL, 10);
284 }
285
286 } else {
287 *value = 0;
288 }
289 }
290}
291
292/* ---------------------------------------------------------
293
294This function searches for a substring in the name of a variable. If
295var is set to NULL, it starts from the beginning of the list. If it is
296different from NULL, it assumes it is the result of a previous search and
297it starts searching from that point. It returns NULL if nothing is
298found.
299
300 --------------------------------------------------------- */
301
302/* used by liboct_parser/symbols.c */
303int varinfo_variable_exists(const char *var_name) {
304 var_type *lvar;
305 for (lvar = vars; (lvar != NULL) && (strcasecmp(var_name, lvar->name) != 0);
306 lvar = lvar->next)
307 ;
308 return (lvar != NULL);
309}
310
311void varinfo_search_var(const char * name_c, var_type **var) {
312 var_type *lvar;
313
314 if (*var == NULL)
315 lvar = vars;
316 else
317 lvar = (*var)->next;
318
319 for (; (lvar != NULL) && (strcasestr(lvar->name, name_c) == 0);
320 lvar = lvar->next)
321 ;
322
323 *var = lvar;
324}
325
326void varinfo_search_option(const var_type **var,
327 const char * name_c, int *value,
328 int *ierr) {
329 opt_type *opt;
330
331 opt = (*var)->opt;
332 *ierr = -1;
333
334 while (opt != NULL) {
335 if (strcmp(opt->name, name_c) == 0) {
336 *value = atoi(opt->value);
337 printf("%s|%s|\n", opt->name, name_c);
338 *ierr = 0;
339 break;
340 }
341 opt = opt->next;
342 }
343}
long long int fint8
Definition: fortran_types.h:16
__int64_t int64_t
Definition: io_binary.c:1328
integer, parameter in
Definition: pes_mask.F90:260
real(real64) function s()
char * default_str
Definition: varinfo_low.c:2753
char * type
Definition: varinfo_low.c:2752
char * section
Definition: varinfo_low.c:2754
char * desc
Definition: varinfo_low.c:2755
char * name
Definition: varinfo_low.c:2751
opt_type * opt
Definition: varinfo_low.c:2756
struct var_type * next
Definition: varinfo_low.c:2757
int varinfo_variable_exists(const char *var_name)
Definition: varinfo_low.c:3161
void varinfo_getvar(const char *name_c, var_type **var)
Definition: varinfo_low.c:3035
void varinfo_search_option(const var_type **var, const char *name_c, int *value, int *ierr)
Definition: varinfo_low.c:3200
char * get_token(char *s, char **dest)
Definition: varinfo_low.c:2767
void varinfo_init(const char *fname_c)
Definition: varinfo_low.c:2859
void FC_FUNC_(varinfo_end, VARINFO_END)()
Definition: varinfo_low.c:3000
void varinfo_search_var(const char *name_c, var_type **var)
Definition: varinfo_low.c:3177
int fclose(FILE *__stream)
struct var_type var_type
static var_type * vars
Definition: varinfo_low.c:2760
void get_text(FILE *in, char **dest)
Definition: varinfo_low.c:2816
opt_type
Definition: varinfo_low.c:2748