From 14e903d8a1eb4b3591b2915cfbc0a83dc0ebe850 Mon Sep 17 00:00:00 2001 From: Christian Speckner Date: Thu, 25 Apr 2019 21:36:04 +0200 Subject: [PATCH] Introduce KeyValueRepository, change OSystem and Settings to use it. --- src/common/repository/KeyValueRepository.hxx | 37 ++++++ .../KeyValueRepositoryConfigfile.cxx | 107 ++++++++++++++++++ .../KeyValueRepositoryConfigfile.hxx | 38 +++++++ .../repository/KeyValueRepositoryNoop.hxx | 34 ++++++ 4 files changed, 216 insertions(+) create mode 100644 src/common/repository/KeyValueRepository.hxx create mode 100644 src/common/repository/KeyValueRepositoryConfigfile.cxx create mode 100644 src/common/repository/KeyValueRepositoryConfigfile.hxx create mode 100644 src/common/repository/KeyValueRepositoryNoop.hxx diff --git a/src/common/repository/KeyValueRepository.hxx b/src/common/repository/KeyValueRepository.hxx new file mode 100644 index 000000000..58652cc3c --- /dev/null +++ b/src/common/repository/KeyValueRepository.hxx @@ -0,0 +1,37 @@ +//============================================================================ +// +// SSSS tt lll lll +// SS SS tt ll ll +// SS tttttt eeee ll ll aaaa +// SSSS tt ee ee ll ll aa +// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" +// SS SS tt ee ll ll aa aa +// SSSS ttt eeeee llll llll aaaaa +// +// Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony +// and the Stella Team +// +// See the file "License.txt" for information on usage and redistribution of +// this file, and for a DISCLAIMER OF ALL WARRANTIES. +//============================================================================ + +#ifndef KEY_VALUE_REPOSITORY_HXX +#define KEY_VALUE_REPOSITORY_HXX + +#include + +#include "Variant.hxx" +#include "bspf.hxx" + +class KeyValueRepository +{ + public: + + virtual ~KeyValueRepository() = default; + + virtual std::map load() = 0; + + virtual void save(std::map& values) = 0; +}; + +#endif // KEY_VALUE_REPOSITORY_HXX diff --git a/src/common/repository/KeyValueRepositoryConfigfile.cxx b/src/common/repository/KeyValueRepositoryConfigfile.cxx new file mode 100644 index 000000000..6b514ba8b --- /dev/null +++ b/src/common/repository/KeyValueRepositoryConfigfile.cxx @@ -0,0 +1,107 @@ +//============================================================================ +// +// SSSS tt lll lll +// SS SS tt ll ll +// SS tttttt eeee ll ll aaaa +// SSSS tt ee ee ll ll aa +// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" +// SS SS tt ee ll ll aa aa +// SSSS ttt eeeee llll llll aaaaa +// +// Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony +// and the Stella Team +// +// See the file "License.txt" for information on usage and redistribution of +// this file, and for a DISCLAIMER OF ALL WARRANTIES. +//============================================================================ + +#include "KeyValueRepositoryConfigfile.hxx" + +namespace { + string trim(const string& str) + { + string::size_type first = str.find_first_not_of(' '); + return (first == string::npos) ? EmptyString : + str.substr(first, str.find_last_not_of(' ')-first+1); + } +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +KeyValueRepositoryConfigfile::KeyValueRepositoryConfigfile(const string& filename) + : myFilename(filename) +{} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +std::map KeyValueRepositoryConfigfile::load() +{ + std::map pairs; + + string line, key, value; + string::size_type equalPos, garbage; + + ifstream in(myFilename); + if(!in || !in.is_open()) { + // FIXME - make logger available everywhere + cout << "ERROR: Couldn't load from settings file " << myFilename << endl; + + return pairs; + } + + while(getline(in, line)) + { + // Strip all whitespace and tabs from the line + while((garbage = line.find("\t")) != string::npos) + line.erase(garbage, 1); + + // Ignore commented and empty lines + if((line.length() == 0) || (line[0] == ';')) + continue; + + // Search for the equal sign and discard the line if its not found + if((equalPos = line.find("=")) == string::npos) + continue; + + // Split the line into key/value pairs and trim any whitespace + key = trim(line.substr(0, equalPos)); + value = trim(line.substr(equalPos + 1, line.length() - key.length() - 1)); + + // Skip absent key + if(key.length() == 0) + continue; + + pairs[key] = value; + } + + return pairs; +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void KeyValueRepositoryConfigfile::save(std::map& values) +{ +ofstream out(myFilename); + if(!out || !out.is_open()) { + // FIXME - make logger available everywhere + cout << "ERROR: Couldn't save to settings file " << myFilename << endl; + + return; + } + + out << "; Stella configuration file" << endl + << ";" << endl + << "; Lines starting with ';' are comments and are ignored." << endl + << "; Spaces and tabs are ignored." << endl + << ";" << endl + << "; Format MUST be as follows:" << endl + << "; command = value" << endl + << ";" << endl + << "; Commands are the same as those specified on the commandline," << endl + << "; without the '-' character." << endl + << ";" << endl + << "; Values are the same as those allowed on the commandline." << endl + << "; Boolean values are specified as 1 (or true) and 0 (or false)" << endl + << ";" << endl; + + // Write out each of the key and value pairs + for(const auto& pair: values) + out << pair.first << " = " << pair.second << endl; +} diff --git a/src/common/repository/KeyValueRepositoryConfigfile.hxx b/src/common/repository/KeyValueRepositoryConfigfile.hxx new file mode 100644 index 000000000..4bb799506 --- /dev/null +++ b/src/common/repository/KeyValueRepositoryConfigfile.hxx @@ -0,0 +1,38 @@ +//============================================================================ +// +// SSSS tt lll lll +// SS SS tt ll ll +// SS tttttt eeee ll ll aaaa +// SSSS tt ee ee ll ll aa +// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" +// SS SS tt ee ll ll aa aa +// SSSS ttt eeeee llll llll aaaaa +// +// Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony +// and the Stella Team +// +// See the file "License.txt" for information on usage and redistribution of +// this file, and for a DISCLAIMER OF ALL WARRANTIES. +//============================================================================ + +#ifndef KEY_VALUE_REPOSITORY_CONFIGFILE_HXX +#define KEY_VALUE_REPOSITORY_CONFIGFILE_HXX + +#include "KeyValueRepository.hxx" + +class KeyValueRepositoryConfigfile : public KeyValueRepository +{ + public: + + KeyValueRepositoryConfigfile(const string& filename); + + virtual std::map load(); + + virtual void save(std::map& values); + + private: + + const string& myFilename; +}; + +#endif // KEY_VALUE_REPOSITORY_CONFIGFILE_HXX diff --git a/src/common/repository/KeyValueRepositoryNoop.hxx b/src/common/repository/KeyValueRepositoryNoop.hxx new file mode 100644 index 000000000..863364b31 --- /dev/null +++ b/src/common/repository/KeyValueRepositoryNoop.hxx @@ -0,0 +1,34 @@ +//============================================================================ +// +// SSSS tt lll lll +// SS SS tt ll ll +// SS tttttt eeee ll ll aaaa +// SSSS tt ee ee ll ll aa +// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" +// SS SS tt ee ll ll aa aa +// SSSS ttt eeeee llll llll aaaaa +// +// Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony +// and the Stella Team +// +// See the file "License.txt" for information on usage and redistribution of +// this file, and for a DISCLAIMER OF ALL WARRANTIES. +//============================================================================ + +#ifndef KEY_VALUE_REPOSITORY_NOOP_HXX +#define KEY_VALUE_REPOSITORY_NOOP_HXX + +#include "KeyValueRepository.hxx" + +class KeyValueRepositoryNoop : public KeyValueRepository +{ + public: + + virtual std::map load() { + return std::map(); + } + + virtual void save(std::map& values) {} +}; + +#endif // KEY_VALUE_REPOSITORY_NOOP_HXX