Parser_dh.c

Go to the documentation of this file.
00001 /*@HEADER
00002 // ***********************************************************************
00003 //
00004 //       Ifpack: Object-Oriented Algebraic Preconditioner Package
00005 //                 Copyright (2009) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00025 //
00026 // ***********************************************************************
00027 //@HEADER
00028 */
00029 
00030 #include "Parser_dh.h"
00031 #include "Mem_dh.h"
00032 
00033 typedef struct _optionsNode OptionsNode;
00034 
00035 struct _parser_dh
00036 {
00037   OptionsNode *head;
00038   OptionsNode *tail;
00039 };
00040 
00041 struct _optionsNode
00042 {
00043   char *name;
00044   char *value;
00045   OptionsNode *next;
00046 };
00047 
00048 static bool find (Parser_dh p, char *option, OptionsNode ** ptr);
00049 static void init_from_default_settings_private (Parser_dh p);
00050 
00051 
00052 #undef __FUNC__
00053 #define __FUNC__ "Parser_dhCreate"
00054 void
00055 Parser_dhCreate (Parser_dh * p)
00056 {
00057   START_FUNC_DH OptionsNode * ptr;
00058 
00059   /* allocate storage for object */
00060   struct _parser_dh *tmp =
00061     (struct _parser_dh *) MALLOC_DH (sizeof (struct _parser_dh));
00062   CHECK_V_ERROR;
00063   *p = tmp;
00064 
00065   /* consruct header node */
00066   tmp->head = tmp->tail = (OptionsNode *) MALLOC_DH (sizeof (OptionsNode));
00067   CHECK_V_ERROR;
00068   ptr = tmp->head;
00069   ptr->next = NULL;
00070   ptr->name = (char *) MALLOC_DH (6 * sizeof (char));
00071   CHECK_V_ERROR;
00072   ptr->value = (char *) MALLOC_DH (6 * sizeof (char));
00073   CHECK_V_ERROR;
00074   strcpy (ptr->name, "JUNK");
00075   strcpy (ptr->value, "JUNK");
00076 END_FUNC_DH}
00077 
00078 #undef __FUNC__
00079 #define __FUNC__ "Parser_dhDestroy"
00080 void
00081 Parser_dhDestroy (Parser_dh p)
00082 {
00083   START_FUNC_DH OptionsNode * ptr2 = p->head, *ptr1 = ptr2;
00084   if (ptr1 != NULL)
00085     {
00086       do
00087   {
00088     ptr2 = ptr2->next;
00089     FREE_DH (ptr1->name);
00090     FREE_DH (ptr1->value);
00091     FREE_DH (ptr1);
00092     ptr1 = ptr2;
00093   }
00094       while (ptr1 != NULL);
00095     }
00096   FREE_DH (p);
00097 END_FUNC_DH}
00098 
00099 
00100 #undef __FUNC__
00101 #define __FUNC__ "Parser_dhUpdateFromFile"
00102 void
00103 Parser_dhUpdateFromFile (Parser_dh p, char *filename)
00104 {
00105   START_FUNC_DH_2 char line[80], name[80], value[80];
00106   FILE *fp;
00107 
00108   if ((fp = fopen (filename, "r")) == NULL)
00109     {
00110       sprintf (msgBuf_dh, "can't open >>%s<< for reading", filename);
00111       SET_INFO (msgBuf_dh);
00112     }
00113   else
00114     {
00115       sprintf (msgBuf_dh, "updating parser from file: >>%s<<", filename);
00116       SET_INFO (msgBuf_dh);
00117       while (!feof (fp))
00118   {
00119     if (fgets (line, 80, fp) == NULL)
00120       break;
00121     if (line[0] != '#')
00122       {
00123         if (sscanf (line, "%s %s", name, value) != 2)
00124     break;
00125         Parser_dhInsert (p, name, value);
00126       }
00127   }
00128       fclose (fp);
00129     }
00130 END_FUNC_DH_2}
00131 
00132 #undef __FUNC__
00133 #define __FUNC__ "Parser_dhInit"
00134 void
00135 Parser_dhInit (Parser_dh p, int argc, char *argv[])
00136 {
00137   START_FUNC_DH_2 int j;
00138 
00139   /* read option names and values from default database */
00140 /*  Parser_dhUpdateFromFile(p, MASTER_OPTIONS_LIST); CHECK_V_ERROR;
00141 */
00142   init_from_default_settings_private (p);
00143   CHECK_V_ERROR;
00144 
00145   /* attempt to update from "./database" in local directory */
00146   Parser_dhUpdateFromFile (p, "./database");
00147   CHECK_V_ERROR;
00148 
00149   /* attempt to update from specified file */
00150   for (j = 1; j < argc; ++j)
00151     {
00152       if (strcmp (argv[j], "-db_filename") == 0)
00153   {
00154     ++j;
00155     if (j < argc)
00156       {
00157         Parser_dhUpdateFromFile (p, argv[j]);
00158         CHECK_V_ERROR;
00159       }
00160   }
00161     }
00162 
00163   /* update from command-line options and values */
00164   {
00165     int i = 0;
00166     while (i < argc)
00167       {
00168   if (argv[i][0] == '-')
00169     {
00170       char value[] = { "1" }; /* option's default value */
00171       bool flag = false;  /* yuck! flag for negative numbers */
00172       if (i + 1 < argc && argv[i + 1][0] == '-'
00173     && argv[i + 1][1] == '-')
00174         {
00175     flag = true;
00176         }
00177 
00178       if ((i + 1 == argc || argv[i + 1][0] == '-') && !flag)
00179         {
00180     Parser_dhInsert (p, argv[i], value);
00181         }
00182       else if (flag)
00183         {
00184     Parser_dhInsert (p, argv[i], argv[i + 1] + 1);  /* insert a negative number */
00185         }
00186       else
00187         {
00188     Parser_dhInsert (p, argv[i], argv[i + 1]);
00189         }
00190     }
00191   ++i;
00192       }
00193   }
00194 END_FUNC_DH_2}
00195 
00196 
00197 #undef __FUNC__
00198 #define __FUNC__ "Parser_dhHasSwitch"
00199 bool
00200 Parser_dhHasSwitch (Parser_dh p, char *s)
00201 {
00202   START_FUNC_DH_2 bool has_switch = false;
00203   OptionsNode *node;
00204 
00205   if (p != NULL && find (p, s, &node))
00206     {
00207       if (!strcmp (node->value, "0"))
00208   {
00209     has_switch = false;
00210   }
00211       else if (!strcmp (node->value, "false"))
00212   {
00213     has_switch = false;
00214   }
00215       else if (!strcmp (node->value, "False"))
00216   {
00217     has_switch = false;
00218   }
00219       else if (!strcmp (node->value, "FALSE"))
00220   {
00221     has_switch = false;
00222   }
00223       else
00224   {
00225     has_switch = true;
00226   }
00227     }
00228 END_FUNC_VAL_2 (has_switch)}
00229 
00230 /* returns false if option isn't found, or if
00231  * its value is zero.
00232  */
00233 #undef __FUNC__
00234 #define __FUNC__ "Parser_dhReadInt"
00235 bool
00236 Parser_dhReadInt (Parser_dh p, char *in, int *out)
00237 {
00238   START_FUNC_DH_2 bool has_switch = false;
00239   OptionsNode *node;
00240 
00241   if (p != NULL && find (p, in, &node))
00242     {
00243       *out = atoi (node->value);
00244       if (!strcmp (node->value, "0"))
00245   {
00246     has_switch = false;
00247   }
00248       else
00249   {
00250     has_switch = true;
00251   }
00252     }
00253 END_FUNC_VAL_2 (has_switch)}
00254 
00255 
00256 #undef __FUNC__
00257 #define __FUNC__ "Parser_dhReadDouble"
00258 bool
00259 Parser_dhReadDouble (Parser_dh p, char *in, double *out)
00260 {
00261   START_FUNC_DH_2 bool optionExists = false;
00262   OptionsNode *node;
00263 
00264   if (p != NULL && find (p, in, &node))
00265     {
00266       *out = atof (node->value);
00267       optionExists = true;
00268     }
00269 END_FUNC_VAL_2 (optionExists)}
00270 
00271 #undef __FUNC__
00272 #define __FUNC__ "Parser_dhReadString"
00273 bool
00274 Parser_dhReadString (Parser_dh p, char *in, char **out)
00275 {
00276   START_FUNC_DH_2 bool optionExists = false;
00277   OptionsNode *node;
00278 
00279   if (p != NULL && find (p, in, &node))
00280     {
00281       *out = node->value;
00282       optionExists = true;
00283     }
00284 END_FUNC_VAL_2 (optionExists)}
00285 
00286 
00287 #undef __FUNC__
00288 #define __FUNC__ "Parser_dhPrint"
00289 void
00290 Parser_dhPrint (Parser_dh p, FILE * fp, bool allPrint)
00291 {
00292   START_FUNC_DH_2 OptionsNode * ptr = p->head;
00293 
00294   if (fp == NULL)
00295     SET_V_ERROR ("fp == NULL");
00296 
00297   if (myid_dh == 0 || allPrint)
00298     {
00299       fprintf (fp, "------------------------ registered options:\n");
00300       if (ptr == NULL)
00301   {
00302     fprintf (fp, "Parser object is invalid; nothing to print!\n");
00303   }
00304       else
00305   {
00306     ptr = ptr->next;
00307     while (ptr != NULL)
00308       {
00309         fprintf (fp, "   %s  %s\n", ptr->name, ptr->value);
00310         fflush (fp);
00311         ptr = ptr->next;
00312       }
00313   }
00314       fprintf (fp, "\n");
00315       fflush (fp);
00316     }
00317 END_FUNC_DH_2}
00318 
00319 #undef __FUNC__
00320 #define __FUNC__ "Parser_dhInsert"
00321 void
00322 Parser_dhInsert (Parser_dh p, char *option, char *value)
00323 {
00324   START_FUNC_DH_2 OptionsNode * node;
00325   int length;
00326 
00327   if (p == NULL)
00328     goto PARSER_NOT_INITED;
00329 
00330   /* if option is already in the list, update its value */
00331   if (find (p, option, &node))
00332     {
00333       int length2 = strlen (node->value) + 1;
00334       length = strlen (value) + 1;
00335       if (length2 < length)
00336   {
00337     FREE_DH (node->value);
00338     node->value = (char *) MALLOC_DH (length * sizeof (char));
00339     CHECK_V_ERROR;
00340   }
00341       strcpy (node->value, value);
00342     }
00343   /* otherwise, add a new node to the list */
00344   else
00345     {
00346       node = p->tail;
00347       p->tail = node->next = (OptionsNode *) MALLOC_DH (sizeof (OptionsNode));
00348       CHECK_V_ERROR;
00349       node = node->next;
00350       length = strlen (option) + 1;
00351       node->name = (char *) MALLOC_DH (length * sizeof (char));
00352       CHECK_V_ERROR;
00353       strcpy (node->name, option);
00354       length = strlen (value) + 1;
00355       node->value = (char *) MALLOC_DH (length * sizeof (char));
00356       CHECK_V_ERROR;
00357       strcpy (node->value, value);
00358       node->next = NULL;
00359     }
00360 
00361 PARSER_NOT_INITED:
00362   ;
00363 
00364 END_FUNC_DH_2}
00365 
00366 #undef __FUNC__
00367 #define __FUNC__ "find"
00368 bool
00369 find (Parser_dh p, char *option, OptionsNode ** ptr)
00370 {
00371   START_FUNC_DH_2 OptionsNode * tmpPtr = p->head;
00372   bool foundit = false;
00373   while (tmpPtr != NULL)
00374     {
00375       if (strcmp (tmpPtr->name, option) == 0)
00376   {
00377     foundit = true;
00378     *ptr = tmpPtr;
00379     break;
00380   }
00381       tmpPtr = tmpPtr->next;
00382     }
00383 END_FUNC_VAL_2 (foundit)}
00384 
00385 
00386 #undef __FUNC__
00387 #define __FUNC__ "init_from_default_settings_private"
00388 void
00389 init_from_default_settings_private (Parser_dh p)
00390 {
00391   START_FUNC_DH_2
00392     /* default is to intercept certain signals
00393        (floating point error, segmentation violation, etc.)
00394      */
00395     Parser_dhInsert (p, "-sig_dh", "1");
00396   CHECK_V_ERROR;
00397 
00398   /* used by MetGenFD */
00399   Parser_dhInsert (p, "-px", "1");
00400   CHECK_V_ERROR;
00401   Parser_dhInsert (p, "-py", "1");
00402   CHECK_V_ERROR;
00403   Parser_dhInsert (p, "-pz", "0");
00404   CHECK_V_ERROR;
00405   Parser_dhInsert (p, "-m", "4");
00406   CHECK_V_ERROR;
00407 
00408   Parser_dhInsert (p, "-xx_coeff", "-1.0");
00409   CHECK_V_ERROR;
00410   Parser_dhInsert (p, "-yy_coeff", "-1.0");
00411   CHECK_V_ERROR;
00412   Parser_dhInsert (p, "-zz_coeff", "-1.0");
00413   CHECK_V_ERROR;
00414 
00415   Parser_dhInsert (p, "-level", "1");
00416   CHECK_V_ERROR;
00417 
00418   Parser_dhInsert (p, "-printStats", "0");
00419   CHECK_V_ERROR;
00420 END_FUNC_DH_2}
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 10:05:35 2011 for Ifpack Package Browser (Single Doxygen Collection) by  doxygen 1.6.3