[MOAB-dev] r3273 - in MOAB/trunk: . tools/dagmc

kraftche at cae.wisc.edu kraftche at cae.wisc.edu
Tue Nov 3 13:06:18 CST 2009


Author: kraftche
Date: 2009-11-03 13:06:17 -0600 (Tue, 03 Nov 2009)
New Revision: 3273

Modified:
   MOAB/trunk/FileOptions.cpp
   MOAB/trunk/FileOptions.hpp
   MOAB/trunk/MBCore.cpp
   MOAB/trunk/tools/dagmc/test_geom.cc
Log:
return error code if unrecognized options are passed to load_file or write_file

Modified: MOAB/trunk/FileOptions.cpp
===================================================================
--- MOAB/trunk/FileOptions.cpp	2009-11-03 18:44:52 UTC (rev 3272)
+++ MOAB/trunk/FileOptions.cpp	2009-11-03 19:06:17 UTC (rev 3273)
@@ -23,6 +23,7 @@
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
+#include <algorithm>
 
 const char DEFAULT_SEPARATOR = ';';
 
@@ -55,6 +56,8 @@
       if (!strempty(i)) // skip empty strings
         mOptions.push_back( i );
   }
+  
+  mSeen.resize( mOptions.size(), false );
 }
 
 FileOptions::FileOptions( const FileOptions& copy ) :
@@ -69,6 +72,7 @@
     for (size_t i = 0; i < mOptions.size(); ++i)
       mOptions[i] = mData + (copy.mOptions[i] - copy.mData);
   }
+  mSeen = copy.mSeen;
 }
 
 FileOptions& FileOptions::operator=( const FileOptions& copy )
@@ -86,7 +90,8 @@
     for (size_t i = 0; i < mOptions.size(); ++i)
       mOptions[i] = mData + (copy.mOptions[i] - copy.mData);
   }
-    
+
+  mSeen = copy.mSeen;
   return *this;
 }
 
@@ -228,7 +233,8 @@
         // name must be either the null char or an equals symbol.
       if (*value == '=') 
         ++value;
-        
+      
+      mSeen[i - mOptions.begin()] = true;
       return MB_SUCCESS;
     }
   }
@@ -281,6 +287,25 @@
   std::copy( mOptions.begin(), mOptions.end(), list.begin() );
 }
 
+bool FileOptions::all_seen() const
+{
+  return std::find( mSeen.begin(), mSeen.end(), false ) == mSeen.end();
+}
+
+MBErrorCode FileOptions::get_unseen_option( std::string& name ) const
+{
+  std::vector<bool>::iterator i = std::find( mSeen.begin(), mSeen.end(), false );
+  if (i == mSeen.end()) {
+    name.clear();
+    return MB_ENTITY_NOT_FOUND;
+  }
+  
+  const char* opt = mOptions[i - mSeen.begin()];
+  const char* end = strchr( opt, '=' );
+  name = end ? std::string(opt, end-opt) : std::string(opt);
+  return MB_SUCCESS;
+}
+
 #ifdef TEST
 
 #include <iostream>
@@ -384,6 +409,12 @@
   rval = tool.get_str_option( "nul3", s );
   EQUAL( rval, MB_TYPE_OUT_OF_RANGE );
   
+    // We haven't looked at all of the options yet
+  EQUAL( false, tool.all_seen() );
+  rval = tool.get_unseen_option( s );
+  CHECK( rval );
+  EQUAL( s, "str3" );
+  
     // test options using generic get_option method
     
   rval = tool.get_option( "NUL3", s );
@@ -398,6 +429,10 @@
   unsigned l = tool.size();
   EQUAL( l, 12u );
   
+    // We requested every option
+  EQUAL( true, tool.all_seen() );
+  rval = tool.get_unseen_option( s );
+  EQUAL( MB_ENTITY_NOT_FOUND, rval );
   
     // test alternate separator
   
@@ -405,6 +440,12 @@
   l = tool2.size();
   EQUAL( l, 2 );
   
+    // We haven't looked at all of the options yet
+  EQUAL( false, tool2.all_seen() );
+  rval = tool2.get_unseen_option( s );
+  CHECK( rval );
+  EQUAL( s, "OPT1" );
+   
   rval = tool2.get_option( "opt1", s );
   CHECK( rval );
   EQUAL( s, "ABC" );
@@ -417,6 +458,11 @@
   l = tool2.size();
   EQUAL( l, 2 );
   
+    // We requested every option
+  EQUAL( true, tool2.all_seen() );
+  rval = tool2.get_unseen_option( s );
+  EQUAL( MB_ENTITY_NOT_FOUND, rval );
+  
     
     // test empty options string
     
