Hello everyone.
This is my first thread here but that's not important. The real problem is my member function of cGame class which load config files from the same folder where the .exe files is. It works but the statements must hardcoded. Here is what I mean (behold the chaos):
void cGame::FindOption(std::string wantedTag, std::string wantedOption, std::ifstream *InputStream)
{
//It works somehow
char buffer[10];
std::string tagName;
std::string optionName;
bool success = false;
bool tagFound = false;
while (!(InputStream->eof()) && (success == false)) // Keeps doing it's stuff untill EOF
{
while (buffer[0] != '[' && tagFound == false && !(InputStream->eof())) // looks for a tag eg. [TAG]
InputStream->get(buffer[0]);
if (buffer[0] == '[') // if [ sign found then it gets the word between []
{
InputStream->get(buffer[0]);
while (buffer[0] != ']')
{
tagName += buffer[0]; // letter by letter T - A - G
InputStream->get(buffer[0]);
}
}
if (tagName == wantedTag) // if the tag matches the wanted tag it begins to search for option names
{
tagFound = true; // also sets the tagFound to success if the tag matches
InputStream->get(buffer[0]); // skips to next line
while (InputStream->get(buffer[0]) && buffer[0] != '=' && buffer[0] != ' ' && optionName != wantedOption && !(InputStream->eof())) // begins to get words untill wantedoption found
{
if (buffer[0] != '\n') // if not a new line then ...
optionName += buffer[0]; // letter by letter again :D it works...
else if (buffer[0] == '\n') // new lines mean that a word hasn't been found in the current line...
optionName = ""; // so we clear the buffer
}
if (optionName == wantedOption) // checks for possible matches // TODO: find a way to make a template for this chaos ( so it doesn't have to be hardcoded this way : / )
{
if (wantedOption == "WINDOWED")
{
InputStream->get(buffer[0]); //
InputStream->get(buffer[0]); // skips " = "
bool get;
*InputStream >> get;
GraphicsManager->SetWindowed(get);
std::cout << wantedOption << " = " << get << std::endl;
}
else if (wantedOption == "VSYNC")
{
InputStream->get(buffer[0]);
InputStream->get(buffer[0]);
bool get;
*InputStream >> get;
GraphicsManager->SetVsync(get);
std::cout << wantedOption << " = " << get << std::endl;
}
else if (wantedOption == "SCREEN_WIDTH")
{
InputStream->get(buffer[0]);
InputStream->get(buffer[0]);
int get;
*InputStream >> get;
GraphicsManager->SetWidth(get);
std::cout << wantedOption << " = " << get << std::endl;
}
else if (wantedOption == "SCREEN_HEIGHT")
{
InputStream->get(buffer[0]);
InputStream->get(buffer[0]);
int get;
*InputStream >> get;
GraphicsManager->SetHeight(get);
std::cout << wantedOption << " = " << get << std::endl;
}
else if (wantedOption == "FREQUENCY")
{
InputStream->get(buffer[0]);
InputStream->get(buffer[0]);
int get;
*InputStream >> get;
SoundManager->SetFrequency(get);
std::cout << wantedOption << " = " << get << std::endl;
}
else if (wantedOption == "FORMAT")
{
InputStream->get(buffer[0]);
InputStream->get(buffer[0]);
Uint16 get;
*InputStream >> get;
SoundManager->SetFormat(get);
std::cout << wantedOption << " = " << get << std::endl;
}
else if (wantedOption == "CHANNELS")
{
InputStream->get(buffer[0]);
InputStream->get(buffer[0]);
int get;
*InputStream >> get;
SoundManager->SetChannels(get);
std::cout << wantedOption << " = " << get << std::endl;
}
else if (wantedOption == "CHUNK_SIZE")
{
InputStream->get(buffer[0]);
InputStream->get(buffer[0]);
int get;
*InputStream >> get;
SoundManager->SetChunkSize(get);
std::cout << wantedOption << " = " << get << std::endl;
}
success = true; // if optionName matches wantedName it sets success to true, meaning that we can end this loop
}
else
optionName = ""; // if option hasn't been found, check another line
}
else
tagName = ""; // if tag hasn't been found, check another line
}
InputStream->seekg(0, std::ios::beg); // if we're done with this TAG - OPTION set we shall return the bricks to original state ( get to the beginning of the file )
}
void cGame::LoadSettings()
{
std::ifstream Input; // creates our input variable
Input.open("game.cfg"); // open cfg file
if (!(Input.good())) // this checks of the file has been opened and if not it creates a new cfg file with default variables from cGraphicsManager.h and cSoundManager.h
{
std::cout << "File doesn't exist. Creating new CFG file.\n";
Input.close(); // we don't need input now
std::ofstream Output;
Output.open("game.cfg");
SDL_Delay(2000);
GraphicsManager->SetWindowed();
GraphicsManager->SetVsync();
GraphicsManager->SetWidth();
GraphicsManager->SetHeight();
SoundManager->SetFrequency();
SoundManager->SetFormat();
SoundManager->SetChannels();
SoundManager->SetChunkSize();
Output << "[GRAPHICS]\n";
Output << "WINDOWED = " << GraphicsManager->GetWindowed() << "\n";
Output << "VSYNC = " << GraphicsManager->GetVsync() << "\n";
Output << "SCREEN_WIDTH = " << GraphicsManager->GetWidth() << "\n";
Output << "SCREEN_HEIGHT = " << GraphicsManager->GetHeight() << "\n";
Output << "[SOUND]\n";
Output << "FREQUENCY = " << SoundManager->GetFrequency() << '\n';
Output << "FORMAT = " << SoundManager->GetFormat() << '\n';
Output << "CHANNELS = " << SoundManager->GetChannels() << '\n';
Output << "CHUNNK_SIZE = " << SoundManager->GetChunkSize() << '\n';
Output.close();
}
else if (Input.good()) // if file has been found then it begins to load the settings
{
std::cout << "CFG file found. Loading settings.\n";
std::cout << "GRAPHICS: \n";
FindOption("GRAPHICS", "WINDOWED", &Input);
FindOption("GRAPHICS", "SCREEN_WIDTH", &Input);
FindOption("GRAPHICS", "SCREEN_HEIGHT", &Input);
FindOption("GRAPHICS", "VSYNC", &Input);
std::cout << "\nSOUND: \n";
FindOption("SOUND", "FREQUENCY", &Input);
FindOption("SOUND", "FORMAT", &Input);
FindOption("SOUND", "CHANNELS", &Input);
FindOption("SOUND", "CHUNK_SIZE", &Input);
Input.close();
}
std::cout << "CFG file loaded.\n\n";
}
Is there any way to shorten this example:
if (optionName == wantedOption)
{
if (wantedOption == "WINDOWED")
{
InputStream->get(buffer[0]); //
InputStream->get(buffer[0]); // skips " = "
bool get;
*InputStream >> get;
GraphicsManager->SetWindowed(get);
std::cout << wantedOption << " = " << get << std::endl;
}
}
into something like this:
if (optionName == wantedOption)
{
InputStream->get(buffer[0]); //
InputStream->get(buffer[0]); // skips " = "
type get; // type declared in function call or template?
*InputStream >> Function() // function passed by pointer as an argument
}
So instead of making dozens of new cases I could call one which will automaticaly detect what type of variable I'd like to get from the file and pass it to a function (function pointer) that might be my FindOption function parameter.
Here's function prototype that I thought about but after all it didn't make any sense to me:
void cGame::FindOption(std::string wantedTag, std::string wantedOption, std::ifstream *InputStream, type (*function)(some type));
Function call:
FindOption("SOUND", "CHANNELS", &Input, SoundManager->SetChannels(type must be integer));
Normally I'd just keep on adding new statements until I'd end up with 30 of them and a vacation in a hospital with mental disorder but I'd like make this project clear for future developement and not waste my time in any kind of hospital.
Thanks in advance.