moab
get_file_options.hpp
Go to the documentation of this file.
00001 #ifndef GET_FILE_OPTIONS_H
00002 #define GET_FILE_OPTIONS_H
00003 
00004 #include "moab/Core.hpp"
00005 
00006 namespace moab {
00007 
00008 // Check first character for a '-'.
00009 // Return true if one is found.  False otherwise.
00010 bool check_for_flag(const char *str) {
00011   if (str[0] == '-')
00012     return true;
00013   else
00014     return false;
00015 }
00016 
00017 // New get_file_options() function with added possibilities for mbcoupler_test.
00018 ErrorCode get_file_options(int argc, char **argv, 
00019                            std::vector<std::string> &meshFiles,
00020                            std::string &interpTag,
00021                            std::string &gNormTag,
00022                            std::string &ssNormTag,
00023                            std::vector<const char *> &ssTagNames,
00024                            std::vector<const char *> &ssTagValues,
00025                            std::string &readOpts,
00026                            std::string &outFile,
00027                            std::string &writeOpts,
00028                            std::string &dbgFile,
00029                            bool &help)
00030 {
00031   #ifdef MESHDIR
00032   std::string TestDir( STRINGIFY(MESHDIR) );
00033   #else
00034   std::string TestDir(".");
00035   #endif
00036 
00037   // Initialize some of the outputs to null values indicating not present
00038   // in the argument list.
00039   gNormTag = "";
00040   ssNormTag = "";
00041   readOpts = "PARALLEL=READ_PART;PARTITION=PARALLEL_PARTITION;PARTITION_DISTRIBUTE;PARALLEL_RESOLVE_SHARED_ENTS;PARALLEL_GHOSTS=3.0.1;CPUTIME";
00042   outFile = "";
00043   writeOpts = "PARALLEL=WRITE_PART;CPUTIME";
00044   dbgFile = "";
00045   std::string defaultDbgFile = argv[0];  // The executable name will be the default debug output file.
00046 
00047   // These will indicate if we've gotten our required parameters at the end of parsing.
00048   bool haveMeshes = false;
00049   bool haveInterpTag = false;
00050 
00051   // Loop over the values in argv pulling out an parsing each one
00052   int npos = 1;
00053 
00054   if (argc > 1 && argv[1] == std::string("-h")) {
00055     help = true;
00056     return MB_SUCCESS;
00057   }
00058 
00059   while (npos < argc) {
00060     if (argv[npos] == std::string("-meshes")) {
00061       // Parse out the mesh filenames
00062       npos++;
00063       int numFiles = 2;
00064       meshFiles.resize(numFiles);
00065       for (int i = 0; i < numFiles; i++) {
00066         if ((npos < argc) && (!check_for_flag(argv[npos])))
00067           meshFiles[i] = argv[npos++];
00068         else {
00069           std::cerr << "    ERROR - missing correct number of mesh filenames" << std::endl;
00070           return MB_FAILURE;
00071         }
00072       }
00073 
00074       haveMeshes = true;
00075     }
00076     else if (argv[npos] == std::string("-itag")) {
00077       // Parse out the interpolation tag
00078       npos++;
00079       if ((npos < argc) && (!check_for_flag(argv[npos])))
00080         interpTag = argv[npos++];
00081       else {
00082         std::cerr << "    ERROR - missing <interp_tag>" << std::endl;
00083         return MB_FAILURE;
00084       }
00085 
00086       haveInterpTag = true;
00087     }
00088     else if (argv[npos] == std::string("-gnorm")) {
00089       // Parse out the global normalization tag
00090       npos++;
00091       if ((npos < argc) && (!check_for_flag(argv[npos])))
00092         gNormTag = argv[npos++];
00093       else {
00094         std::cerr << "    ERROR - missing <gnorm_tag>" << std::endl;
00095         return MB_FAILURE;
00096       }
00097     }
00098     else if (argv[npos] == std::string("-ssnorm")) {
00099       // Parse out the subset normalization tag and selection criteria
00100       npos++;
00101       if ((npos < argc) && (!check_for_flag(argv[npos])))
00102         ssNormTag = argv[npos++];
00103       else {
00104         std::cerr << "    ERROR - missing <ssnorm_tag>" << std::endl;
00105         return MB_FAILURE;
00106       }
00107 
00108       if ((npos < argc) && (!check_for_flag(argv[npos]))) {
00109         char* opts = argv[npos++];
00110         char sep1[1] = {';'};
00111         char sep2[1] = {'='};
00112         bool end_vals_seen = false;
00113         std::vector<char *> tmpTagOpts;
00114 
00115         // first get the options
00116         for (char* i = strtok(opts, sep1); i; i = strtok(0, sep1)) {
00117           tmpTagOpts.push_back(i);
00118         }
00119 
00120         // parse out the name and val or just name.
00121         for (unsigned int j = 0; j < tmpTagOpts.size(); j++) {
00122           char* e = strtok(tmpTagOpts[j], sep2);
00123           ssTagNames.push_back(e);
00124           e = strtok(0, sep2);
00125           if (e != NULL) {
00126             // We have a value
00127             if (end_vals_seen) {
00128               // ERROR we should not have a value after none are seen
00129               std::cerr << "    ERROR - new value seen after end of values in <ssnorm_selection>" << std::endl;
00130               return MB_FAILURE;
00131             }
00132             // Otherwise get the value string from e and convert it to an int
00133             int *valp = new int;
00134             *valp = atoi(e);
00135             ssTagValues.push_back((const char *) valp);
00136           }
00137           else {
00138             // Otherwise there is no '=' so push a null on the list
00139             end_vals_seen = true;
00140             ssTagValues.push_back((const char *) 0);
00141           }
00142         }
00143       }
00144       else {
00145         std::cerr << "    ERROR - missing <ssnorm_selection>" << std::endl;
00146         return MB_FAILURE;
00147       }
00148     }
00149     else if (argv[npos] == std::string("-ropts")) {
00150       // Parse out the mesh file read options
00151       npos++;
00152       if ((npos < argc) && (!check_for_flag(argv[npos])))
00153         readOpts = argv[npos++];
00154       else {
00155         std::cerr << "    ERROR - missing <roptions>" << std::endl;
00156         return MB_FAILURE;
00157       }
00158     }
00159     else if (argv[npos] == std::string("-outfile")) {
00160       // Parse out the output file name
00161       npos++;
00162       if ((npos < argc) && (!check_for_flag(argv[npos])))
00163         outFile = argv[npos++];
00164       else {
00165         std::cerr << "    ERROR - missing <out_file>" << std::endl;
00166         return MB_FAILURE;
00167       }
00168     }
00169     else if (argv[npos] == std::string("-wopts")) {
00170       // Parse out the output file write options
00171       npos++;
00172       if ((npos < argc) && (!check_for_flag(argv[npos])))
00173         writeOpts = argv[npos++];
00174       else {
00175         std::cerr << "    ERROR - missing <woptions>" << std::endl;
00176         return MB_FAILURE;
00177       }
00178     }
00179     else if (argv[npos] == std::string("-dbgout")) {
00180       // Parse out the debug output file name.
00181       // If no name then use the default.
00182       npos++;
00183       if ((npos < argc) && (!check_for_flag(argv[npos])))
00184         dbgFile = argv[npos++];
00185       else
00186         dbgFile = defaultDbgFile;
00187     }
00188     else {
00189       // Unrecognized parameter.  Skip it and move along.
00190       std::cerr << "    ERROR - Unrecognized parameter:" << argv[npos] << std::endl;
00191       std::cerr << "            Skipping..." << std::endl;
00192       npos++;
00193     }
00194   }
00195 
00196   if (!haveMeshes) {
00197     meshFiles.resize(2);
00198     meshFiles[0] = std::string(TestDir + "/64bricks_1khex.h5m");
00199     meshFiles[1] = std::string(TestDir + "/64bricks_12ktet.h5m");
00200     std::cout << "Mesh files not entered; using default files " 
00201               << meshFiles[0] << " and " << meshFiles[1] << std::endl;
00202   }
00203   
00204   if (!haveInterpTag) {
00205     interpTag = "vertex_field";
00206     std::cout << "Interpolation field name not given, using default of " << interpTag << std::endl;
00207   }
00208 
00209 #ifdef HDF5_FILE
00210   if (1 == argc) {
00211     std::cout << "No arguments given; using output file dum.h5m." << std::endl;
00212     outFile = "dum.h5m";
00213   }
00214 #endif
00215     
00216   return MB_SUCCESS;
00217 }
00218 }
00219 
00220 #endif // GET_FILE_OPTIONS_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines