Ifpack Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Parser_dh.c
Go to the documentation of this file.
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack: Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2002) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 */
42 
43 #include "Parser_dh.h"
44 #include "Mem_dh.h"
45 
46 typedef struct _optionsNode OptionsNode;
47 
48 struct _parser_dh
49 {
52 };
53 
55 {
56  char *name;
57  char *value;
59 };
60 
61 static bool find (Parser_dh p, char *option, OptionsNode ** ptr);
63 
64 
65 #undef __FUNC__
66 #define __FUNC__ "Parser_dhCreate"
67 void
69 {
71 
72  /* allocate storage for object */
73  struct _parser_dh *tmp =
74  (struct _parser_dh *) MALLOC_DH (sizeof (struct _parser_dh));
76  *p = tmp;
77 
78  /* consruct header node */
79  tmp->head = tmp->tail = (OptionsNode *) MALLOC_DH (sizeof (OptionsNode));
81  ptr = tmp->head;
82  ptr->next = NULL;
83  ptr->name = (char *) MALLOC_DH (6 * sizeof (char));
85  ptr->value = (char *) MALLOC_DH (6 * sizeof (char));
87  strcpy (ptr->name, "JUNK");
88  strcpy (ptr->value, "JUNK");
90 
91 #undef __FUNC__
92 #define __FUNC__ "Parser_dhDestroy"
93 void
95 {
96  START_FUNC_DH OptionsNode * ptr2 = p->head, *ptr1 = ptr2;
97  if (ptr1 != NULL)
98  {
99  do
100  {
101  ptr2 = ptr2->next;
102  FREE_DH (ptr1->name);
103  FREE_DH (ptr1->value);
104  FREE_DH (ptr1);
105  ptr1 = ptr2;
106  }
107  while (ptr1 != NULL);
108  }
109  FREE_DH (p);
111 
112 
113 #undef __FUNC__
114 #define __FUNC__ "Parser_dhUpdateFromFile"
115 void
117 {
118  START_FUNC_DH_2 char line[80], name[80], value[80];
119  FILE *fp;
120 
121  if ((fp = fopen (filename, "r")) == NULL)
122  {
123  sprintf (msgBuf_dh, "can't open >>%s<< for reading", filename);
125  }
126  else
127  {
128  sprintf (msgBuf_dh, "updating parser from file: >>%s<<", filename);
130  while (!feof (fp))
131  {
132  if (fgets (line, 80, fp) == NULL)
133  break;
134  if (line[0] != '#')
135  {
136  if (sscanf (line, "%s %s", name, value) != 2)
137  break;
138  Parser_dhInsert (p, name, value);
139  }
140  }
141  fclose (fp);
142  }
144 
145 #undef __FUNC__
146 #define __FUNC__ "Parser_dhInit"
147 void
148 Parser_dhInit (Parser_dh p, int argc, char *argv[])
149 {
150  START_FUNC_DH_2 int j;
151 
152  /* read option names and values from default database */
153 /* Parser_dhUpdateFromFile(p, MASTER_OPTIONS_LIST); CHECK_V_ERROR;
154 */
157 
158  /* attempt to update from "./database" in local directory */
159  Parser_dhUpdateFromFile (p, "./database");
161 
162  /* attempt to update from specified file */
163  for (j = 1; j < argc; ++j)
164  {
165  if (strcmp (argv[j], "-db_filename") == 0)
166  {
167  ++j;
168  if (j < argc)
169  {
170  Parser_dhUpdateFromFile (p, argv[j]);
172  }
173  }
174  }
175 
176  /* update from command-line options and values */
177  {
178  int i = 0;
179  while (i < argc)
180  {
181  if (argv[i][0] == '-')
182  {
183  char value[] = { "1" }; /* option's default value */
184  bool flag = false; /* yuck! flag for negative numbers */
185  if (i + 1 < argc && argv[i + 1][0] == '-'
186  && argv[i + 1][1] == '-')
187  {
188  flag = true;
189  }
190 
191  if ((i + 1 == argc || argv[i + 1][0] == '-') && !flag)
192  {
193  Parser_dhInsert (p, argv[i], value);
194  }
195  else if (flag)
196  {
197  Parser_dhInsert (p, argv[i], argv[i + 1] + 1); /* insert a negative number */
198  }
199  else
200  {
201  Parser_dhInsert (p, argv[i], argv[i + 1]);
202  }
203  }
204  ++i;
205  }
206  }
208 
209 
210 #undef __FUNC__
211 #define __FUNC__ "Parser_dhHasSwitch"
212 bool
214 {
215  START_FUNC_DH_2 bool has_switch = false;
216  OptionsNode *node;
217 
218  if (p != NULL && find (p, s, &node))
219  {
220  if (!strcmp (node->value, "0"))
221  {
222  has_switch = false;
223  }
224  else if (!strcmp (node->value, "false"))
225  {
226  has_switch = false;
227  }
228  else if (!strcmp (node->value, "False"))
229  {
230  has_switch = false;
231  }
232  else if (!strcmp (node->value, "FALSE"))
233  {
234  has_switch = false;
235  }
236  else
237  {
238  has_switch = true;
239  }
240  }
241 END_FUNC_VAL_2 (has_switch)}
242 
243 /* returns false if option isn't found, or if
244  * its value is zero.
245  */
246 #undef __FUNC__
247 #define __FUNC__ "Parser_dhReadInt"
248 bool
249 Parser_dhReadInt (Parser_dh p, char *in, int *out)
250 {
251  START_FUNC_DH_2 bool has_switch = false;
252  OptionsNode *node;
253 
254  if (p != NULL && find (p, in, &node))
255  {
256  *out = atoi (node->value);
257  if (!strcmp (node->value, "0"))
258  {
259  has_switch = false;
260  }
261  else
262  {
263  has_switch = true;
264  }
265  }
266 END_FUNC_VAL_2 (has_switch)}
267 
268 
269 #undef __FUNC__
270 #define __FUNC__ "Parser_dhReadDouble"
271 bool
272 Parser_dhReadDouble (Parser_dh p, char *in, double *out)
273 {
274  START_FUNC_DH_2 bool optionExists = false;
275  OptionsNode *node;
276 
277  if (p != NULL && find (p, in, &node))
278  {
279  *out = atof (node->value);
280  optionExists = true;
281  }
282 END_FUNC_VAL_2 (optionExists)}
283 
284 #undef __FUNC__
285 #define __FUNC__ "Parser_dhReadString"
286 bool
287 Parser_dhReadString (Parser_dh p, char *in, char **out)
288 {
289  START_FUNC_DH_2 bool optionExists = false;
290  OptionsNode *node;
291 
292  if (p != NULL && find (p, in, &node))
293  {
294  *out = node->value;
295  optionExists = true;
296  }
297 END_FUNC_VAL_2 (optionExists)}
298 
299 
300 #undef __FUNC__
301 #define __FUNC__ "Parser_dhPrint"
302 void
303 Parser_dhPrint (Parser_dh p, FILE * fp, bool allPrint)
304 {
305  START_FUNC_DH_2 OptionsNode * ptr = p->head;
306 
307  if (fp == NULL)
308  SET_V_ERROR ("fp == NULL");
309 
310  if (myid_dh == 0 || allPrint)
311  {
312  fprintf (fp, "------------------------ registered options:\n");
313  if (ptr == NULL)
314  {
315  fprintf (fp, "Parser object is invalid; nothing to print!\n");
316  }
317  else
318  {
319  ptr = ptr->next;
320  while (ptr != NULL)
321  {
322  fprintf (fp, " %s %s\n", ptr->name, ptr->value);
323  fflush (fp);
324  ptr = ptr->next;
325  }
326  }
327  fprintf (fp, "\n");
328  fflush (fp);
329  }
331 
332 #undef __FUNC__
333 #define __FUNC__ "Parser_dhInsert"
334 void
335 Parser_dhInsert (Parser_dh p, char *option, char *value)
336 {
338  int length;
339 
340  if (p == NULL)
341  goto PARSER_NOT_INITED;
342 
343  /* if option is already in the list, update its value */
344  if (find (p, option, &node))
345  {
346  int length2 = strlen (node->value) + 1;
347  length = strlen (value) + 1;
348  if (length2 < length)
349  {
350  FREE_DH (node->value);
351  node->value = (char *) MALLOC_DH (length * sizeof (char));
353  }
354  strcpy (node->value, value);
355  }
356  /* otherwise, add a new node to the list */
357  else
358  {
359  node = p->tail;
360  p->tail = node->next = (OptionsNode *) MALLOC_DH (sizeof (OptionsNode));
362  node = node->next;
363  length = strlen (option) + 1;
364  node->name = (char *) MALLOC_DH (length * sizeof (char));
366  strcpy (node->name, option);
367  length = strlen (value) + 1;
368  node->value = (char *) MALLOC_DH (length * sizeof (char));
370  strcpy (node->value, value);
371  node->next = NULL;
372  }
373 
374 PARSER_NOT_INITED:
375  ;
376 
378 
379 #undef __FUNC__
380 #define __FUNC__ "find"
381 bool
382 find (Parser_dh p, char *option, OptionsNode ** ptr)
383 {
384  START_FUNC_DH_2 OptionsNode * tmpPtr = p->head;
385  bool foundit = false;
386  while (tmpPtr != NULL)
387  {
388  if (strcmp (tmpPtr->name, option) == 0)
389  {
390  foundit = true;
391  *ptr = tmpPtr;
392  break;
393  }
394  tmpPtr = tmpPtr->next;
395  }
396 END_FUNC_VAL_2 (foundit)}
397 
398 
399 #undef __FUNC__
400 #define __FUNC__ "init_from_default_settings_private"
401 void
403 {
405  /* default is to intercept certain signals
406  (floating point error, segmentation violation, etc.)
407  */
408  Parser_dhInsert (p, "-sig_dh", "1");
410 
411  /* used by MetGenFD */
412  Parser_dhInsert (p, "-px", "1");
414  Parser_dhInsert (p, "-py", "1");
416  Parser_dhInsert (p, "-pz", "0");
418  Parser_dhInsert (p, "-m", "4");
420 
421  Parser_dhInsert (p, "-xx_coeff", "-1.0");
423  Parser_dhInsert (p, "-yy_coeff", "-1.0");
425  Parser_dhInsert (p, "-zz_coeff", "-1.0");
427 
428  Parser_dhInsert (p, "-level", "1");
430 
431  Parser_dhInsert (p, "-printStats", "0");
void Parser_dhCreate(Parser_dh *p)
Definition: Parser_dh.c:68
OptionsNode * head
Definition: Parser_dh.c:50
#define CHECK_V_ERROR
Definition: macros_dh.h:138
static void init_from_default_settings_private(Parser_dh p)
Definition: Parser_dh.c:402
void Parser_dhUpdateFromFile(Parser_dh p, char *filename)
Definition: Parser_dh.c:116
OptionsNode * tail
Definition: Parser_dh.c:51
#define END_FUNC_DH
Definition: macros_dh.h:187
#define START_FUNC_DH_2
Definition: macros_dh.h:194
#define MALLOC_DH(s)
bool Parser_dhHasSwitch(Parser_dh p, char *s)
Definition: Parser_dh.c:213
#define END_FUNC_DH_2
Definition: macros_dh.h:200
#define SET_INFO(msg)
Definition: macros_dh.h:156
void Parser_dhPrint(Parser_dh p, FILE *fp, bool allPrint)
Definition: Parser_dh.c:303
void Parser_dhInsert(Parser_dh p, char *option, char *value)
Definition: Parser_dh.c:335
bool Parser_dhReadDouble(Parser_dh p, char *in, double *out)
Definition: Parser_dh.c:272
bool Parser_dhReadInt(Parser_dh p, char *in, int *out)
Definition: Parser_dh.c:249
void Parser_dhDestroy(Parser_dh p)
Definition: Parser_dh.c:94
OptionsNode * next
Definition: Parser_dh.c:58
#define SET_V_ERROR(msg)
Definition: macros_dh.h:126
#define START_FUNC_DH
Definition: macros_dh.h:181
void Parser_dhInit(Parser_dh p, int argc, char *argv[])
Definition: Parser_dh.c:148
bool Parser_dhReadString(Parser_dh p, char *in, char **out)
Definition: Parser_dh.c:287
#define END_FUNC_VAL_2(retval)
Definition: macros_dh.h:215
char * name
Definition: Parser_dh.c:56
int myid_dh
Definition: globalObjects.c:63
char msgBuf_dh[MSG_BUF_SIZE_DH]
Definition: globalObjects.c:61
#define FREE_DH(p)
char * value
Definition: Parser_dh.c:57
static bool find(Parser_dh p, char *option, OptionsNode **ptr)
Definition: Parser_dh.c:382