newtx/sdk/sane_opt_json/base_opt.cpp

114 lines
2.1 KiB
C++

#include "base_opt.h"
#include <json/gb_json.h>
#include <huagao/hgscanner_error.h>
#include <sane/sane_ex.h>
#include <string.h>
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_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)
{}