#include "base_opt.h" #include #include #include #include sane_opt_provider::sane_opt_provider() { set_where(nullptr); } sane_opt_provider::~sane_opt_provider() { for (auto& v : following_) v.second->release(); following_.clear(); } std::string sane_opt_provider::sane_value_2_text(const char* type, void* value) { if(strcmp(type, JSON_SANE_TYPE_BOOL) == 0) return *(bool*)value ? "true" : "false"; else if(strcmp(type, JSON_SANE_TYPE_INT) == 0) return std::to_string(*(int*)value); else if(strcmp(type, JSON_SANE_TYPE_FIXED) == 0) return std::to_string(*(double*)value); else if(strcmp(type, JSON_SANE_TYPE_STRING) == 0) return (char*)value; else return ""; } bool sane_opt_provider::set_opt_value(gb_json* opt, void* value, const char* key) { std::string type(""); bool ret = true; opt->get_value("type", type); if (!key || *key == 0) key = "cur"; if (type == JSON_SANE_TYPE_BOOL) { opt->set_value(key, *(bool*)value); } else if (type == JSON_SANE_TYPE_INT) { opt->set_value(key, *(int*)value); } else if (type == JSON_SANE_TYPE_FIXED) { opt->set_value(key, *(double*)value); } else if (type == JSON_SANE_TYPE_STRING) { opt->set_value(key, (char*)value); } else { ret = false; } return ret; } bool sane_opt_provider::is_opt_value_equal(gb_json* opt, void* v1, void* v2) { std::string type(""); bool ret = true; opt->get_value("type", type); if (type == JSON_SANE_TYPE_BOOL) { ret = *(bool*)v1 == *(bool*)v2; } else if (type == JSON_SANE_TYPE_INT) { ret = *(int*)v1 == *(int*)v2; } else if (type == JSON_SANE_TYPE_FIXED) { ret = *(double*)v1 == *(double*)v2; } else if (type == JSON_SANE_TYPE_STRING) { ret = strcmp((char*)v1, (char*)v2) == 0; } return ret; } bool sane_opt_provider::set_opt_json_text(char* txt) { gb_json* jsn = new gb_json(); bool ret = jsn->attach_text(txt); jsn->release(); if (ret) opt_jsn_txt_ = txt; else opt_jsn_txt_ = ""; return ret; } void sane_opt_provider::set_where(const char* where) { if (where && *where) { where_ = where; } else { char buf[20] = { 0 }; sprintf(buf, "%p", this); where_ = buf; } } const char* sane_opt_provider::get_opt_json(void) { return opt_jsn_txt_.c_str(); } const char* sane_opt_provider::from(void) { return where_.c_str(); } void sane_opt_provider::set_following_provider(const char* name, sane_opt_provider* following) { if (following_.count(name)) { following_[name]->release(); following_.erase(name); } if (following) { following_[name] = following; following->add_ref(); } } sane_opt_provider* sane_opt_provider::get_following(const char* name) { sane_opt_provider* prvd = nullptr; if (following_.count(name)) { prvd = following_[name]; if (prvd) prvd->add_ref(); } return prvd; } char* sane_opt_provider::get_value(const char* name, void* value, size_t* size, int* err) { if (err) *err = SCANNER_ERR_DEVICE_NOT_SUPPORT; if (size) *size = 0; return nullptr; } int sane_opt_provider::set_value(const char* name, void* val) { return SCANNER_ERR_DEVICE_NOT_SUPPORT; } void sane_opt_provider::enable(const char* name, bool able) {}