1 #ifndef DUNE_FEM_IO_PARAMETER_CONTAINER_HH
2 #define DUNE_FEM_IO_PARAMETER_CONTAINER_HH
15 #include <dune/grid/io/file/dgfparser/dgfparser.hh>
39 Value ( std::string v, std::string fn ) :
value( std::move( v ) ),
fileName( std::move( fn ) ) {}
46 const std::string *
operator() (
const std::string &key,
const std::string *defaultValue )
const;
48 static std::string
trim (
const std::string &s )
50 const std::size_t first = s.find_first_not_of(
" \t\n" );
51 return (first != s.npos ? s.substr( first, s.find_last_not_of(
" \t\n" ) + 1 - first ) : std::string());
54 std::string
resolveEscape (
const std::string &key, std::string &value )
const;
56 std::string
getShadowKey (
const std::string key,
const char delimter, std::string &value )
const;
62 mutable std::map< std::string, Value >
map;
79 static std::string stripComment (
const std::string &line );
81 const std::string &insert (
const std::string &key,
const std::string &value );
82 bool insert (
const std::string &s, std::queue< std::string > &includes );
84 void processFile (
const std::string &filename );
85 void processIncludes( std::queue< std::string > &includes );
102 void append (
int &argc,
char **argv );
109 void append (
const std::string &filename )
111 processFile( filename );
120 void append (
const std::string &key,
const std::string &value )
122 if( key !=
"paramfile" )
124 curFileName_ =
"program code";
125 insert( key, value );
139 void appendDGF (
const std::string &filename );
149 return getValue(
"fem.prefix.input", std::string(
"." ) );
154 return getValue(
"fem.prefix", std::string(
"." ) );
171 void write ( std::ostream &out,
bool writeAll =
true )
const;
174 std::string curFileName_;
186 explicit DGFBlock ( std::istream &in ) : BasicBlock( in,
"FemParameter" ) {}
189 std::string
getLine ()
const {
return line.str(); }
202 std::map< std::string, Value >::iterator pos;
205 auto info =
map.insert( std::make_pair( key,
Value( *defaultValue,
"default" ) ) );
207 std::cout <<
"Adding default: " << key <<
" = " << *defaultValue << std::endl;
211 pos =
map.find( key );
213 if( pos ==
map.end() )
215 Value &val = pos->second;
219 if( val.
hasDefault !=
static_cast< bool >( defaultValue ) )
220 DUNE_THROW(
ParameterInvalid,
"Parameter '" << key <<
"' used with and without default" );
221 if( defaultValue && (val.
defaultValue != *defaultValue) )
222 DUNE_THROW(
ParameterInvalid,
"Parameter '" << key <<
"' used with different default values" );
227 val.
hasDefault =
static_cast< bool >( defaultValue );
240 DUNE_THROW(
ParameterInvalid,
"Parameter '" << key <<
"' contains trailing '$'." );
242 const char escapedChar = value[ 0 ];
243 value.replace( 0, 1,
"" );
245 switch( escapedChar )
250 return std::string(
"" ) + escapedChar;
255 if( pos ==
map.end() )
258 return pos->second.value;
272 std::string &realValue = val.
value;
277 DUNE_THROW(
ParameterInvalid,
"Parameter '" << key <<
"' invalid, contains infinite loop" );
280 std::string realValueHelper;
281 realValue.swap( realValueHelper );
283 while( !realValueHelper.empty() )
285 std::size_t startPoint = realValueHelper.find_first_of(
'$' );
286 realValue += realValueHelper.substr( 0, startPoint );
288 if( startPoint == std::string::npos )
291 realValueHelper.replace( 0, startPoint+1,
"" );
301 std::string shadowKey;
305 std::size_t startPoint = value.find_first_of( std::string(
"$" ) + delimiter );
307 if( startPoint == std::string::npos )
310 shadowKey += value.substr( 0, startPoint );
311 const char startChar = value[ startPoint ];
313 value.replace( 0, startPoint+1,
"" );
315 if( startChar == delimiter )
317 assert( startChar ==
'$' );
328 inline const std::string &ParameterContainer::insert (
const std::string &key,
const std::string &value )
330 auto info =
parameter_.
map.insert( std::make_pair( key, Value( value, curFileName_ ) ) );
331 Value &val =
info.first->second;
332 if( key ==
"fem.verboserank" )
336 std::cout <<
"Warning: Parameter 'fem.verboserank' is neither a " <<
"valid rank nor -1." << std::endl;
341 std::cout << curFileName_ <<
"[" << curLineNumber_ <<
"]: ";
343 std::cout <<
"Adding " << key <<
" = " << value << std::endl;
345 std::cout <<
"Ignored " << key <<
" = " << value <<
", using " << val.value << std::endl;
352 inline std::string ParameterContainer::stripComment (
const std::string &line )
354 std::size_t size = line.size();
355 std::size_t end = line.find_first_of (
"%#$" );
357 while( (end != std::string::npos) && (line[end] ==
'$') )
360 end = line.find_first_of (
"%#$", end+2 );
362 end = std::string::npos;
369 inline bool ParameterContainer::insert (
const std::string &s, std::queue< std::string > &includes )
371 const std::size_t size = s.size();
373 std::size_t key_start = 0;
374 for( ; key_start < size; ++key_start )
376 if( (s[ key_start ] !=
' ') && (s[ key_start ] !=
'\t') )
380 std::size_t key_end = key_start;
381 for( ; key_end < size; ++key_end )
383 const char &c = s[ key_end ];
384 if( (c ==
' ') || (c ==
'\t') || (c ==
':') )
388 std::size_t value_start = key_end;
389 for( ; value_start < size ; ++value_start )
391 if( s[ value_start ] ==
':' )
396 for( ; value_start < size; ++value_start )
398 if( (s[ value_start ] !=
' ') && (s[ value_start ] !=
'\t') )
402 std::size_t value_end = value_start;
403 for( std::size_t i = 0; i < size; ++i )
405 if( (s[ i ] !=
' ') && (s[ i ] !=
'\t') )
409 if( value_start >= size )
412 std::string key = s.substr( key_start, key_end - key_start );
413 std::string value = s.substr( value_start, value_end - value_start );
415 if( key ==
"paramfile" )
417 else if( key ==
"deprecated" )
420 insert( key, value );
425 inline void ParameterContainer::processFile (
const std::string &filename )
428 std::cout <<
"Parameter: Processing '" << filename <<
"'..." << std::endl;
430 std::ifstream file( filename );
431 if( !file.is_open() )
433 std::cerr <<
"Warning: Unable to read parameter file '" << filename <<
"'" << std::endl;
437 curFileName_ = filename;
439 std::queue< std::string > includes;
444 std::getline( file, line );
446 line = stripComment( line );
448 insert( line, includes );
452 processIncludes( includes );
456 inline void ParameterContainer::processIncludes( std::queue< std::string > &includes )
458 while( !includes.empty() )
461 val.
value = includes.front();
464 processFile( val.value );
471 std::queue< std::string > includes;
472 curFileName_ =
"program arguments";
474 for(
int i = 1 ; i < argc; ++i )
477 if( !insert( std::string( argv[ i ] ), includes ) )
480 std::copy( argv + (i+1), argv + argc, argv + i );
485 processIncludes( includes );
492 std::cout <<
"Parameter: Processing DGF '" << filename <<
"'..." << std::endl;
494 std::ifstream file( filename );
495 if( !file.is_open() )
497 std::cerr <<
"Warning: Unable to read DGF file '" << filename <<
"'" << std::endl;
501 if( !DuneGridFormatParser::isDuneGridFormat( file ) )
505 if( !block.isactive() )
508 curFileName_ = filename;
510 std::queue< std::string > includes;
515 const std::string line = stripComment( block.
getLine() );
517 insert( line, includes );
520 processIncludes( includes );
526 std::map< std::string, std::map<std::string, std::string> > writeMap;
529 const Value &val = param.second;
534 for(
const auto &source : writeMap )
536 out <<
"# from " << source.first << std::endl;
537 for(
const auto ¶m : source.second )
538 out << param.first <<
": " << param.second << std::endl;
Definition: bindguard.hh:11
std::string executeCommand(const std::string &command)
executes a command and return the output
Definition: io.cc:70
BasicParameterReader< std::function< const std::string *(const std::string &, const std::string *) > > ParameterReader
Definition: reader.hh:264
Definition: container.hh:32
void resolveShadows(const std::string &key, Value &val) const
Definition: container.hh:270
std::string getShadowKey(const std::string key, const char delimter, std::string &value) const
Definition: container.hh:299
static std::string trim(const std::string &s)
Definition: container.hh:48
int verboseRank
Definition: container.hh:64
const std::string * operator()(const std::string &key, const std::string *defaultValue) const
Definition: container.hh:197
std::string resolveEscape(const std::string &key, std::string &value) const
Definition: container.hh:237
bool verbose() const
Definition: container.hh:58
std::map< std::string, Value > map
Definition: container.hh:62
std::set< std::string > deprecated
Definition: container.hh:63
Definition: container.hh:34
std::string value
Definition: container.hh:41
ShadowStatus
Definition: container.hh:35
@ resolved
Definition: container.hh:35
@ resolving
Definition: container.hh:35
@ unresolved
Definition: container.hh:35
bool hasDefault
Definition: container.hh:42
Value(std::string v, std::string fn)
Definition: container.hh:39
std::string defaultValue
Definition: container.hh:41
ShadowStatus shadowStatus
Definition: container.hh:43
bool used
Definition: container.hh:42
std::string fileName
Definition: container.hh:41
Definition: container.hh:74
std::string commonInputPath() const
Definition: container.hh:147
void append(const std::string &key, const std::string &value)
add a single parameter to the container
Definition: container.hh:120
void clear()
clear all parameters
Definition: container.hh:142
void append(const std::string &filename)
add parameters from a file
Definition: container.hh:109
void write(std::ostream &out, bool writeAll=true) const
write the parameter database to a stream
Definition: container.hh:524
bool verbose() const
obtain the cached value for fem.verbose
Definition: container.hh:145
void append(int &argc, char **argv)
add parameters from the command line
Definition: container.hh:469
void appendDGF(const std::string &filename)
add parameters from a DGF file
Definition: container.hh:489
std::string commonOutputPath() const
Definition: container.hh:152
Definition: container.hh:185
bool advance()
Definition: container.hh:188
DGFBlock(std::istream &in)
Definition: container.hh:186
std::string getLine() const
Definition: container.hh:189
Definition: io/parameter/exceptions.hh:17
Definition: io/parameter/exceptions.hh:26
static bool parse(const std::string &s, T &value)
Definition: parser.hh:22
T getValue(const std::string &key) const
get mandatory parameter
Definition: reader.hh:149
ParameterContainerData parameter_
Definition: reader.hh:256
Definition: grcommon.hh:31
static int size()
Definition: mpimanager.hh:110
static int rank()
Definition: mpimanager.hh:105