mirror of https://github.com/stella-emu/stella.git
Introduce KeyValueRepository, change OSystem and Settings to use it.
This commit is contained in:
parent
dd09187fc0
commit
14e903d8a1
|
@ -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 <map>
|
||||
|
||||
#include "Variant.hxx"
|
||||
#include "bspf.hxx"
|
||||
|
||||
class KeyValueRepository
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~KeyValueRepository() = default;
|
||||
|
||||
virtual std::map<string, Variant> load() = 0;
|
||||
|
||||
virtual void save(std::map<string, Variant>& values) = 0;
|
||||
};
|
||||
|
||||
#endif // KEY_VALUE_REPOSITORY_HXX
|
|
@ -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<string, Variant> KeyValueRepositoryConfigfile::load()
|
||||
{
|
||||
std::map<string, Variant> 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<string, Variant>& 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;
|
||||
}
|
|
@ -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<string, Variant> load();
|
||||
|
||||
virtual void save(std::map<string, Variant>& values);
|
||||
|
||||
private:
|
||||
|
||||
const string& myFilename;
|
||||
};
|
||||
|
||||
#endif // KEY_VALUE_REPOSITORY_CONFIGFILE_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<string, Variant> load() {
|
||||
return std::map<string, Variant>();
|
||||
}
|
||||
|
||||
virtual void save(std::map<string, Variant>& values) {}
|
||||
};
|
||||
|
||||
#endif // KEY_VALUE_REPOSITORY_NOOP_HXX
|
Loading…
Reference in New Issue