IFPACK  Development
 All Classes Files Functions Variables Enumerations Friends
Parser_dh.c
00001 /*@HEADER
00002 // ***********************************************************************
00003 //
00004 //       Ifpack: Object-Oriented Algebraic Preconditioner Package
00005 //                 Copyright (2002) 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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 //@HEADER
00041 */
00042 
00043 #include "Parser_dh.h"
00044 #include "Mem_dh.h"
00045 
00046 typedef struct _optionsNode OptionsNode;
00047 
00048 struct _parser_dh
00049 {
00050   OptionsNode *head;
00051   OptionsNode *tail;
00052 };
00053 
00054 struct _optionsNode
00055 {
00056   char *name;
00057   char *value;
00058   OptionsNode *next;
00059 };
00060 
00061 static bool find (Parser_dh p, char *option, OptionsNode ** ptr);
00062 static void init_from_default_settings_private (Parser_dh p);
00063 
00064 
00065 #undef __FUNC__
00066 #define __FUNC__ "Parser_dhCreate"
00067 void
00068 Parser_dhCreate (Parser_dh * p)
00069 {
00070   START_FUNC_DH OptionsNode * ptr;
00071 
00072   /* allocate storage for object */
00073   struct _parser_dh *tmp =
00074     (struct _parser_dh *) MALLOC_DH (sizeof (struct _parser_dh));
00075   CHECK_V_ERROR;
00076   *p = tmp;
00077 
00078   /* consruct header node */
00079   tmp->head = tmp->tail = (OptionsNode *) MALLOC_DH (sizeof (OptionsNode));
00080   CHECK_V_ERROR;
00081   ptr = tmp->head;
00082   ptr->next = NULL;
00083   ptr->name = (char *) MALLOC_DH (6 * sizeof (char));
00084   CHECK_V_ERROR;
00085   ptr->value = (char *) MALLOC_DH (6 * sizeof (char));
00086   CHECK_V_ERROR;
00087   strcpy (ptr->name, "JUNK");
00088   strcpy (ptr->value, "JUNK");
00089 END_FUNC_DH}
00090 
00091 #undef __FUNC__
00092 #define __FUNC__ "Parser_dhDestroy"
00093 void
00094 Parser_dhDestroy (Parser_dh p)
00095 {
00096   START_FUNC_DH OptionsNode * ptr2 = p->head, *ptr1 = ptr2;
00097   if (ptr1 != NULL)
00098     {
00099       do
00100     {
00101       ptr2 = ptr2->next;
00102       FREE_DH (ptr1->name);
00103       FREE_DH (ptr1->value);
00104       FREE_DH (ptr1);
00105       ptr1 = ptr2;
00106     }
00107       while (ptr1 != NULL);
00108     }
00109   FREE_DH (p);
00110 END_FUNC_DH}
00111 
00112 
00113 #undef __FUNC__
00114 #define __FUNC__ "Parser_dhUpdateFromFile"
00115 void
00116 Parser_dhUpdateFromFile (Parser_dh p, char *filename)
00117 {
00118   START_FUNC_DH_2 char line[80], name[80], value[80];
00119   FILE *fp;
00120 
00121   if ((fp = fopen (filename, "r")) == NULL)
00122     {
00123       sprintf (msgBuf_dh, "can't open >>%s<< for reading", filename);
00124       SET_INFO (msgBuf_dh);
00125     }
00126   else
00127     {
00128       sprintf (msgBuf_dh, "updating parser from file: >>%s<<", filename);
00129       SET_INFO (msgBuf_dh);
00130       while (!feof (fp))
00131     {
00132       if (fgets (line, 80, fp) == NULL)
00133         break;
00134       if (line[0] != '#')
00135         {
00136           if (sscanf (line, "%s %s", name, value) != 2)
00137         break;
00138           Parser_dhInsert (p, name, value);
00139         }
00140     }
00141       fclose (fp);
00142     }
00143 END_FUNC_DH_2}
00144 
00145 #undef __FUNC__
00146 #define __FUNC__ "Parser_dhInit"
00147 void
00148 Parser_dhInit (Parser_dh p, int argc, char *argv[])
00149 {
00150   START_FUNC_DH_2 int j;
00151 
00152   /* read option names and values from default database */
00153 /*  Parser_dhUpdateFromFile(p, MASTER_OPTIONS_LIST); CHECK_V_ERROR;
00154 */
00155   init_from_default_settings_private (p);
00156   CHECK_V_ERROR;
00157 
00158   /* attempt to update from "./database" in local directory */
00159   Parser_dhUpdateFromFile (p, "./database");
00160   CHECK_V_ERROR;
00161 
00162   /* attempt to update from specified file */
00163   for (j = 1; j < argc; ++j)
00164     {
00165       if (strcmp (argv[j], "-db_filename") == 0)
00166     {
00167       ++j;
00168       if (j < argc)
00169         {
00170           Parser_dhUpdateFromFile (p, argv[j]);
00171           CHECK_V_ERROR;
00172         }
00173     }
00174     }
00175 
00176   /* update from command-line options and values */
00177   {
00178     int i = 0;
00179     while (i < argc)
00180       {
00181     if (argv[i][0] == '-')
00182       {
00183         char value[] = { "1" }; /* option's default value */
00184         bool flag = false;  /* yuck! flag for negative numbers */
00185         if (i + 1 < argc && argv[i + 1][0] == '-'
00186         && argv[i + 1][1] == '-')
00187           {
00188         flag = true;
00189           }
00190 
00191         if ((i + 1 == argc || argv[i + 1][0] == '-') && !flag)
00192           {
00193         Parser_dhInsert (p, argv[i], value);
00194           }
00195         else if (flag)
00196           {
00197         Parser_dhInsert (p, argv[i], argv[i + 1] + 1);  /* insert a negative number */
00198           }
00199         else
00200           {
00201         Parser_dhInsert (p, argv[i], argv[i + 1]);
00202           }
00203       }
00204     ++i;
00205       }
00206   }
00207 END_FUNC_DH_2}
00208 
00209 
00210 #undef __FUNC__
00211 #define __FUNC__ "Parser_dhHasSwitch"
00212 bool
00213 Parser_dhHasSwitch (Parser_dh p, char *s)
00214 {
00215   START_FUNC_DH_2 bool has_switch = false;
00216   OptionsNode *node;
00217 
00218   if (p != NULL && find (p, s, &node))
00219     {
00220       if (!strcmp (node->value, "0"))
00221     {
00222       has_switch = false;
00223     }
00224       else if (!strcmp (node->value, "false"))
00225     {
00226       has_switch = false;
00227     }
00228       else if (!strcmp (node->value, "False"))
00229     {
00230       has_switch = false;
00231     }
00232       else if (!strcmp (node->value, "FALSE"))
00233     {
00234       has_switch = false;
00235     }
00236       else
00237     {
00238       has_switch = true;
00239     }
00240     }
00241 END_FUNC_VAL_2 (has_switch)}
00242 
00243 /* returns false if option isn't found, or if
00244  * its value is zero.
00245  */
00246 #undef __FUNC__
00247 #define __FUNC__ "Parser_dhReadInt"
00248 bool
00249 Parser_dhReadInt (Parser_dh p, char *in, int *out)
00250 {
00251   START_FUNC_DH_2 bool has_switch = false;
00252   OptionsNode *node;
00253 
00254   if (p != NULL && find (p, in, &node))
00255     {
00256       *out = atoi (node->value);
00257       if (!strcmp (node->value, "0"))
00258     {
00259       has_switch = false;
00260     }
00261       else
00262     {
00263       has_switch = true;
00264     }
00265     }
00266 END_FUNC_VAL_2 (has_switch)}
00267 
00268 
00269 #undef __FUNC__
00270 #define __FUNC__ "Parser_dhReadDouble"
00271 bool
00272 Parser_dhReadDouble (Parser_dh p, char *in, double *out)
00273 {
00274   START_FUNC_DH_2 bool optionExists = false;
00275   OptionsNode *node;
00276 
00277   if (p != NULL && find (p, in, &node))
00278     {
00279       *out = atof (node->value);
00280       optionExists = true;
00281     }
00282 END_FUNC_VAL_2 (optionExists)}
00283 
00284 #undef __FUNC__
00285 #define __FUNC__ "Parser_dhReadString"
00286 bool
00287 Parser_dhReadString (Parser_dh p, char *in, char **out)
00288 {
00289   START_FUNC_DH_2 bool optionExists = false;
00290   OptionsNode *node;
00291 
00292   if (p != NULL && find (p, in, &node))
00293     {
00294       *out = node->value;
00295       optionExists = true;
00296     }
00297 END_FUNC_VAL_2 (optionExists)}
00298 
00299 
00300 #undef __FUNC__
00301 #define __FUNC__ "Parser_dhPrint"
00302 void
00303 Parser_dhPrint (Parser_dh p, FILE * fp, bool allPrint)
00304 {
00305   START_FUNC_DH_2 OptionsNode * ptr = p->head;
00306 
00307   if (fp == NULL)
00308     SET_V_ERROR ("fp == NULL");
00309 
00310   if (myid_dh == 0 || allPrint)
00311     {
00312       fprintf (fp, "------------------------ registered options:\n");
00313       if (ptr == NULL)
00314     {
00315       fprintf (fp, "Parser object is invalid; nothing to print!\n");
00316     }
00317       else
00318     {
00319       ptr = ptr->next;
00320       while (ptr != NULL)
00321         {
00322           fprintf (fp, "   %s  %s\n", ptr->name, ptr->value);
00323           fflush (fp);
00324           ptr = ptr->next;
00325         }
00326     }
00327       fprintf (fp, "\n");
00328       fflush (fp);
00329     }
00330 END_FUNC_DH_2}
00331 
00332 #undef __FUNC__
00333 #define __FUNC__ "Parser_dhInsert"
00334 void
00335 Parser_dhInsert (Parser_dh p, char *option, char *value)
00336 {
00337   START_FUNC_DH_2 OptionsNode * node;
00338   int length;
00339 
00340   if (p == NULL)
00341     goto PARSER_NOT_INITED;
00342 
00343   /* if option is already in the list, update its value */
00344   if (find (p, option, &node))
00345     {
00346       int length2 = strlen (node->value) + 1;
00347       length = strlen (value) + 1;
00348       if (length2 < length)
00349     {
00350       FREE_DH (node->value);
00351       node->value = (char *) MALLOC_DH (length * sizeof (char));
00352       CHECK_V_ERROR;
00353     }
00354       strcpy (node->value, value);
00355     }
00356   /* otherwise, add a new node to the list */
00357   else
00358     {
00359       node = p->tail;
00360       p->tail = node->next = (OptionsNode *) MALLOC_DH (sizeof (OptionsNode));
00361       CHECK_V_ERROR;
00362       node = node->next;
00363       length = strlen (option) + 1;
00364       node->name = (char *) MALLOC_DH (length * sizeof (char));
00365       CHECK_V_ERROR;
00366       strcpy (node->name, option);
00367       length = strlen (value) + 1;
00368       node->value = (char *) MALLOC_DH (length * sizeof (char));
00369       CHECK_V_ERROR;
00370       strcpy (node->value, value);
00371       node->next = NULL;
00372     }
00373 
00374 PARSER_NOT_INITED:
00375   ;
00376 
00377 END_FUNC_DH_2}
00378 
00379 #undef __FUNC__
00380 #define __FUNC__ "find"
00381 bool
00382 find (Parser_dh p, char *option, OptionsNode ** ptr)
00383 {
00384   START_FUNC_DH_2 OptionsNode * tmpPtr = p->head;
00385   bool foundit = false;
00386   while (tmpPtr != NULL)
00387     {
00388       if (strcmp (tmpPtr->name, option) == 0)
00389     {
00390       foundit = true;
00391       *ptr = tmpPtr;
00392       break;
00393     }
00394       tmpPtr = tmpPtr->next;
00395     }
00396 END_FUNC_VAL_2 (foundit)}
00397 
00398 
00399 #undef __FUNC__
00400 #define __FUNC__ "init_from_default_settings_private"
00401 void
00402 init_from_default_settings_private (Parser_dh p)
00403 {
00404   START_FUNC_DH_2
00405     /* default is to intercept certain signals
00406        (floating point error, segmentation violation, etc.)
00407      */
00408     Parser_dhInsert (p, "-sig_dh", "1");
00409   CHECK_V_ERROR;
00410 
00411   /* used by MetGenFD */
00412   Parser_dhInsert (p, "-px", "1");
00413   CHECK_V_ERROR;
00414   Parser_dhInsert (p, "-py", "1");
00415   CHECK_V_ERROR;
00416   Parser_dhInsert (p, "-pz", "0");
00417   CHECK_V_ERROR;
00418   Parser_dhInsert (p, "-m", "4");
00419   CHECK_V_ERROR;
00420 
00421   Parser_dhInsert (p, "-xx_coeff", "-1.0");
00422   CHECK_V_ERROR;
00423   Parser_dhInsert (p, "-yy_coeff", "-1.0");
00424   CHECK_V_ERROR;
00425   Parser_dhInsert (p, "-zz_coeff", "-1.0");
00426   CHECK_V_ERROR;
00427 
00428   Parser_dhInsert (p, "-level", "1");
00429   CHECK_V_ERROR;
00430 
00431   Parser_dhInsert (p, "-printStats", "0");
00432   CHECK_V_ERROR;
00433 END_FUNC_DH_2}
 All Classes Files Functions Variables Enumerations Friends