diff options
author | Kevin Smith <git@kismith.co.uk> | 2011-02-27 22:45:32 (GMT) |
---|---|---|
committer | Kevin Smith <git@kismith.co.uk> | 2011-03-06 20:25:01 (GMT) |
commit | 27d21b371f24272466a2d6a5bf2e2b717ee2d9fc (patch) | |
tree | 5f53281711d4f467933e4b3315241e4eee58a64c /Swiftob/Storage.cpp | |
parent | d9c9df3b4ae5432552417fc4db74d62ab34f066d (diff) | |
download | swift-contrib-27d21b371f24272466a2d6a5bf2e2b717ee2d9fc.zip swift-contrib-27d21b371f24272466a2d6a5bf2e2b717ee2d9fc.tar.bz2 |
A start on Swiftob, a Swiften-based chatbot.
Diffstat (limited to 'Swiftob/Storage.cpp')
-rw-r--r-- | Swiftob/Storage.cpp | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/Swiftob/Storage.cpp b/Swiftob/Storage.cpp new file mode 100644 index 0000000..0cf16d7 --- /dev/null +++ b/Swiftob/Storage.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2011 Kevin Smith + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include "Swiftob/Storage.h" + +#include <Swiften/Base/String.h> +#include <Swiften/Base/ByteArray.h> +#include <Swiften/Base/foreach.h> + +typedef std::pair<std::string, std::string> Strings; + +Storage::Storage(const std::string& path) : settingsPath_(boost::filesystem::path(path)) { + load(); +} + +Storage::Storage(const boost::filesystem::path& path) : settingsPath_(path) { + load(); +} + +void Storage::load() { + if (boost::filesystem::exists(settingsPath_)) { + Swift::ByteArray data; + data.readFromFile(settingsPath_.string()); + foreach (std::string line, Swift::String::split(data.toString(), '\n')) { + std::pair<std::string, std::string> pair = Swift::String::getSplittedAtFirst(line, '\t'); + settings_[pair.first] = pair.second; + } + } +} + +void Storage::saveSetting(const std::string& setting, const std::string& value) { + settings_[setting] = value; + std::string settingsString; + foreach(Strings pair, settings_) { + settingsString += pair.first + '\t' + pair.second + '\n'; + } + boost::filesystem::ofstream file(settingsPath_); + file << settingsString; + file.close(); +} + +std::string Storage::getSetting(const std::string& setting) { + return settings_[setting]; +} |