C++读取Properties的工具类

文章介绍了如何在C++中读取Java的properties配置文件,通过创建一个Properties类,利用STL的map存储键值对,set存储所有key。核心方法loadHelper负责加载文件,过滤注释和非指定前缀的键值对。类提供了加载、获取、设置键值对以及保存到文件的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近的一个任务中需要在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();
}

完整代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值