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