2023-02-23 15:08:08 +08:00
|
|
|
// SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
|
2023-02-14 18:13:19 +08:00
|
|
|
//
|
2023-02-23 15:08:08 +08:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2022-04-24 14:52:13 +08:00
|
|
|
|
|
|
|
#ifndef KEYFILE_H
|
|
|
|
#define KEYFILE_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
typedef std::map<std::string, std::string> KeyMap;
|
|
|
|
typedef std::map<std::string, KeyMap> MainKeyMap;
|
|
|
|
|
2023-03-07 10:52:06 +08:00
|
|
|
// 解析ini文件类
|
2022-04-24 14:52:13 +08:00
|
|
|
class KeyFile
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit KeyFile(char separtor = ';');
|
2023-03-07 10:52:06 +08:00
|
|
|
virtual ~KeyFile();
|
2022-04-24 14:52:13 +08:00
|
|
|
|
|
|
|
bool getBool(const std::string §ion, const std::string &key, bool defaultValue = false);
|
2022-05-15 12:10:42 +08:00
|
|
|
void setBool(const std::string §ion, const std::string &key, const std::string &defaultValue = "false");
|
2022-04-24 14:52:13 +08:00
|
|
|
std::vector<bool> getBoolList(const std::string §ion, const std::string &key, bool defaultValue = false);
|
|
|
|
int getInt(const std::string §ion, const std::string &key, int defaultValue = 0);
|
|
|
|
std::vector<int> getIntList(const std::string §ion, const std::string &key, int defaultValue = 0);
|
|
|
|
int64_t getInt64(const std::string §ion, const std::string &key, int64_t defaultValue = 0);
|
2022-05-15 12:10:42 +08:00
|
|
|
uint64_t getUint64(const std::string §ion, const std::string &key, int64_t defaultValue = 0);
|
2022-04-24 14:52:13 +08:00
|
|
|
float getFloat(const std::string §ion, const std::string &key, float defaultValue = 0);
|
|
|
|
std::string getStr(const std::string §ion, const std::string &key, std::string defaultValue = "");
|
|
|
|
bool containKey(const std::string §ion, const std::string &key);
|
|
|
|
std::string getLocaleStr(const std::string §ion, const std::string &key, std::string defaultLocale = "");
|
|
|
|
std::vector<std::string> getStrList(const std::string §ion, const std::string &key);
|
|
|
|
std::vector<std::string> getLocaleStrList(const std::string §ion, const std::string &key, std::string defaultLocale = "");
|
|
|
|
|
|
|
|
void setKey(const std::string §ion, const std::string &key, const std::string &value);
|
2023-03-07 10:52:06 +08:00
|
|
|
virtual bool saveToFile(const std::string &filePath);
|
2022-04-24 14:52:13 +08:00
|
|
|
bool loadFile(const std::string &filePath);
|
|
|
|
std::vector<std::string> getMainKeys();
|
2022-06-15 14:14:43 +08:00
|
|
|
std::string getFilePath()
|
|
|
|
{
|
|
|
|
return m_filePath;
|
|
|
|
}
|
2022-04-24 14:52:13 +08:00
|
|
|
|
|
|
|
// for test
|
|
|
|
void print();
|
|
|
|
|
2023-03-07 10:52:06 +08:00
|
|
|
protected:
|
2022-05-15 12:10:42 +08:00
|
|
|
MainKeyMap m_mainKeyMap; // section -> key : value
|
|
|
|
std::string m_filePath;
|
|
|
|
FILE *m_fp;
|
|
|
|
bool m_modified;
|
|
|
|
char m_listSeparator;
|
2023-03-07 10:52:06 +08:00
|
|
|
|
|
|
|
static bool writeSectionToFile(const std::string& sectionName, const KeyMap& keyMap, FILE * file);
|
2022-04-24 14:52:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // KEYFILE_H
|