2022-07-01 07:24:58 +00:00
# pragma once
2022-07-19 01:42:16 +00:00
# if defined(WIN32) || defined(_WIN64)
2022-07-01 07:24:58 +00:00
# include <Windows.h>
2022-10-28 08:55:11 +00:00
# include "../../code_device/hgsane/cJSON.h"
2022-10-26 06:41:18 +00:00
# define PATH_SYMBOL "\\"
# else
2022-10-28 08:55:11 +00:00
# include "cJSON.h"
2022-10-26 06:41:18 +00:00
# define PATH_SYMBOL " / "
2022-10-28 08:55:11 +00:00
# define NULL nullptr
# define DWORD_PTR char*
# define _countof(a) sizeof(a) / sizeof(a[0])
2022-07-01 07:24:58 +00:00
# endif
2022-10-28 08:55:11 +00:00
//
2022-07-01 07:24:58 +00:00
# include <vector>
# include <string>
# include <map>
2022-10-28 08:55:11 +00:00
# include <algorithm>
2022-07-01 07:24:58 +00:00
namespace gb
{
2022-10-26 06:41:18 +00:00
class scanner_cfg ;
2022-10-25 08:41:09 +00:00
class refer
{
volatile long ref_ ;
protected :
refer ( ) ;
virtual ~ refer ( ) ;
public :
long add_ref ( void ) ;
long release ( void ) ;
} ;
class json : public refer
2022-07-01 07:24:58 +00:00
{
2023-02-07 08:55:51 +00:00
enum val_type
{
VAL_TYPE_NULL = 0 ,
VAL_TYPE_BOOL ,
VAL_TYPE_INT ,
VAL_TYPE_FLOAT ,
VAL_TYPE_STRING ,
VAL_TYPE_OBJECT ,
VAL_TYPE_ARRAY ,
} ;
val_type type_ ;
std : : string key_ ;
union
{
bool bval ;
int nval ;
double dval ;
} simple_val_ ;
std : : string strval_ ;
std : : vector < json * > arr_val_ ;
size_t cur_child_ ;
2022-07-01 07:24:58 +00:00
2023-02-07 08:55:51 +00:00
static std : : string object_key ( json * jsn ) ;
static std : : string array_key ( json * jsn ) ;
2022-07-01 07:24:58 +00:00
2023-02-07 08:55:51 +00:00
void from_cjson ( cJSON * cj ) ;
json * find_child ( const char * key , bool remove = false ) ;
2022-10-25 08:41:09 +00:00
2022-07-01 07:24:58 +00:00
public :
json ( char * json_txt = 0 ) ;
2023-02-07 08:55:51 +00:00
protected :
json ( const char * key , bool val ) ;
json ( const char * key , int val ) ;
json ( const char * key , double val ) ;
json ( const char * key , const char * val ) ;
json ( json & r ) ;
~ json ( ) ;
2022-07-01 07:24:58 +00:00
public :
2023-02-07 08:55:51 +00:00
// parse/un-parse ...
void copy_from ( json & r ) ;
2022-07-01 07:24:58 +00:00
bool attach_text ( char * json_txt ) ;
2023-02-07 08:55:51 +00:00
void clear ( bool as_array = false ) ;
std : : string to_string ( void ) ;
// attributes ...
std : : string & key ( void ) ;
bool is_array ( void ) ;
bool is_leaf_node ( void ) ; // whether this object is a leaf node contains final value
2022-07-01 07:24:58 +00:00
2023-02-07 08:55:51 +00:00
// value access ...
2022-07-01 07:24:58 +00:00
bool get_value ( const char * key , bool & val ) ;
bool get_value ( const char * key , int & val ) ;
bool get_value ( const char * key , double & val ) ;
bool get_value ( const char * key , std : : string & val ) ;
2023-02-07 08:55:51 +00:00
bool get_value ( const char * key , json * & val ) ;
2022-07-01 07:24:58 +00:00
2023-02-07 08:55:51 +00:00
// enumeration ...
size_t children ( void ) ; // return children count if was object or array, or else -1 returned
json * child ( size_t ind ) ;
json * first_child ( void ) ;
json * next_child ( void ) ;
2022-07-01 07:24:58 +00:00
2023-02-07 08:55:51 +00:00
// change the item matching 'key', otherwise add a new item
2022-07-01 07:24:58 +00:00
bool set_value ( const char * key , bool val ) ;
bool set_value ( const char * key , int val ) ;
bool set_value ( const char * key , double val ) ;
bool set_value ( const char * key , const char * val ) ;
2023-02-07 08:55:51 +00:00
bool set_value ( const char * key , json * val ) ;
// operator+= only for array
json & operator + = ( bool val ) ;
json & operator + = ( int val ) ;
json & operator + = ( double val ) ;
json & operator + = ( const char * val ) ;
json & operator + = ( json * val ) ;
2022-07-01 07:24:58 +00:00
2023-02-07 08:55:51 +00:00
// remove item
json & operator - = ( int ind ) ;
2022-07-01 07:24:58 +00:00
bool remove ( const char * key ) ;
2023-02-07 08:55:51 +00:00
bool remove ( json * child ) ;
bool remove ( int ind ) ;
// position management
int index ( json * child ) ;
int index_move_to ( json * child , int ind ) ;
// leaf node value ...
bool value ( bool & val ) ;
bool value ( int & val ) ;
bool value ( double & val ) ;
bool value ( std : : string & val ) ;
json & operator = ( bool val ) ;
json & operator = ( int val ) ;
json & operator = ( double val ) ;
json & operator = ( const char * val ) ;
2022-07-01 07:24:58 +00:00
} ;
class base64
{
char base64_ind_ [ 128 ] ;
char base64_char_ [ 80 ] ;
char padding_char_ ;
bool is_valid_base64_table ( const char * table ) ;
bool initialize_base64_table ( const char * table ) ;
public :
base64 ( ) ;
~ base64 ( ) ;
public :
bool set_base64_table ( const char * table = NULL ) ;
std : : string encode ( const char * data , size_t bytes , unsigned int line_bytes = - 1 , bool need_padding = true ) ;
std : : string decode ( const char * data , size_t bytes ) ;
} ;
2023-01-29 07:03:32 +00:00
// NOTE: 2021-01-28 all stored string value are in default language (i.e. Chinese-Simplified), user should call app_language::to_default_language/from_default_language for real value
2022-10-25 08:41:09 +00:00
class sane_config_schm : public refer
2022-07-01 07:24:58 +00:00
{
2022-10-25 08:41:09 +00:00
std : : string scheme_name_ ;
2022-10-26 06:41:18 +00:00
scanner_cfg * scanner_ ;
std : : string file_ ;
2022-07-01 07:24:58 +00:00
json * jsn_ ;
json * bkp_ ;
2023-02-07 08:55:51 +00:00
json * def_val_ ; // name_id: id, name_val: val
2022-07-01 07:24:58 +00:00
bool in_setting_ ;
void clear ( ) ;
2022-10-08 06:13:09 +00:00
std : : string default_value ( const char * name ) ;
2022-07-01 07:24:58 +00:00
2022-10-25 08:41:09 +00:00
protected :
~ sane_config_schm ( ) ;
2022-07-01 07:24:58 +00:00
public :
2022-10-26 06:41:18 +00:00
sane_config_schm ( scanner_cfg * scanner = nullptr ) ;
2022-07-01 07:24:58 +00:00
2022-10-13 03:58:29 +00:00
static std : : string opt_data_appendix_ ;
2023-02-07 08:55:51 +00:00
static std : : string opt_def_id_appendix_ ;
static std : : string opt_def_val_appendix_ ;
static std : : string opt_def_type_appendix_ ;
static std : : string opt_def_title_appendix_ ;
2022-07-01 07:24:58 +00:00
static bool hex ( unsigned char ch , unsigned char * val ) ;
static bool hex_char ( const char * data , unsigned char * val ) ;
2022-10-26 06:41:18 +00:00
static std : : string to_hex_letter ( const char * data , size_t bytes ) ;
static std : : string from_hex_letter ( const char * data , size_t bytes ) ;
2022-10-13 03:58:29 +00:00
static bool is_option_data ( std : : string & name ) ; // reset baase option name into 'name' if name was option data, and return true
2023-02-07 08:55:51 +00:00
static void set_default_value ( json * jsn , int id , const char * name , const char * title , void * data , size_t bytes , int type ) ;
static int option_name_2_id ( json * jsn , const char * name ) ;
static int option_type ( json * jsn , const char * name ) ;
static std : : string option_title ( json * jsn , const char * name ) ;
static std : : string option_default_value ( json * jsn , const char * name ) ;
static std : : string sane_option_value_2_string ( void * val , size_t bytes , int type ) ;
2022-07-01 07:24:58 +00:00
public :
2022-10-28 08:55:11 +00:00
sane_config_schm * copy ( void ) ;
2022-10-26 06:41:18 +00:00
bool load_from_file ( const char * file ) ;
bool load_from_mem ( const char * mem , bool in_b64 = true ) ;
bool save_to ( const char * file ) ;
2023-02-07 08:55:51 +00:00
void set_default_value ( int sn , const char * name , const char * title , const char * val , size_t bytes , int type ) ;
2022-10-08 06:13:09 +00:00
bool first_config ( std : : string & name , std : : string & val ) ;
bool next_config ( std : : string & name , std : : string & val ) ;
2022-10-28 08:55:11 +00:00
bool get_config ( const char * name , std : : string & val ) ;
2022-07-01 07:24:58 +00:00
void begin_setting ( bool restore = false ) ;
2022-10-25 08:41:09 +00:00
void config_changed ( const char * name , const char * val , size_t bytes , bool extra = false ) ;
2022-10-13 03:58:29 +00:00
void config_changed ( int sn , const char * val , size_t bytes , bool extra = false ) ;
2022-10-08 06:13:09 +00:00
void remove_config ( const char * name ) ;
2022-10-26 06:41:18 +00:00
void set_value ( const char * name , const char * val , size_t bytes , bool extra = false ) ;
2023-02-07 08:55:51 +00:00
bool has_changed ( int * items = nullptr ) ;
2022-07-01 07:24:58 +00:00
void end_setting ( bool cancel ) ;
2022-10-08 06:13:09 +00:00
int id_from_name ( const char * name ) ;
2022-10-26 06:41:18 +00:00
std : : string to_text_stream ( bool b64 = true , bool with_ver = true ) ;
2022-09-27 01:48:57 +00:00
std : : string get_version ( void ) ;
2022-10-25 08:41:09 +00:00
std : : string get_scheme_name ( void ) ;
void set_scheme_name ( const char * name ) ;
2023-02-07 08:55:51 +00:00
std : : string auto_gen_scheme_name ( const char * ( __stdcall * lang_trans ) ( const char * , bool /*true - default language to cur language*/ , void * ) , void * param ) ;
2022-07-01 07:24:58 +00:00
} ;
2022-10-26 06:41:18 +00:00
class scanner_cfg : public refer
{
// format: in base64
//
// {
// "global": {
// "ver": "4.33",
// "cur": -1
// },
// "scheme_1": sane_config_schm*,
// "scheme_2": sane_config_schm*,
// "scheme_3": sane_config_schm*,
// ...
// }
//
std : : string path_ ;
std : : string scanner_name_ ; // scanner type: HUAGOSCAN G100 - 0100
json * global_ ; // version, current scheme, ...
2023-02-07 08:55:51 +00:00
json * opt_default_value_ ;
2022-10-26 06:41:18 +00:00
2023-02-07 08:55:51 +00:00
const char * ( __stdcall * lang_trans_ ) ( const char * , bool /*true - default language to cur language*/ , void * ) ;
void * lang_param_ ;
2022-10-26 06:41:18 +00:00
typedef struct _cfg_schm
{
std : : string name ;
2023-02-07 08:55:51 +00:00
bool should_rename ;
2022-10-26 06:41:18 +00:00
sane_config_schm * schm ;
2023-02-07 08:55:51 +00:00
struct _cfg_schm ( )
{
name = " " ;
should_rename = false ;
schm = nullptr ;
}
2022-10-26 06:41:18 +00:00
bool operator = = ( const char * n )
{
return name = = n ;
}
} CFGSCHM ;
std : : vector < CFGSCHM > schemes_ ;
2023-02-07 08:55:51 +00:00
static const char * __stdcall language_trans ( const char * in , bool from_def , void * param ) ;
2022-10-26 06:41:18 +00:00
void clear ( void ) ;
void init_version ( void ) ;
void init_select ( void ) ;
2023-02-07 08:55:51 +00:00
const char * trans_language ( const char * in , bool from_default ) ;
2022-10-26 06:41:18 +00:00
protected :
~ scanner_cfg ( ) ;
public :
scanner_cfg ( ) ;
typedef struct _update_func
{
2022-10-28 08:55:11 +00:00
void ( * trans_number ) ( const char * name , std : : string & val , void * param ) ;
const char * ( * title2name ) ( const char * title , void * param ) ;
2022-10-26 06:41:18 +00:00
std : : string discard_msg ; // update failed items ...
void * func_param ;
} UDF , * LPUDF ;
static bool update ( const char * file , LPUDF func ) ;
2023-02-07 08:55:51 +00:00
static std : : string global_name_ ;
static std : : string cur_sel_ ;
static std : : string default_setting_name_ ;
2022-10-26 06:41:18 +00:00
public :
int load_file ( const char * file ) ;
int load_mem ( const char * mem ) ;
int save ( const char * file = nullptr ) ;
2023-02-07 08:55:51 +00:00
void set_language_transform ( const char * ( __stdcall * lang_trans ) ( const char * , bool /*true - default language to cur language*/ , void * ) , void * param ) ;
2022-10-26 06:41:18 +00:00
2023-02-07 08:55:51 +00:00
json * get_option_default_value ( void ) ;
2022-10-26 06:41:18 +00:00
void get_all_schemes ( std : : vector < std : : string > & schemes ) ; // return all schemes name queue, the first is always be 'Default settings'
sane_config_schm * get_scheme ( const char * scheme_name = nullptr /*return current scheme if was null*/ ) ; // call sane_config_schm::release() if not use anymore
2023-02-07 08:55:51 +00:00
sane_config_schm * create_empty_scheme ( bool selected ) ; // create an empty scheme and add to scheme queue
2022-10-28 08:55:11 +00:00
std : : string get_current_scheme_name ( void ) ;
2023-02-07 08:55:51 +00:00
std : : string to_text_stream ( void ) ;
void set_default_value ( int id , const char * name , const char * title , void * data , size_t bytes , int type ) ;
int option_value_type ( const char * name ) ;
std : : string option_title ( const char * name ) ;
2022-10-26 06:41:18 +00:00
bool remove_scheme ( const char * scheme_name ) ;
2022-10-28 08:55:11 +00:00
void remove_all_schemes ( void ) ;
2022-10-26 06:41:18 +00:00
bool select_scheme ( const char * scheme_name ) ;
sane_config_schm * copy_scheme ( const char * cp_from_name ) ; // for UI setting, call release() if not use anymore
2023-02-07 08:55:51 +00:00
bool add_scheme ( sane_config_schm * schm , const char * name = nullptr , bool should_rename = false ) ;
2022-10-28 08:55:11 +00:00
bool rename_scheme ( const char * from , const char * to ) ;
2022-10-26 06:41:18 +00:00
} ;
2022-10-28 08:55:11 +00:00
} ;