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

View File

@@ -13,7 +13,8 @@ struct DictCache {
int count;
};
class DictData {
class DictData
{
public:
static const size_t DICT_CACHE_SIZE = 5;
@@ -22,6 +23,7 @@ public:
bool open(const std::string &filename, int computeCRC);
void close();
void read(char *buffer, unsigned long start, unsigned long size);
private:
const char *start; /* start 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);
};

View File

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

View File

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

View File

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

View File

@@ -3,8 +3,8 @@
#include <string>
#include <vector>
#include "stardict_lib.hpp"
#include "readline.hpp"
#include "stardict_lib.hpp"
//this structure is wrapper and it need for unification
//results of search whith return Dicts class
@@ -14,7 +14,9 @@ struct TSearchResult {
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
//of it
class Library : public Libs {
class Library : public Libs
{
public:
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);
setFuzzy(!no_fuzzy);
}
bool process_phrase(const char *loc_str, IReadLine &io, bool force = false);
private:
bool utf8_input_;
bool utf8_output_;
@@ -44,4 +52,3 @@ private:
void LookupData(const std::string &str, TSearchResultList &res_list);
void print_search_result(FILE *out, const TSearchResult &res, bool &first_result);
};

View File

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

View File

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

View File

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

View File

@@ -22,16 +22,16 @@
#include "config.h"
#endif
#include <algorithm>
#include <cerrno>
#include <clocale>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <glib.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)
fputs("]\n", stdout);
}

View File

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

View File

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

View File

@@ -22,13 +22,13 @@
#include "config.h"
#endif
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <glib.h>
#include <glib/gi18n.h>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <sstream>
#include <iomanip>
#include <sstream>
#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);
if (g_file_test(fullfilename.c_str(), G_FILE_TEST_IS_DIR))
__for_each_file(fullfilename, suff, order_list, disable_list, f);
else if (g_str_has_suffix(filename, suff.c_str()) &&
std::find(order_list.begin(), order_list.end(),
fullfilename)==order_list.end()) {
else if (g_str_has_suffix(filename, suff.c_str()) && std::find(order_list.begin(), order_list.end(), fullfilename) == order_list.end()) {
const bool disable = std::find(disable_list.begin(),
disable_list.end(),
fullfilename)!=disable_list.end();
fullfilename)
!= disable_list.end();
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,
const std::list<std::string> &order_list, const std::list<std::string> &disable_list,
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
std::string json_escape_string(const std::string &s) {
std::string json_escape_string(const std::string &s)
{
std::ostringstream o;
for (auto c = s.cbegin(); c != s.cend(); c++) {
switch (*c) {
case '"': o << "\\\""; break;
case '\\': o << "\\\\"; 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;
case '"':
o << "\\\"";
break;
case '\\':
o << "\\\\";
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:
if ('\x00' <= *c && *c <= '\x1f') {
o << "\\u"

View File

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