MOAB: Mesh Oriented datABase
(version 5.4.1)
|
00001 /* 00002 * cleanTags.cpp 00003 * this tool will remove a list of tags from a file, or keep only the tags from a given list 00004 * will use just the names of the tags 00005 * 00006 * example of usage: 00007 * ./mbcleantags -i input_file.h5m -o outfile.h5m -d list_tags_separated_by':' -k list_tags separated by ':' 00008 * 00009 */ 00010 #include "moab/MOABConfig.h" 00011 00012 #include "moab/ProgOptions.hpp" 00013 #include "moab/Core.hpp" 00014 00015 using namespace moab; 00016 using namespace std; 00017 00018 vector< string > split( const string& i_str, const string& i_delim ) 00019 { 00020 vector< string > result; 00021 00022 size_t found = i_str.find( i_delim ); 00023 size_t startIndex = 0; 00024 00025 while( found != string::npos ) 00026 { 00027 result.push_back( string( i_str.begin() + startIndex, i_str.begin() + found ) ); 00028 startIndex = found + i_delim.size(); 00029 found = i_str.find( i_delim, startIndex ); 00030 } 00031 if( startIndex != i_str.size() ) result.push_back( string( i_str.begin() + startIndex, i_str.end() ) ); 00032 return result; 00033 } 00034 00035 int main( int argc, char* argv[] ) 00036 { 00037 00038 ProgOptions opts; 00039 00040 string inputfile, outputfile, deleteTags, keepTags; 00041 opts.addOpt< string >( "input,i", "input filename ", &inputfile ); 00042 opts.addOpt< string >( "output,o", "output file", &outputfile ); 00043 00044 opts.addOpt< string >( "deleteTags,d", "delete tags ", &deleteTags ); 00045 opts.addOpt< string >( "keepTags,k", "keep tags ", &keepTags ); 00046 opts.parseCommandLine( argc, argv ); 00047 00048 Core core; 00049 Interface* mb = &core; 00050 ErrorCode rval; 00051 rval = mb->load_file( inputfile.c_str() );MB_CHK_ERR( rval ); 00052 vector< Tag > existingTags; 00053 rval = mb->tag_get_tags( existingTags );MB_CHK_ERR( rval ); 00054 vector< string > tagsToDelete; 00055 if( !keepTags.empty() ) 00056 { 00057 vector< string > tagsToKeep = split( keepTags, string( ":" ) ); 00058 for( size_t i = 0; i < existingTags.size(); i++ ) 00059 { 00060 string tname; 00061 rval = mb->tag_get_name( existingTags[i], tname );MB_CHK_ERR( rval ); 00062 bool deleteTag = false; 00063 for( size_t k = 0; k < tagsToKeep.size() && !deleteTag; k++ ) 00064 { 00065 if( tname.compare( tagsToKeep[k] ) == 0 ) deleteTag = true; 00066 } 00067 if( !deleteTag ) tagsToDelete.push_back( tname ); 00068 } 00069 } 00070 if( !deleteTags.empty() ) 00071 { 00072 tagsToDelete = split( deleteTags, string( ":" ) ); 00073 } 00074 for( size_t i = 0; i < tagsToDelete.size(); i++ ) 00075 { 00076 Tag tag; 00077 rval = mb->tag_get_handle( tagsToDelete[i].c_str(), tag ); 00078 if( rval == MB_SUCCESS && tag != NULL ) 00079 { 00080 rval = mb->tag_delete( tag );MB_CHK_ERR( rval ); 00081 } 00082 } 00083 cout << "write file " << outputfile << endl; 00084 rval = mb->write_file( outputfile.c_str() );MB_CHK_ERR( rval ); 00085 00086 return 0; 00087 }