@@ -425,18 +471,21 @@
   EQUAL( e, true );
   l = tool3.size();
   EQUAL( l, 0 );
+  EQUAL( true, tool3.all_seen() );
   
   FileOptions tool4(NULL);
   e = tool4.empty();
   EQUAL( e, true );
   l = tool4.size();
   EQUAL( l, 0 );
+  EQUAL( true, tool4.all_seen() );
   
   FileOptions tool5(";+");
   e = tool5.empty();
   EQUAL( e, true );
   l = tool5.size();
   EQUAL( l, 0 );
+  EQUAL( true, tool5.all_seen() );
   
     // test copy constructor
   

Modified: MOAB/trunk/FileOptions.hpp
===================================================================
--- MOAB/trunk/FileOptions.hpp	2009-11-03 18:44:52 UTC (rev 3272)
+++ MOAB/trunk/FileOptions.hpp	2009-11-03 19:06:17 UTC (rev 3273)
@@ -150,6 +150,12 @@
   /** Get list of options */
   void get_options( std::vector<std::string>& list ) const;
   
+  /** Check if all options have been looked at */
+  bool all_seen() const;
+  
+  /** Get first unseen option */
+  MBErrorCode get_unseen_option( std::string& value ) const;
+  
 private:
   
   /**\brief Check for option 
@@ -163,6 +169,7 @@
 
   char* mData;
   std::vector<const char*> mOptions;
+  mutable std::vector<bool> mSeen;
 
     /** Case-insensitive compare of name with option value. */
   static bool compare( const char* name, const char* option );

Modified: MOAB/trunk/MBCore.cpp
===================================================================
--- MOAB/trunk/MBCore.cpp	2009-11-03 18:44:52 UTC (rev 3272)
+++ MOAB/trunk/MBCore.cpp	2009-11-03 19:06:17 UTC (rev 3273)
@@ -395,9 +395,9 @@
     else if (rval != MB_ENTITY_NOT_FOUND) 
       return rval;
     if (set_tag_name && num_set_tag_vals) 
-      return ReadParallel(this,pcomm).load_file( file_name, file_set, opts, &t, 1 );
+      rval = ReadParallel(this,pcomm).load_file( file_name, file_set, opts, &t, 1 );
     else
-      return ReadParallel(this,pcomm).load_file( file_name, file_set, opts );
+      rval = ReadParallel(this,pcomm).load_file( file_name, file_set, opts );
 #else
     mError->set_last_error( "PARALLEL option not valid, this instance"
                             " compiled for serial execution.\n" );
@@ -406,10 +406,21 @@
   }
   else {
     if (set_tag_name && num_set_tag_vals) 
-      return serial_load_file( file_name, file_set, opts, &t, 1 );
+      rval = serial_load_file( file_name, file_set, opts, &t, 1 );
     else 
-      return serial_load_file( file_name, file_set, opts );
+      rval = serial_load_file( file_name, file_set, opts );
   }
+  
+  if (MB_SUCCESS == rval && !opts.all_seen()) {
+    std::string bad_opt;
+    if (MB_SUCCESS == opts.get_unseen_option( bad_opt ))
+      mError->set_last_error( "Unrecognized option: \"%s\"", bad_opt.c_str() );
+    else
+      mError->set_last_error( "Unrecognized option." );
+    rval = MB_FAILURE;
+  }
+  
+  return rval;
 }
 
 MBErrorCode MBCore::serial_load_file( const char* file_name,
@@ -588,6 +599,15 @@
                             tag_list, num_tags );
   delete writer;
   
+  if (MB_SUCCESS == rval && !opts.all_seen()) {
+    std::string bad_opt;
+    if (MB_SUCCESS == opts.get_unseen_option( bad_opt ))
+      mError->set_last_error( "Unrecognized option: \"%s\"", bad_opt.c_str() );
+    else
+      mError->set_last_error( "Unrecognized option." );
+    rval = MB_FAILURE;
+  }
+
   return rval;
 }
    

Modified: MOAB/trunk/tools/dagmc/test_geom.cc
===================================================================
--- MOAB/trunk/tools/dagmc/test_geom.cc	2009-11-03 18:44:52 UTC (rev 3272)
+++ MOAB/trunk/tools/dagmc/test_geom.cc	2009-11-03 19:06:17 UTC (rev 3273)
@@ -174,7 +174,9 @@
   }
   
   DagMC& dagmc = *DagMC::instance();
-  rval = dagmc.load_file( filename, 0 );
+  MBEntityHandle file_set;
+  rval = dagmc.moab_instance()->load_file( filename, file_set );
+  //rval = dagmc.load_file( filename, 0 );
   remove( filename );
   if (MB_SUCCESS != rval) {
     std::cerr << "Failed to load file." << std::endl;



More information about the moab-dev mailing list