// Renegade Map Selector // // Douglas Thrift // // RenegadeConfig.cpp #include "RenegadeConfig.h" bool RenegadeConfig::load() { ifstream fin(file.c_str()); if (!fin.is_open()) return false; do { string line; getline(fin, line); if (line[line.length() - 1] == '\r') { line.erase(line.length() - 1); linux_ = true; } if (line.find("MapName=") == 0) { text << "MapName=" << (fin.good() ? "\n" : ""); } else if (line.find("MapName") == 0) { if (line.find("MapName00=") == 0) { text << "MapName00=" << (fin.good() ? "\n" : ""); } maps.push_back(line.substr(10)); } else { text << line << (fin.good() ? "\n" : ""); } } while (fin.good()); fin.close(); if (debug) { cerr << "linux_ = " << (linux_ ? "true" : "false") << "\nmaps = {\n"; for (unsigned index = 0; index < maps.size(); index++) { cerr << " [" << index << "] = " << maps[index] << "\n"; } cerr << "}\n"; } return true; } void RenegadeConfig::save() { ofstream fout(file.c_str()); if (debug) { cerr << "maps = {\n"; for (unsigned index = 0; index < maps.size(); index++) { cerr << " [" << index << "] = " << maps[index] << "\n"; } cerr << "}\n"; } do { string line; getline(text, line); if (line == "MapName=") { fout << "MapName=" << maps[0] << (text.good() ? (linux_ ? "\r\n" : "\n") : ""); } else if (line == "MapName00=") { for (unsigned index = 0; index < maps.size(); index++) { char* number = new char[3]; sprintf(number, "%02u", index); fout << "MapName" << number << "=" << maps[index] << (index + 1 < maps.size() || text.good() ? (linux_ ? "\r\n" : "\n") : ""); delete [] number; } } else { fout << line << (text.good() ? (linux_ ? "\r\n" : "\n") : ""); } } while (text.good()); fout.close(); }