E
easy
i need to read in parameter files of unknown length where the
parameters are given in pairs such as "key value" (without quotes). I
only recognize one pair per line and if I encounter a '#' I ignore
anything past the '#' and if an "end" I end the loop. This code works
fine but I want to apply it to other things.
I am new to C++ and I have the suspicion that there is a more elegant
way to do what I did. Before I use the same method in other places I'd
like to know how this could be improved and made "more correct" in
general, and specifically on how I searched for alphanums.
int ParameterFile_t::read( map<string,string>* ParameterTable )
{
string readout;
stromg alphanum =
"AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890_.,";
string read1;
string read2;
unsigned int start = 0;
unsigned int end = 0;
while( !parameterfile.eof() )
{
getline(parameterfile, readout);
unsigned int found;
if( (found = readout,find('#')) != string::npos )
readout.erase( found , readout.length() );
// get first string
if( (start = readout.find_first_of(alphanum )) == string::npos)
continue;
end = readout.find_first_not_of(alphanum,start);
read1 = readout.substr(start, end - start);
// is end?
if( read.compare("end") == 0 )
break;
// get second string
if( (start = readout.find_first_of(alphanum, end+1 )) ==
string::npos)
continue;
end = readout.find_first_not_of(alphanum,start);
read2 = readout.substr(start, end - start);
(*ParameterTable)[read1] = read2;
}
return 0;
}
Thanks, Chris.
and please forgive me if I'm in the wrong group.
parameters are given in pairs such as "key value" (without quotes). I
only recognize one pair per line and if I encounter a '#' I ignore
anything past the '#' and if an "end" I end the loop. This code works
fine but I want to apply it to other things.
I am new to C++ and I have the suspicion that there is a more elegant
way to do what I did. Before I use the same method in other places I'd
like to know how this could be improved and made "more correct" in
general, and specifically on how I searched for alphanums.
int ParameterFile_t::read( map<string,string>* ParameterTable )
{
string readout;
stromg alphanum =
"AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890_.,";
string read1;
string read2;
unsigned int start = 0;
unsigned int end = 0;
while( !parameterfile.eof() )
{
getline(parameterfile, readout);
unsigned int found;
if( (found = readout,find('#')) != string::npos )
readout.erase( found , readout.length() );
// get first string
if( (start = readout.find_first_of(alphanum )) == string::npos)
continue;
end = readout.find_first_not_of(alphanum,start);
read1 = readout.substr(start, end - start);
// is end?
if( read.compare("end") == 0 )
break;
// get second string
if( (start = readout.find_first_of(alphanum, end+1 )) ==
string::npos)
continue;
end = readout.find_first_not_of(alphanum,start);
read2 = readout.substr(start, end - start);
(*ParameterTable)[read1] = read2;
}
return 0;
}
Thanks, Chris.
and please forgive me if I'm in the wrong group.