Skip to content
Snippets Groups Projects
Commit 1b7b0705 authored by Robert Klöfkorn's avatar Robert Klöfkorn
Browse files

function readParameter parses ascii file for keywords and reads paramters.

[[Imported from SVN: r1256]]
parent 6a0b02a4
No related branches found
No related tags found
No related merge requests found
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef __DUNE_ASCIIPARSER_HH__
#define __DUNE_ASCIIPARSER_HH__
namespace Dune {
static const int MAXTAB = 30;
//! reads data folowing the given keyword
//! if verbose is true then an output of what was read is given
//! the token '%' stands for comment
template <class T>
bool readParameter (const char * filename,
const char keywd[], T & data, bool verbose = true)
{
std::fstream file (filename,std::ios::in);
std::basic_string<char> keyword ( keywd );
bool readData = false;
char ch[1024];
while (! file.eof() )
{
std::basic_string <char> keyHelp;
file >> keyHelp;
if(keyHelp[0] == '%')
{
file.getline(ch,1024,'\n');
}
else
{
// copy only keyword size and compare
int pos = keyHelp.find_first_of(':');
int pos1 = keyHelp.find_first_of(' ');
if (pos > 0)
{
if(pos1 > 0)
pos -= pos1;
else
pos = 1;
}
else
pos = 0;
std::basic_string <char> key (keyHelp,0,keyHelp.size() - pos);
if(key == keyword)
{
file >> data;
readData = true;
break;
}
}
}
file.close();
if(readData)
{
if(verbose)
{
int length = MAXTAB - keyword.size();
std::cout << "Reading " << keyword;
for(int i=0; i<length; i++) std::cout << ".";
std::cout << " " << data << "\n";;
}
}
else
{
std::cerr << "WARNING: couldn't read " << keyword << "\n";
}
return readData;
}
} // end namespace
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment