-
Автор темы
- #1
Обратите внимание, пользователь заблокирован на форуме. Не рекомендуется проводить сделки.
Будем делать все с помощью обертки save\load от Сергея (Крайслер )
config.h
Код:
#pragma once
#include <vector>
#include <string>
#include <map>
#include <iostream>
#include <fstream>
#include <algorithm>
class cConfiguration
{
public:
cConfiguration(std::string Path);
cConfiguration(std::string Path, std::map<std::string, std::string> Default);
~cConfiguration();
void Load();
void LoadDefault();
void Save() const;
void Print() const;
void set(const std::string& szKey, const std::string& value);
void set(const std::string& szKey, bool value);
void set(const std::string& szKey, float value);
void set(const std::string& szKey, int value);
void set(const std::string& szKey, const char* value);
std::string get(const std::string& szKey) const;
bool getBool(const std::string& szKey) const;
float getFloat(const std::string& szKey) const;
int getInt(const std::string& szKey) const;
private:
std::map<std::string, std::string> m_Data;
std::map<std::string, std::string> m_Default;
std::string m_PathFile;
};
Код:
#include "config.h"
cConfiguration::cConfiguration(std::string Path) :
m_PathFile(Path)
{
}
cConfiguration::cConfiguration(std::string Path, std::map<std::string, std::string> Default) :
m_PathFile(Path),
m_Default(Default)
{
}
cConfiguration::~cConfiguration() {}
void cConfiguration::LoadDefault()
{
m_Data = m_Default;
}
void cConfiguration::Load()
{
std::ifstream fsStream(m_PathFile);
std::string ln;
this->LoadDefault();
if (!fsStream.is_open())
this->Save();
else
{
while (std::getline(fsStream, ln))
{
std::string key;
std::string value;
bool keyFound = false;
bool readingValue = false;
for (char x : ln)
{
if (x == '#' || x == '!' || x == ';')
break;
else if ((x == ' ' || x == '\t') && !readingValue)
continue;
else if (x == '=')
keyFound = true;
else if (!keyFound)
key.push_back(x);
else if (keyFound)
{
value.push_back(x);
readingValue = true;
}
}
if (keyFound)
this->set(key, value);
}
}
fsStream.close();
}
void cConfiguration::Save() const
{
std::fstream fsStream(m_PathFile, std::fstream::out);
fsStream << "" << std::endl;
fsStream << std::endl;
for (auto i : m_Data)
fsStream << i.first << " = " << i.second << std::endl;
fsStream.close();
}
void cConfiguration::Print() const
{
for (auto i : m_Data)
std::cout << i.first << " = " << i.second << std::endl;
}
void cConfiguration::set(const std::string& szKey, const std::string& value)
{
if (m_Data.find(szKey) != m_Data.end())
m_Data.at(szKey) = value;
else
m_Data.insert({ szKey, value });
}
void cConfiguration::set(const std::string& szKey, bool value)
{
this->set(szKey, std::string((value) ? "true" : "false"));
}
void cConfiguration::set(const std::string& szKey, float value)
{
this->set(szKey, std::to_string(value));
}
void cConfiguration::set(const std::string& szKey, int value)
{
this->set(szKey, std::to_string(value));
}
void cConfiguration::set(const std::string& szKey, const char* value)
{
this->set(szKey, std::string(value));
}
std::string cConfiguration::get(const std::string& szKey) const
{
if (m_Data.find(szKey) != m_Data.end())
return m_Data.at(szKey);
else
return "";
}
bool cConfiguration::getBool(const std::string& szKey) const
{
if (m_Data.find(szKey) != m_Data.end())
{
std::string value = m_Data.at(szKey);
std::transform(value.begin(), value.end(), value.begin(), tolower);
if (value == "true" || value == "1" ||
value == "on" || value == "enable")
return true;
}
return false;
}
float cConfiguration::getFloat(const std::string& szKey) const
{
if (m_Data.find(szKey) != m_Data.end())
return std::atof(m_Data.at(szKey).c_str());
else
return 0.f;
}
int cConfiguration::getInt(const std::string& szKey) const
{
if (m_Data.find(szKey) != m_Data.end())
return std::atoi(m_Data.at(szKey).c_str());
else
return 0;
}
Сама логика :
Код:
std::map<std::string, std::string> default_ =
{
{ "save_password", "sv_password" },
};
cConfiguration Config("configuration.data", default_);
Config.Load();
static bool savepass = Config.getBool("save_password"), save_pass = Config.getBool("save_password");
static char password_buf2[32] = "Password";
if (save_pass == true)
{
Config.set("save_password", true);
Config.Save();
Config.Load();
if (savepass == true)
{
save_pass = Config.getBool("save_password");
strcpy(password_buf2, pass.c_str());
}
}
else {
Config.set("save_password", false);
save_pass = false;
Config.Save();
Config.Load();
}