From 988c9e00314166a4f045471c661614daa79c29e5 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Tue, 10 Jun 2008 01:01:47 +0100 Subject: [PATCH] Add config file --- src/ConfigFile.cpp | 128 +++++++++++++++++++++++++++++++++++++++++++++ src/ConfigFile.hpp | 44 ++++++++++++++++ src/Main.cpp | 37 +++++++------ src/Makefile.am | 3 +- src/Platform.hpp | 1 + 5 files changed, 195 insertions(+), 18 deletions(-) create mode 100644 src/ConfigFile.cpp create mode 100644 src/ConfigFile.hpp diff --git a/src/ConfigFile.cpp b/src/ConfigFile.cpp new file mode 100644 index 0000000..23e8e8f --- /dev/null +++ b/src/ConfigFile.cpp @@ -0,0 +1,128 @@ +/* ConfigFile.cpp -- Settings persistence. + * Copyright (C) 2008 Nick Gasson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "ConfigFile.hpp" + +ConfigFile::ConfigFile(string filename) + : dirty(false), filename(filename) +{ + ifstream ifs((GetConfigDir() + filename).c_str(), ifstream::in); + + while (ifs.good()) { + string key, value; + ifs >> key; + ifs >> value; + + if (key.size() == 0 || value.size() == 0) + break; + + cout << "key=" << key << " value=" << value << endl; + } + + ifs.close(); +} + +ConfigFile::~ConfigFile() +{ + if (dirty) { + ofstream of((GetConfigDir() + filename).c_str()); + + for (map::iterator it = settings.begin(); + it != settings.end(); + ++it) { + of << (*it).first; + of << " "; + of << (*it).second; + of << endl; + } + } +} + +bool ConfigFile::has(const string &key) const +{ + return settings.find(key) != settings.end(); +} + +const string &ConfigFile::get(const string &key) +{ + if (!has(key)) + throw runtime_error(key + " not in config file"); + else + return settings[key]; +} + +const string &ConfigFile::get_string(const string &key, const string &def) +{ + if (has(key)) + return get(key); + else { + put(key, def); + return def; + } +} + +int ConfigFile::get_int(const string &key, int def) +{ + if (has(key)) { + istringstream is(get(key)); + int i; + is >> i; + return i; + } + else { + ostringstream os; + os << def; + put(key, os.str()); + return def; + } +} + +bool ConfigFile::get_bool(const string &key, bool def) +{ + if (has(key)) { + const string &value = get(key); + if (value == "true") + return true; + else if (value == "false") + return false; + else + throw runtime_error(value + " is not boolean"); + } + else { + put(key, def); + return def; + } +} + +void ConfigFile::put(string key, string value) +{ + settings[key] = value; + dirty = true; +} + +void ConfigFile::put(string key, int value) +{ + ostringstream ss; + ss << value; + put(key, ss.str()); +} + +void ConfigFile::put(string key, bool value) +{ + put(key, string(value ? "true" : "false")); +} + diff --git a/src/ConfigFile.hpp b/src/ConfigFile.hpp new file mode 100644 index 0000000..b7f0cb8 --- /dev/null +++ b/src/ConfigFile.hpp @@ -0,0 +1,44 @@ +/* ConfigFile.hpp -- Settings persistence. + * Copyright (C) 2008 Nick Gasson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef INC_CONFIGFILE_HPP +#define INC_CONFIGFILE_HPP + +#include "Platform.hpp" + +class ConfigFile { +public: + ConfigFile(string filename = ".lander.config"); + ~ConfigFile(); + + bool has(const string &key) const; + + const string &get(const string &key); + const string &get_string(const string &key, const string &def = ""); + int get_int(const string &key, int def = 0); + bool get_bool(const string &key, bool def = false); + + void put(string key, string value); + void put(string key, int value); + void put(string key, bool value); +private: + map settings; + bool dirty; + string filename; +}; + +#endif diff --git a/src/Main.cpp b/src/Main.cpp index 083681a..0b2ba5c 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -19,13 +19,11 @@ #include "Game.hpp" #include "Options.hpp" - -#define DEBUG_WINDOW -#define DEBUG_WIN_X 800 -#define DEBUG_WIN_Y 600 +#include "ConfigFile.hpp" #include + /* * Entry point. */ @@ -37,19 +35,15 @@ int main(int argc, char **argv) setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); - - // Get current resolution from Windows -#ifdef DEBUG_WINDOW - width = DEBUG_WIN_X; - height = DEBUG_WIN_Y; - fullscreen = false; -#else - // width = GetSystemMetrics(SM_CXSCREEN); - // height = GetSystemMetrics(SM_CYSCREEN); - width = 800; - height = 600; - fullscreen = true; -#endif + + const int DEFAULT_HRES = 800; + const int DEFAULT_VRES = 600; + const int DEFAULT_FSCREEN = false; + + ConfigFile cfile; + width = cfile.get_int("hres", DEFAULT_HRES); + height = cfile.get_int("vres", DEFAULT_VRES); + fullscreen = cfile.get_bool("fullscreen", DEFAULT_FSCREEN); #ifdef WIN32 // Work out colour depth @@ -152,3 +146,12 @@ bool FileExists(const char *file) } #endif } + +string GetConfigDir() +{ +#ifdef UNIX + return string(getenv("HOME")) + "/"; +#else +#error "Need to port GetConfigDir to this platform" +#endif +} diff --git a/src/Makefile.am b/src/Makefile.am index d5e80e1..44aee26 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -13,7 +13,8 @@ lander_SOURCES = Main.cpp Lander.hpp Game.cpp \ ElectricGate.hpp ElectricGate.cpp Key.hpp Key.cpp \ Mechanics.hpp Mechanics.cpp Geometry.hpp Image.hpp \ Image.cpp AnimatedImage.hpp AnimatedImage.cpp \ - Texture.hpp Texture.cpp Options.hpp Options.cpp + Texture.hpp Texture.cpp Options.hpp Options.cpp \ + ConfigFile.hpp ConfigFile.cpp localedir = $(datadir)/locale diff --git a/src/Platform.hpp b/src/Platform.hpp index 7c6b3f4..e88af4f 100644 --- a/src/Platform.hpp +++ b/src/Platform.hpp @@ -152,5 +152,6 @@ using namespace std; const char *LocateResource(const char *file); bool FileExists(const char *file); +string GetConfigDir(); #endif /* #ifdef INC_PLATFORM_HPP */ -- 2.39.2