技术专栏
qt json 配置文件
在项目使用中,配置文件使用的频率比较多,比如INI,以及JSON的方式。json文件比较的清晰灵活,使用起来比较方便。
使用qt工程时,只需将该模块加入到pro工程中,就可以使用。
#include "configure.h"
#include "xpr_json.h"
#define CONFLIG_JSON "/app/config/sofia.json"
Configure::Configure()
{
config_json = XPR_JSON_LoadFileName(CONFLIG_JSON);
if(!config_json) {
printf("load sofia.json failed\n");
}
}
Configure::~Configure()
{
if(config_json) {
XPR_JSON_DumpFileName(config_json, CONFLIG_JSON);
XPR_JSON_DecRef(config_json);
config_json = NULL;
}
}
int Configure::getInt32(const char *key, ConfigType type)
{
// XPR_JSON *json = type == CONFIG_SYSTEM ? config_json : air_json;
XPR_JSON *json = equipmentType(type);
return XPR_JSON_XPathGetInt(json, key);
}
int Configure::setInt32(const char *key, int value, ConfigType type)
{
// XPR_JSON *json = type == CONFIG_SYSTEM ? config_json : air_json;
XPR_JSON *json = equipmentType(type);
return XPR_JSON_XPathSetInt(json, key, value);
}
const char *Configure::getString(const char *key, ConfigType type)
{
// XPR_JSON *json = type == CONFIG_SYSTEM ? config_json : air_json;
XPR_JSON *json = equipmentType(type);
return XPR_JSON_XPathGetString(json, key);
}
int Configure::setString(const char *key, const char *value, ConfigType type)
{
// XPR_JSON *json = type == CONFIG_SYSTEM ? config_json : air_json;
XPR_JSON *json = equipmentType(type);
return XPR_JSON_XPathSetString(json, key, value);
}
double Configure::getDouble(const char *key, ConfigType type)
{
// XPR_JSON *json = type == CONFIG_SYSTEM ? config_json : air_json;
XPR_JSON *json = equipmentType(type);
return XPR_JSON_XPathGetDouble(json, key);
}
int Configure::setDouble(const char *key, double value, ConfigType type)
{
// XPR_JSON *json = type == CONFIG_SYSTEM ? config_json : air_json;
XPR_JSON *json = equipmentType(type);
return XPR_JSON_XPathSetDouble(json, key, value);
}
int Configure::saveToFile(ConfigType type)
{
if(type == CONFIG_SYSTEM)
return XPR_JSON_DumpFileName(config_json, CONFLIG_JSON);
}
XPR_JSON* Configure ::equipmentType(ConfigType type)
{
if(type ==CONFIG_SYSTEM )
return config_json;
}
Configure *APN::Config()
{
static Configure config;
return &config;
}
#ifndef CONFIGURE_H
#define CONFIGURE_H
struct XPR_JSON;
enum ConfigType
{
CONFIG_SYSTEM,
};
class Configure
{
public:
Configure();
~Configure();
int getInt32(const char* key, ConfigType type = CONFIG_SYSTEM);
int setInt32(const char* key, int value, ConfigType type = CONFIG_SYSTEM);
const char* getString(const char* key, ConfigType type = CONFIG_SYSTEM);
int setString(const char* key, const char* value, ConfigType type = CONFIG_SYSTEM);
double getDouble(const char* key, ConfigType type = CONFIG_SYSTEM);
int setDouble(const char* key, double value, ConfigType type = CONFIG_SYSTEM);
int saveToFile(ConfigType type = CONFIG_SYSTEM);
Configure(const Configure&) = delete;
Configure& operator=(const Configure&) = delete;
XPR_JSON* equipmentType(ConfigType type);
private:
XPR_JSON* config_json;
};
namespace APN {
Configure* Config();
}
#endif
通过上诉的代码,可以看到对应的读写的接口,可使用此函数,对配置文件中的数据,进行配置。
声明:本文内容由易百纳平台入驻作者撰写,文章观点仅代表作者本人,不代表易百纳立场。如有内容侵权或者其他问题,请联系本站进行删除。
红包
点赞
收藏
评论
打赏
- 分享
- 举报
评论
0个
手气红包

相关专栏
-
浏览量:1566次2020-08-30 00:47:44
-
浏览量:1571次2020-02-18 12:23:42
-
浏览量:7294次2021-01-16 14:52:44
-
浏览量:7297次2020-07-17 15:42:06
-
浏览量:7251次2020-10-09 13:56:00
-
浏览量:4323次2020-09-23 22:57:24
-
浏览量:4367次2020-08-20 11:08:00
-
浏览量:14846次2020-11-10 15:08:06
-
浏览量:2324次2017-11-20 11:48:55
-
浏览量:1348次2023-08-31 16:29:15
-
浏览量:10599次2020-08-30 00:41:53
-
浏览量:1560次2020-08-30 00:47:29
-
浏览量:5795次2021-08-10 14:04:57
-
浏览量:672次2023-12-22 22:16:53
-
浏览量:3402次2020-09-30 18:33:31
-
浏览量:3468次2020-08-19 18:32:47
-
浏览量:995次2023-11-24 15:49:33
-
浏览量:2428次2021-10-14 17:11:05
-
浏览量:3279次2017-11-23 12:41:22
置顶时间设置
结束时间
删除原因
-
广告/SPAM
-
恶意灌水
-
违规内容
-
文不对题
-
重复发帖
打赏作者
big_anana
您的支持将鼓励我继续创作!
打赏金额:
¥1

¥5

¥10

¥50

¥100

支付方式:

举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
审核成功
发布时间设置
发布时间:
请选择发布时间设置
是否关联周任务-专栏模块
审核失败
失败原因
请选择失败原因
备注
请输入备注