修复浮点数1.0在SANE属性中被解析为整数的问题 - 首次添加的时候作修正

This commit is contained in:
gb 2024-01-13 16:15:33 +08:00
parent dcf2c0215a
commit 94982d331f
4 changed files with 103 additions and 4 deletions

View File

@ -960,3 +960,36 @@ bool gb_json::operator!=(const gb_json& r)
{ {
return !(*this == r); return !(*this == r);
} }
bool gb_json::revise_number_type(bool dbval)
{
bool chg = false;
if (dbval)
{
// int -> double
if (type_ == VAL_TYPE_INT)
{
type_ = VAL_TYPE_FLOAT;
simple_val_.dval = simple_val_.nval;
chg = true;
}
else
chg = type_ == VAL_TYPE_FLOAT;
}
else
{
// double -> int
if (type_ == VAL_TYPE_FLOAT)
{
type_ = VAL_TYPE_INT;
simple_val_.nval = simple_val_.dval + .5f;
chg = true;
}
else
chg = type_ == VAL_TYPE_INT;
}
return chg;
}

View File

@ -115,6 +115,11 @@ public:
bool operator==(const gb_json& r); bool operator==(const gb_json& r);
bool operator!=(const gb_json& r); bool operator!=(const gb_json& r);
// [2024-01-13] double value maybe consider as integer if the number just an integer, this method is to set numbre to right type
// dbval - true: make integer to double and simple_val_.dval = simple_val_.nval;
// false: make double to integer and simple_val_.nval = simple_val_.dval
bool revise_number_type(bool dbval);
}; };
#ifdef DUMP_JSON_OBJECT_LIFE #ifdef DUMP_JSON_OBJECT_LIFE

View File

@ -1239,6 +1239,12 @@ bool device_option::arrange_raw_json(sane_opt_provider* sop)
child->get_value("type", str); child->get_value("type", str);
if (str != JSON_SANE_TYPE_GROUP) // omit group if (str != JSON_SANE_TYPE_GROUP) // omit group
{ {
if (str == JSON_SANE_TYPE_FIXED)
{
// revise cur and default
device_option::revise_number_type(child, true);
}
if(no_grp_) if(no_grp_)
{ {
insert_option(child, sop); insert_option(child, sop);
@ -1329,7 +1335,7 @@ void device_option::init_depends(gb_json* opt)
delete v; delete v;
} }
} }
gb_json* device_option::copy_opt(gb_json* from) gb_json* device_option::copy_opt(gb_json* from, bool* changed_cur)
{ {
std::string text(from->to_string()); std::string text(from->to_string());
gb_json* to = new gb_json(); gb_json* to = new gb_json();
@ -1385,6 +1391,9 @@ gb_json* device_option::copy_opt(gb_json* from)
if (!from->get_value("readonly", rdo)) if (!from->get_value("readonly", rdo))
rdo = false; rdo = false;
to->get_value("type", type); to->get_value("type", type);
if (changed_cur)
*changed_cur = apply_cur;
if (type == JSON_SANE_TYPE_BOOL) if (type == JSON_SANE_TYPE_BOOL)
{ {
to->set_value("default", *(bool*)val.c_str()); to->set_value("default", *(bool*)val.c_str());
@ -1421,6 +1430,8 @@ gb_json* device_option::copy_opt(gb_json* from)
update_provider_value(to->key().c_str(), &val[0], rdo); update_provider_value(to->key().c_str(), &val[0], rdo);
} }
} }
else if (changed_cur)
*changed_cur = false;
} }
// 4: range value ... // 4: range value ...
@ -1560,6 +1571,7 @@ bool device_option::to_now(bool init, bool* changed)
while (from) while (from)
{ {
std::string name(from->key()); std::string name(from->key());
bool cur_chged = false;
if (init) if (init)
init_depends(from); init_depends(from);
@ -1577,12 +1589,12 @@ bool device_option::to_now(bool init, bool* changed)
} }
} }
to = copy_opt(from); to = copy_opt(from, &cur_chged);
from->release(); from->release();
if (to) if (to)
{ {
// copy cur value ... // copy cur value ...
if (now_) if (now_ && !cur_chged)
{ {
gb_json* now = nullptr; gb_json* now = nullptr;
now_->get_value(to->key().c_str(), now); now_->get_value(to->key().c_str(), now);
@ -1718,6 +1730,54 @@ std::string device_option::option_value(gb_json* jsn, bool def_val)
return std::move(type); return std::move(type);
} }
void device_option::revise_number_type(gb_json* opt, bool to_double)
{
gb_json* child = nullptr;
opt->get_value("cur", child);
if (child)
{
if (child->is_leaf_node())
child->revise_number_type(to_double);
//else
// no object now, fixed me if be object
child->release();
}
opt->get_value("default", child);
if (child)
{
if (child->is_leaf_node())
child->revise_number_type(to_double);
else
{
gb_json* v = child->first_child();
while (v)
{
if (v->is_leaf_node())
v->revise_number_type(to_double);
v->release();
v = child->next_child();
}
}
child->release();
}
opt->get_value("range", child);
if (child)
{
gb_json* v = child->first_child();
while (v)
{
if (v->is_leaf_node())
v->revise_number_type(to_double);
v->release();
v = child->next_child();
}
child->release();
}
}
std::string device_option::trans_group(const char* utf8, bool to_title) std::string device_option::trans_group(const char* utf8, bool to_title)
{ {

View File

@ -181,13 +181,14 @@ class device_option : public refer
void insert_option(gb_json* opt, sane_opt_provider* from, const char* group = nullptr); void insert_option(gb_json* opt, sane_opt_provider* from, const char* group = nullptr);
bool arrange_raw_json(sane_opt_provider* sop); // create origin_ and re-arrange groups bool arrange_raw_json(sane_opt_provider* sop); // create origin_ and re-arrange groups
void init_depends(gb_json* opt); void init_depends(gb_json* opt);
gb_json* copy_opt(gb_json* from); gb_json* copy_opt(gb_json* from, bool *changed_cur = nullptr);
int visibility(gb_json* jsn); int visibility(gb_json* jsn);
bool to_now(bool init, bool* changed); bool to_now(bool init, bool* changed);
void update_provider_value(const char* name, void* value, bool skip_first = false/*readonly value should skip first*/); void update_provider_value(const char* name, void* value, bool skip_first = false/*readonly value should skip first*/);
protected: protected:
static std::string option_value(gb_json* jsn, bool def_val); static std::string option_value(gb_json* jsn, bool def_val);
static void revise_number_type(gb_json* opt, bool to_double);
template<class T> template<class T>
static condition_value* to_condition_value(gb_json* jsn, const char* key, const char* type, device_option* parent) static condition_value* to_condition_value(gb_json* jsn, const char* key, const char* type, device_option* parent)