123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- /*
- * preference.h
- *
- * Created on: 2021年4月16日
- * Author: pengzc
- */
- #ifndef _FY_PREFERENCE_HPP_
- #define _FY_PREFERENCE_HPP_
- #include <unistd.h>
- #include <string>
- #include <sstream>
- #include <iostream>
- #include <fstream>
- #include <json/Json.h>
- #include <system/Thread.h>
- #include <utils/Log.h>
- #define FY_APPLICATION_PREFERENCES_PATH "/data/application_preferences"
- namespace base {
- class preference {
- public:
- virtual ~preference() {
- }
- static void put(const std::string& name, const std::string &value) {
- instance().write_json(name, value);
- }
- static void put(const std::string& name, const char* value) {
- instance().write_json(name, value);
- }
- static void put(const std::string& name, const int value) {
- instance().write_json(name, value);
- }
- static void put(const std::string& name, const bool value) {
- instance().write_json(name, value);
- }
- static std::string get_string(const std::string &name, const std::string &def) {
- Mutex::Autolock lock(instance().mutex_);
- if (!instance().json_.isMember(name)) {
- return def;
- }
- if (!instance().json_[name].isString()) {
- return def;
- }
- return instance().json_[name].asString();
- }
- static int get_int(const std::string &name, const int def) {
- Mutex::Autolock lock(instance().mutex_);
- if (!instance().json_.isMember(name)) {
- return def;
- }
- if (!instance().json_[name].isInt()) {
- return def;
- }
- return instance().json_[name].asInt();
- }
- static bool get_bool(const std::string &name, const bool def) {
- Mutex::Autolock lock(instance().mutex_);
- if (!instance().json_.isMember(name)) {
- return def;
- }
- if (!instance().json_[name].isBool()) {
- return def;
- }
- return instance().json_[name].asBool();
- }
- static void remove(const std::string &name) {
- instance().remove_key(name);
- }
- static void clear() {
- instance().remove_all();
- }
- private:
- preference() {
- std::ifstream ifs(FY_APPLICATION_PREFERENCES_PATH, std::ios::in | std::ios::binary);
- if (!ifs.is_open()) {
- //兼容
- ifs.open("/data/preferences.json", std::ios::in | std::ios::binary);
- }
- std::stringstream ss;
- ss << ifs.rdbuf();
- std::string file_content(ss.str());
- if (file_content.empty()) {
- return;
- }
- ifs.close();
- Json::Reader reader;
- if (!reader.parse(file_content.c_str(), file_content.c_str() + file_content.length(), json_, false)) {
- LOGE("%s parse failed", __PRETTY_FUNCTION__);
- }
- }
- static preference& instance() {
- static preference singleton;
- return singleton;
- }
- private:
- template<class T>
- void write_json(const std::string &name, T value) {
- Mutex::Autolock lock(mutex_);
- if (json_.isMember(name) && (json_[name].compare(value) == 0)) {
- return;
- }
- json_[name] = value;
- write_json(json_);
- }
- void write_json(const Json::Value &json) {
- std::ofstream ofs(FY_APPLICATION_PREFERENCES_PATH, std::ios::out | std::ios::binary);
- if (!ofs.is_open()) {
- return;
- }
- std::string str = json_.toStyledString();
- ofs.write(str.c_str(), str.length());
- ofs.flush();
- ofs.close();
- sync();
- }
- void remove_key(const std::string &name) {
- Mutex::Autolock lock(mutex_);
- json_.removeMember(name);
- write_json(json_);
- }
- void remove_all() {
- Mutex::Autolock lock(mutex_);
- json_.clear();
- write_json(json_);
- }
- private:
- Json::Value json_;
- Mutex mutex_;
- };
- } /* namespace fy */
- #endif /* _FY_PREFERENCE_HPP_ */
|