TDME2 1.9.121
Properties.cpp
Go to the documentation of this file.
2
3#include <map>
4#include <string>
5#include <vector>
6
7#include <tdme/tdme.h>
11
12using std::map;
13using std::string;
14using std::vector;
15
17
21
22Properties::Properties()
23{
24}
25
26void Properties::load(const string& pathName, const string& fileName, FileSystemInterface* fileSystem)
27{
28 properties.clear();
29 vector<string> lines;
30 if (fileSystem == nullptr) fileSystem = FileSystem::getInstance();
31 fileSystem->getContentAsStringArray(pathName, fileName, lines);
32 for (auto i = 0; i < lines.size(); i++) {
33 string line = StringTools::trim(lines[i]);
34 if (line.length() == 0 || StringTools::startsWith(line, "#")) continue;
35 auto separatorPos = line.find('=');
36 if (separatorPos == -1) continue;
37 string key = StringTools::substring(line, 0, separatorPos);
38 string value = StringTools::substring(line, separatorPos + 1);
39 properties[key] = value;
40 }
41}
42
43void Properties::store(const string& pathName, const string& fileName, FileSystemInterface* fileSystem) const {
44 vector<string> result;
45 int32_t idx = 0;
46 for (auto it = properties.begin(); it != properties.end(); ++it) {
47 string key = it->first;
48 string value = it->second;
49 result.push_back(key + "=" + value);
50 }
51 if (fileSystem == nullptr) fileSystem = FileSystem::getInstance();
52 fileSystem->setContentFromStringArray(pathName, fileName, result);
53}
File system singleton class.
Definition: FileSystem.h:14
Properties class, which helps out with storeing or loading key value pairs from/to property files.
Definition: Properties.h:23
map< string, string > properties
Definition: Properties.h:25
void load(const string &pathName, const string &fileName, FileSystemInterface *fileSystem=nullptr)
Load property file.
Definition: Properties.cpp:26
void store(const string &pathName, const string &fileName, FileSystemInterface *fileSystem=nullptr) const
Store property file.
Definition: Properties.cpp:43
String tools class.
Definition: StringTools.h:20
static const string trim(const string &src)
Trim string.
Definition: StringTools.cpp:51
static const bool startsWith(const string &src, const string &prefix)
Checks if string starts with prefix.
Definition: StringTools.h:29
static const string substring(const string &src, int32_t beginIndex)
Returns substring of given string from begin index.
Definition: StringTools.h:133
virtual void setContentFromStringArray(const string &pathName, const string &fileName, const vector< string > &content)=0
Set file content as string array.
virtual void getContentAsStringArray(const string &pathName, const string &fileName, vector< string > &content)=0
Get file content as string array.