完善SANE-OPT属性逻辑操作

This commit is contained in:
gb 2023-09-13 11:20:51 +08:00
parent 852e508edb
commit 631fa03792
4 changed files with 161 additions and 33 deletions

View File

@ -223,6 +223,16 @@ static std::string get_real_value(gb_json* jsn, const char* type)
return "";
}
static struct
{
std::string name;
std::string title; // in UTF8
}g_known_group_with_sn[] = { {"base", "\xE5\x9F\xBA\xE6\x9C\xAC\xE8\xAE\xBE\xE7\xBD\xAE"}
, {"bright", "\xE4\xBA\xAE\xE5\xBA\xA6"}
, {"imgp", "\xE5\x9B\xBE\xE5\x83\x8F\xE5\xA4\x84\xE7\x90\x86"}
, {"feeder", "\xE9\x80\x81\xE7\xBA\xB8\xE6\x96\xB9\xE5\xBC\x8F\xE8\xAE\xBE\xE7\xBD\xAE"} // ÉèÖãº\xE8\xAE\xBE\xE7\xBD\xAE
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// class device_opt ...
bool device_option::condition_value::set_value(gb_json* jsn, const char* type, device_option* parent) // jsn contains only ONE value or its object
@ -854,6 +864,90 @@ void device_option::clear(void)
delete v.second;
range_value_.clear();
}
gb_json* device_option::group_opt(const char* title)
{
gb_json* jsn = new gb_json();
jsn->set_value("title", title);
jsn->set_value("type", "group");
return jsn;
}
bool device_option::arrange_raw_json(const char* txt)
{
std::vector<std::string> groups;
std::vector<gb_json*> ungroup;
std::map<std::string, std::vector<gb_json*>> ingroup;
gb_json* jsn = new gb_json(), *child = nullptr;
std::string text(txt), str("");
if (jsn->attach_text(&text[0]))
{
text.clear();
child = jsn->first_child();
while (child)
{
child->get_value("group", str);
if (str.empty())
ungroup.push_back(child);
else if (ingroup.count(str))
ingroup[str].push_back(child);
else
{
std::vector<gb_json*> c;
c.push_back(child);
ingroup[str] = c;
}
child = jsn->next_child();
}
}
jsn->release();
if (ungroup.size() || ingroup.size())
{
origin_ = new gb_json();
for (auto& v : ungroup)
{
origin_->set_value(v->key().c_str(), v);
v->release();
}
// named group ...
int grpind = 1;
for (auto& v : g_known_group_with_sn)
{
if (ingroup.count(v.name))
{
gb_json* grp = group_opt(v.title.c_str());
origin_->set_value(("grp-" + std::to_string(grpind++)).c_str(), grp);
grp->release();
for (auto& opt : ingroup[v.name])
{
origin_->set_value(opt->key().c_str(), opt);
opt->release();
}
ingroup.erase(v.name);
}
}
for (auto& v : ingroup)
{
gb_json* grp = group_opt(v.first.c_str());
origin_->set_value(("grp-" + std::to_string(grpind++)).c_str(), grp);
grp->release();
for (auto& opt : v.second)
{
origin_->set_value(opt->key().c_str(), opt);
opt->release();
}
}
}
return origin_ != nullptr;
}
void device_option::init_depends(gb_json* opt)
{
gb_json* range = nullptr;
@ -1019,9 +1113,8 @@ bool device_option::to_now(bool init)
if (!origin_)
return false;
gb_json* from = nullptr, * to = nullptr, * tmp = now_; // new gb_json();
gb_json* from = nullptr, * to = nullptr, * tmp = new gb_json();
now_ = new gb_json();
from = origin_->first_child();
while (from)
{
@ -1035,10 +1128,10 @@ bool device_option::to_now(bool init)
if (to)
{
// copy cur value ...
if (tmp)
if (now_)
{
gb_json* now = nullptr;
tmp->get_value(to->key().c_str(), now);
now_->get_value(to->key().c_str(), now);
if (now)
{
std::string type("");
@ -1071,21 +1164,23 @@ bool device_option::to_now(bool init)
}
}
now_->set_value(name.c_str(), to);
tmp->set_value(name.c_str(), to);
to->release();
from = origin_->next_child();
}
else
{
now_->release();
now_ = nullptr;
tmp->release();
tmp = nullptr;
break;
}
}
if (tmp)
tmp->release();
if (now_)
now_->release();
now_ = tmp;
return now_ != nullptr;
}
@ -1122,16 +1217,42 @@ std::string device_option::option_value(gb_json* jsn, bool def_val)
return std::move(type);
}
std::string device_option::trans_group(const char* utf8, bool to_title)
{
if (to_title)
{
for (auto& v : g_known_group_with_sn)
{
if (v.name == utf8)
return v.title;
}
}
else
{
for (auto& v : g_known_group_with_sn)
{
if (v.title == utf8)
return v.name;
}
}
return utf8;
}
std::string device_option::get_group(int ind, bool title)
{
if (ind >= 0 && ind < _countof(g_known_group_with_sn))
return title ? g_known_group_with_sn[ind].title : g_known_group_with_sn[ind].name;
else
return "";
}
bool device_option::init(const char* opt_json)
{
bool ret = false;
std::string text(opt_json);
clear();
origin_ = new gb_json();
if (origin_->attach_text(&text[0]))
if (arrange_raw_json(opt_json))
{
text.clear();
ret = to_now(true);
if (!ret)
clear();

View File

@ -161,6 +161,8 @@ class device_option
static bool calc_simple_logic_expression(const char* expr, void* param);
void clear(void);
gb_json* group_opt(const char* title);
bool arrange_raw_json(const char* txt); // create origin_ and re-arrange groups
void init_depends(gb_json* opt);
gb_json* copy_opt(gb_json* from);
bool to_now(bool init);
@ -195,6 +197,9 @@ public:
return ret;
}
static std::string trans_group(const char* utf8, bool to_title);
static std::string get_group(int ind, bool title);
public:
bool init(const char* opt_json);
bool refine_data(const char* name, void* value); // return true if the 'value' is out of range and refined it in the range

View File

@ -251,7 +251,7 @@ bool simple_logic::parse_internal(const char* expr, int* end, void(*leaf)(const
else
{
good = false;
*end = ptr - expr;
*end = ptr - expr + 1;
break;
}
}
@ -287,7 +287,7 @@ bool simple_logic::parse_internal(const char* expr, int* end, void(*leaf)(const
else
{
good = false;
*end = ptr - expr;
*end = ptr - expr + 1;
break;
}
}
@ -335,7 +335,7 @@ bool simple_logic::parse_internal(const char* expr, int* end, void(*leaf)(const
string_util::skip_space(ptr);
}
if (first)
if (good && first)
{
if (need_oper)
{

View File

@ -151,28 +151,30 @@ void gb_json::from_cjson(cJSON* cj)
child = new gb_json();
child->from_cjson(cj->child);
child->key_ = cj->string ? cj->string : "";
if (child->key_.empty())
child->type_ = VAL_TYPE_OBJECT;
}
arr_val_.push_back(child);
cj = cj->next;
}
if (arr_val_.size() == 1 && arr_val_[0]->arr_val_.size() == 0)
{
gb_json* child = arr_val_[0];
if (!child->key_.empty()) // array
{
arr_val_.clear();
type_ = child->type_;
key_ = child->key_;
simple_val_.dval = child->simple_val_.dval;
strval_ = child->strval_;
for (auto& v : child->arr_val_)
arr_val_.push_back(v);
child->arr_val_.clear();
child->release();
}
}
//if (arr_val_.size() == 1 && arr_val_[0]->arr_val_.size() == 0)
//{
// gb_json* child = arr_val_[0];
//
// if (!child->key_.empty()) // array
// {
// arr_val_.clear();
// type_ = child->type_;
// key_ = child->key_;
// simple_val_.dval = child->simple_val_.dval;
// strval_ = child->strval_;
// for (auto& v : child->arr_val_)
// arr_val_.push_back(v);
// child->arr_val_.clear();
// child->release();
// }
//}
if (arr_val_.size())
{