mirror of
https://github.com/Dushistov/sdcv.git
synced 2025-12-15 17:31:56 +00:00
lib: c++11
This commit is contained in:
1096
src/lib/lib.cpp
1096
src/lib/lib.cpp
File diff suppressed because it is too large
Load Diff
@@ -1,11 +1,11 @@
|
|||||||
#ifndef __SD_LIB_H__
|
#pragma once
|
||||||
#define __SD_LIB_H__
|
|
||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
#include "dictziplib.hpp"
|
#include "dictziplib.hpp"
|
||||||
|
|
||||||
@@ -16,8 +16,8 @@ struct cacheItem {
|
|||||||
guint32 offset;
|
guint32 offset;
|
||||||
gchar *data;
|
gchar *data;
|
||||||
//write code here to make it inline
|
//write code here to make it inline
|
||||||
cacheItem() {data= NULL;}
|
cacheItem() { data = nullptr;}
|
||||||
~cacheItem() {g_free(data);}
|
~cacheItem() { g_free(data); }
|
||||||
};
|
};
|
||||||
|
|
||||||
const int WORDDATA_CACHE_NUM = 10;
|
const int WORDDATA_CACHE_NUM = 10;
|
||||||
@@ -25,18 +25,27 @@ const int INVALID_INDEX=-100;
|
|||||||
|
|
||||||
class DictBase {
|
class DictBase {
|
||||||
public:
|
public:
|
||||||
DictBase();
|
DictBase() {}
|
||||||
~DictBase();
|
~DictBase() {
|
||||||
|
if (dictfile)
|
||||||
|
fclose(dictfile);
|
||||||
|
}
|
||||||
|
DictBase(const DictBase&) = delete;
|
||||||
|
DictBase& operator=(const DictBase&) = delete;
|
||||||
gchar * GetWordData(guint32 idxitem_offset, guint32 idxitem_size);
|
gchar * GetWordData(guint32 idxitem_offset, guint32 idxitem_size);
|
||||||
bool containSearchData();
|
bool containSearchData() const {
|
||||||
|
if (sametypesequence.empty())
|
||||||
|
return true;
|
||||||
|
return sametypesequence.find_first_of("mlgxty") != std::string::npos;
|
||||||
|
}
|
||||||
bool SearchData(std::vector<std::string> &SearchWords, guint32 idxitem_offset, guint32 idxitem_size, gchar *origin_data);
|
bool SearchData(std::vector<std::string> &SearchWords, guint32 idxitem_offset, guint32 idxitem_size, gchar *origin_data);
|
||||||
protected:
|
protected:
|
||||||
std::string sametypesequence;
|
std::string sametypesequence;
|
||||||
FILE *dictfile;
|
FILE *dictfile = nullptr;
|
||||||
std::auto_ptr<dictData> dictdzfile;
|
std::unique_ptr<dictData> dictdzfile;
|
||||||
private:
|
private:
|
||||||
cacheItem cache[WORDDATA_CACHE_NUM];
|
cacheItem cache[WORDDATA_CACHE_NUM];
|
||||||
gint cache_cur;
|
gint cache_cur = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
//this structure contain all information about dictionary
|
//this structure contain all information about dictionary
|
||||||
@@ -51,64 +60,69 @@ struct DictInfo {
|
|||||||
std::string description;
|
std::string description;
|
||||||
guint32 index_file_size;
|
guint32 index_file_size;
|
||||||
std::string sametypesequence;
|
std::string sametypesequence;
|
||||||
|
|
||||||
bool load_from_ifo_file(const std::string& ifofilename, bool istreedict);
|
bool load_from_ifo_file(const std::string& ifofilename, bool istreedict);
|
||||||
};
|
};
|
||||||
|
|
||||||
class index_file {
|
class IIndexFile {
|
||||||
public:
|
public:
|
||||||
guint32 wordentry_offset;
|
guint32 wordentry_offset;
|
||||||
guint32 wordentry_size;
|
guint32 wordentry_size;
|
||||||
|
|
||||||
virtual ~index_file() {}
|
virtual ~IIndexFile() {}
|
||||||
virtual bool load(const std::string& url, gulong wc, gulong fsize) = 0;
|
virtual bool load(const std::string& url, gulong wc, gulong fsize) = 0;
|
||||||
virtual const gchar *get_key(glong idx) = 0;
|
virtual const gchar *get_key(glong idx) = 0;
|
||||||
virtual void get_data(glong idx) = 0;
|
virtual void get_data(glong idx) = 0;
|
||||||
virtual const gchar *get_key_and_data(glong idx) = 0;
|
virtual const gchar *get_key_and_data(glong idx) = 0;
|
||||||
virtual bool lookup(const char *str, glong &idx) = 0;
|
virtual bool lookup(const char *str, glong &idx) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Dict : public DictBase {
|
class Dict : public DictBase {
|
||||||
|
public:
|
||||||
|
Dict() {}
|
||||||
|
Dict(const Dict&) = delete;
|
||||||
|
Dict& operator=(const Dict&) = delete;
|
||||||
|
bool load(const std::string& ifofilename);
|
||||||
|
|
||||||
|
gulong narticles() const { return wordcount; }
|
||||||
|
const std::string& dict_name() const { return bookname; }
|
||||||
|
const std::string& ifofilename() const { return ifo_file_name; }
|
||||||
|
|
||||||
|
const gchar *get_key(glong index) { return idx_file->get_key(index); }
|
||||||
|
gchar *get_data(glong index) {
|
||||||
|
idx_file->get_data(index);
|
||||||
|
return DictBase::GetWordData(idx_file->wordentry_offset, idx_file->wordentry_size);
|
||||||
|
}
|
||||||
|
void get_key_and_data(glong index, const gchar **key, guint32 *offset, guint32 *size) {
|
||||||
|
*key = idx_file->get_key_and_data(index);
|
||||||
|
*offset = idx_file->wordentry_offset;
|
||||||
|
*size = idx_file->wordentry_size;
|
||||||
|
}
|
||||||
|
bool Lookup(const char *str, glong &idx) { return idx_file->lookup(str, idx); }
|
||||||
|
|
||||||
|
bool LookupWithRule(GPatternSpec *pspec, glong *aIndex, int iBuffLen);
|
||||||
private:
|
private:
|
||||||
std::string ifo_file_name;
|
std::string ifo_file_name;
|
||||||
gulong wordcount;
|
gulong wordcount;
|
||||||
std::string bookname;
|
std::string bookname;
|
||||||
|
|
||||||
std::auto_ptr<index_file> idx_file;
|
std::unique_ptr<IIndexFile> idx_file;
|
||||||
|
|
||||||
bool load_ifofile(const std::string& ifofilename, gulong &idxfilesize);
|
bool load_ifofile(const std::string& ifofilename, gulong &idxfilesize);
|
||||||
public:
|
|
||||||
Dict() {}
|
|
||||||
bool load(const std::string& ifofilename);
|
|
||||||
|
|
||||||
gulong narticles() { return wordcount; }
|
|
||||||
const std::string& dict_name() { return bookname; }
|
|
||||||
const std::string& ifofilename() { return ifo_file_name; }
|
|
||||||
|
|
||||||
const gchar *get_key(glong index) { return idx_file->get_key(index); }
|
|
||||||
gchar *get_data(glong index)
|
|
||||||
{
|
|
||||||
idx_file->get_data(index);
|
|
||||||
return DictBase::GetWordData(idx_file->wordentry_offset, idx_file->wordentry_size);
|
|
||||||
}
|
|
||||||
void get_key_and_data(glong index, const gchar **key, guint32 *offset, guint32 *size)
|
|
||||||
{
|
|
||||||
*key = idx_file->get_key_and_data(index);
|
|
||||||
*offset = idx_file->wordentry_offset;
|
|
||||||
*size = idx_file->wordentry_size;
|
|
||||||
}
|
|
||||||
bool Lookup(const char *str, glong &idx) { return idx_file->lookup(str, idx); }
|
|
||||||
|
|
||||||
bool LookupWithRule(GPatternSpec *pspec, glong *aIndex, int iBuffLen);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef std::list<std::string> strlist_t;
|
typedef std::list<std::string> strlist_t;
|
||||||
|
|
||||||
class Libs {
|
class Libs {
|
||||||
public:
|
public:
|
||||||
typedef void (*progress_func_t)(void);
|
Libs(std::function<void(void)> f = std::function<void(void)>()) {
|
||||||
|
progress_func = f;
|
||||||
Libs(progress_func_t f=NULL);
|
iMaxFuzzyDistance = MAX_FUZZY_DISTANCE; //need to read from cfg.
|
||||||
|
}
|
||||||
~Libs();
|
~Libs();
|
||||||
|
Libs(const Libs&) = delete;
|
||||||
|
Libs& operator=(const Libs&) = delete;
|
||||||
|
|
||||||
void load_dict(const std::string& url);
|
void load_dict(const std::string& url);
|
||||||
void load(const strlist_t& dicts_dirs,
|
void load(const strlist_t& dicts_dirs,
|
||||||
const strlist_t& order_list,
|
const strlist_t& order_list,
|
||||||
@@ -117,16 +131,16 @@ public:
|
|||||||
const strlist_t& order_list,
|
const strlist_t& order_list,
|
||||||
const strlist_t& disable_list);
|
const strlist_t& disable_list);
|
||||||
|
|
||||||
glong narticles(int idict) { return oLib[idict]->narticles(); }
|
glong narticles(int idict) const { return oLib[idict]->narticles(); }
|
||||||
const std::string& dict_name(int idict) { return oLib[idict]->dict_name(); }
|
const std::string& dict_name(int idict) const { return oLib[idict]->dict_name(); }
|
||||||
gint ndicts() const { return oLib.size(); }
|
gint ndicts() const { return oLib.size(); }
|
||||||
|
|
||||||
const gchar * poGetWord(glong iIndex,int iLib) {
|
const gchar *poGetWord(glong iIndex, int iLib) {
|
||||||
return oLib[iLib]->get_key(iIndex);
|
return oLib[iLib]->get_key(iIndex);
|
||||||
}
|
}
|
||||||
gchar * poGetWordData(glong iIndex,int iLib) {
|
gchar * poGetWordData(glong iIndex,int iLib) {
|
||||||
if (iIndex==INVALID_INDEX)
|
if (iIndex == INVALID_INDEX)
|
||||||
return NULL;
|
return nullptr;
|
||||||
return oLib[iLib]->get_data(iIndex);
|
return oLib[iLib]->get_data(iIndex);
|
||||||
}
|
}
|
||||||
const gchar *poGetCurrentWord(glong *iCurrent);
|
const gchar *poGetCurrentWord(glong *iCurrent);
|
||||||
@@ -145,14 +159,13 @@ public:
|
|||||||
private:
|
private:
|
||||||
std::vector<Dict *> oLib; // word Libs.
|
std::vector<Dict *> oLib; // word Libs.
|
||||||
int iMaxFuzzyDistance;
|
int iMaxFuzzyDistance;
|
||||||
progress_func_t progress_func;
|
std::function<void(void)> progress_func;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
typedef enum {
|
enum query_t {
|
||||||
qtSIMPLE, qtREGEXP, qtFUZZY, qtDATA
|
qtSIMPLE, qtREGEXP, qtFUZZY, qtDATA
|
||||||
} query_t;
|
};
|
||||||
|
|
||||||
extern query_t analyze_query(const char *s, std::string& res);
|
extern query_t analyze_query(const char *s, std::string& res);
|
||||||
|
|
||||||
#endif//!__SD_LIB_H__
|
|
||||||
Reference in New Issue
Block a user