Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
cleanTags.cpp
Go to the documentation of this file.
1 /*
2  * cleanTags.cpp
3  * this tool will remove a list of tags from a file, or keep only the tags from a given list
4  * will use just the names of the tags
5  *
6  * example of usage:
7  * ./mbcleantags -i input_file.h5m -o outfile.h5m -d list_tags_separated_by':' -k list_tags separated by ':'
8  *
9  */
10 #include "moab/MOABConfig.h"
11 
12 #include "moab/ProgOptions.hpp"
13 #include "moab/Core.hpp"
14 
15 using namespace moab;
16 using namespace std;
17 
18 vector< string > split( const string& i_str, const string& i_delim )
19 {
20  vector< string > result;
21 
22  size_t found = i_str.find( i_delim );
23  size_t startIndex = 0;
24 
25  while( found != string::npos )
26  {
27  result.push_back( string( i_str.begin() + startIndex, i_str.begin() + found ) );
28  startIndex = found + i_delim.size();
29  found = i_str.find( i_delim, startIndex );
30  }
31  if( startIndex != i_str.size() ) result.push_back( string( i_str.begin() + startIndex, i_str.end() ) );
32  return result;
33 }
34 
35 int main( int argc, char* argv[] )
36 {
37 
38  ProgOptions opts;
39 
40  string inputfile, outputfile, deleteTags, keepTags;
41  opts.addOpt< string >( "input,i", "input filename ", &inputfile );
42  opts.addOpt< string >( "output,o", "output file", &outputfile );
43 
44  opts.addOpt< string >( "deleteTags,d", "delete tags ", &deleteTags );
45  opts.addOpt< string >( "keepTags,k", "keep tags ", &keepTags );
46  opts.parseCommandLine( argc, argv );
47 
48  Core core;
49  Interface* mb = &core;
50  ErrorCode rval;
51  rval = mb->load_file( inputfile.c_str() );MB_CHK_ERR( rval );
52  vector< Tag > existingTags;
53  rval = mb->tag_get_tags( existingTags );MB_CHK_ERR( rval );
54  vector< string > tagsToDelete;
55  if( !keepTags.empty() )
56  {
57  vector< string > tagsToKeep = split( keepTags, string( ":" ) );
58  for( size_t i = 0; i < existingTags.size(); i++ )
59  {
60  string tname;
61  rval = mb->tag_get_name( existingTags[i], tname );MB_CHK_ERR( rval );
62  bool deleteTag = false;
63  for( size_t k = 0; k < tagsToKeep.size() && !deleteTag; k++ )
64  {
65  if( tname.compare( tagsToKeep[k] ) == 0 ) deleteTag = true;
66  }
67  if( !deleteTag ) tagsToDelete.push_back( tname );
68  }
69  }
70  if( !deleteTags.empty() )
71  {
72  tagsToDelete = split( deleteTags, string( ":" ) );
73  }
74  for( size_t i = 0; i < tagsToDelete.size(); i++ )
75  {
76  Tag tag;
77  rval = mb->tag_get_handle( tagsToDelete[i].c_str(), tag );
78  if( rval == MB_SUCCESS && tag != NULL )
79  {
80  rval = mb->tag_delete( tag );MB_CHK_ERR( rval );
81  }
82  }
83  cout << "write file " << outputfile << endl;
84  rval = mb->write_file( outputfile.c_str() );MB_CHK_ERR( rval );
85 
86  return 0;
87 }