473,324 Members | 2,246 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

reading para values from config file with type

Hi

I am working on a class that reads parameter values from a
configuration file
and inserts them into a hash. There are many good dimplementations of
this on
the Web. What I would like to do, however, is to automatically
determine the
type (int, double, or bool) of each value (similar to what MATLAB does)
and
insert them accordingly.

For example, if the config file has
VAL1 150
VAL2 0.005
I would like my hash to have one int and one double value,
rather than two strings

The current Read function below reads all pairs as strings. Problem
with this is
that each time I want to use the values in my program, I have to cast
them.

With my funstion template knowledge I was unable to write the code to
do this.
I would very much appreciate any help.

Thanks,

Cuneyt

class CSysConfig {
friend std::ostream & operator<<( std::ostream &lhs, const
CSysConfig &rhs );

private:
std::map<std::string,std::string> m_params; ///<
configuration parameters
std::string m_commentStartStr; ///< string
that marks a line as a comment
std::ifstream m_fileStream; ///< input stream
for config file

public:
CSysConfig();
virtual ~CSysConfig() {};

initialize( char* configFileName );
initialize( char* configFileName, std::string str );
int CSysConfig::isInitalized();
int CSysConfig::read( char* readType );
std::string getParamVal( std::string key );
};
///////////////////////////////////////////////////////////////////////////////
/// Reads all key,value pairs from given stream into hash data member.
///////////////////////////////////////////////////////////////////////////////
int CSysConfig::read( char* readType )
{
std::vector<std::string> line;
int nLine = 0, nTokens;

if( ! this->isInitalized() ) {
std::cerr << "read method called for uninitialized CSysConfig
object!" << std::endl;
return SYSCONFIG_READ_ERROR;
}

bool infer;
if( strcmp(readType,"all_string") == 0 ) {
infer = false;
}
else if ( strcmp(readType,"infer_types") == 0 ) {
infer = true;
}
else {
std::cerr << "Unknown read type (" << readType << ") in
CSysConfig::read!" << std::endl;
return SYSCONFIG_READ_ERROR;
}

while( !m_fileStream.eof() ) {
nLine++;
nTokens = getNextTokenizedLine( m_fileStream, line );
if( m_fileStream.eof() ) { break; }
if( nTokens > 0 ) { // line is not empty
// If the substring m_commentStartStr is found the first token
// starting from position 0, line is a comment.
if( line[0].find( m_commentStartStr,0 ) != 0 ) {
// line is not a comment
if( nTokens != 2 ) {
std::cout << "syntax error in line " << nLine << ", more than two
tokens in noncomment line! " << std::endl;
return SYSCONFIG_READ_ERROR;
}
else {
if( infer ) {
/* DETERMINING THE TYPE OF EACH PARAMETER VALUE
AND
INSERT IT TO THE HASH ACCORDINGLY */
}
else {
if( m_params.find( line[0] ) != m_params.end() ) {
std::cout << "repeated key (" << line[0] << ") in line " <<
nLine << std::endl;
return SYSCONFIG_READ_ERROR;
}
m_params[line[0]] = line[1];
}
}
}
else { // line is a comment, do nothing.
}
}
}
return SYSCONFIG_OK;
}

Dec 14 '05 #1
3 1666
With the assumption that you know how to seperate the two strings you
can do the following:
Value seperation:
if the number consist of numbers and a dot
it is float (or double)
else if the number consist only number
it is an int
else if the number (as string) equal 'true' or 'false'
it is bool, and assign the appropriate value

Once you have that the question is what do you want to do with this
information? how do you want to store?

Dec 14 '05 #2
cuneyt wrote:
Hi

I am working on a class that reads parameter values from a
configuration file
and inserts them into a hash. There are many good dimplementations of
this on
the Web. What I would like to do, however, is to automatically
determine the
type (int, double, or bool) of each value (similar to what MATLAB does)
and
insert them accordingly.


you can try to parse the value string and look for a full match. this
works because floating point numbers always include at least a trailing
dot that is not parsed if istream reads the preceding integer value.
this way you can also read scientific notation, hex, etc...

enum Type { TBool, TDouble, TLong, TInvalid };

Type parse(string val, bool& b, double& d, long& l)
{
// manually check for bool
if (val == "true") {
b = true;
return TBool;
}
else if (val == "false") {
b = false;
return TBool;
}

// check for long
istringstream in(str);
if (in >> l && in.eof())
return TLong;

// check for double
in.str(str);
if (in >> d && in.eof())
return TDouble;

return TInvalid;
}

-- peter

Dec 14 '05 #3
btw, you may also want to use a variant container like boost::variant
to be able to store the different types in a single container and
access them in a safe way.

Dec 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Paul C-T | last post by:
Hi, Am I trying to be too clever here? I am trying to write a PHP page to enable me to enter values into a form then write those values to a text file. I want to use the form & table that...
3
by: Eroc | last post by:
I'm new to XML files so I'm kinda lost here. I found some example code on reading an XML file. My objective is simple. Read the whole XML file into memory. Here is part of my code: Private...
1
by: MasterLalit | last post by:
Hi, i am using a app.config file in my console application. App.config code--> <configuration> <configSections> <section name="enterpriseLibrary.ConfigurationSource"...
4
ammoos
by: ammoos | last post by:
Hi Friends, I need to read data from an excel sheet. I am keeping this excel sheet in a remote machine. I am using OLEDB connection to read the data from this excel sheet. But when I am trying to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.