Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // .h
- class Ini
- {
- private:
- Ini();
- ~Ini();
- public:
- bool Load(std::string path);
- void Load(istream& input);
- bool Save(std::string path);
- void Save(ostream& output);
- void Create(std::string path);
- class IniSection
- {
- friend class Ini;
- Ini& pIni;
- std::string sectionName;
- private:
- IniSection(Ini& ini, const std::string& section);
- ~IniSection();
- friend bool operator == (const IniSection& _this, const IniSection& _other)
- {
- return _this.sectionName == _other.sectionName;
- }
- public:
- std::string GetSectionName();
- class IniKey
- {
- friend class IniSection;
- IniSection *pSection;
- std::string keyName;
- std::string keyValue;
- std::string commentValue;
- private:
- IniKey(IniSection& section, const std::string& key);
- ~IniKey();
- friend bool operator == (const IniKey& _this, const IniKey& _other)
- {
- return _this.keyName == _other.keyName;
- }
- public:
- void SetValue(std::string value);
- std::string GetValue();
- std::string GetKeyName();
- void AddComment(std::string comment);
- std::string GetComment();
- };
- void RemoveAllKeys();
- vector<IniKey>::iterator IniSection::FindKey(const std::string& key);
- vector<IniKey>::iterator AddKey(const std::string& key);
- IniKey *GetKey(std::string key);
- std::string GetKeyValue(std::string key);
- void SetKeyValue(std::string key, std::string value);
- private:
- std::vector<IniKey> Keys;
- };
- void RemoveAllSections();
- std::vector<IniSection>::iterator FindSection(const std::string& section);
- std::vector<IniSection>::iterator AddSection(const std::string& section);
- IniSection *GetSection(std::string section);
- std::string GetKeyValue(std::string section, std::string key);
- void SetKeyValue(std::string section, std::string key, std::string value);
- private:
- std::vector<IniSection> Sections;
- };
- typedef Ini::IniSection IniSection;
- typedef IniSection::IniKey IniKey;
- // .cpp
- Ini::Ini()
- {
- }
- Ini::~Ini()
- {
- RemoveAllSections();
- }
- bool Ini::Load(std::string path)
- {
- ifstream input;
- input.open(path.c_str(), ios::binary);
- if (!input.is_open())
- return false;
- Load(input);
- input.close();
- return true;
- }
- void Ini::Load(istream& input)
- {
- IniSection *section = NULL;
- std::string lineValue;
- enum
- {
- KEY,
- SECTION,
- COMMENT,
- OTHER
- };
- while (getline(input, lineValue))
- {
- TrimLeft(lineValue);
- TrimRight(lineValue, "\n\r");
- if (!lineValue.empty())
- {
- unsigned int type = OTHER;
- type = (lineValue.find_first_of("[") == 0 && (lineValue[lineValue.find_last_not_of(" \t\r\n")] == ']')) ? SECTION : OTHER;
- type = ((type == OTHER) && (lineValue.find_first_of("=") != std::string::npos && lineValue.find_first_of("=") > 0)) ? KEY : type;
- type = ((type == OTHER) && (lineValue.find_first_of("#") == 0)) ? COMMENT : type;
- switch (type)
- {
- case SECTION:
- section = AddSection(lineValue.substr(1, lineValue.size() - 2));
- break;
- case KEY:
- if (section)
- {
- size_t equalSpot = lineValue.find_first_of("=");
- std::string keyName = lineValue.substr(0, equalSpot);
- std::string keyValue = lineValue.substr(equalSpot + 1);
- IniKey *key = section->AddKey(keyName);
- if (key)
- key->SetValue(keyValue);
- }
- break;
- default:
- break;
- }
- }
- }
- }
- void Ini::Create(std::string path)
- {
- fstream file;
- file.open(path, fstream::out);
- file.close();
- }
- bool Ini::Save(std::string path)
- {
- ofstream output;
- output.open(path.c_str(), ios::binary);
- if (!output.is_open())
- {
- output.close();
- return false;
- }
- Save(output);
- output.close();
- return true;
- }
- void Ini::Save(ostream& output)
- {
- std::string section;
- vector<IniSection *>::iterator iter1;
- for (iter1 = Sections.begin(); iter1 != Sections.end(); iter1++)
- {
- section = "[" + (*iter1)->GetSectionName() + "]";
- output << section << "\r\n";
- vector<IniKey *>::iterator iter2;
- for (iter2 = (*iter1)->Keys.begin(); iter2 != (*iter1)->Keys.end(); iter2++)
- {
- std::string comment = "# " + (*iter2)->GetComment();
- if (comment != "# ")
- output << comment << "\r\n";
- std::string key = (*iter2)->GetKeyName() + "=" + (*iter2)->GetValue();
- output << key << "\r\n";
- }
- output << "\r\n";
- }
- }
- std::string Ini::GetKeyValue(std::string section, std::string key)
- {
- std::string value;
- IniSection *tempSection = GetSection(section);
- if (tempSection)
- {
- IniKey *tempKey = tempSection->GetKey(key);
- if (tempKey)
- value = tempKey->GetValue();
- }
- return value;
- }
- void Ini::SetKeyValue(std::string section, std::string key, std::string value)
- {
- IniSection *tempSection = AddSection(section);
- if (tempSection)
- {
- IniKey *tempKey = tempSection->AddKey(key);
- if (tempKey)
- tempKey->SetValue(value);
- }
- }
- // IniSection -----------------------------------------------------------------------------------
- IniSection::IniSection(Ini& ini, const std::string& section) : pIni(ini), sectionName(section)
- {
- }
- IniSection::~IniSection()
- {
- RemoveAllKeys();
- }
- void Ini::RemoveAllSections()
- {
- vector<IniSection *>::iterator iter;
- for (iter = Sections.begin(); iter != Sections.end(); iter++)
- delete *iter;
- Sections.clear();
- }
- vector<IniSection>::iterator Ini::FindSection(const std::string& section)
- {
- IniSection tempSection(*this, section);
- return std::find(Sections.begin(), Sections.end(), tempSection);
- }
- vector<IniSection>::iterator Ini::AddSection(const std::string& section)
- {
- std::vector<IniSection>::iterator iter = FindSection(section);
- if (iter == Sections.end())
- {
- Sections.emplace_back(*this, section);
- return Sections.end() - 1;
- }
- else
- return iter;
- }
- IniSection *Ini::GetSection(std::string section)
- {
- vector<IniSection *>::iterator iter = FindSection(section);
- if (iter != Sections.end())
- return *iter;
- return NULL;
- }
- std::string IniSection::GetSectionName()
- {
- return sectionName;
- }
- std::string IniSection::GetKeyValue(std::string key)
- {
- std::string value;
- IniKey *tempKey = GetKey(key);
- if (tempKey)
- value = tempKey->GetValue();
- return value;
- }
- void IniSection::SetKeyValue(std::string key, std::string value)
- {
- IniKey *tempKey = AddKey(key);
- if (tempKey)
- tempKey->SetValue(value);
- }
- // IniKey -----------------------------------------------------------------------------------
- IniKey::IniKey(IniSection& section, const std::string& key) : pSection(section), keyName(key)
- {
- }
- IniKey::~IniKey()
- {
- }
- void IniSection::RemoveAllKeys()
- {
- vector<IniKey *>::iterator iter;
- for (iter = Keys.begin(); iter != Keys.end(); iter++)
- delete *iter;
- Keys.clear();
- }
- vector<IniKey>::iterator IniSection::FindKey(const std::string& key)
- {
- IniKey tempKey(*this, key);
- return std::find(Keys.begin(), Keys.end(), tempKey);
- }
- vector<IniKey>::iterator IniSection::AddKey(const std::string& key)
- {
- std::vector<IniKey>::iterator iter = FindKey(key);
- if (iter == Keys.end())
- {
- Keys.emplace_back(*this, key);
- return Keys.end() - 1;
- }
- else
- return iter;
- }
- void IniKey::SetValue(std::string value)
- {
- keyValue = value;
- }
- std::string IniKey::GetValue()
- {
- return keyValue;
- }
- std::string IniKey::GetKeyName()
- {
- return keyName;
- }
- IniKey *IniSection::GetKey(std::string key)
- {
- vector<IniKey *>::iterator iter = FindKey(key);
- if (iter != Keys.end())
- return *iter;
- return NULL;
- }
- void IniKey::AddComment(std::string comment)
- {
- commentValue = comment;
- }
- std::string IniKey::GetComment()
- {
- return commentValue;
- }
- // How i want to use it:
- // Write:
- Ini ini;
- ini.Create(IniPath);
- ini.Load(IniPath);
- ini.AddSection("Test");
- IniSection *Section = ini.GetSection("Test");
- if (Section)
- {
- Section->AddKey("Key1")->SetValue("KeyValue1")->AddComment("This is a Test");
- }
- ini.Save(IniPath);
- // Read:
- Ini ini;
- ini.Load(IniPath);
- IniSection *Section = ini.GetSection("Test");
- if (Section)
- {
- std::string keyValue = Section->GetKeyValue("Key1");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement