refactoring: apply clang-format rules

This commit is contained in:
Evgeniy A. Dushistov
2017-08-09 07:46:27 +03:00
parent d0c0a0837f
commit 8f16ceae59
14 changed files with 2587 additions and 2537 deletions

View File

@@ -33,13 +33,12 @@
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <unistd.h>
#include <limits.h>
#include <fcntl.h> #include <fcntl.h>
#include <limits.h>
#include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "dictziplib.hpp" #include "dictziplib.hpp"
#define USE_CACHE 1 #define USE_CACHE 1
@@ -112,7 +111,6 @@
#define DICT_GZIP 2 #define DICT_GZIP 2
#define DICT_DZIP 3 #define DICT_DZIP 3
int DictData::read_header(const std::string &fname, int computeCRC) int DictData::read_header(const std::string &fname, int computeCRC)
{ {
FILE *str; FILE *str;
@@ -358,7 +356,6 @@ void DictData::read(char *buffer, unsigned long start, unsigned long size)
// ("dict_data_read( %p, %lu, %lu )\n", // ("dict_data_read( %p, %lu, %lu )\n",
//h, start, size )); //h, start, size ));
switch (this->type) { switch (this->type) {
case DICT_GZIP: case DICT_GZIP:
//err_fatal( __FUNCTION__, //err_fatal( __FUNCTION__,

View File

@@ -13,7 +13,8 @@ struct DictCache {
int count; int count;
}; };
class DictData { class DictData
{
public: public:
static const size_t DICT_CACHE_SIZE = 5; static const size_t DICT_CACHE_SIZE = 5;
@@ -22,6 +23,7 @@ public:
bool open(const std::string &filename, int computeCRC); bool open(const std::string &filename, int computeCRC);
void close(); void close();
void read(char *buffer, unsigned long start, unsigned long size); void read(char *buffer, unsigned long start, unsigned long size);
private: private:
const char *start; /* start of mmap'd area */ const char *start; /* start of mmap'd area */
const char *end; /* end of mmap'd area */ const char *end; /* end of mmap'd area */
@@ -52,4 +54,3 @@ private:
int read_header(const std::string &filename, int computeCRC); int read_header(const std::string &filename, int computeCRC);
}; };

View File

@@ -33,7 +33,6 @@ The Levenshtein distance algorithm has been used in:
* Plagiarism detection * Plagiarism detection
*/ */
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
@@ -68,30 +67,26 @@ int EditDistance::CalEditDistance(const gunichar *s,const gunichar *t,const int
{ {
int n = 0, m = 0, iLenDif, k, i, j, cost; int n = 0, m = 0, iLenDif, k, i, j, cost;
// Remove leftmost matching portion of strings // Remove leftmost matching portion of strings
while ( *s && (*s==*t) ) while (*s && (*s == *t)) {
{
s++; s++;
t++; t++;
} }
while (s[n]) while (s[n]) {
{
n++; n++;
} }
while (t[m]) while (t[m]) {
{
m++; m++;
} }
// Remove rightmost matching portion of strings by decrement n and m. // Remove rightmost matching portion of strings by decrement n and m.
while ( n && m && (*(s+n-1)==*(t+m-1)) ) while (n && m && (*(s + n - 1) == *(t + m - 1))) {
{ n--;
n--;m--; m--;
} }
if (m == 0 || n == 0 || d == nullptr) if (m == 0 || n == 0 || d == nullptr)
return (m + n); return (m + n);
if ( m < n ) if (m < n) {
{
const gunichar *temp = s; const gunichar *temp = s;
int itemp = n; int itemp = n;
s = t; s = t;
@@ -103,10 +98,10 @@ int EditDistance::CalEditDistance(const gunichar *s,const gunichar *t,const int
if (iLenDif >= limit) if (iLenDif >= limit)
return iLenDif; return iLenDif;
// step 1 // step 1
n++;m++; n++;
m++;
// d=(int*)malloc(sizeof(int)*m*n); // d=(int*)malloc(sizeof(int)*m*n);
if ( m*n > currentelements ) if (m * n > currentelements) {
{
currentelements = m * n * 2; // double the request currentelements = m * n * 2; // double the request
d = static_cast<int *>(realloc(d, sizeof(int) * currentelements)); d = static_cast<int *>(realloc(d, sizeof(int) * currentelements));
if (nullptr == d) if (nullptr == d)
@@ -118,11 +113,9 @@ int EditDistance::CalEditDistance(const gunichar *s,const gunichar *t,const int
for (k = 1; k < m; k++) for (k = 1; k < m; k++)
d[k * n] = k; d[k * n] = k;
// step 3 // step 3
for (i=1;i<n;i++) for (i = 1; i < n; i++) {
{
// first calculate column, d(i,j) // first calculate column, d(i,j)
for ( j=1;j<iLenDif+i;j++ ) for (j = 1; j < iLenDif + i; j++) {
{
cost = s[i - 1] == t[j - 1] ? 0 : 1; cost = s[i - 1] == t[j - 1] ? 0 : 1;
d[j * n + i] = minimum(d[(j - 1) * n + i] + 1, d[j * n + i - 1] + 1, d[(j - 1) * n + i - 1] + cost); d[j * n + i] = minimum(d[(j - 1) * n + i] + 1, d[j * n + i - 1] + 1, d[(j - 1) * n + i - 1] + cost);
#ifdef COVER_TRANSPOSITION #ifdef COVER_TRANSPOSITION
@@ -133,8 +126,7 @@ int EditDistance::CalEditDistance(const gunichar *s,const gunichar *t,const int
} }
// second calculate row, d(k,j) // second calculate row, d(k,j)
// now j==iLenDif+i; // now j==iLenDif+i;
for ( k=1;k<=i;k++ ) for (k = 1; k <= i; k++) {
{
cost = s[k - 1] == t[j - 1] ? 0 : 1; cost = s[k - 1] == t[j - 1] ? 0 : 1;
d[j * n + k] = minimum(d[(j - 1) * n + k] + 1, d[j * n + k - 1] + 1, d[(j - 1) * n + k - 1] + cost); d[j * n + k] = minimum(d[(j - 1) * n + k] + 1, d[j * n + k - 1] + 1, d[(j - 1) * n + k - 1] + cost);
#ifdef COVER_TRANSPOSITION #ifdef COVER_TRANSPOSITION
@@ -144,8 +136,7 @@ int EditDistance::CalEditDistance(const gunichar *s,const gunichar *t,const int
#endif #endif
} }
// test if d(i,j) limit gets equal or exceed // test if d(i,j) limit gets equal or exceed
if ( d[j*n+i] >= limit ) if (d[j * n + i] >= limit) {
{
return d[j * n + i]; return d[j * n + i];
} }
} }

View File

@@ -3,21 +3,24 @@
#include <cstdlib> #include <cstdlib>
#include <glib.h> #include <glib.h>
class EditDistance { class EditDistance
{
public: public:
EditDistance() { EditDistance()
{
currentelements = 2500; // It's enough for most conditions :-) currentelements = 2500; // It's enough for most conditions :-)
d = static_cast<int *>(malloc(sizeof(int) * currentelements)); d = static_cast<int *>(malloc(sizeof(int) * currentelements));
} }
~EditDistance() { ~EditDistance()
{
if (d != nullptr) if (d != nullptr)
free(d); free(d);
} }
EditDistance(const EditDistance &) = delete; EditDistance(const EditDistance &) = delete;
EditDistance &operator=(const EditDistance &) = delete; EditDistance &operator=(const EditDistance &) = delete;
int CalEditDistance(const gunichar *s, const gunichar *t, const int limit); int CalEditDistance(const gunichar *s, const gunichar *t, const int limit);
private: private:
int *d; int *d;
int currentelements; int currentelements;
}; };

View File

@@ -194,7 +194,6 @@ static std::string parse_data(const gchar *data, bool colorize_output)
p += sec_size; p += sec_size;
} }
return res; return res;
} }
@@ -285,10 +284,13 @@ void Library::print_search_result(FILE *out, const TSearchResult & res, bool &fi
} }
} }
namespace { namespace
class sdcv_pager final { {
class sdcv_pager final
{
public: public:
explicit sdcv_pager(bool ignore_env = false) { explicit sdcv_pager(bool ignore_env = false)
{
output = stdout; output = stdout;
if (ignore_env) { if (ignore_env) {
return; return;
@@ -301,12 +303,14 @@ namespace {
} }
sdcv_pager(const sdcv_pager &) = delete; sdcv_pager(const sdcv_pager &) = delete;
sdcv_pager &operator=(const sdcv_pager &) = delete; sdcv_pager &operator=(const sdcv_pager &) = delete;
~sdcv_pager() { ~sdcv_pager()
{
if (output != stdout) { if (output != stdout) {
pclose(output); pclose(output);
} }
} }
FILE *get_stream() { return output; } FILE *get_stream() { return output; }
private: private:
FILE *output; FILE *output;
}; };

View File

@@ -3,8 +3,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "stardict_lib.hpp"
#include "readline.hpp" #include "readline.hpp"
#include "stardict_lib.hpp"
//this structure is wrapper and it need for unification //this structure is wrapper and it need for unification
//results of search whith return Dicts class //results of search whith return Dicts class
@@ -14,7 +14,9 @@ struct TSearchResult {
std::string exp; std::string exp;
TSearchResult(const std::string &bookname_, const std::string &def_, const std::string &exp_) TSearchResult(const std::string &bookname_, const std::string &def_, const std::string &exp_)
: bookname(bookname_), def(def_), exp(exp_) : bookname(bookname_)
, def(def_)
, exp(exp_)
{ {
} }
}; };
@@ -23,15 +25,21 @@ typedef std::vector<TSearchResult> TSearchResultList;
//this class is wrapper around Dicts class for easy use //this class is wrapper around Dicts class for easy use
//of it //of it
class Library : public Libs { class Library : public Libs
{
public: public:
Library(bool uinput, bool uoutput, bool colorize_output, bool use_json, bool no_fuzzy) Library(bool uinput, bool uoutput, bool colorize_output, bool use_json, bool no_fuzzy)
: utf8_input_(uinput), utf8_output_(uoutput), colorize_output_(colorize_output), json_(use_json) { : utf8_input_(uinput)
, utf8_output_(uoutput)
, colorize_output_(colorize_output)
, json_(use_json)
{
setVerbose(!use_json); setVerbose(!use_json);
setFuzzy(!no_fuzzy); setFuzzy(!no_fuzzy);
} }
bool process_phrase(const char *loc_str, IReadLine &io, bool force = false); bool process_phrase(const char *loc_str, IReadLine &io, bool force = false);
private: private:
bool utf8_input_; bool utf8_input_;
bool utf8_output_; bool utf8_output_;
@@ -44,4 +52,3 @@ private:
void LookupData(const std::string &str, TSearchResultList &res_list); void LookupData(const std::string &str, TSearchResultList &res_list);
void print_search_result(FILE *out, const TSearchResult &res, bool &first_result); void print_search_result(FILE *out, const TSearchResult &res, bool &first_result);
}; };

View File

@@ -5,16 +5,17 @@
#endif #endif
#ifdef HAVE_MMAP #ifdef HAVE_MMAP
# include <sys/types.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/types.h>
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#endif #endif
#include <glib.h> #include <glib.h>
class MapFile { class MapFile
{
public: public:
MapFile() {} MapFile() {}
~MapFile(); ~MapFile();
@@ -22,6 +23,7 @@ public:
MapFile &operator=(const MapFile &) = delete; MapFile &operator=(const MapFile &) = delete;
bool open(const char *file_name, unsigned long file_size); bool open(const char *file_name, unsigned long file_size);
gchar *begin() { return data; } gchar *begin() { return data; }
private: private:
char *data = nullptr; char *data = nullptr;
unsigned long size = 0ul; unsigned long size = 0ul;
@@ -82,4 +84,3 @@ inline MapFile::~MapFile()
#endif #endif
#endif #endif
} }

View File

@@ -25,8 +25,8 @@
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#ifdef WITH_READLINE #ifdef WITH_READLINE
# include <readline/readline.h>
#include <readline/history.h> #include <readline/history.h>
#include <readline/readline.h>
#endif #endif
#include <glib.h> #include <glib.h>
@@ -46,10 +46,13 @@ bool stdio_getline(FILE *in, std::string & str)
} }
#ifndef WITH_READLINE #ifndef WITH_READLINE
namespace { namespace
class dummy_readline : public IReadLine { {
class dummy_readline : public IReadLine
{
public: public:
bool read(const std::string &banner, std::string &line) override { bool read(const std::string &banner, std::string &line) override
{
printf("%s", banner.c_str()); printf("%s", banner.c_str());
return stdio_getline(stdin, line); return stdio_getline(stdin, line);
} }
@@ -57,18 +60,22 @@ namespace {
} }
#else #else
namespace { namespace
class real_readline : public IReadLine { {
class real_readline : public IReadLine
{
public: public:
real_readline() { real_readline()
{
rl_readline_name = "sdcv"; rl_readline_name = "sdcv";
using_history(); using_history();
const std::string histname = std::string(g_get_home_dir()) + G_DIR_SEPARATOR + ".sdcv_history"; const std::string histname = std::string(g_get_home_dir()) + G_DIR_SEPARATOR + ".sdcv_history";
read_history(histname.c_str()); read_history(histname.c_str());
} }
~real_readline() { ~real_readline()
{
const std::string histname = std::string(g_get_home_dir()) + G_DIR_SEPARATOR + ".sdcv_history"; const std::string histname = std::string(g_get_home_dir()) + G_DIR_SEPARATOR + ".sdcv_history";
write_history(histname.c_str()); write_history(histname.c_str());
const gchar *hist_size_str = g_getenv("SDCV_HISTSIZE"); const gchar *hist_size_str = g_getenv("SDCV_HISTSIZE");
@@ -78,7 +85,8 @@ namespace {
history_truncate_file(histname.c_str(), hist_size); history_truncate_file(histname.c_str(), hist_size);
} }
bool read(const std::string &banner, std::string& line) override { bool read(const std::string &banner, std::string &line) override
{
char *phrase = nullptr; char *phrase = nullptr;
phrase = readline(banner.c_str()); phrase = readline(banner.c_str());
if (phrase) { if (phrase) {
@@ -89,7 +97,8 @@ namespace {
return false; return false;
} }
void add_to_history(const std::string& phrase) override { void add_to_history(const std::string &phrase) override
{
add_history(phrase.c_str()); add_history(phrase.c_str());
} }
}; };

View File

@@ -2,7 +2,8 @@
#include <string> #include <string>
class IReadLine { class IReadLine
{
public: public:
virtual ~IReadLine() {} virtual ~IReadLine() {}
virtual bool read(const std::string &banner, std::string &line) = 0; virtual bool read(const std::string &banner, std::string &line) = 0;

View File

@@ -22,16 +22,16 @@
#include "config.h" #include "config.h"
#endif #endif
#include <algorithm>
#include <cerrno> #include <cerrno>
#include <clocale> #include <clocale>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <map>
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include <algorithm>
#include <map>
#include <glib.h> #include <glib.h>
#include <glib/gi18n.h> #include <glib/gi18n.h>
@@ -257,5 +257,4 @@ static void list_dicts(const std::list<std::string> &dicts_dir_list, bool use_js
}); });
if (use_json) if (use_json)
fputs("]\n", stdout); fputs("]\n", stdout);
} }

View File

@@ -2,14 +2,14 @@
#include "config.h" #include "config.h"
#endif #endif
#include <stdexcept>
#include <algorithm> #include <algorithm>
#include <cstring>
#include <cctype> #include <cctype>
#include <cstring>
#include <stdexcept>
#include <glib/gstdio.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <zlib.h> #include <zlib.h>
#include <glib/gstdio.h>
#include "distance.hpp" #include "distance.hpp"
#include "mapfile.hpp" #include "mapfile.hpp"
@@ -17,11 +17,11 @@
#include "stardict_lib.hpp" #include "stardict_lib.hpp"
#define TO_STR2(xstr) #xstr #define TO_STR2(xstr) #xstr
#define TO_STR1(xstr) TO_STR2(xstr) #define TO_STR1(xstr) TO_STR2(xstr)
#define THROW_IF_ERROR(expr) do { \ #define THROW_IF_ERROR(expr) \
do { \
assert((expr)); \ assert((expr)); \
if (!(expr)) \ if (!(expr)) \
throw std::runtime_error(#expr " not true at " __FILE__ ": " TO_STR1(__LINE__)); \ throw std::runtime_error(#expr " not true at " __FILE__ ": " TO_STR1(__LINE__)); \
@@ -30,7 +30,8 @@
// Notice: read src/tools/DICTFILE_FORMAT for the dictionary // Notice: read src/tools/DICTFILE_FORMAT for the dictionary
// file's format information! // file's format information!
namespace { namespace
{
struct Fuzzystruct { struct Fuzzystruct {
char *pMatchWord; char *pMatchWord;
int iMatchWordDistance; int iMatchWordDistance;
@@ -70,7 +71,6 @@ namespace {
++str; ++str;
} }
} }
} }
bool DictInfo::load_from_ifo_file(const std::string &ifofilename, bool DictInfo::load_from_ifo_file(const std::string &ifofilename,
@@ -359,7 +359,6 @@ bool DictBase::SearchData(std::vector<std::string> &SearchWords, guint32 idxitem
++nfound; ++nfound;
} }
if (nfound == nWord) if (nfound == nWord)
return true; return true;
sec_size = strlen(p) + 1; sec_size = strlen(p) + 1;
@@ -385,13 +384,11 @@ bool DictBase::SearchData(std::vector<std::string> &SearchWords, guint32 idxitem
case 'k': case 'k':
sec_size = idxitem_size - (p - origin_data); sec_size = idxitem_size - (p - origin_data);
for (j = 0; j < nWord; j++) for (j = 0; j < nWord; j++)
if (!WordFind[j] && if (!WordFind[j] && g_strstr_len(p, sec_size, SearchWords[j].c_str())) {
g_strstr_len(p, sec_size, SearchWords[j].c_str())) {
WordFind[j] = true; WordFind[j] = true;
++nfound; ++nfound;
} }
if (nfound == nWord) if (nfound == nWord)
return true; return true;
break; break;
@@ -431,21 +428,29 @@ bool DictBase::SearchData(std::vector<std::string> &SearchWords, guint32 idxitem
return false; return false;
} }
namespace { namespace
class OffsetIndex : public IIndexFile { {
class OffsetIndex : public IIndexFile
{
public: public:
OffsetIndex() : idxfile(nullptr) {} OffsetIndex()
~OffsetIndex() { : idxfile(nullptr)
{
}
~OffsetIndex()
{
if (idxfile) if (idxfile)
fclose(idxfile); fclose(idxfile);
} }
bool load(const std::string &url, gulong wc, gulong fsize, bool verbose) override; bool load(const std::string &url, gulong wc, gulong fsize, bool verbose) override;
const gchar *get_key(glong idx) override; const gchar *get_key(glong idx) override;
void get_data(glong idx) override { get_key(idx); } void get_data(glong idx) override { get_key(idx); }
const gchar *get_key_and_data(glong idx) override { const gchar *get_key_and_data(glong idx) override
{
return get_key(idx); return get_key(idx);
} }
bool lookup(const char *str, glong &idx) override; bool lookup(const char *str, glong &idx) override;
private: private:
static const gint ENTR_PER_PAGE = 32; static const gint ENTR_PER_PAGE = 32;
static const char *CACHE_MAGIC; static const char *CACHE_MAGIC;
@@ -458,7 +463,8 @@ namespace {
struct index_entry { struct index_entry {
glong idx; glong idx;
std::string keystr; std::string keystr;
void assign(glong i, const std::string& str) { void assign(glong i, const std::string &str)
{
idx = i; idx = i;
keystr.assign(str); keystr.assign(str);
} }
@@ -487,19 +493,24 @@ namespace {
const char *OffsetIndex::CACHE_MAGIC = "StarDict's Cache, Version: 0.1"; const char *OffsetIndex::CACHE_MAGIC = "StarDict's Cache, Version: 0.1";
class WordListIndex : public IIndexFile
class WordListIndex : public IIndexFile { {
public: public:
WordListIndex() : idxdatabuf(nullptr) {} WordListIndex()
: idxdatabuf(nullptr)
{
}
~WordListIndex() { g_free(idxdatabuf); } ~WordListIndex() { g_free(idxdatabuf); }
bool load(const std::string &url, gulong wc, gulong fsize, bool verbose) override; bool load(const std::string &url, gulong wc, gulong fsize, bool verbose) override;
const gchar *get_key(glong idx) override { return wordlist[idx]; } const gchar *get_key(glong idx) override { return wordlist[idx]; }
void get_data(glong idx) override; void get_data(glong idx) override;
const gchar *get_key_and_data(glong idx) override { const gchar *get_key_and_data(glong idx) override
{
get_data(idx); get_data(idx);
return get_key(idx); return get_key(idx);
} }
bool lookup(const char *str, glong &idx) override; bool lookup(const char *str, glong &idx) override;
private: private:
gchar *idxdatabuf; gchar *idxdatabuf;
std::vector<gchar *> wordlist; std::vector<gchar *> wordlist;
@@ -553,8 +564,7 @@ namespace {
for (const std::string &item : vars) { for (const std::string &item : vars) {
struct ::stat idxstat, cachestat; struct ::stat idxstat, cachestat;
if (g_stat(url.c_str(), &idxstat)!=0 || if (g_stat(url.c_str(), &idxstat) != 0 || g_stat(item.c_str(), &cachestat) != 0)
g_stat(item.c_str(), &cachestat)!=0)
continue; continue;
if (cachestat.st_mtime < idxstat.st_mtime) if (cachestat.st_mtime < idxstat.st_mtime)
continue; continue;
@@ -565,7 +575,6 @@ namespace {
continue; continue;
memcpy(&wordoffset[0], mf.begin() + strlen(CACHE_MAGIC), wordoffset.size() * sizeof(wordoffset[0])); memcpy(&wordoffset[0], mf.begin() + strlen(CACHE_MAGIC), wordoffset.size() * sizeof(wordoffset[0]));
return true; return true;
} }
return false; return false;
@@ -574,8 +583,7 @@ namespace {
std::list<std::string> OffsetIndex::get_cache_variant(const std::string &url) std::list<std::string> OffsetIndex::get_cache_variant(const std::string &url)
{ {
std::list<std::string> res = { url + ".oft" }; std::list<std::string> res = { url + ".oft" };
if (!g_file_test(g_get_user_cache_dir(), G_FILE_TEST_EXISTS) && if (!g_file_test(g_get_user_cache_dir(), G_FILE_TEST_EXISTS) && g_mkdir(g_get_user_cache_dir(), 0700) == -1)
g_mkdir(g_get_user_cache_dir(), 0700)==-1)
return res; return res;
const std::string cache_dir = std::string(g_get_user_cache_dir()) + G_DIR_SEPARATOR_S + "sdcv"; const std::string cache_dir = std::string(g_get_user_cache_dir()) + G_DIR_SEPARATOR_S + "sdcv";
@@ -659,7 +667,6 @@ namespace {
if ((nentr = (wordcount % ENTR_PER_PAGE)) == 0) if ((nentr = (wordcount % ENTR_PER_PAGE)) == 0)
nentr = ENTR_PER_PAGE; nentr = ENTR_PER_PAGE;
if (page_idx != page.idx) { if (page_idx != page.idx) {
page_data.resize(wordoffset[page_idx + 1] - wordoffset[page_idx]); page_data.resize(wordoffset[page_idx + 1] - wordoffset[page_idx]);
fseek(idxfile, wordoffset[page_idx], SEEK_SET); fseek(idxfile, wordoffset[page_idx], SEEK_SET);
@@ -849,7 +856,8 @@ bool SynFile::lookup(const char *str, glong &idx)
return false; return false;
} }
bool Dict::Lookup(const char *str, glong &idx) { bool Dict::Lookup(const char *str, glong &idx)
{
return syn_file->lookup(str, idx) || idx_file->lookup(str, idx); return syn_file->lookup(str, idx) || idx_file->lookup(str, idx);
} }
@@ -1024,7 +1032,6 @@ const gchar *Libs::poGetNextWord(const gchar *sWord, glong *iCurrent)
return poCurrentWord; return poCurrentWord;
} }
const gchar * const gchar *
Libs::poGetPreWord(glong *iCurrent) Libs::poGetPreWord(glong *iCurrent)
{ {
@@ -1142,8 +1149,7 @@ bool Libs::LookupSimilarWord(const gchar* sWord, glong & iWordIndex, int iLib)
strcpy(sNewWord, sWord); strcpy(sNewWord, sWord);
sNewWord[iWordLen - 2] = '\0'; // cut "ly" sNewWord[iWordLen - 2] = '\0'; // cut "ly"
if (iWordLen > 5 && sNewWord[iWordLen - 3] == sNewWord[iWordLen - 4] if (iWordLen > 5 && sNewWord[iWordLen - 3] == sNewWord[iWordLen - 4]
&& !bIsVowel(sNewWord[iWordLen-4]) && && !bIsVowel(sNewWord[iWordLen - 4]) && bIsVowel(sNewWord[iWordLen - 5])) { //doubled
bIsVowel(sNewWord[iWordLen-5])) {//doubled
sNewWord[iWordLen - 3] = '\0'; sNewWord[iWordLen - 3] = '\0';
if (oLib[iLib]->Lookup(sNewWord, iIndex)) if (oLib[iLib]->Lookup(sNewWord, iIndex))
@@ -1183,8 +1189,7 @@ bool Libs::LookupSimilarWord(const gchar* sWord, glong & iWordIndex, int iLib)
strcpy(sNewWord, sWord); strcpy(sNewWord, sWord);
sNewWord[iWordLen - 3] = '\0'; sNewWord[iWordLen - 3] = '\0';
if (iWordLen > 6 && (sNewWord[iWordLen - 4] == sNewWord[iWordLen - 5]) if (iWordLen > 6 && (sNewWord[iWordLen - 4] == sNewWord[iWordLen - 5])
&& !bIsVowel(sNewWord[iWordLen-5]) && && !bIsVowel(sNewWord[iWordLen - 5]) && bIsVowel(sNewWord[iWordLen - 6])) { //doubled
bIsVowel(sNewWord[iWordLen-6])) { //doubled
sNewWord[iWordLen - 4] = '\0'; sNewWord[iWordLen - 4] = '\0';
if (oLib[iLib]->Lookup(sNewWord, iIndex)) if (oLib[iLib]->Lookup(sNewWord, iIndex))
bFound = true; bFound = true;
@@ -1234,19 +1239,8 @@ bool Libs::LookupSimilarWord(const gchar* sWord, glong & iWordIndex, int iLib)
//cut two char "es" //cut two char "es"
if (!bFound && iWordLen > 3) { if (!bFound && iWordLen > 3) {
isupcase = (!strncmp(&sWord[iWordLen-2],"ES",2) && isupcase = (!strncmp(&sWord[iWordLen - 2], "ES", 2) && (sWord[iWordLen - 3] == 'S' || sWord[iWordLen - 3] == 'X' || sWord[iWordLen - 3] == 'O' || (iWordLen > 4 && sWord[iWordLen - 3] == 'H' && (sWord[iWordLen - 4] == 'C' || sWord[iWordLen - 4] == 'S'))));
(sWord[iWordLen-3] == 'S' || if (isupcase || (!strncmp(&sWord[iWordLen - 2], "es", 2) && (sWord[iWordLen - 3] == 's' || sWord[iWordLen - 3] == 'x' || sWord[iWordLen - 3] == 'o' || (iWordLen > 4 && sWord[iWordLen - 3] == 'h' && (sWord[iWordLen - 4] == 'c' || sWord[iWordLen - 4] == 's'))))) {
sWord[iWordLen-3] == 'X' ||
sWord[iWordLen-3] == 'O' ||
(iWordLen >4 && sWord[iWordLen-3] == 'H' &&
(sWord[iWordLen-4] == 'C' ||
sWord[iWordLen-4] == 'S'))));
if (isupcase ||
(!strncmp(&sWord[iWordLen-2],"es",2) &&
(sWord[iWordLen-3] == 's' || sWord[iWordLen-3] == 'x' ||
sWord[iWordLen-3] == 'o' ||
(iWordLen >4 && sWord[iWordLen-3] == 'h' &&
(sWord[iWordLen-4] == 'c' || sWord[iWordLen-4] == 's'))))) {
strcpy(sNewWord, sWord); strcpy(sNewWord, sWord);
sNewWord[iWordLen - 2] = '\0'; sNewWord[iWordLen - 2] = '\0';
if (oLib[iLib]->Lookup(sNewWord, iIndex)) if (oLib[iLib]->Lookup(sNewWord, iIndex))
@@ -1269,8 +1263,7 @@ bool Libs::LookupSimilarWord(const gchar* sWord, glong & iWordIndex, int iLib)
strcpy(sNewWord, sWord); strcpy(sNewWord, sWord);
sNewWord[iWordLen - 2] = '\0'; sNewWord[iWordLen - 2] = '\0';
if (iWordLen > 5 && (sNewWord[iWordLen - 3] == sNewWord[iWordLen - 4]) if (iWordLen > 5 && (sNewWord[iWordLen - 3] == sNewWord[iWordLen - 4])
&& !bIsVowel(sNewWord[iWordLen-4]) && && !bIsVowel(sNewWord[iWordLen - 4]) && bIsVowel(sNewWord[iWordLen - 5])) { //doubled
bIsVowel(sNewWord[iWordLen-5])) {//doubled
sNewWord[iWordLen - 3] = '\0'; sNewWord[iWordLen - 3] = '\0';
if (oLib[iLib]->Lookup(sNewWord, iIndex)) if (oLib[iLib]->Lookup(sNewWord, iIndex))
bFound = true; bFound = true;
@@ -1445,8 +1438,7 @@ bool Libs::LookupWithFuzzy(const gchar *sWord, gchar *reslist[], gint reslist_si
sCheck = poGetWord(index, iLib); sCheck = poGetWord(index, iLib);
// tolower and skip too long or too short words // tolower and skip too long or too short words
iCheckWordLen = g_utf8_strlen(sCheck, -1); iCheckWordLen = g_utf8_strlen(sCheck, -1);
if (iCheckWordLen-ucs4_str2_len>=iMaxDistance || if (iCheckWordLen - ucs4_str2_len >= iMaxDistance || ucs4_str2_len - iCheckWordLen >= iMaxDistance)
ucs4_str2_len-iCheckWordLen>=iMaxDistance)
continue; continue;
ucs4_str1 = g_utf8_to_ucs4_fast(sCheck, -1, nullptr); ucs4_str1 = g_utf8_to_ucs4_fast(sCheck, -1, nullptr);
if (iCheckWordLen > ucs4_str2_len) if (iCheckWordLen > ucs4_str2_len)
@@ -1461,8 +1453,7 @@ bool Libs::LookupWithFuzzy(const gchar *sWord, gchar *reslist[], gint reslist_si
bool bAlreadyInList = false; bool bAlreadyInList = false;
int iMaxDistanceAt = 0; int iMaxDistanceAt = 0;
for (int j = 0; j < reslist_size; j++) { for (int j = 0; j < reslist_size; j++) {
if (oFuzzystruct[j].pMatchWord && if (oFuzzystruct[j].pMatchWord && strcmp(oFuzzystruct[j].pMatchWord, sCheck) == 0) { //already in list
strcmp(oFuzzystruct[j].pMatchWord,sCheck)==0 ) {//already in list
bAlreadyInList = true; bAlreadyInList = true;
break; break;
} }

View File

@@ -2,12 +2,12 @@
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <functional>
#include <list> #include <list>
#include <map>
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include <functional>
#include <map>
#include "dictziplib.hpp" #include "dictziplib.hpp"
@@ -26,7 +26,6 @@ inline void set_uint32(gchar *addr, guint32 val)
memcpy(addr, &val, sizeof(guint32)); memcpy(addr, &val, sizeof(guint32));
} }
struct cacheItem { struct cacheItem {
guint32 offset; guint32 offset;
gchar *data; gchar *data;
@@ -38,26 +37,31 @@ struct cacheItem {
const int WORDDATA_CACHE_NUM = 10; const int WORDDATA_CACHE_NUM = 10;
const int INVALID_INDEX = -100; const int INVALID_INDEX = -100;
class DictBase { class DictBase
{
public: public:
DictBase() {} DictBase() {}
~DictBase() { ~DictBase()
{
if (dictfile) if (dictfile)
fclose(dictfile); fclose(dictfile);
} }
DictBase(const DictBase &) = delete; DictBase(const DictBase &) = delete;
DictBase &operator=(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() const { bool containSearchData() const
{
if (sametypesequence.empty()) if (sametypesequence.empty())
return true; return true;
return sametypesequence.find_first_of("mlgxty") != std::string::npos; 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 = nullptr; FILE *dictfile = nullptr;
std::unique_ptr<DictData> dictdzfile; std::unique_ptr<DictData> dictdzfile;
private: private:
cacheItem cache[WORDDATA_CACHE_NUM]; cacheItem cache[WORDDATA_CACHE_NUM];
gint cache_cur = 0; gint cache_cur = 0;
@@ -81,7 +85,8 @@ struct DictInfo {
bool load_from_ifo_file(const std::string &ifofilename, bool istreedict); bool load_from_ifo_file(const std::string &ifofilename, bool istreedict);
}; };
class IIndexFile { class IIndexFile
{
public: public:
guint32 wordentry_offset; guint32 wordentry_offset;
guint32 wordentry_size; guint32 wordentry_size;
@@ -94,15 +99,18 @@ public:
virtual bool lookup(const char *str, glong &idx) = 0; virtual bool lookup(const char *str, glong &idx) = 0;
}; };
class SynFile { class SynFile
{
public: public:
bool load(const std::string &url, gulong wc); bool load(const std::string &url, gulong wc);
bool lookup(const char *str, glong &idx); bool lookup(const char *str, glong &idx);
private: private:
std::map<std::string, gulong> synonyms; std::map<std::string, gulong> synonyms;
}; };
class Dict : public DictBase { class Dict : public DictBase
{
public: public:
Dict() {} Dict() {}
Dict(const Dict &) = delete; Dict(const Dict &) = delete;
@@ -114,11 +122,13 @@ public:
const std::string &ifofilename() const { return ifo_file_name; } const std::string &ifofilename() const { return ifo_file_name; }
const gchar *get_key(glong index) { return idx_file->get_key(index); } const gchar *get_key(glong index) { return idx_file->get_key(index); }
gchar *get_data(glong index) { gchar *get_data(glong index)
{
idx_file->get_data(index); idx_file->get_data(index);
return DictBase::GetWordData(idx_file->wordentry_offset, idx_file->wordentry_size); 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) { void get_key_and_data(glong index, const gchar **key, guint32 *offset, guint32 *size)
{
*key = idx_file->get_key_and_data(index); *key = idx_file->get_key_and_data(index);
*offset = idx_file->wordentry_offset; *offset = idx_file->wordentry_offset;
*size = idx_file->wordentry_size; *size = idx_file->wordentry_size;
@@ -126,6 +136,7 @@ public:
bool Lookup(const char *str, glong &idx); bool Lookup(const char *str, glong &idx);
bool LookupWithRule(GPatternSpec *pspec, glong *aIndex, int iBuffLen); bool LookupWithRule(GPatternSpec *pspec, glong *aIndex, int iBuffLen);
private: private:
std::string ifo_file_name; std::string ifo_file_name;
gulong wordcount; gulong wordcount;
@@ -138,9 +149,11 @@ private:
bool load_ifofile(const std::string &ifofilename, gulong &idxfilesize); bool load_ifofile(const std::string &ifofilename, gulong &idxfilesize);
}; };
class Libs { class Libs
{
public: public:
Libs(std::function<void(void)> f = std::function<void(void)>()) { Libs(std::function<void(void)> f = std::function<void(void)>())
{
progress_func = f; progress_func = f;
iMaxFuzzyDistance = MAX_FUZZY_DISTANCE; //need to read from cfg. iMaxFuzzyDistance = MAX_FUZZY_DISTANCE; //need to read from cfg.
} }
@@ -158,10 +171,12 @@ public:
const std::string &dict_name(int idict) const { 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 nullptr; return nullptr;
return oLib[iLib]->get_data(iIndex); return oLib[iLib]->get_data(iIndex);
@@ -169,18 +184,20 @@ public:
const gchar *poGetCurrentWord(glong *iCurrent); const gchar *poGetCurrentWord(glong *iCurrent);
const gchar *poGetNextWord(const gchar *word, glong *iCurrent); const gchar *poGetNextWord(const gchar *word, glong *iCurrent);
const gchar *poGetPreWord(glong *iCurrent); const gchar *poGetPreWord(glong *iCurrent);
bool LookupWord(const gchar* sWord, glong& iWordIndex, int iLib) { bool LookupWord(const gchar *sWord, glong &iWordIndex, int iLib)
{
return oLib[iLib]->Lookup(sWord, iWordIndex); return oLib[iLib]->Lookup(sWord, iWordIndex);
} }
bool LookupSimilarWord(const gchar *sWord, glong &iWordIndex, int iLib); bool LookupSimilarWord(const gchar *sWord, glong &iWordIndex, int iLib);
bool SimpleLookupWord(const gchar *sWord, glong &iWordIndex, int iLib); bool SimpleLookupWord(const gchar *sWord, glong &iWordIndex, int iLib);
bool LookupWithFuzzy(const gchar *sWord, gchar *reslist[], gint reslist_size); bool LookupWithFuzzy(const gchar *sWord, gchar *reslist[], gint reslist_size);
gint LookupWithRule(const gchar *sWord, gchar *reslist[]); gint LookupWithRule(const gchar *sWord, gchar *reslist[]);
bool LookupData(const gchar *sWord, std::vector<gchar *> *reslist); bool LookupData(const gchar *sWord, std::vector<gchar *> *reslist);
protected: protected:
bool fuzzy_; bool fuzzy_;
private: private:
std::vector<Dict *> oLib; // word Libs. std::vector<Dict *> oLib; // word Libs.
int iMaxFuzzyDistance; int iMaxFuzzyDistance;
@@ -188,10 +205,11 @@ private:
bool verbose_; bool verbose_;
}; };
enum query_t { enum query_t {
qtSIMPLE, qtREGEXP, qtFUZZY, qtDATA qtSIMPLE,
qtREGEXP,
qtFUZZY,
qtDATA
}; };
extern query_t analyze_query(const char *s, std::string &res); extern query_t analyze_query(const char *s, std::string &res);

View File

@@ -22,13 +22,13 @@
#include "config.h" #include "config.h"
#endif #endif
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <glib.h> #include <glib.h>
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <sstream>
#include <iomanip> #include <iomanip>
#include <sstream>
#include "utils.hpp" #include "utils.hpp"
@@ -67,12 +67,11 @@ static void __for_each_file(const std::string& dirname, const std::string& suff,
const std::string fullfilename(dirname + G_DIR_SEPARATOR_S + filename); const std::string fullfilename(dirname + G_DIR_SEPARATOR_S + filename);
if (g_file_test(fullfilename.c_str(), G_FILE_TEST_IS_DIR)) if (g_file_test(fullfilename.c_str(), G_FILE_TEST_IS_DIR))
__for_each_file(fullfilename, suff, order_list, disable_list, f); __for_each_file(fullfilename, suff, order_list, disable_list, f);
else if (g_str_has_suffix(filename, suff.c_str()) && else if (g_str_has_suffix(filename, suff.c_str()) && std::find(order_list.begin(), order_list.end(), fullfilename) == order_list.end()) {
std::find(order_list.begin(), order_list.end(),
fullfilename)==order_list.end()) {
const bool disable = std::find(disable_list.begin(), const bool disable = std::find(disable_list.begin(),
disable_list.end(), disable_list.end(),
fullfilename)!=disable_list.end(); fullfilename)
!= disable_list.end();
f(fullfilename, disable); f(fullfilename, disable);
} }
} }
@@ -80,7 +79,6 @@ static void __for_each_file(const std::string& dirname, const std::string& suff,
} }
} }
void for_each_file(const std::list<std::string> &dirs_list, const std::string &suff, void for_each_file(const std::list<std::string> &dirs_list, const std::string &suff,
const std::list<std::string> &order_list, const std::list<std::string> &disable_list, const std::list<std::string> &order_list, const std::list<std::string> &disable_list,
const std::function<void(const std::string &, bool)> &f) const std::function<void(const std::string &, bool)> &f)
@@ -94,17 +92,32 @@ void for_each_file(const std::list<std::string>& dirs_list, const std::string& s
} }
// based on https://stackoverflow.com/questions/7724448/simple-json-string-escape-for-c/33799784#33799784 // based on https://stackoverflow.com/questions/7724448/simple-json-string-escape-for-c/33799784#33799784
std::string json_escape_string(const std::string &s) { std::string json_escape_string(const std::string &s)
{
std::ostringstream o; std::ostringstream o;
for (auto c = s.cbegin(); c != s.cend(); c++) { for (auto c = s.cbegin(); c != s.cend(); c++) {
switch (*c) { switch (*c) {
case '"': o << "\\\""; break; case '"':
case '\\': o << "\\\\"; break; o << "\\\"";
case '\b': o << "\\b"; break; break;
case '\f': o << "\\f"; break; case '\\':
case '\n': o << "\\n"; break; o << "\\\\";
case '\r': o << "\\r"; break; break;
case '\t': o << "\\t"; break; case '\b':
o << "\\b";
break;
case '\f':
o << "\\f";
break;
case '\n':
o << "\\n";
break;
case '\r':
o << "\\r";
break;
case '\t':
o << "\\t";
break;
default: default:
if ('\x00' <= *c && *c <= '\x1f') { if ('\x00' <= *c && *c <= '\x1f') {
o << "\\u" o << "\\u"

View File

@@ -1,56 +1,71 @@
#pragma once #pragma once
#include <glib.h>
#include <cstddef>
#include <cassert> #include <cassert>
#include <string> #include <cstddef>
#include <list>
#include <functional> #include <functional>
#include <glib.h>
#include <list>
#include <string>
template <typename T, typename unref_res_t, void (*unref_res)(unref_res_t *)> template <typename T, typename unref_res_t, void (*unref_res)(unref_res_t *)>
class ResourceWrapper { class ResourceWrapper
{
public: public:
ResourceWrapper(T *p = nullptr) : p_(p) {} ResourceWrapper(T *p = nullptr)
: p_(p)
{
}
~ResourceWrapper() { free_resource(); } ~ResourceWrapper() { free_resource(); }
ResourceWrapper(const ResourceWrapper &) = delete; ResourceWrapper(const ResourceWrapper &) = delete;
ResourceWrapper &operator=(const ResourceWrapper &) = delete; ResourceWrapper &operator=(const ResourceWrapper &) = delete;
T *operator->() const { return p_; } T *operator->() const { return p_; }
bool operator!() const { return p_ == nullptr; } bool operator!() const { return p_ == nullptr; }
const T& operator[](size_t idx) const { const T &operator[](size_t idx) const
{
assert(p_ != nullptr); assert(p_ != nullptr);
return p_[idx]; return p_[idx];
} }
void reset(T *newp) { void reset(T *newp)
{
if (p_ != newp) { if (p_ != newp) {
free_resource(); free_resource();
p_ = newp; p_ = newp;
} }
} }
friend inline bool operator==(const ResourceWrapper& lhs, std::nullptr_t) noexcept { friend inline bool operator==(const ResourceWrapper &lhs, std::nullptr_t) noexcept
{
return !lhs.p_; return !lhs.p_;
} }
friend inline bool operator!=(const ResourceWrapper& lhs, std::nullptr_t) noexcept { friend inline bool operator!=(const ResourceWrapper &lhs, std::nullptr_t) noexcept
{
return !!lhs.p_; return !!lhs.p_;
} }
friend inline T *get_impl(const ResourceWrapper& rw) { friend inline T *get_impl(const ResourceWrapper &rw)
{
return rw.p_; return rw.p_;
} }
friend inline T **get_addr(ResourceWrapper& rw) { friend inline T **get_addr(ResourceWrapper &rw)
{
return &rw.p_; return &rw.p_;
} }
private: private:
T *p_; T *p_;
void free_resource() { if (p_) unref_res(p_); } void free_resource()
{
if (p_)
unref_res(p_);
}
}; };
namespace glib { namespace glib
{
typedef ResourceWrapper<gchar, void, g_free> CharStr; typedef ResourceWrapper<gchar, void, g_free> CharStr;
typedef ResourceWrapper<GError, GError, g_error_free> Error; typedef ResourceWrapper<GError, GError, g_error_free> Error;
} }