From 93ad04eb6a75ae4183ed883a0e607a6212173952 Mon Sep 17 00:00:00 2001 From: gb <741021719@qq.com> Date: Tue, 25 Oct 2022 16:41:09 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4JSON=EF=BC=8C=E4=B8=BA?= =?UTF-8?q?=E7=BB=9F=E4=B8=80APP=E9=85=8D=E7=BD=AE=E5=81=9A=E5=87=86?= =?UTF-8?q?=E5=A4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sane/DlgPage.cpp | 10 +-- sane/DlgPage.h | 4 +- sane/DlgSetting.cpp | 4 +- sane/DlgSetting.h | 6 +- sane/gb_json.cpp | 161 +++++++++++++++++++++++++++----------------- sane/gb_json.h | 35 ++++++++-- sane/scanner.cpp | 9 ++- sane/scanner.h | 4 +- 8 files changed, 149 insertions(+), 84 deletions(-) diff --git a/sane/DlgPage.cpp b/sane/DlgPage.cpp index acf2e9f..6294c4a 100644 --- a/sane/DlgPage.cpp +++ b/sane/DlgPage.cpp @@ -158,9 +158,9 @@ void dlg_base::notify_ui_event(int ev) if (ui_event_notify_) ui_event_notify_(ev, this, ui_notify_param_); } -gb::sane_config* dlg_base::get_config(void) +gb::sane_config_schm* dlg_base::get_config(void) { - gb::sane_config* cfg = NULL; + gb::sane_config_schm* cfg = NULL; SendMessage(parent_, WM_GET_CONFIG_OBJ, 0, (LPARAM)&cfg); @@ -796,7 +796,7 @@ void dlg_page::handle_command(WORD code, WORD id, HANDLE ctrl) { SANE_Fixed val = SANE_FIX(dlg.x_in_mm()); SANE_Int after = 0; - gb::sane_config* cfg = get_config(); + gb::sane_config_schm* cfg = get_config(); sane_.sane_control_option_api(dev_, id_custom_left_, SANE_ACTION_SET_VALUE, &val, &after); if (cfg) @@ -833,7 +833,7 @@ void dlg_page::handle_command(WORD code, WORD id, HANDLE ctrl) dlg.get_gamma(&gamma); sane_.sane_io_control_api(dev_, IO_CTRL_CODE_SET_CUSTOM_GAMMA, &gamma, &len); - gb::sane_config* cfg = get_config(); + gb::sane_config_schm* cfg = get_config(); if (cfg) cfg->config_changed(id_custom_gamma_, (char*)&gamma, sizeof(gamma), true); } @@ -1077,7 +1077,7 @@ void dlg_page::control_action(HWND wnd) { SANE_Int after = 0; SANE_Status statu = sane_.sane_control_option_api(dev_, id - dlg_page::dyn_id_base, SANE_ACTION_SET_VALUE, val, &after); - gb::sane_config* cfg = get_config(); + gb::sane_config_schm* cfg = get_config(); done_ = false; if (cfg) { diff --git a/sane/DlgPage.h b/sane/DlgPage.h index dea3b04..306ea50 100644 --- a/sane/DlgPage.h +++ b/sane/DlgPage.h @@ -22,7 +22,7 @@ extern HMODULE g_my_inst; namespace gb { - class sane_config; + class sane_config_schm; } class dlg_base @@ -41,7 +41,7 @@ protected: virtual void on_font_changed(void); void create(void); void notify_ui_event(int ev); - gb::sane_config* get_config(void); + gb::sane_config_schm* get_config(void); public: dlg_base(HWND parent, UINT idd); diff --git a/sane/DlgSetting.cpp b/sane/DlgSetting.cpp index 22403aa..ab8183e 100644 --- a/sane/DlgSetting.cpp +++ b/sane/DlgSetting.cpp @@ -58,7 +58,7 @@ BOOL dlg_setting::handle_message(UINT msg, WPARAM wp, LPARAM lp) refresh_controls((int)wp); break; case WM_GET_CONFIG_OBJ: - *((gb::sane_config**)lp) = cfg_; + *((gb::sane_config_schm**)lp) = cfg_; break; default: ret = FALSE; @@ -351,7 +351,7 @@ void dlg_setting::refresh_controls(int src_sn) } } -void dlg_setting::set_config(gb::sane_config* cfg, const wchar_t* file) +void dlg_setting::set_config(gb::sane_config_schm* cfg, const wchar_t* file) { cfg_ = cfg; cfg_file_ = file; diff --git a/sane/DlgSetting.h b/sane/DlgSetting.h index d81fc79..efc005f 100644 --- a/sane/DlgSetting.h +++ b/sane/DlgSetting.h @@ -17,7 +17,7 @@ namespace gb { - class sane_config; + class sane_config_schm; } class dlg_setting : public dlg_base { @@ -29,7 +29,7 @@ class dlg_setting : public dlg_base bool err_; int id_help_; int id_restore_; - gb::sane_config* cfg_; + gb::sane_config_schm* cfg_; std::wstring cfg_file_; HWND tab_; @@ -55,7 +55,7 @@ public: ~dlg_setting(); public: - void set_config(gb::sane_config* cfg, const wchar_t* file); + void set_config(gb::sane_config_schm* cfg, const wchar_t* file); HWND window(void); HWND parent(void); //void show(void); diff --git a/sane/gb_json.cpp b/sane/gb_json.cpp index 28da594..cf1d576 100644 --- a/sane/gb_json.cpp +++ b/sane/gb_json.cpp @@ -31,6 +31,34 @@ namespace gb return ret; } + refer::refer() : ref_(1) + {} + refer::~refer() + {} + + long refer::add_ref(void) + { +#if defined(WIN32) || defined(_WIN64) + return InterlockedIncrement(&ref_); +#else + return ++ref_; +#endif + } + long refer::release(void) + { +#if defined(WIN32) || defined(_WIN64) + long ref = InterlockedDecrement(&ref_); +#else + long ref = --ref_; +#endif + + if (ref == 0) + delete this; + + return ref; + } + + json::json(char* json_txt) : obj_(0), cur_child_(0), is_array_(false) { memset(&walk_head_, 0, sizeof(walk_head_)); @@ -325,7 +353,7 @@ namespace gb val = new json(); if (!val->attach_cjson(obj)) { - delete val; + val->release(); return false; } @@ -831,19 +859,19 @@ namespace gb namespace gb { - std::string sane_config::opt_data_appendix_("_data"); + std::string sane_config_schm::opt_data_appendix_("_data"); - sane_config::sane_config() : jsn_(NULL), bkp_(NULL), in_setting_(false) + sane_config_schm::sane_config_schm() : jsn_(NULL), bkp_(NULL), in_setting_(false), scheme_name_("") { def_val_ = new gb::json(); } - sane_config::~sane_config() + sane_config_schm::~sane_config_schm() { clear(); - delete def_val_; + def_val_->release(); } - bool sane_config::hex(unsigned char ch, unsigned char* val) + bool sane_config_schm::hex(unsigned char ch, unsigned char* val) { bool ret = true; @@ -864,15 +892,15 @@ namespace gb return ret; } - bool sane_config::hex_char(const char* data, unsigned char* val) + bool sane_config_schm::hex_char(const char* data, unsigned char* val) { unsigned char v = 0; bool ret = false; - if (sane_config::hex(*data++, &v)) + if (sane_config_schm::hex(*data++, &v)) { v <<= 4; - if (sane_config::hex(*data++, &v)) + if (sane_config_schm::hex(*data++, &v)) { if (val) *val = v; @@ -883,13 +911,13 @@ namespace gb return ret; } - bool sane_config::is_option_data(std::string& name) + bool sane_config_schm::is_option_data(std::string& name) { - size_t pos = name.find(sane_config::opt_data_appendix_); + size_t pos = name.find(sane_config_schm::opt_data_appendix_); if (pos != std::string::npos) { - if (pos + sane_config::opt_data_appendix_.length() == name.length()) + if (pos + sane_config_schm::opt_data_appendix_.length() == name.length()) { name.erase(pos); @@ -900,17 +928,17 @@ namespace gb return false; } - void sane_config::clear() + void sane_config_schm::clear() { if (jsn_) - delete jsn_; + jsn_->release(); jsn_ = NULL; if (bkp_) - delete bkp_; + bkp_->release(); bkp_ = NULL; file_ = L""; } - std::string sane_config::to_hex_letter(const char* data, size_t bytes) + std::string sane_config_schm::to_hex_letter(const char* data, size_t bytes) { std::string hex(""); const unsigned char* ptr = (const unsigned char*)data; @@ -924,7 +952,7 @@ namespace gb return hex; } - std::string sane_config::from_hex_letter(const char* data, size_t bytes) + std::string sane_config_schm::from_hex_letter(const char* data, size_t bytes) { std::string stream(""); @@ -932,7 +960,7 @@ namespace gb for (size_t i = 0; i < bytes; ++i) { unsigned char ch = 0; - if (!sane_config::hex_char(data, &ch)) + if (!sane_config_schm::hex_char(data, &ch)) break; stream.append(1, ch); data += 2; @@ -940,7 +968,7 @@ namespace gb return stream; } - std::string sane_config::default_value(const char* hex_title) + std::string sane_config_schm::default_value(const char* hex_title) { std::string val(""); @@ -949,7 +977,7 @@ namespace gb return val; } - bool sane_config::load_from_file(const wchar_t* file) + bool sane_config_schm::load_from_file(const wchar_t* file) { clear(); @@ -975,7 +1003,7 @@ namespace gb return ret; } - bool sane_config::load_from_mem(const char* mem) + bool sane_config_schm::load_from_mem(const char* mem) { gb::base64 b64; std::string stream(b64.decode(mem, lstrlenA(mem))); @@ -984,7 +1012,7 @@ namespace gb jsn_ = new gb::json(); if (!jsn_->attach_text(&stream[0])) { - delete jsn_; + jsn_->release(); jsn_ = NULL; return false; @@ -992,7 +1020,7 @@ namespace gb return true; } - bool sane_config::save_to(const wchar_t* file) + bool sane_config_schm::save_to(const wchar_t* file) { bool ret = false; std::string encode(to_text_stream()); @@ -1014,43 +1042,43 @@ namespace gb return ret; } - void sane_config::set_default_value(int sn, const char* name, const char* val, size_t bytes) + void sane_config_schm::set_default_value(int sn, const char* name, const char* val, size_t bytes) { id_name_[sn] = name; def_val_->set_value(name, to_hex_letter(val, bytes).c_str()); } - bool sane_config::first_config(std::string& name, std::string& val) + bool sane_config_schm::first_config(std::string& name, std::string& val) { bool ret = false; std::string raw_v(""); if (jsn_ && jsn_->first_child(raw_v, &name)) { - val = sane_config::from_hex_letter(raw_v.c_str(), raw_v.length()); + val = sane_config_schm::from_hex_letter(raw_v.c_str(), raw_v.length()); ret = true; } return ret; } - bool sane_config::next_config(std::string& name, std::string& val) + bool sane_config_schm::next_config(std::string& name, std::string& val) { bool ret = false; std::string raw_v(""); if (jsn_ && jsn_->next_child(raw_v, &name)) { - val = sane_config::from_hex_letter(raw_v.c_str(), raw_v.length()); + val = sane_config_schm::from_hex_letter(raw_v.c_str(), raw_v.length()); ret = true; } return ret; } - void sane_config::begin_setting(bool restore) + void sane_config_schm::begin_setting(bool restore) { if (bkp_) - delete bkp_; + bkp_->release(); bkp_ = jsn_; in_setting_ = true; jsn_ = new gb::json(); @@ -1061,7 +1089,7 @@ namespace gb jsn_->attach_text(&stream[0]); } } - void sane_config::config_changed(const char* name, const char* val, size_t bytes) + void sane_config_schm::config_changed(const char* name, const char* val, size_t bytes, bool extra) { std::string hex_v(to_hex_letter(val, bytes)), def(default_value(name)); @@ -1070,58 +1098,57 @@ namespace gb jsn_->remove(name); else jsn_->set_value(name, hex_v.c_str()); + + if (extra) + { + jsn_->set_value((name + sane_config_schm::opt_data_appendix_).c_str(), hex_v.c_str()); + } + else + { + def = default_value(name); + if (hex_v == def) + { + jsn_->remove(name); + jsn_->remove((name + sane_config_schm::opt_data_appendix_).c_str()); + } + else + jsn_->set_value(name, hex_v.c_str()); + } } - void sane_config::config_changed(int sn, const char* val, size_t bytes, bool extra) + void sane_config_schm::config_changed(int sn, const char* val, size_t bytes, bool extra) { - std::string name(""), - hex_v(to_hex_letter(val, bytes)), - def(""); + std::string name(""); if (id_name_.count(sn)) { name = id_name_[sn]; - if (extra) - { - name += sane_config::opt_data_appendix_; - jsn_->set_value(name.c_str(), hex_v.c_str()); - } - else - { - def = default_value(name.c_str()); - if (hex_v == def) - { - jsn_->remove(name.c_str()); - jsn_->remove((name + sane_config::opt_data_appendix_).c_str()); - } - else - jsn_->set_value(name.c_str(), hex_v.c_str()); - } + config_changed(name.c_str(), val, bytes, extra); } } - void sane_config::remove_config(const char* name) + void sane_config_schm::remove_config(const char* name) { if (jsn_) jsn_->remove(name); } - void sane_config::end_setting(bool cancel) + void sane_config_schm::end_setting(bool cancel) { if (in_setting_) { if (cancel) { - delete jsn_; + jsn_->release(); jsn_ = bkp_; bkp_ = NULL; } else if (bkp_) { - delete bkp_; + bkp_->release(); bkp_ = NULL; } } in_setting_ = false; } - int sane_config::id_from_name(const char* name) + int sane_config_schm::id_from_name(const char* name) { for (const auto& v : id_name_) { @@ -1131,7 +1158,7 @@ namespace gb return -1; } - std::string sane_config::to_text_stream(void) + std::string sane_config_schm::to_text_stream(void) { if (jsn_) { @@ -1151,7 +1178,7 @@ namespace gb else return ""; } - std::string sane_config::get_version(void) + std::string sane_config_schm::get_version(void) { std::string ver(""); @@ -1160,7 +1187,15 @@ namespace gb return ver; } - void sane_config::update(bool(__stdcall* is_float)(int, void*), void* param, const char* (__stdcall* t2n)(const char*), std::string* discard) + std::string sane_config_schm::get_scheme_name(void) + { + return scheme_name_; + } + void sane_config_schm::set_scheme_name(const char* name) + { + scheme_name_ = name ? name : ""; + } + void sane_config_schm::update(bool(__stdcall* is_float)(int, void*), void* param, const char* (__stdcall* t2n)(const char*), std::string* discard) { if (!jsn_) return; @@ -1184,7 +1219,7 @@ namespace gb changed = true; do { - jsn_->change_key(name.c_str(), t2n(sane_config::from_hex_letter(name.c_str(), name.length()).c_str())); + jsn_->change_key(name.c_str(), t2n(sane_config_schm::from_hex_letter(name.c_str(), name.length()).c_str())); } while (jsn_->next_child(val, &name)); } } @@ -1213,4 +1248,10 @@ namespace gb if (changed) save_to(NULL); } + + + + + /////////////////////////////////////////////////////////////////////////////////// + // scanner_cfg } diff --git a/sane/gb_json.h b/sane/gb_json.h index 4a24ef1..be55419 100644 --- a/sane/gb_json.h +++ b/sane/gb_json.h @@ -12,7 +12,21 @@ namespace gb { - class json + class refer + { + volatile long ref_; + + protected: + refer(); + virtual ~refer(); + + public: + long add_ref(void); + long release(void); + }; + + + class json : public refer { cJSON *obj_; cJSON *cur_child_; @@ -23,9 +37,11 @@ namespace gb cJSON* find_child(cJSON *parent, std::vector& path, bool create, cJSON*** addr = NULL); cJSON* find(const char* path, bool create = false, cJSON*** addr = NULL); + protected: + ~json(); + public: json(char* json_txt = 0); - ~json(); static std::string to_string(cJSON* root, bool formatted); static std::string get_value_as_string(cJSON* root, bool integer = false); @@ -82,23 +98,26 @@ namespace gb std::string decode(const char* data, size_t bytes); }; - class sane_config + class sane_config_schm : public refer { + std::string scheme_name_; std::wstring file_; json* jsn_; json* bkp_; json* def_val_; bool in_setting_; - std::map id_name_; + std::map id_name_; // (id, default-val) void clear(); std::string to_hex_letter(const char* data, size_t bytes); std::string from_hex_letter(const char* data, size_t bytes); std::string default_value(const char* name); + protected: + ~sane_config_schm(); + public: - sane_config(); - ~sane_config(); + sane_config_schm(); static std::string opt_data_appendix_; static bool hex(unsigned char ch, unsigned char* val); @@ -113,13 +132,15 @@ namespace gb bool first_config(std::string& name, std::string& val); bool next_config(std::string& name, std::string& val); void begin_setting(bool restore = false); - void config_changed(const char* name, const char* val, size_t bytes); + void config_changed(const char* name, const char* val, size_t bytes, bool extra = false); void config_changed(int sn, const char* val, size_t bytes, bool extra = false); void remove_config(const char* name); void end_setting(bool cancel); int id_from_name(const char* name); std::string to_text_stream(void); std::string get_version(void); + std::string get_scheme_name(void); + void set_scheme_name(const char* name); void update(bool(__stdcall* is_float)(int, void*), void* param, const char*(__stdcall* t2n)(const char*), std::string* discard = NULL); }; } diff --git a/sane/scanner.cpp b/sane/scanner.cpp index d3c4534..0f6b2aa 100644 --- a/sane/scanner.cpp +++ b/sane/scanner.cpp @@ -323,7 +323,7 @@ scanner::scanner(SCANNERID id) : handle_(NULL), id_(id), ex_id_(EXTENSION_ID_BAS , scanner_name_(L""), cfg_(NULL), is_ui_wait_img_(false), is_scanning_(false) , scanner_ev_handler_(NULL), evh_param_(NULL), app_wnd_(NULL) { - cfg_ = new gb::sane_config(); + cfg_ = new gb::sane_config_schm(); tmp_path_ = local_trans::a2u(hg_sane_middleware::sane_path().c_str()); { char* tmp = getenv("LOCALAPPDATA"); @@ -350,7 +350,10 @@ scanner::~scanner() { close(); if (cfg_) - delete cfg_; + { + cfg_->release(); + cfg_ = NULL; + } } bool scanner::is_belong_serial(int vid, int pid, SCANNERID serial) @@ -574,7 +577,7 @@ void scanner::apply_config(void) int id = cfg_->id_from_name(n.c_str()); if (id == -1) { - if (gb::sane_config::is_option_data(n)) + if (gb::sane_config_schm::is_option_data(n)) { id = cfg_->id_from_name(n.c_str()); if (id == is_custom_gamma_id_) diff --git a/sane/scanner.h b/sane/scanner.h index 4262370..6ef08ca 100644 --- a/sane/scanner.h +++ b/sane/scanner.h @@ -26,7 +26,7 @@ class dlg_indicator; class dlg_setting; namespace gb { - class sane_config; + class sane_config_schm; } class scanner : public ISaneInvoker, virtual public refer @@ -51,7 +51,7 @@ class scanner : public ISaneInvoker, virtual public refer SANE_FinalImgFormat img_fmt_; std::unique_ptr indicator_; std::unique_ptr setting_; - gb::sane_config* cfg_; + gb::sane_config_schm* cfg_; int(__stdcall* scanner_ev_handler_)(int, void*); void* evh_param_;