mirror of
https://github.com/Dushistov/sdcv.git
synced 2025-12-15 17:31:56 +00:00
remove not used code, use glib wrappers where possible
This commit is contained in:
@@ -113,7 +113,7 @@
|
||||
#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;
|
||||
int id1, id2, si1, si2;
|
||||
@@ -130,6 +130,7 @@ int dictData::read_header(const std::string &fname, int computeCRC)
|
||||
if (!(str = fopen(fname.c_str(), "rb"))) {
|
||||
//err_fatal_errno( __FUNCTION__,
|
||||
// "Cannot open data file \"%s\" for read\n", filename );
|
||||
return -1;
|
||||
}
|
||||
|
||||
this->headerLength = GZ_XLEN - 1;
|
||||
@@ -267,10 +268,9 @@ int dictData::read_header(const std::string &fname, int computeCRC)
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool dictData::open(const std::string& fname, int computeCRC)
|
||||
bool DictData::open(const std::string& fname, int computeCRC)
|
||||
{
|
||||
struct stat sb;
|
||||
int j;
|
||||
int fd;
|
||||
|
||||
this->initialized = 0;
|
||||
@@ -306,7 +306,7 @@ bool dictData::open(const std::string& fname, int computeCRC)
|
||||
this->start=mapfile.begin();
|
||||
this->end = this->start + this->size;
|
||||
|
||||
for (j = 0; j < DICT_CACHE_SIZE; j++) {
|
||||
for (size_t j = 0; j < DICT_CACHE_SIZE; j++) {
|
||||
cache[j].chunk = -1;
|
||||
cache[j].stamp = -1;
|
||||
cache[j].inBuffer = nullptr;
|
||||
@@ -316,10 +316,8 @@ bool dictData::open(const std::string& fname, int computeCRC)
|
||||
return true;
|
||||
}
|
||||
|
||||
void dictData::close()
|
||||
void DictData::close()
|
||||
{
|
||||
int i;
|
||||
|
||||
if (this->chunks)
|
||||
free(this->chunks);
|
||||
if (this->offsets)
|
||||
@@ -333,13 +331,13 @@ void dictData::close()
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < DICT_CACHE_SIZE; ++i){
|
||||
for (size_t i = 0; i < DICT_CACHE_SIZE; ++i){
|
||||
if (this -> cache [i].inBuffer)
|
||||
free (this -> cache [i].inBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
void dictData::read(char *buffer, unsigned long start, unsigned long size)
|
||||
void DictData::read(char *buffer, unsigned long start, unsigned long size)
|
||||
{
|
||||
char *pt;
|
||||
unsigned long end;
|
||||
@@ -348,7 +346,7 @@ void dictData::read(char *buffer, unsigned long start, unsigned long size)
|
||||
char outBuffer[OUT_BUFFER_SIZE];
|
||||
int firstChunk, lastChunk;
|
||||
int firstOffset, lastOffset;
|
||||
int i, j;
|
||||
int i;
|
||||
int found, target, lastStamp;
|
||||
static int stamp = 0;
|
||||
|
||||
@@ -403,7 +401,7 @@ void dictData::read(char *buffer, unsigned long start, unsigned long size)
|
||||
found = 0;
|
||||
target = 0;
|
||||
lastStamp = INT_MAX;
|
||||
for (j = 0; j < DICT_CACHE_SIZE; j++) {
|
||||
for (size_t j = 0; j < DICT_CACHE_SIZE; j++) {
|
||||
#if USE_CACHE
|
||||
if (this->cache[j].chunk == i) {
|
||||
found = 1;
|
||||
|
||||
@@ -6,22 +6,22 @@
|
||||
|
||||
#include "mapfile.hpp"
|
||||
|
||||
|
||||
#define DICT_CACHE_SIZE 5
|
||||
|
||||
struct dictCache {
|
||||
struct DictCache {
|
||||
int chunk;
|
||||
char *inBuffer;
|
||||
int stamp;
|
||||
int count;
|
||||
};
|
||||
|
||||
struct dictData {
|
||||
dictData() {}
|
||||
class DictData {
|
||||
public:
|
||||
static const size_t DICT_CACHE_SIZE = 5;
|
||||
|
||||
DictData() {}
|
||||
~DictData() { close(); }
|
||||
bool open(const std::string& filename, int computeCRC);
|
||||
void close();
|
||||
void read(char *buffer, unsigned long start, unsigned long size);
|
||||
~dictData() { close(); }
|
||||
private:
|
||||
const char *start; /* start of mmap'd area */
|
||||
const char *end; /* end of mmap'd area */
|
||||
@@ -47,7 +47,7 @@ private:
|
||||
unsigned long crc;
|
||||
unsigned long length;
|
||||
unsigned long compressedLength;
|
||||
dictCache cache[DICT_CACHE_SIZE];
|
||||
DictCache cache[DICT_CACHE_SIZE];
|
||||
MapFile mapfile;
|
||||
|
||||
int read_header(const std::string &filename, int computeCRC);
|
||||
|
||||
@@ -304,17 +304,16 @@ bool Library::process_phrase(const char *loc_str, IReadLine &io, bool force)
|
||||
|
||||
gsize bytes_read;
|
||||
gsize bytes_written;
|
||||
GError *err = nullptr;
|
||||
char *str = nullptr;
|
||||
glib::Error err;
|
||||
glib::CharStr str;
|
||||
if (!utf8_input_)
|
||||
str = g_locale_to_utf8(loc_str, -1, &bytes_read, &bytes_written, &err);
|
||||
str.reset(g_locale_to_utf8(loc_str, -1, &bytes_read, &bytes_written, get_addr(err)));
|
||||
else
|
||||
str = g_strdup(loc_str);
|
||||
str.reset(g_strdup(loc_str));
|
||||
|
||||
if (nullptr == str) {
|
||||
if (nullptr == get_impl(str)) {
|
||||
fprintf(stderr, _("Can not convert %s to utf8.\n"), loc_str);
|
||||
fprintf(stderr, "%s\n", err->message);
|
||||
g_error_free(err);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -323,7 +322,7 @@ bool Library::process_phrase(const char *loc_str, IReadLine &io, bool force)
|
||||
|
||||
TSearchResultList res_list;
|
||||
|
||||
switch (analyze_query(str, query)) {
|
||||
switch (analyze_query(get_impl(str), query)) {
|
||||
case qtFUZZY:
|
||||
LookupWithFuzzy(query, res_list);
|
||||
break;
|
||||
@@ -331,9 +330,9 @@ bool Library::process_phrase(const char *loc_str, IReadLine &io, bool force)
|
||||
LookupWithRule(query, res_list);
|
||||
break;
|
||||
case qtSIMPLE:
|
||||
SimpleLookup(str, res_list);
|
||||
SimpleLookup(get_impl(str), res_list);
|
||||
if (res_list.empty())
|
||||
LookupWithFuzzy(str, res_list);
|
||||
LookupWithFuzzy(get_impl(str), res_list);
|
||||
break;
|
||||
case qtDATA:
|
||||
LookupData(query, res_list);
|
||||
@@ -367,7 +366,7 @@ bool Library::process_phrase(const char *loc_str, IReadLine &io, bool force)
|
||||
|
||||
if (!show_all_results && !force) {
|
||||
printf(_("Found %zu items, similar to %s.\n"), res_list.size(),
|
||||
utf8_output_ ? str : utf8_to_locale_ign_err(str).c_str());
|
||||
utf8_output_ ? get_impl(str) : utf8_to_locale_ign_err(get_impl(str)).c_str());
|
||||
for (size_t i = 0; i < res_list.size(); ++i) {
|
||||
const std::string loc_bookname = utf8_to_locale_ign_err(res_list[i].bookname);
|
||||
const std::string loc_def = utf8_to_locale_ign_err(res_list[i].def);
|
||||
@@ -398,7 +397,7 @@ bool Library::process_phrase(const char *loc_str, IReadLine &io, bool force)
|
||||
} else {
|
||||
sdcv_pager pager(force);
|
||||
fprintf(pager.get_stream(), _("Found %zu items, similar to %s.\n"),
|
||||
res_list.size(), utf8_output_ ? str : utf8_to_locale_ign_err(str).c_str());
|
||||
res_list.size(), utf8_output_ ? get_impl(str) : utf8_to_locale_ign_err(get_impl(str)).c_str());
|
||||
for (const TSearchResult& search_res : res_list)
|
||||
print_search_result(pager.get_stream(), search_res);
|
||||
}
|
||||
@@ -406,11 +405,10 @@ bool Library::process_phrase(const char *loc_str, IReadLine &io, bool force)
|
||||
} else {
|
||||
std::string loc_str;
|
||||
if (!utf8_output_)
|
||||
loc_str = utf8_to_locale_ign_err(str);
|
||||
loc_str = utf8_to_locale_ign_err(get_impl(str));
|
||||
|
||||
printf(_("Nothing similar to %s, sorry :(\n"), utf8_output_ ? str : loc_str.c_str());
|
||||
printf(_("Nothing similar to %s, sorry :(\n"), utf8_output_ ? get_impl(str) : loc_str.c_str());
|
||||
}
|
||||
g_free(str);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -66,8 +66,8 @@ bool DictInfo::load_from_ifo_file(const std::string& ifofilename,
|
||||
bool istreedict)
|
||||
{
|
||||
ifo_file_name = ifofilename;
|
||||
gchar *buffer;
|
||||
if (!g_file_get_contents(ifofilename.c_str(), &buffer, nullptr, nullptr))
|
||||
glib::CharStr buffer;
|
||||
if (!g_file_get_contents(ifofilename.c_str(), get_addr(buffer), nullptr, nullptr))
|
||||
return false;
|
||||
|
||||
static const char TREEDICT_MAGIC_DATA[] = "StarDict's treedict ifo file";
|
||||
@@ -76,23 +76,19 @@ bool DictInfo::load_from_ifo_file(const std::string& ifofilename,
|
||||
const gchar *magic_data = istreedict ? TREEDICT_MAGIC_DATA : DICT_MAGIC_DATA;
|
||||
static const unsigned char utf8_bom[] = { 0xEF, 0xBB, 0xBF, '\0'};
|
||||
if (!g_str_has_prefix(
|
||||
g_str_has_prefix(buffer, (const gchar *)(utf8_bom)) ? buffer + 3 : buffer,
|
||||
g_str_has_prefix(get_impl(buffer), (const gchar *)(utf8_bom)) ? get_impl(buffer) + 3 : get_impl(buffer),
|
||||
magic_data)) {
|
||||
g_free(buffer);
|
||||
return false;
|
||||
}
|
||||
|
||||
gchar *p1,*p2,*p3;
|
||||
gchar *p1 = get_impl(buffer) + strlen(magic_data)-1;
|
||||
|
||||
p1 = buffer + strlen(magic_data)-1;
|
||||
|
||||
p2 = strstr(p1,"\nwordcount=");
|
||||
if (!p2) {
|
||||
g_free(buffer);
|
||||
gchar *p2 = strstr(p1, "\nwordcount=");
|
||||
if (p2 == nullptr)
|
||||
return false;
|
||||
}
|
||||
|
||||
p3 = strchr(p2+ sizeof("\nwordcount=")-1,'\n');
|
||||
gchar *p3 = strchr(p2 + sizeof("\nwordcount=") - 1, '\n');
|
||||
gchar *tmpstr = (gchar *)g_memdup(p2+sizeof("\nwordcount=")-1, p3-(p2+sizeof("\nwordcount=")-1)+1);
|
||||
tmpstr[p3-(p2+sizeof("\nwordcount=")-1)] = '\0';
|
||||
wordcount = atol(tmpstr);
|
||||
@@ -100,10 +96,9 @@ bool DictInfo::load_from_ifo_file(const std::string& ifofilename,
|
||||
|
||||
if (istreedict) {
|
||||
p2 = strstr(p1,"\ntdxfilesize=");
|
||||
if (!p2) {
|
||||
g_free(buffer);
|
||||
if (p2 == nullptr)
|
||||
return false;
|
||||
}
|
||||
|
||||
p3 = strchr(p2+ sizeof("\ntdxfilesize=")-1,'\n');
|
||||
tmpstr = (gchar *)g_memdup(p2+sizeof("\ntdxfilesize=")-1, p3-(p2+sizeof("\ntdxfilesize=")-1)+1);
|
||||
tmpstr[p3-(p2+sizeof("\ntdxfilesize=")-1)] = '\0';
|
||||
@@ -112,10 +107,8 @@ bool DictInfo::load_from_ifo_file(const std::string& ifofilename,
|
||||
} else {
|
||||
|
||||
p2 = strstr(p1,"\nidxfilesize=");
|
||||
if (!p2) {
|
||||
g_free(buffer);
|
||||
if (p2 == nullptr)
|
||||
return false;
|
||||
}
|
||||
|
||||
p3 = strchr(p2+ sizeof("\nidxfilesize=")-1,'\n');
|
||||
tmpstr = (gchar *)g_memdup(p2+sizeof("\nidxfilesize=")-1, p3-(p2+sizeof("\nidxfilesize=")-1)+1);
|
||||
@@ -126,10 +119,8 @@ bool DictInfo::load_from_ifo_file(const std::string& ifofilename,
|
||||
|
||||
p2 = strstr(p1,"\nbookname=");
|
||||
|
||||
if (!p2) {
|
||||
g_free(buffer);
|
||||
if (p2 == nullptr)
|
||||
return false;
|
||||
}
|
||||
|
||||
p2 = p2 + sizeof("\nbookname=") -1;
|
||||
p3 = strchr(p2, '\n');
|
||||
@@ -177,8 +168,6 @@ bool DictInfo::load_from_ifo_file(const std::string& ifofilename,
|
||||
sametypesequence.assign(p2, p3-p2);
|
||||
}
|
||||
|
||||
g_free(buffer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -809,7 +798,7 @@ bool Dict::load(const std::string& ifofilename)
|
||||
fullfilename.replace(fullfilename.length()-sizeof("ifo")+1, sizeof("ifo")-1, "dict.dz");
|
||||
|
||||
if (g_file_test(fullfilename.c_str(), G_FILE_TEST_EXISTS)) {
|
||||
dictdzfile.reset(new dictData);
|
||||
dictdzfile.reset(new DictData);
|
||||
if (!dictdzfile->open(fullfilename, 0)) {
|
||||
//g_print("open file %s failed!\n",fullfilename);
|
||||
return false;
|
||||
|
||||
@@ -56,7 +56,7 @@ public:
|
||||
protected:
|
||||
std::string sametypesequence;
|
||||
FILE *dictfile = nullptr;
|
||||
std::unique_ptr<dictData> dictdzfile;
|
||||
std::unique_ptr<DictData> dictdzfile;
|
||||
private:
|
||||
cacheItem cache[WORDDATA_CACHE_NUM];
|
||||
gint cache_cur = 0;
|
||||
|
||||
@@ -53,22 +53,6 @@ std::string utf8_to_locale_ign_err(const std::string& utf8_str)
|
||||
return res;
|
||||
}
|
||||
|
||||
char *locale_to_utf8(const char *loc_str)
|
||||
{
|
||||
if (nullptr == loc_str)
|
||||
return nullptr;
|
||||
gsize bytes_read, bytes_written;
|
||||
glib::Error err;
|
||||
gchar *str = g_locale_to_utf8(loc_str, -1, &bytes_read, &bytes_written, get_addr(err));
|
||||
if (nullptr == str){
|
||||
fprintf(stderr, _("Can not convert %s to utf8.\n"), loc_str);
|
||||
fprintf(stderr, "%s\n", err->message);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
static void __for_each_file(const std::string& dirname, 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)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <glib.h>
|
||||
#include <cassert>
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <functional>
|
||||
@@ -14,6 +15,10 @@ public:
|
||||
ResourceWrapper& operator=(const ResourceWrapper&) = delete;
|
||||
T *operator->() const { return p_; }
|
||||
bool operator!() const { return p_ == nullptr; }
|
||||
const T& operator[](size_t idx) const {
|
||||
assert(p_ != nullptr);
|
||||
return p_[idx];
|
||||
}
|
||||
|
||||
void reset(T *newp) {
|
||||
if (p_ != newp) {
|
||||
@@ -56,8 +61,6 @@ namespace glib {
|
||||
typedef ResourceWrapper<GError, GError, g_error_free> Error;
|
||||
}
|
||||
|
||||
|
||||
extern char *locale_to_utf8(const char *locstr);
|
||||
extern std::string utf8_to_locale_ign_err(const std::string& utf8_str);
|
||||
|
||||
extern void for_each_file(const std::list<std::string>& dirs_list, const std::string& suff,
|
||||
|
||||
Reference in New Issue
Block a user