I haven't done anything similar so I got nothing to go on. The point was to load everything from the file and I thought a struct
would be a good choice to be the handler to store the information.
The loading part is, however, not really something that is to my satisfaction as I find it very primitive and illusive. I try to walk around issues as I don't really know how to handle them if anything goes awry.
//Set Config Value from File
bool ConfigValue(std::string contents, std::string setting, unsigned int& MySettings){
int pos1 = NULL, pos2 = NULL;
pos1 = contents.find(setting);
pos2 = contents.find('\n', pos1);
if (pos1 == -1)
return false;
else
pos1 += setting.length();
contents = contents.substr(pos1, pos2 - pos1);
for (int i = 0; i < contents.length();i++){
if (!(contents[i] >= '0' && contents[i] <= '9' || contents[i] == ' '))
return false;
}
MySettings = std::stoi(contents);
return true;
}
//Reads ConfigFile with some safe measures if it's wrongly written for some reason
Config ConfigFile(){
start:
video::E_DRIVER_TYPE renderer = video::EDT_DIRECT3D9;
//Set Default if file doesn't contain them.
Config MySettings;
std::string line;
fstream input("config.ini", fstream::in);
if (input){
//Read config file to buffer
std::stringstream buffer;
buffer << input.rdbuf();
std::string contents(buffer.str());
if (!ConfigValue(contents, "width=", MySettings.Width) || //Set Width
!ConfigValue(contents, "height=", MySettings.Height) || //Set Height
!ConfigValue(contents, "fullscreen=", MySettings.Fullscreen) || //Set Fullscreen
!ConfigValue(contents, "renderer=", MySettings.renderer)) //Gets renderer value
goto makeDefault;
//Set renderer according to value obtained
switch (MySettings.renderer){
case 0: MySettings.renderer = video::EDT_DIRECT3D8;
break;
case 1: MySettings.renderer = video::EDT_DIRECT3D9;
break;
case 2: MySettings.renderer = video::EDT_OPENGL;
break;
default: goto makeDefault;
}
}
else{
makeDefault:
input.close();
input.open("config.ini", fstream::out);
input << "//renderer options - 0: D3D8, 1: D3D9, 2: OpenGL\nwidth=640\nheight=480\nfullscreen=0\nrenderer=1"; //Set Default Resolution;
goto start;
}
return MySettings;
}
Config
:
struct Config{
unsigned int renderer;
unsigned int Width;
unsigned int Height;
unsigned int Fullscreen; //should be bool
};
As you can see, I use goto
a lot as a kind of safety net. If anything ever goes awry (in my detection), I reset everything. However, the tests I do to make sure it's correct is something I would like reviewed.
The config in its current state is supposed to contain this:
//renderer options - 0: D3D8, 1: D3D9, 2: OpenGL
width=640
height=480
fullscreen=0
renderer=1
The first line is just an instruction for the renderer as it's kinda hard to guess it. Nothing else needs any instructions so they are normal. It's always supposed to be "WhatItIs=Value". (Worth noting is that Fullscreen
is a bool, as 0 and 1 works the same I see no reason to mess with conversion).
Any suggestion on this is wanted as I really find pretty much all of this code to be extremely bad even though it works. But as I got nothing to compare to it, I can only guess.