prefs.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * preference.h
  3. *
  4. * Created on: 2021年4月16日
  5. * Author: pengzc
  6. */
  7. #ifndef _FY_PREFERENCE_HPP_
  8. #define _FY_PREFERENCE_HPP_
  9. #include <unistd.h>
  10. #include <string>
  11. #include <sstream>
  12. #include <iostream>
  13. #include <fstream>
  14. #include <json/Json.h>
  15. #include <system/Thread.h>
  16. #include <utils/Log.h>
  17. #define FY_APPLICATION_PREFERENCES_PATH "/data/prefs.json"
  18. namespace base {
  19. class prefs {
  20. public:
  21. virtual ~prefs() {
  22. }
  23. static void put(const std::string& name, const std::string &value) {
  24. instance().write_json(name, value);
  25. }
  26. static void put(const std::string& name, const char* value) {
  27. instance().write_json(name, value);
  28. }
  29. static void put(const std::string& name, const int value) {
  30. instance().write_json(name, value);
  31. }
  32. static void put(const std::string& name, const bool value) {
  33. instance().write_json(name, value);
  34. }
  35. static std::string get_string(const std::string &name, const std::string &def) {
  36. Mutex::Autolock lock(instance().mutex_);
  37. if (!instance().json_.isMember(name)) {
  38. return def;
  39. }
  40. if (!instance().json_[name].isString()) {
  41. return def;
  42. }
  43. return instance().json_[name].asString();
  44. }
  45. static int get_int(const std::string &name, const int def) {
  46. Mutex::Autolock lock(instance().mutex_);
  47. if (!instance().json_.isMember(name)) {
  48. return def;
  49. }
  50. if (!instance().json_[name].isInt()) {
  51. return def;
  52. }
  53. return instance().json_[name].asInt();
  54. }
  55. static bool get_bool(const std::string &name, const bool def) {
  56. Mutex::Autolock lock(instance().mutex_);
  57. if (!instance().json_.isMember(name)) {
  58. return def;
  59. }
  60. if (!instance().json_[name].isBool()) {
  61. return def;
  62. }
  63. return instance().json_[name].asBool();
  64. }
  65. static void remove(const std::string &name) {
  66. instance().remove_key(name);
  67. }
  68. static void clear() {
  69. instance().remove_all();
  70. }
  71. private:
  72. prefs() {
  73. LOGD("prefs 1");
  74. std::ifstream ifs(FY_APPLICATION_PREFERENCES_PATH, std::ios::in | std::ios::binary);
  75. if (!ifs.is_open()) {
  76. //兼容
  77. ifs.open("/data/preferences.json", std::ios::in | std::ios::binary);
  78. }
  79. std::stringstream ss;
  80. ss << ifs.rdbuf();
  81. std::string file_content(ss.str());
  82. if (file_content.empty()) {
  83. return;
  84. }
  85. ifs.close();
  86. LOGD("prefs 2");
  87. Json::Reader reader;
  88. if (!reader.parse(file_content.c_str(), file_content.c_str() + file_content.length(), json_, false)) {
  89. LOGE("%s parse failed", __PRETTY_FUNCTION__);
  90. }
  91. LOGD("prefs 3");
  92. }
  93. static prefs& instance() {
  94. static prefs singleton;
  95. return singleton;
  96. }
  97. private:
  98. template<class T>
  99. void write_json(const std::string &name, T value) {
  100. Mutex::Autolock lock(mutex_);
  101. if (json_.isMember(name) && (json_[name].compare(value) == 0)) {
  102. return;
  103. }
  104. json_[name] = value;
  105. write_json(json_);
  106. }
  107. void write_json(const Json::Value &json) {
  108. std::ofstream ofs(FY_APPLICATION_PREFERENCES_PATH, std::ios::out | std::ios::binary);
  109. if (!ofs.is_open()) {
  110. return;
  111. }
  112. std::string str = json_.toStyledString();
  113. ofs.write(str.c_str(), str.length());
  114. ofs.flush();
  115. ofs.close();
  116. sync();
  117. }
  118. void remove_key(const std::string &name) {
  119. Mutex::Autolock lock(mutex_);
  120. json_.removeMember(name);
  121. write_json(json_);
  122. }
  123. void remove_all() {
  124. Mutex::Autolock lock(mutex_);
  125. json_.clear();
  126. write_json(json_);
  127. }
  128. private:
  129. Json::Value json_;
  130. Mutex mutex_;
  131. };
  132. } /* namespace fy */
  133. #endif /* _FY_PREFERENCE_HPP_ */