字符串函数改为小括号;设置属性值时添加日志

This commit is contained in:
gb 2023-11-03 18:03:22 +08:00
parent b2a999c605
commit 68e062cb46
10 changed files with 135 additions and 73 deletions

View File

@ -939,6 +939,7 @@ scanner_err hg_scanner_mgr::hg_scanner_set_parameter(scanner_handle h, const cha
{
sane_opt_provider* sop = dynamic_cast<sane_opt_provider*>(SCAN_PTR(h));
err = (scanner_err)dev_opts_->restore(sop);
utils::to_log(LOG_LEVEL_DEBUG, "Restore all options ...\n");
}
else
{

View File

@ -140,32 +140,6 @@ static std::string get_real_value(gb_json* jsn, const char* type)
return "";
}
static bool pick_bracket(const char* exp, const char** end)
{
bool ret = false;
int balance = 1;
exp = strstr(exp, "[");
if (exp++)
{
while (*exp && balance)
{
if (*exp == '[')
balance++;
else if (*exp == ']')
balance--;
exp++;
}
if (balance == 0)
{
*end = exp - 1;
ret = true;
}
}
return ret;
}
static struct
{
std::string name;
@ -765,13 +739,14 @@ bool device_option::parse_simple_logic_expression(gb_json* root, const char* exp
// substr function ...
if (strstr(tag, "left[") || strstr(tag, "right[") || strstr(tag, "mid["))
{
const char* end = nullptr;
bool ret = pick_bracket(tag, &end);
const char* end = nullptr, *start = strstr(tag, "[");
int l = string_util::find_end_of_pair(start + 1, '[', ']');
bool ret = start[++l] == ']';
if (ret)
{
std::string exp(calc.name + (end + 1)),
name1(tag, end - tag + 1);
std::string exp(calc.name + (start + l + 1)),
name1(tag, start + l - tag + 1);
name1.insert(0, calc.name + ".");
ret = device_option::parse_simple_logic_expression(root, exp.c_str(), name, calc);
if (ret)

View File

@ -342,6 +342,7 @@ protected:
}
}
public:
device_option(std::function<bool(int)> user_priv = std::function<bool(int)>()
, std::function<void(const char*)> log = std::function<void(const char*)>());

View File

@ -52,7 +52,7 @@ namespace string_util
return end;
}
void skip_space(const char*& ptr, const char* space = " \t")
void skip_space(const char*& ptr, const char* space)
{
char mark[2] = { 0 };
@ -65,13 +65,7 @@ namespace string_util
}
}
enum
{
TRIM_LEFT = 1,
TRIM_RIGHT,
TRIM_BOTH,
};
void trim(std::string& str, int type = TRIM_BOTH)
void trim(std::string& str, int type)
{
int pos = 0;
@ -181,7 +175,16 @@ bool simple_logic::parse_internal(const char* expr, int* end, void(*leaf)(const
}
std::string sub(ptr + 1, len - 1);
simple_logic *e = new simple_logic();
if (sub.find("||") == std::string::npos ||
sub.find("&&") == std::string::npos ||
sub.find("^") == std::string::npos)
{
// count as function ...
ptr += len;
}
else
{
simple_logic* e = new simple_logic();
int over = 0;
bool not_v = false;
@ -220,6 +223,7 @@ bool simple_logic::parse_internal(const char* expr, int* end, void(*leaf)(const
break;
}
}
}
else if (*ptr == '|')
{
if (*(ptr + 1) == '|')

View File

@ -16,6 +16,29 @@
#include <string>
#include <vector>
namespace string_util
{
// Function: find the ending position in str
//
// Parameter: str - the string beginning after the first letter 'head'
//
// head - the leading letter, can be emblaced
//
// tail - the ending letter
//
// Return: position at the ending letter, or '\0' in the string
int find_end_of_pair(const char* str, char head, char tail);
void skip_space(const char*& ptr, const char* space = " \t");
enum
{
TRIM_LEFT = 1,
TRIM_RIGHT,
TRIM_BOTH,
};
void trim(std::string& str, int type = TRIM_BOTH);
};
class simple_logic
{
int oper_;

View File

@ -24,6 +24,7 @@
#include <lang/app_language.h>
#include "../sdk/hginclude/utils.h"
#include <huagao/brand.h>
#include "../hgdriver/hgdev/user-opt/device_opt.h" // for readable_text(SANE_Value_Type type, void* value)
#ifndef SIGUSR1
#define SIGUSR1 10
@ -527,7 +528,7 @@ SANE_Status hg_sane_middleware::get_current_value(LPDEVINST inst, const void* op
return SANE_STATUS_INVAL;
}
}
SANE_Status hg_sane_middleware::set_value(LPDEVINST inst, const void* opt, void* value, SANE_Int* affect, bool to_default)
SANE_Status hg_sane_middleware::set_value(LPDEVINST inst, const void* opt, void* value, SANE_Int* affect, bool to_default, std::string* title, std::string* val_text_before, std::string* val_text_after)
{
bool empty_value = false;
SANE_Option_Descriptor* desc = inst->opts->get_opt_descriptor(opt);
@ -545,12 +546,21 @@ SANE_Status hg_sane_middleware::set_value(LPDEVINST inst, const void* opt, void*
return SANE_STATUS_INVAL;
}
SANE_Value_Type type = desc->type;
if (val_text_before && value)
*val_text_before = sane_opt::readable_text(desc->type, value);
if (title)
*title = desc->title;
if (IS_CAP_READONLY(desc->cap))
return SANE_STATUS_UNSUPPORTED;
return SANE_STATUS_ACCESS_DENIED;
if (to_default && (desc->cap & SANE_CAP_AUTOMATIC) == 0)
return SANE_STATUS_UNSUPPORTED;
scanner_err err = write_value(inst->dev, desc->name, desc->type, value, to_default, inst, affect);
if (val_text_after && value)
*val_text_after = sane_opt::readable_text(type, value);
return local_utility::scanner_err_2_sane_statu(err);
}
@ -784,7 +794,25 @@ SANE_Status hg_sane_middleware::control_option(SANE_Handle h, const void* option
}
else if (action == SANE_ACTION_SET_VALUE || action == SANE_ACTION_SET_AUTO)
{
err = set_value(inst, option, value, after_do, action == SANE_ACTION_SET_AUTO);
std::string title(""), before(""), after("");
err = set_value(inst, option, value, after_do, action == SANE_ACTION_SET_AUTO, &title, &before, &after);
if (action == SANE_ACTION_SET_AUTO)
{
if(after.empty())
utils::to_log_with_api(hg_scanner_log_is_enable, hg_scanner_log, LOG_LEVEL_DEBUG, "Restore '%s' = %s\n"
, title.c_str(), hg_scanner_err_description(local_utility::sane_statu_2_scanner_err(err)));
else
utils::to_log_with_api(hg_scanner_log_is_enable, hg_scanner_log, LOG_LEVEL_DEBUG, "Restore '%s' to '%s' = %s\n"
, title.c_str(), after.c_str(), hg_scanner_err_description(local_utility::sane_statu_2_scanner_err(err)));
}
else if(before != after)
utils::to_log_with_api(hg_scanner_log_is_enable, hg_scanner_log, LOG_LEVEL_DEBUG, "set '%s' to '%s'(APPLIED: %s) = %s\n"
, title.c_str(), before.c_str(), after.c_str(), hg_scanner_err_description(local_utility::sane_statu_2_scanner_err(err)));
else
utils::to_log_with_api(hg_scanner_log_is_enable, hg_scanner_log, LOG_LEVEL_DEBUG, "set '%s' to '%s' = %s\n"
, title.c_str(), before.c_str(), hg_scanner_err_description(local_utility::sane_statu_2_scanner_err(err)));
}
// extension ...

View File

@ -53,7 +53,7 @@ class hg_sane_middleware
scanner_err read_value(scanner_handle h, const char* name, SANE_Value_Type type, size_t len, void* value, bool to_default);
scanner_err write_value(scanner_handle h, const char* name, SANE_Value_Type type, void* value, bool to_default, LPDEVINST optinst, SANE_Int* affect);
SANE_Status get_current_value(LPDEVINST inst, const void* opt, void* value, bool default_val);
SANE_Status set_value(LPDEVINST inst, const void* opt, void* value, SANE_Int* affect, bool to_default);
SANE_Status set_value(LPDEVINST inst, const void* opt, void* value, SANE_Int* affect, bool to_default, std::string* title = nullptr, std::string* val_text_before = nullptr, std::string* val_text_after = nullptr);
SANE_Status get_option_fixed_id(LPDEVINST inst, const void* opt, void* value);
protected:

View File

@ -16,6 +16,33 @@ sane_opt::~sane_opt()
clear();
}
std::string sane_opt::readable_text(const char* type, void* value)
{
if (strcmp(type, JSON_SANE_TYPE_BOOL) == 0)
return sane_opt::readable_text(SANE_TYPE_BOOL, value);
else if (strcmp(type, JSON_SANE_TYPE_INT) == 0)
return sane_opt::readable_text(SANE_TYPE_INT, value);
else if (strcmp(type, JSON_SANE_TYPE_FIXED) == 0)
return sane_opt::readable_text(SANE_TYPE_FIXED, value);
else if (strcmp(type, JSON_SANE_TYPE_STRING) == 0)
return sane_opt::readable_text(SANE_TYPE_STRING, value);
else
return "";
}
std::string sane_opt::readable_text(SANE_Value_Type type, void* value)
{
if (type == SANE_TYPE_BOOL)
return *(bool*)value ? "true" : "false";
else if (type == SANE_TYPE_INT)
return std::to_string(*(int*)value);
else if (type == SANE_TYPE_FIXED)
return std::to_string(*(double*)value);
else if (type == SANE_TYPE_STRING)
return (char*)value;
else
return "";
}
void sane_opt::clear()
{
if (opt_desc_.desc)

View File

@ -34,6 +34,9 @@ public:
sane_opt();
~sane_opt();
static std::string readable_text(const char* type, void* value);
static std::string readable_text(SANE_Value_Type type, void* value);
public:
int get_fix_id(void);
SANE_Option_Descriptor* get_descriptor(void);

View File

@ -235,9 +235,9 @@ bool gb_json::attach_text(char* json_txt)
cJSON* jsn = cJSON_Parse(json_txt);
if (jsn)
{
char *text = cJSON_Print(jsn);
if (text)
free(text);
//char *text = cJSON_Print(jsn);
//if (text)
// free(text);
from_cjson(jsn->child);
cJSON_Delete(jsn);