From 5a5404e01823f5ff4fef5d7e2b8ac3d6533806ae Mon Sep 17 00:00:00 2001 From: gb <741021719@qq.com> Date: Tue, 27 Sep 2022 12:35:37 +0800 Subject: [PATCH] =?UTF-8?q?=E8=80=81=E7=89=88=E6=9C=AC=E6=B5=AE=E7=82=B9?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=B8=8D=E8=83=BD=E6=81=A2=E5=A4=8D=EF=BC=8C?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E6=81=A2=E5=A4=8D=E4=B8=BA=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=80=BC=E5=B9=B6=E6=8F=90=E9=86=92=E7=94=A8=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sane/gb_json.cpp | 6 ++++++ sane/gb_json.h | 1 + sane/scanner.cpp | 44 +++++++++++++++++++++++++++++++++----------- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/sane/gb_json.cpp b/sane/gb_json.cpp index fb3530a..6d4dd91 100644 --- a/sane/gb_json.cpp +++ b/sane/gb_json.cpp @@ -1035,6 +1035,12 @@ namespace gb jsn_->set_value(hex_t.c_str(), hex_v.c_str()); } } + void sane_config::remove_config(const char* title) + { + std::string hex_t(to_hex_letter(title, lstrlenA(title))); + if (jsn_) + jsn_->remove(hex_t.c_str()); + } void sane_config::end_setting(bool cancel) { if (in_setting_) diff --git a/sane/gb_json.h b/sane/gb_json.h index df60a36..8eea348 100644 --- a/sane/gb_json.h +++ b/sane/gb_json.h @@ -110,6 +110,7 @@ namespace gb void begin_setting(bool restore = false); void config_changed(const char* title, const char* val, size_t bytes); void config_changed(int sn, const char* val, size_t bytes); + void remove_config(const char* title); void end_setting(bool cancel); int id_from_title(const char* title); std::string to_text_stream(void); diff --git a/sane/scanner.cpp b/sane/scanner.cpp index c4aef61..3cab24f 100644 --- a/sane/scanner.cpp +++ b/sane/scanner.cpp @@ -237,8 +237,10 @@ void scanner::save_config(const wchar_t* file) void scanner::apply_config(void) { std::string t(""), v(""), ver(cfg_->get_version()); + std::wstring notice(L""); if (cfg_->first_config(t, v)) { + bool rewrite = false; do { int id = cfg_->id_from_title(t.c_str()); @@ -249,7 +251,8 @@ void scanner::apply_config(void) const SANE_Option_Descriptor* desc = hg_sane_middleware::instance()->get_option_descriptor(handle_, id); if (desc) { - char* buf = NULL; + char* buf = NULL; + bool omit = false; if (desc->type == SANE_TYPE_STRING) { buf = new char[desc->size + 4]; @@ -259,26 +262,40 @@ void scanner::apply_config(void) } else if (ver.empty() && desc->type == SANE_TYPE_FIXED) // 升级早期版本没将float转为SANE_Fixed的BUG { - fixed = SANE_FIX(*(float*)data); - cfg_->config_changed(t.c_str(), (char*)&fixed, sizeof(fixed)); - data = &fixed; + cfg_->remove_config(t.c_str()); + rewrite = true; + omit = true; { wchar_t msg[128] = { 0 }; - unsigned char* f = (unsigned char*)&v[0], * to = (unsigned char*)data; - swprintf_s(msg, _countof(msg) - 1, L"Upgrage '%s' from '%02X%02X%02X%02X' to '%02X%02X%02X%02X'\r\n" - , local_trans::a2u(t.c_str(), CP_UTF8).c_str(), f[0], f[1], f[2], f[3] - , to[0], to[1], to[2], to[3]); - log_info(msg, 0); + std::wstring name(local_trans::a2u(t.c_str(), CP_UTF8)); + unsigned char* f = (unsigned char*)&v[0]; + + notice += name + L"\r\n"; + name.insert(0, L"Discard old configuration '"); + swprintf_s(msg, _countof(msg) - 1, L"' of value '%02X%02X%02X%02X'\r\n", f[0], f[1], f[2], f[3]); + log_info((name + msg).c_str(), 0); } } - SANE_Int after = 0; - hg_sane_middleware::instance()->set_option(handle_, id, SANE_ACTION_SET_VALUE, data, &after); + if (!omit) + { + SANE_Int after = 0; + hg_sane_middleware::instance()->set_option(handle_, id, SANE_ACTION_SET_VALUE, data, &after); + } if (buf) delete[] buf; } } } while (cfg_->next_config(t, v)); + if (rewrite) + { + cfg_->save_to(NULL); + if (notice.length()) + { + notice.insert(0, L"\u4E0B\u5217\u914D\u7F6E\u6570\u636E\u9519\u8BEF\uFF0C\u5DF2\u7ECF\u6062\u590D\u5230\u9ED8\u8BA4\u503C\u3002\u5982\u679C\u9700\u8981\uFF0C\u8BF7\u91CD\u65B0\u8BBE\u7F6E\uFF1A\r\n\r\n"); + MessageBoxW(NULL, notice.c_str(), L"\u52A0\u8F7D\u914D\u7F6E", MB_OK | MB_ICONINFORMATION); + } + } } } void scanner::on_ui_event(int uev, void* sender) @@ -2358,6 +2375,11 @@ void init_log(void) path = std::string(name) + "_twain.log"; g_file_ = fopen(path.c_str(), "wb"); } + if (g_file_) + { + unsigned short bom = 0x0feff; + fwrite(&bom, sizeof(bom), 1, g_file_); + } } void close_log(void) {