首页 > 技术知识 > 正文

在项目使用中,配置文件使用的频率比较多,比如INI,以及JSON的方式。json文件比较的清晰灵活,使用起来比较方便。

qt  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
<

通过上诉的代码,可以看到对应的读写的接口,可使用此函数,对配置文件中的数据,进行配置。

猜你喜欢