ファイルから値を読み取り、それに応じて設定するより良い方法はありますか? どうにかしてファイルから変数名を読み取り、それに応じてプログラムに設定できますか? たとえば、BitDepth を読み取り、ファイルに記載されている値に設定しますか? したがって、この行が BitDepth であるかどうかを確認してから、bit_depth を次の値に設定する必要はありませんか?
std::ifstream config (exec_path + "/Data/config.ini");
if (!config.good ()) SetStatus (EXIT_FAILURE);
while (!config.eof ()) {
std::string tmp;
std::getline (config, tmp);
if (tmp.find ("=") != 1) {
if (!tmp.substr (0, tmp.find (" ")).compare ("BitDepth")) {
tmp = tmp.substr (tmp.find_last_of (" ") + 1, tmp.length ());
bit_depth = atoi (tmp.c_str ());
} else if (!tmp.substr (0, tmp.find (" ")).compare ("WindowWidth")) {
tmp = tmp.substr (tmp.find_last_of (" ") + 1, tmp.length ());
screen_width = atoi (tmp.c_str ());
} else if (!tmp.substr (0, tmp.find (" ")).compare ("WindowHeight")) {
tmp = tmp.substr (tmp.find_last_of (" ") + 1, tmp.length ());
screen_height = atoi (tmp.c_str ());
}
}
}
config.ini
[Display]
BitDepth = 32
WindowWidth = 853
WindowHeight = 480