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