Below are the files that I have. Most of everything below ConfigINI.cpp was given and have minimal edits by me (ConfigINI.cpp, configINI.h and ConfigStuff.cpp have the edits, everything else is as it was given to me).
The program should run (no arguments), read in an INI file [simulator.ini -- not included], display two values from the ini file, make two changes, and then 'edit' or write out the ini file. This is to be done with pointers and arrays.
I'm not exactly sure what to do, nor what the pertinent information is when I search google. I have quite a bit figured out, but my issue holding me up right now is assigning the value of the parameter to the pointer in ConfigINI::Set() so that main can print the value out and make the change.
#include "ConfigINI.h"
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
usingnamespace std;
ConfigINI::ConfigINI(): numberOfComments(0), numberOfParameters(0) {}
ConfigINI::ConfigINI(constchar* filename)
: numberOfComments (0),
numberOfParameters(0)
{
Load(filename);
}
ConfigINI::~ConfigINI() {}
void ConfigINI::Load(constchar* name)
{
// Store filename for later Save()
filename = name;
// Read each line of text in the ini file
// and store the Parameter/Value pairs in the arrays
// If you find a comment, store it separately
/* Variables */
string readLine;
string paramName, paramValue, comment;
size_t found_eq, found_lb;
/* Code */
// set file for reading
std::ifstream inFile;
// open file, use parameters
inFile.open (filename.c_str(), ifstream::in);
// set counters to zero
numberOfComments = 0;
numberOfParameters = 0;
// Loop through file, read into memory
while (inFile)
{
getline(inFile, readLine);
// Check to see if line is a comment
if ( readLine[0] == '#' )
{
comment = readLine;
// std::cout << "This is a comment --> " << comment << std::endl;
comments[numberOfComments]=comment;
numberOfComments++;
}
// Check to see if line is a parameter
found_eq = readLine.find('=');
found_lb = readLine.find('#');
if ( found_eq != string::npos )
{
// Get first part and second part based on the equal sign
paramName = readLine.substr( 0,readLine.find("=") );
paramValue = readLine.substr( readLine.find("=")+1 );
// std::cout << "Parameter: " << paramName << std::endl;
// std::cout << "Value: " << paramValue << std::endl;
iniParameter[numberOfParameters] = paramName;
iniValue[numberOfParameters] = paramValue;
numberOfParameters++;
}
// print out results -- for testing purposes
// std::cout << readLine << std::endl;
}
// close file from reading
inFile.close();
}
void ConfigINI::Save()
{
/* Variables */
int i;
/* Code */
// set file for reading
std::ofstream outFile;
// open file, use parameters
outFile.open (filename.c_str(), ofstream::out);
// write to file
for (i = 0; i < numberOfComments; i++)
{
outFile << comments[i] << std::endl;
}
for (i = 0; i < numberOfParameters; i++)
{
outFile << iniParameter[i]
<< "=" << iniValue[i] << std::endl;
}
// close file for writing
outFile.close();
}
void ConfigINI::Set(constchar* inistring,int* value)
{
/* variables */
int i;
int pValue;
// loop through to find inistring in iniParameter
for (i=0; i < numberOfParameters; i++)
{
if (inistring == iniParameter[i])
{
stringstream convert(iniValue[i]);
convert >> pValue
*value = &pValue;
break;
}
}
std::cout << arrParams[i] << " " << inistring << std::endl;
std::cout << value << " " << arrValues[i] << " " << &iniValue[i] << std::endl;
}
#ifndef _CONFIGINI__H_
#define _CONFIGINI__H_
#include <string>
#define MAX_INI_PARAMETERS 100
#define MAX_COMMENTS 20
using std::string;
using std::ifstream;
using std::ofstream;
class ConfigINI
{
public:
ConfigINI();
ConfigINI(constchar* filename);
~ConfigINI();
void Load(constchar* filename);
void Save();
void Set(constchar* inistring,string* value);
void Set(constchar* inistring,int* value);
void Set(constchar* inistring,float* value);
private:
string filename; // store the file name for the Save()
// We have a requirement to preserve comments
string comments[MAX_COMMENTS];
int numberOfComments; // don't run past the end of the array!!!
// student must create storage for all data here
string* arrValues[MAX_INI_PARAMETERS];
string* arrParams[MAX_INI_PARAMETERS];
// Note: these 2 arrays are the ini file
// broken out into their component parts
string iniParameter[MAX_INI_PARAMETERS];
string iniValue [MAX_INI_PARAMETERS];
int numberOfParameters;
};
#endif
#include "ConfigStuff.h"
ConfigStuff::ConfigStuff() {}
ConfigStuff::ConfigStuff(constchar* filename)
{
// This one will call "ConfigStuff::Load()"
Load(filename);
}
ConfigStuff::~ConfigStuff()
{
}
void ConfigStuff::Load(constchar* filename)
{
// Execute this to invoke the Base Class
// "Load()" function to get the config file
// loaded into the Parameter/Value pairs
ConfigINI::Load(filename);
// Put a series of "Set(const char* inistring,...)"
// calls here to load each of the configuration values
// and the pointers to those values
Set("srcPort", &srcPort);
Set("delayTime", &delayTime);
Set("defaultPortSpeed", &defaultPortSpeed);
}
#include <iostream>
#include "ConfigStuff.h"
usingnamespace std;
int main()
{
ConfigStuff config;
try
{
config.Load("simulator.ini");
// When you run this and the
// srcPort and delayTime are
// not initialized they will
// display undefined values
cout << config.srcPort << endl;
cout << config.delayTime << endl;
config.delayTime = 10;
config.defaultPortSpeed = 2400;
config.Save();
}
catch(constchar* s)
{
cerr << s << endl;
}
return 0;
}
void ConfigINI::Set(constchar* inistring,int* value) //value is a pointer, why not call it pValue
{
int i;
int pValue; //This is not a pointer, this name is misleading
//...
{
if (inistring == iniParameter[i]) //This is doing a character comparison (if what you are comparing is a string use strcmp(inistring, iniParameter[i]) == 0
{
stringstream convert(iniValue[i]);
convert >> pValue
*value = pValue; //See my changes here... removed &
break;
}
}
//...
}
The function ConfigINI::Set( ... ) itself was what I was given. I had no hand in writing that.
At the time I posted, I was frustrated and just trying anything at that point, thus the variable name pValue -- which in my mind was Parameter Value, just something that I could convert from a string to an integer so that I could assign back to 'int* value'.
So - it seems to me I was on a basic correct track, just a little off on execution? Or am I still totally f'ed on this.
However thank you for the hint about strcmp -- I'll look that up and make sure I understand what it is doing.
I'll work on this some more and post more results as I get stuck, but I appreciate your taking the time to look at it.
I had been missing that - I was sure I had tried the cianmjc's solution, but tried it again -- and then I caught it. Grrrrr -- that's what I get for being tired and trying to code.