Future proofed file read/write
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
#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