Refactor Config
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
#include "config/config.h"
|
||||
|
||||
namespace germanairlinesva
|
||||
{
|
||||
namespace file
|
||||
{
|
||||
namespace config
|
||||
{
|
||||
void Config::writeFile() const
|
||||
{
|
||||
std::ofstream out(XPLANE_PLUGIN_DIRECTORY CONFIG);
|
||||
out << "scenery=" << this->scenery << "\n";
|
||||
out << "user=" << this->user << "\n";
|
||||
out << "token=" << this->token << "\n";
|
||||
out.close();
|
||||
}
|
||||
|
||||
Config::Config()
|
||||
{
|
||||
std::ifstream in(XPLANE_PLUGIN_DIRECTORY CONFIG);
|
||||
std::string line;
|
||||
while (std::getline(in, line)) {
|
||||
std::vector<std::string> fields =
|
||||
germanairlinesva::util::split(line, '=');
|
||||
if (fields.size() >= 2) {
|
||||
germanairlinesva::util::trim(fields[0]);
|
||||
germanairlinesva::util::trim(fields[1]);
|
||||
if (fields[0] == "scenery") {
|
||||
this->scenery = fields[1];
|
||||
} else if (fields[0] == "user") {
|
||||
this->user = fields[1];
|
||||
} else if (fields[0] == "token") {
|
||||
this->token = fields[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
in.close();
|
||||
}
|
||||
|
||||
void Config::updateScenery(std::string scenery)
|
||||
{
|
||||
this->scenery = scenery;
|
||||
|
||||
this->writeFile();
|
||||
}
|
||||
} // namespace config
|
||||
} // namespace file
|
||||
} // namespace germanairlinesva
|
||||
@@ -0,0 +1,42 @@
|
||||
#ifndef GERMANAIRLINESVA_FILE_CONFIG_H
|
||||
#define GERMANAIRLINESVA_FILE_CONFIG_H
|
||||
|
||||
#include <map>
|
||||
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "constants.h"
|
||||
#include "util.hpp"
|
||||
|
||||
namespace germanairlinesva
|
||||
{
|
||||
namespace file
|
||||
{
|
||||
namespace config
|
||||
{
|
||||
class Config
|
||||
{
|
||||
private:
|
||||
std::string user;
|
||||
std::string scenery;
|
||||
std::string token;
|
||||
|
||||
void writeFile() const;
|
||||
|
||||
public:
|
||||
Config();
|
||||
|
||||
void updateScenery(std::string scenery);
|
||||
|
||||
inline const std::string getUser() const { return this->user; }
|
||||
inline const std::string getScenery() const { return this->scenery; }
|
||||
inline const std::string getToken() const { return this->token; }
|
||||
};
|
||||
} // namespace config
|
||||
} // namespace file
|
||||
} // namespace germanairlinesva
|
||||
|
||||
#endif
|
||||
@@ -1,53 +0,0 @@
|
||||
#ifndef GERMANAIRLINESVA_FILE_CONFIG_H
|
||||
#define GERMANAIRLINESVA_FILE_CONFIG_H
|
||||
|
||||
#include <map>
|
||||
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "util.hpp"
|
||||
|
||||
namespace germanairlinesva
|
||||
{
|
||||
namespace file
|
||||
{
|
||||
namespace config
|
||||
{
|
||||
static inline std::map<std::string, std::string>
|
||||
readConfig(const std::string &file)
|
||||
{
|
||||
std::ifstream config(file);
|
||||
std::map<std::string, std::string> settings;
|
||||
|
||||
std::string line;
|
||||
while (std::getline(config, line)) {
|
||||
std::vector<std::string> fields =
|
||||
germanairlinesva::util::split(line, '=');
|
||||
if (fields.size() >= 2) {
|
||||
germanairlinesva::util::trim(fields[0]);
|
||||
germanairlinesva::util::trim(fields[1]);
|
||||
settings[fields[0]] = fields[1];
|
||||
}
|
||||
}
|
||||
|
||||
config.close();
|
||||
return settings;
|
||||
}
|
||||
|
||||
static inline void
|
||||
writeConfig(const std::map<std::string, std::string> &config,
|
||||
const std::string &file)
|
||||
{
|
||||
std::ofstream cfg(file);
|
||||
for (const std::pair<const std::string, std::string> &entry : config) {
|
||||
cfg << entry.first << '=' << entry.second << '\n';
|
||||
}
|
||||
cfg.close();
|
||||
}
|
||||
} // namespace config
|
||||
} // namespace file
|
||||
} // namespace germanairlinesva
|
||||
#endif
|
||||
Reference in New Issue
Block a user