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 <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28
29typedef struct opt_type {
30 char *name;
31 char *value;
32 char *desc;
33 struct opt_type *next;
34} opt_type;
35
36typedef struct var_type {
37 char *name;
38 char *type;
39 char *default_str; /* default is a reserved keyword */
40 char *section;
41 char *desc;
43 struct var_type *next;
44} var_type;
45
46static var_type *vars = NULL;
47
48/* --------------------------------------------------------- */
49char *get_token(char *s, char **dest) {
50 char *s1;
51 size_t len;
52 /* get rid of initial whitespace */
53 for (; *s != '\0' && isspace(*s); s++)
54 ;
55 if (!isalnum(*s) && *s != '-') {
56 *dest = NULL;
57 return s;
58 }
59
60 for (s1 = s; isalnum(*s1) || *s1 == '_' || *s1 == '-' || *s1 == '('; s1++)
61 ;
62 len = s1 - s;
64 *dest = (char *)strndup(s, len);
66 return s1;
69/* --------------------------------------------------------- */
70void get_text(FILE *in, char **dest) {
71 char c, line[256];
72 int b;
73
74 for (;;) {
75 /* check if the next line starts by a space */
76 if ((b = getc(in)) == EOF)
77 return;
78 c = (char)b;
79 ungetc(c, in);
81 if (!isspace(c))
82 return;
83
84 fgets(line, 256, in);
85 if (c == '\n') {
86 line[0] = ' ';
87 line[1] = '\n';
88 line[2] = '\0';
89 }
90
91 if (!*dest)
92 *dest = strdup(line + 1);
93 else {
94 *dest = realloc(*dest, strlen(*dest) + strlen(line + 1) + 1);
95 strcat(*dest, line + 1);
96 }
97 }
98}
99
100/* --------------------------------------------------------- */
101void varinfo_init(const char* fname_c) {
102 char line[256];
103 FILE *in;
104 var_type *lvar = NULL;
105 opt_type *lopt;
107 in = fopen(fname_c, "r");
108 if (!in) {
109 return;
112 while (fgets(line, 256, in)) {
114 if (strncasecmp("Variable", line, 8) == 0) {
115 char *s;
117 get_token(line + 9, &s);
118 if (s) { /* found a token */
119 if (!lvar) {
120 lvar = (var_type *)malloc(sizeof(var_type));
121 vars = lvar;
122 } else {
123 lvar->next = (var_type *)malloc(sizeof(var_type));
124 lvar = lvar->next;
126 lvar->name = s;
127 lvar->desc = NULL;
128 lvar->type = NULL;
129 lvar->default_str = NULL;
130 lvar->section = NULL;
131 lvar->opt = NULL;
132 lvar->next = NULL;
133
134 lopt = NULL;
135 }
136 continue;
137 }
138
139 /* if no variable was found continue */
140 if (!lvar)
141 continue;
142
143 if (strncasecmp("Type", line, 4) == 0)
144 get_token(line + 5, &(lvar->type));
145
146 if (strncasecmp("Default", line, 7) == 0)
147 get_token(line + 8, &(lvar->default_str));
149 if (strncasecmp("Section", line, 7) == 0) {
150 char *s = line + 7;
151 for (; *s != '\0' && isspace(*s); s++)
152 ;
153 lvar->section = strdup(s);
154 }
155
156 if (strncasecmp("Description", line, 11) == 0) {
157 if (lvar->desc) { /* if repeated delete old description */
158 free(lvar->desc);
159 lvar->desc = NULL;
160 }
161 get_text(in, &(lvar->desc));
164 if (strncasecmp("Option", line, 6) == 0) {
165 char *name, *value, *s;
166 s = get_token(line + 6, &name);
167 if (name)
168 get_token(s, &value);
170 if (name) { /* found an option */
171 if (!lopt) {
172 lopt = (opt_type *)malloc(sizeof(opt_type));
173 lvar->opt = lopt;
174 } else {
175 lopt->next = (opt_type *)malloc(sizeof(var_type));
176 lopt = lopt->next;
177 }
178 lopt->name = name;
179 lopt->value = value;
180 lopt->desc = NULL;
181 get_text(in, &(lopt->desc));
182 lopt->next = NULL;
183 }
184 }
185 }
186 fclose(in);
187}
188
189/* --------------------------------------------------------- */
190void varinfo_end() {
191 var_type *v = vars;
192 for (; v;) {
193 var_type *v1 = v->next;
194 opt_type *o = v->opt;
196 if (v->name)
197 free(v->name);
198 if (v->type)
199 free(v->type);
201 free(v->default_str);
202 if (v->section)
203 free(v->section);
204 if (v->desc)
205 free(v->desc);
206 for (; o;) {
207 opt_type *o1 = o->next;
208 if (o->name)
209 free(o->name);
210 if (o->value)
211 free(o->value);
212 if (o->desc)
213 free(o->desc);
214
215 free(o);
216 o = o1;
217 }
218
219 free(v);
220 v = v1;
221 }
222}
223
224/* --------------------------------------------------------- */
225void varinfo_getvar(const char * name_c, var_type **var) {
226 var_type *lvar;
227
228 for (lvar = vars; (lvar != NULL) && (strcasecmp(name_c, lvar->name) != 0);
229 lvar = lvar->next)
230 ;
231
232 *var = lvar;
233}
234
235/* --------------------------------------------------------- */
236void varinfo_getinfo(const var_type **var,
237 char **name, char **type,
238 char **default_str,
239 char **section, char **desc) {
240 if (var == NULL) {
241 *name = NULL;
242 *type = NULL;
243 *default_str = NULL;
244 *section = NULL;
245 *desc = NULL;
246 } else {
247 *name = (*var)->name;
248 *type = (*var)->type;
249 *default_str = (*var)->default_str;
250 *section = (*var)->section;
251 *desc = (*var)->desc;
252 }
253}
254
255/* --------------------------------------------------------- */
256void varinfo_getopt(const var_type **var, opt_type **opt) {
257 if (*var == NULL)
258 *opt = NULL;
259 else if (*opt == NULL)
260 *opt = (*var)->opt;
261 else
262 *opt = (*opt)->next;
263}
264
265/* --------------------------------------------------------- */
266
267void varinfo_opt_getinfo(const opt_type **opt, char **name,
268 int64_t *value, char **desc) {
269 if (opt == NULL) {
270 *name = NULL;
271 *desc = NULL;
272 *value = 0;
273 } else {
274 *name = (*opt)->name;
275 *desc = (*opt)->desc;
276 if ((*opt)->value) {
277 if (strncmp("bit", (*opt)->value, 3) == 0) {
278 *value = ((int64_t)1) << strtoll((*opt)->value + 4, NULL, 10);
279 } else {
280 *value = strtoll((*opt)->value, NULL, 10);
281 }
283 } else {
284 *value = 0;
285 }
286 }
287}
288
289/* ---------------------------------------------------------
290
291This function searches for a substring in the name of a variable. If
292var is set to NULL, it starts from the beginning of the list. If it is
293different from NULL, it assumes it is the result of a previous search and
294it starts searching from that point. It returns NULL if nothing is
295found.
296
297 --------------------------------------------------------- */
298
299/* used by liboct_parser/symbols.c */
300int varinfo_variable_exists(const char *var_name) {
301 var_type *lvar;
302 for (lvar = vars; (lvar != NULL) && (strcasecmp(var_name, lvar->name) != 0);
303 lvar = lvar->next)
304 ;
305 return (lvar != NULL);
306}
307
308void varinfo_search_var(const char * name_c, var_type **var) {
309 var_type *lvar;
310
311 if (*var == NULL)
312 lvar = vars;
313 else
314 lvar = (*var)->next;
315
316 for (; (lvar != NULL) && (strcasestr(lvar->name, name_c) == 0);
317 lvar = lvar->next)
318 ;
319
320 *var = lvar;
321}
322
323void varinfo_search_option(const var_type **var,
324 const char * name_c, int *value,
325 int *ierr) {
326 opt_type *opt;
327
328 opt = (*var)->opt;
329 *ierr = -1;
330
331 while (opt != NULL) {
332 if (strcmp(opt->name, name_c) == 0) {
333 *value = atoi(opt->value);
334 printf("%s|%s|\n", opt->name, name_c);
335 *ierr = 0;
336 break;
337 }
338 opt = opt->next;
339 }
340}
__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:2735
char * type
Definition: varinfo_low.c:2734
char * section
Definition: varinfo_low.c:2736
char * desc
Definition: varinfo_low.c:2737
char * name
Definition: varinfo_low.c:2733
opt_type * opt
Definition: varinfo_low.c:2738
struct var_type * next
Definition: varinfo_low.c:2739
int varinfo_variable_exists(const char *var_name)
Definition: varinfo_low.c:3141
void varinfo_end()
Definition: varinfo_low.c:2982
void varinfo_getvar(const char *name_c, var_type **var)
Definition: varinfo_low.c:3017
void varinfo_search_option(const var_type **var, const char *name_c, int *value, int *ierr)
Definition: varinfo_low.c:3180
char * get_token(char *s, char **dest)
Definition: varinfo_low.c:2749
void varinfo_init(const char *fname_c)
Definition: varinfo_low.c:2841
void varinfo_getopt(const var_type **var, opt_type **opt)
Definition: varinfo_low.c:3076
void varinfo_opt_getinfo(const opt_type **opt, char **name, int64_t *value, char **desc)
Definition: varinfo_low.c:3099
void varinfo_getinfo(const var_type **var, char **name, char **type, char **default_str, char **section, char **desc)
Definition: varinfo_low.c:3032
void varinfo_search_var(const char *name_c, var_type **var)
Definition: varinfo_low.c:3157
int fclose(FILE *__stream)
struct var_type var_type
static var_type * vars
Definition: varinfo_low.c:2742
void get_text(FILE *in, char **dest)
Definition: varinfo_low.c:2798
opt_type
Definition: varinfo_low.c:2730