preference.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/application_preferences"
  18. namespace base {
  19. class preference {
  20. public:
  21. virtual ~preference() {
  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. preference() {
  73. std::ifstream ifs(FY_APPLICATION_PREFERENCES_PATH, std::ios::in | std::ios::binary);
  74. if (!ifs.is_open()) {
  75. //兼容
  76. ifs.open("/data/preferences.json", std::ios::in | std::ios::binary);
  77. }
  78. std::stringstream ss;
  79. ss << ifs.rdbuf();
  80. std::string file_content(ss.str());
  81. if (file_content.empty()) {
  82. return;
  83. }
  84. ifs.close();
  85. Json::Reader reader;
  86. if (!reader.parse(file_content.c_str(), file_content.c_str() + file_content.length(), json_, false)) {
  87. LOGE("%s parse failed", __PRETTY_FUNCTION__);
  88. }
  89. }
  90. static preference& instance() {
  91. static preference singleton;
  92. return singleton;
  93. }
  94. private:
  95. template<class T>
  96. void write_json(const std::string &name, T value) {
  97. Mutex::Autolock lock(mutex_);
  98. if (json_.isMember(name) && (json_[name].compare(value) == 0)) {
  99. return;
  100. }
  101. json_[name] = value;
  102. write_json(json_);
  103. }
  104. void write_json(const Json::Value &json) {
  105. std::ofstream ofs(FY_APPLICATION_PREFERENCES_PATH, std::ios::out | std::ios::binary);
  106. if (!ofs.is_open()) {
  107. return;
  108. }
  109. std::string str = json_.toStyledString();
  110. ofs.write(str.c_str(), str.length());
  111. ofs.flush();
  112. ofs.close();
  113. sync();
  114. }
  115. void remove_key(const std::string &name) {
  116. Mutex::Autolock lock(mutex_);
  117. json_.removeMember(name);
  118. write_json(json_);
  119. }
  120. void remove_all() {
  121. Mutex::Autolock lock(mutex_);
  122. json_.clear();
  123. write_json(json_);
  124. }
  125. private:
  126. Json::Value json_;
  127. Mutex mutex_;
  128. };
  129. } /* namespace fy */
  130. #endif /* _FY_PREFERENCE_HPP_ */