最近的一个任务中需要在c++中读取Java程序中的配置文件(xxx.properties),无奈并没有找到合适的解决方案,为了不耽误时间,自动动手写了一个轮子。技术一般,水平有限,哪里不合适之处读者可以提出来,我也会在以后的使用中做相应的改进。
思路
properties文件实际上就是键值对的一种形式,用等号连接键和值。c++中和键值对最贴切的就是STL中的map了。所以我使用map作为properties的实际内存存储,同时为了方便使用,另外多一个set类型的字段记录所有的key。大致流程为:
1、逐行扫描文件内容;
2、过滤注释(#后面的为注释);
3、根据等号切割key和value;
4、保存key和value到map中;
5、结束文件扫描后将map的所有key保存到set中;
核心代码 (完整代码见文章底部连接)
类的整体结构:
class Properties {
protected:
map<string, string>* props = nullptr;
set<string>* keys = nullptr;
//去除字符串首尾的空格
void trim(string& s);
//从map中加载所有的key
void loadKeys();
//切割字符串
vector<string> split(const string& str, char pattern);
//判断字符串的开头子串
bool startWith(const string& s, const string& sub);
//从文件中加载以prefix为开头的键值对
void loadHelper(const string& path, const string& prefix);
public:
map<string, string>* getProps() const;
set<string>* getKeys() const;
//从path指向的文件中加载数据
virtual bool load(const string& path) = 0;
//根据key获取value
string getProperty(const string& key) = 0;
//增加键值
void setProperty(const string& key, const string& value) = 0;
//将所有键值保存到文件中
bool save(const string& path);
Properties();
virtual ~Properties();
};
loadHelper函数是本工具类的核心,在这里稍微解释一下prefix的作用。在使用中发现,配置文件中不是所有的配置项都是程序需要的,此时就需要过滤一些键值,以prefix为前缀的键才被保存进内存。当需要全部配置时,只需要将prefix设置为空串。
void Properties::loadHelper(const string& path, const string& prefix, bool addPath) {
vector<string> paths = split(path, '.');
if (paths.size() == 0 || paths[paths.size() - 1] != "properties") {
throw new runtime_error(path+" is not a properties file!");
}
ifstream input(path);
if (!input) {
throw new runtime_error("can't load "+path);
}
string line;
//逐行扫描
while (getline(input, line))
{
//过滤空格
trim(line);
//扫描空行和注释行
if (line.empty() || line == "\r" || line[0] == '#')
{
continue;
}
//前缀过滤
if (!startWith(line, prefix)) {
continue;
}
//键值切割
vector<string> res = split(line, '=');
if (res.size() < 2)
{
cerr << "Properties format error!" << endl;
throw new exception("Properties format error!");
}
//过滤行内注释
int t = res[1].find("#");
if (t != string::npos) {
res[1].erase(t);
}
//过滤键和值的空格
for_each(res.begin(), res.end(), [=](string& s)mutable {
trim(s);
});
//保存到内存的map中
props->insert(make_pair(res[0], res[1]));
}
input.close();
}