迁移以设备名称命名的配置文件

This commit is contained in:
gb 2022-10-08 17:20:16 +08:00
parent fdb5649ed2
commit 970db48e17
3 changed files with 86 additions and 9 deletions

View File

@ -1133,6 +1133,9 @@ namespace gb
}
void sane_config::update(bool(__stdcall* is_float)(int, void*), void* param, const char* (__stdcall* t2n)(const char*), std::string* discard)
{
if (!jsn_)
return;
std::string ver(get_version()),
name(""),
val("");

View File

@ -1,6 +1,7 @@
#include "scanner.h"
#include <Windows.h>
#include <Shlwapi.h> // for PathFileExistsW
#include "../sdk/hginclude/huagaoxxx_warraper_ex.h"
#include <sane/sane_option_definitions.h>
#include "../../code_device/hgsane/sane_hg_mdw.h"
@ -11,6 +12,10 @@
#include "DlgSetting.h"
#include "gb_json.h"
#pragma comment(lib, "Shlwapi.lib")
static IMPLEMENT_OPTION_STRING_COMPARE(compare_sane_opt);
#define SET_SANE_OPT_ID(id, id_name, name, val, extension) \
@ -344,6 +349,31 @@ COM_API_IMPLEMENT(scanner, long, release(void))
return refer::release();
}
void scanner::transport_config_file(void)
{
size_t pos = scanner_name_.find(L" - ");
std::wstring pid(L"G"), old(L"");
if (pos == std::wstring::npos)
return;
pid += scanner_name_.substr(pos + 3);
if (scanner_name_.find(L"HUAGOSCAN ") != std::wstring::npos)
{
old = local_trans::a2u("\345\215\216\351\253\230\346\211\253\346\217\217\344\273\252\342\200\224", CP_UTF8);
}
else if (scanner_name_.find(L"LANXUMSCAN ") != std::wstring::npos)
{
old = local_trans::a2u("\347\253\213\346\200\235\350\276\260\346\211\253\346\217\217\344\273\252\342\200\224", CP_UTF8);
pid += L"S";
}
old += pid;
if (PathFileExistsW((cfg_path_ + old).c_str()))
{
log_info((L"Rename config file '" + old + L"' to '" + scanner_name_.substr(0, pos) + L"'\r\n").c_str(), 0);
MoveFileW((cfg_path_ + old).c_str(), (cfg_path_ + scanner_name_.substr(0, pos)).c_str());
}
}
void scanner::update_config(void)
{
std::string notice("");
@ -439,10 +469,15 @@ int scanner::open(void)
ret = hg_sane_middleware::instance()->open_device(name.c_str(), &handle_);
if (ret == SANE_STATUS_GOOD)
{
size_t pid = scanner_name_.find(L" - ");
if (pid == -1)
pid = scanner_name_.length();
transport_config_file();
callback::reg_callback(handle_, this);
scanner_name_ = local_trans::a2u(name.c_str(), CP_UTF8);
ret = init_options_id();
load_config((cfg_path_ + scanner_name_ + L".cfg").c_str());
load_config((cfg_path_ + scanner_name_.substr(0, pid) + L".cfg").c_str());
apply_config();
}
else
@ -459,7 +494,6 @@ int scanner::close(void)
if (handle_)
{
hg_sane_middleware::instance()->close_device(handle_);
// save_config((cfg_path_ + scanner_name_ + L".cfg").c_str());
}
handle_ = NULL;
ex_id_ = EXTENSION_ID_BASE;
@ -2355,9 +2389,12 @@ COM_API_IMPLEMENT(scanner, bool, ui_show_setting(HWND parent, bool with_scan, bo
api.sane_start_api = inner_sane_start;
api.sane_strstatus_api = inner_sane_strstatus;
setting_.reset(new dlg_setting(parent, &api, handle_, with_scan, scanner_name_.c_str()));
size_t pid = scanner_name_.find(L" - ");
if (pid == -1)
pid = scanner_name_.length();
setting_.reset(new dlg_setting(parent, &api, handle_, with_scan, scanner_name_.substr(0, pid).c_str()));
setting_->set_ui_event_notify(&scanner::ui_callback, this);
setting_->set_config(cfg_, (cfg_path_ + scanner_name_).c_str());
setting_->set_config(cfg_, (cfg_path_ + scanner_name_.substr(0, pid)).c_str());
cfg_->begin_setting();
indicator_.reset();
if (indicator)
@ -2478,6 +2515,7 @@ int scanner::last_error(void)
#include <direct.h>
std::mutex g_lock_;
std::string g_path_file_("");
FILE* g_file_ = NULL;
void init_log(void)
@ -2496,7 +2534,8 @@ void init_log(void)
last = name;
path += last;
path += "_twain.log";
g_file_ = fopen(path.c_str(), "wb");
g_file_ = fopen(path.c_str(), "a+b");
g_path_file_ = path;
}
else
{
@ -2505,13 +2544,31 @@ void init_log(void)
GetModuleFileNameA(NULL, name, _countof(name) - 1);
path = std::string(name) + "_twain.log";
g_file_ = fopen(path.c_str(), "wb");
g_file_ = fopen(path.c_str(), "a+b");
g_path_file_ = path;
}
if (g_file_)
{
fseek(g_file_, 0, SEEK_END);
if (ftell(g_file_))
{
std::wstring sep(L"\r\n\r\n\r\n=================================================\r\n");
fwrite(sep.c_str(), 2, sep.length(), g_file_);
}
else
{
unsigned short bom = 0x0feff;
fwrite(&bom, sizeof(bom), 1, g_file_);
}
time_t now = time(NULL);
struct tm* t = localtime(&now);
wchar_t ts[128] = { 0 };
swprintf_s(ts, _countof(ts) - 1, L"==================%04d-%02d-%02d %02d:%02d:%02d==================\r\n"
, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
fwrite(ts, 2, lstrlenW(ts), g_file_);
}
}
void close_log(void)
{
@ -2528,7 +2585,23 @@ void log(const wchar_t* info)
fwrite(info, 2, lstrlenW(info), g_file_);
fflush(g_file_);
if (ftell(g_file_) > 10 * 1024 * 1024)
fseek(g_file_, 0, SEEK_SET);
{
fclose(g_file_);
remove(g_path_file_.c_str());
g_file_ = fopen(g_path_file_.c_str(), "a+b");
if (g_file_)
{
unsigned short bom = 0x0feff;
time_t now = time(NULL);
struct tm* t = localtime(&now);
wchar_t ts[128] = { 0 };
swprintf_s(ts, _countof(ts) - 1, L"==================%04d-%02d-%02d %02d:%02d:%02d (Truncated when size > 10MB) ==================\r\n"
, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
fwrite(&bom, sizeof(bom), 1, g_file_);
fwrite(ts, 2, lstrlenW(ts), g_file_);
}
}
}
}

View File

@ -53,6 +53,7 @@ class scanner : public ISaneInvoker, virtual public refer
std::unique_ptr<dlg_setting> setting_;
gb::sane_config* cfg_;
void transport_config_file(void);
void update_config(void);
void load_config(const wchar_t* file);
void save_config(const wchar_t* file);