The complete source code of IdealIRC http://www.idealirc.org/
 
 
 
 
idealirc/IdealIRC/IniFile.h

109 lines
3.5 KiB

/*
* IdealIRC - Internet Relay Chat client
* Copyright (C) 2021 Tom-Andre Barstad.
* This software is licensed under the Software Attribution License.
* See LICENSE for more information.
*/
#ifndef INIFILE_H
#define INIFILE_H
#include <string>
#include <memory>
struct IniFilePriv;
class IniFile
{
public:
explicit IniFile(const std::string& filename);
IniFile(IniFile&& other) noexcept;
IniFile(IniFile&) = delete;
~IniFile();
IniFile& operator=(IniFile&) = delete;
IniFile& operator=(IniFile&& other) noexcept;
enum class Error {
NoError,
CannotOpenFile,
CannotWriteFile,
/* Errors for renaming and removing sections and items. */
SectionDontExist,
ItemDontExist,
OldDoesntExist,
NewAlreadyExists
};
//! @brief Last encountered error.
Error error() const;
//! @brief Number of all sections.
std::size_t count() const;
//! @brief Number of all items in section. Returns zero even for non-existing sections.
std::size_t count(const std::string& section) const;
//! @brief Read value by key name.
std::string read(const std::string& section, const std::string& item, const std::string& defaultValue = "") const;
//! @brief Read value by item position.
std::string read(const std::string& section, int itemPos, const std::string& defaultValue = "") const;
//! @brief Read value by key name and section position.
std::string read(int sectionPos, const std::string& item, const std::string& defaultValue = "") const;
//! @brief Read value by item position and section position.
std::string read(int sectionPos, int itemPos, const std::string& defaultValue = "") const;
//! @brief Read key name by item position. Non-existing returns empty string.
std::string item(const std::string& section, int itemPos) const;
//! @brief Read key name by item position and section position. Non-existing returns empty string.
std::string item(int sectionPos, int itemPos) const;
//! @brief Read section name by its position. Non-existing returns empty string.
std::string section(int sectionPos) const;
//! @brief Creates an empty section. Existing section will not be touched.
void write(const std::string& section);
//! @brief Write value. Existing is updated, non-existing will be created.
void write(const std::string& section, const std::string& item, const std::string& value); // TODO template 'value'?
//! @brief Renames a section. May return "OldDontExist", "NewAlreadyExists" or "NoError".
Error rename(const std::string& section, const std::string& newSection);
//! @brief Renames an item. May return "SectionDontExist", "OldDontExist", "NewAlreadyExists" or "NoError".
Error rename(const std::string& section, const std::string& item, const std::string& newItem);
//! @brief Deletes entire section. May return "SectionDontExist", or "NoError".
Error remove(const std::string& section);
//! @brief Deletes an item. May return "SectionDontExist", "ItemDontExist", or "NoError".
Error remove(const std::string& section, const std::string& item);
//! @brief Checks if section exists.
bool exist(const std::string& section) const;
//! @brief Checks if an item exists.
bool exist(const std::string& section, const std::string& item) const;
//! @brief Write changes to the file.
Error flush();
//! @brief Reloads contents from the file.
Error reload();
private:
std::unique_ptr<IniFilePriv> mp;
};
#endif // INIFILE_H