#pragma once #if defined(WIN32) || defined(_WIN64) #include #include "../../code_device/hgsane/cJSON.h" #define PATH_SYMBOL "\\" #else #include "cJSON.h" #define PATH_SYMBOL "/" #define NULL nullptr #define DWORD_PTR char* #define _countof(a) sizeof(a) / sizeof(a[0]) #endif // #include #include #include #include namespace gb { class scanner_cfg; class refer { volatile long ref_; protected: refer(); virtual ~refer(); public: long add_ref(void); long release(void); }; class json : public refer { cJSON *obj_; cJSON *cur_child_; cJSON walk_head_; bool is_array_; cJSON* find_sibling(cJSON* first, const char* name, cJSON*** addr); cJSON* find_child(cJSON *parent, std::vector& path, bool create, cJSON*** addr = NULL); cJSON* find(const char* path, bool create = false, cJSON*** addr = NULL); protected: ~json(); public: json(char* json_txt = 0); static std::string to_string(cJSON* root, bool formatted); static std::string get_value_as_string(cJSON* root, bool integer = false); static void free_node_data(cJSON* node); static cJSON* create_element(bool is_array = false); static cJSON* create_element_with_name(const char* name); public: bool attach_text(char* json_txt); bool attach_cjson(cJSON* cjson); bool create_empty(bool array = false); void clear(void); std::string to_string(bool formatted); // can be path: child/value ... 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); bool get_value(const char* key, json*& val); // caller shoud call "delete" to free the returned object !!! bool get_value_as_string(const char* key, std::string& val, bool integer); bool get_as_array(const char* key, std::vector& val); bool first_child(std::string& val, std::string* name = NULL); bool next_child(std::string& val, std::string* name = NULL); 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, std::string val); bool set_value(const char* key, const char* val); bool set_value(const char* key, json* obj); bool change_key(const char* old_key, const char* new_key); bool remove(const char* key); }; 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); }; class sane_config_schm : public refer { std::string scheme_name_; scanner_cfg *scanner_; std::string file_; json* jsn_; json* bkp_; json* def_val_; bool in_setting_; std::map id_name_; // (id, default-val) void clear(); std::string default_value(const char* name); protected: ~sane_config_schm(); public: sane_config_schm(scanner_cfg* scanner = nullptr); static std::string opt_data_appendix_; static bool hex(unsigned char ch, unsigned char* val); static bool hex_char(const char* data, unsigned char* val); static std::string to_hex_letter(const char* data, size_t bytes); static std::string from_hex_letter(const char* data, size_t bytes); static bool is_option_data(std::string& name); // reset baase option name into 'name' if name was option data, and return true public: sane_config_schm* copy(void); bool load_from_file(const char* file); bool load_from_mem(const char* mem, bool in_b64 = true); bool save_to(const char* file); void set_default_value(int sn, const char* name, const char* val, size_t bytes); void copy_default_value(sane_config_schm* from); bool first_config(std::string& name, std::string& val); bool next_config(std::string& name, std::string& val); bool get_config(const char* name, std::string& val); void begin_setting(bool restore = false); void config_changed(const char* name, const char* val, size_t bytes, bool extra = false); void config_changed(int sn, const char* val, size_t bytes, bool extra = false); void remove_config(const char* name); void set_value(const char* name, const char* val, size_t bytes, bool extra = false); bool has_changed(void); void end_setting(bool cancel); int id_from_name(const char* name); std::string to_text_stream(bool b64 = true, bool with_ver = true); std::string get_version(void); std::string get_scheme_name(void); void set_scheme_name(const char* name); void update(bool(* is_float)(int, void*), void* param, const char*(* t2n)(const char*), std::string* discard = NULL); }; 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, ... typedef struct _cfg_schm { std::string name; sane_config_schm* schm; bool operator==(const char* n) { return name == n; } }CFGSCHM; std::vector schemes_; static std::string global_name_; static std::string cur_sel_; static std::string default_setting_name_; void clear(void); void init_version(void); void init_select(void); void walk_sibling_schemes(cJSON* first); protected: ~scanner_cfg(); public: scanner_cfg(); typedef struct _update_func { void(* trans_number)(const char* name, std::string& val, void* param); const char* (* title2name)(const char* title, void* param); std::string discard_msg; // update failed items ... void* func_param; }UDF, *LPUDF; static bool update(const char* file, LPUDF func); public: int load_file(const char* file); int load_mem(const char* mem); int save(const char* file = nullptr); void get_all_schemes(std::vector& 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 std::string get_current_scheme_name(void); bool remove_scheme(const char* scheme_name); void remove_all_schemes(void); 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 bool add_scheme(sane_config_schm* schm, const char* name = nullptr); bool rename_scheme(const char* from, const char* to); }; };