code cleanups + use where possible get_uint32 instead of not safe cast

This commit is contained in:
Evgeniy Dushistov
2013-07-07 17:05:55 +00:00
parent fae4b432e1
commit 9034e792b6

View File

@@ -1,4 +1,4 @@
/* /*
* This file part of sdcv - console version of Stardict program * This file part of sdcv - console version of Stardict program
* http://sdcv.sourceforge.net * http://sdcv.sourceforge.net
* Copyright (C) 2005-2006 Evgeniy <dushistov@mail.ru> * Copyright (C) 2005-2006 Evgeniy <dushistov@mail.ru>
@@ -133,11 +133,11 @@ static std::string parse_data(const gchar *data, bool colorize_output)
return ""; return "";
std::string res; std::string res;
guint32 data_size, sec_size=0; guint32 data_size, sec_size = 0;
gchar *m_str; gchar *m_str;
const gchar *p=data; const gchar *p = data;
data_size=*((guint32 *)p); data_size = get_uint32(p);
p+=sizeof(guint32); p += sizeof(guint32);
while (guint32(p - data)<data_size) { while (guint32(p - data)<data_size) {
switch (*p++) { switch (*p++) {
case 'm': case 'm':
@@ -176,32 +176,31 @@ static std::string parse_data(const gchar *data, bool colorize_output)
break; break;
case 'y': case 'y':
sec_size = strlen(p); sec_size = strlen(p);
sec_size++; sec_size++;
break; break;
case 'W': case 'W':
case 'P': case 'P':
sec_size = *((guint32 *)p); sec_size = get_uint32(p);
sec_size += sizeof(guint32); sec_size += sizeof(guint32);
break; break;
} }
p += sec_size; p += sec_size;
} }
return res; return res;
} }
void Library::SimpleLookup(const std::string &str, TSearchResultList& res_list) void Library::SimpleLookup(const std::string &str, TSearchResultList& res_list)
{ {
glong ind; glong ind;
res_list.reserve(ndicts()); res_list.reserve(ndicts());
for (gint idict = 0; idict < ndicts(); ++idict) for (gint idict = 0; idict < ndicts(); ++idict)
if (SimpleLookupWord(str.c_str(), ind, idict)) if (SimpleLookupWord(str.c_str(), ind, idict))
res_list.push_back( res_list.push_back(
TSearchResult(dict_name(idict), TSearchResult(dict_name(idict),
poGetWord(ind, idict), poGetWord(ind, idict),
parse_data(poGetWordData(ind, idict), colorize_output_))); parse_data(poGetWordData(ind, idict), colorize_output_)));
} }
void Library::LookupWithFuzzy(const std::string &str, TSearchResultList& res_list) void Library::LookupWithFuzzy(const std::string &str, TSearchResultList& res_list)
@@ -211,9 +210,8 @@ void Library::LookupWithFuzzy(const std::string &str, TSearchResultList& res_lis
gchar *fuzzy_res[MAXFUZZY]; gchar *fuzzy_res[MAXFUZZY];
if (!Libs::LookupWithFuzzy(str.c_str(), fuzzy_res, MAXFUZZY)) if (!Libs::LookupWithFuzzy(str.c_str(), fuzzy_res, MAXFUZZY))
return; return;
for (gchar **p=fuzzy_res, **end=fuzzy_res+MAXFUZZY; for (gchar **p = fuzzy_res, **end = (fuzzy_res + MAXFUZZY); p != end && *p; ++p) {
p!=end && *p; ++p) {
SimpleLookup(*p, res_list); SimpleLookup(*p, res_list);
g_free(*p); g_free(*p);
} }
@@ -224,7 +222,7 @@ void Library::LookupWithRule(const std::string &str, TSearchResultList& res_list
std::vector<gchar *> match_res((MAX_MATCH_ITEM_PER_LIB) * ndicts()); std::vector<gchar *> match_res((MAX_MATCH_ITEM_PER_LIB) * ndicts());
const gint nfound = Libs::LookupWithRule(str.c_str(), &match_res[0]); const gint nfound = Libs::LookupWithRule(str.c_str(), &match_res[0]);
if (!nfound) if (nfound == 0)
return; return;
for (gint i = 0; i < nfound; ++i) { for (gint i = 0; i < nfound; ++i) {
@@ -265,7 +263,7 @@ void Library::print_search_result(FILE *out, const TSearchResult & res)
colorize_output_ ? SEARCH_TERM_VISFMT : "", colorize_output_ ? SEARCH_TERM_VISFMT : "",
utf8_output_ ? res.def.c_str() : loc_def.c_str(), utf8_output_ ? res.def.c_str() : loc_def.c_str(),
colorize_output_ ? ESC_END : "", colorize_output_ ? ESC_END : "",
utf8_output_ ? res.exp.c_str() : loc_exp.c_str()); utf8_output_ ? res.exp.c_str() : loc_exp.c_str());
} }
namespace { namespace {
@@ -322,7 +320,7 @@ bool Library::process_phrase(const char *loc_str, IReadLine &io, bool force)
if (str[0]=='\0') if (str[0]=='\0')
return true; return true;
TSearchResultList res_list; TSearchResultList res_list;
switch (analyze_query(str, query)) { switch (analyze_query(str, query)) {
@@ -344,7 +342,7 @@ bool Library::process_phrase(const char *loc_str, IReadLine &io, bool force)
/*nothing*/; /*nothing*/;
} }
if (!res_list.empty()) { if (!res_list.empty()) {
/* try to be more clever, if there are /* try to be more clever, if there are
one or zero results per dictionary show all one or zero results per dictionary show all
*/ */
@@ -368,7 +366,7 @@ bool Library::process_phrase(const char *loc_str, IReadLine &io, bool force)
}//if (!force) }//if (!force)
if (!show_all_results && !force) { if (!show_all_results && !force) {
printf(_("Found %zu items, similar to %s.\n"), res_list.size(), printf(_("Found %zu items, similar to %s.\n"), res_list.size(),
utf8_output_ ? str : utf8_to_locale_ign_err(str).c_str()); utf8_output_ ? str : utf8_to_locale_ign_err(str).c_str());
for (size_t i = 0; i < res_list.size(); ++i) { 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_bookname = utf8_to_locale_ign_err(res_list[i].bookname);
@@ -387,29 +385,29 @@ bool Library::process_phrase(const char *loc_str, IReadLine &io, bool force)
std::string str_choise; std::string str_choise;
choice_readline->read(_("Your choice[-1 to abort]: "), str_choise); choice_readline->read(_("Your choice[-1 to abort]: "), str_choise);
sscanf(str_choise.c_str(), "%d", &choise); sscanf(str_choise.c_str(), "%d", &choise);
if (choise >= 0 && choise < int(res_list.size())) { if (choise >= 0 && choise < int(res_list.size())) {
sdcv_pager pager; sdcv_pager pager;
print_search_result(pager.get_stream(), res_list[choise]); print_search_result(pager.get_stream(), res_list[choise]);
break; break;
} else if (choise == -1){ } else if (choise == -1){
break; break;
} else } else
printf(_("Invalid choice.\nIt must be from 0 to %zu or -1.\n"), printf(_("Invalid choice.\nIt must be from 0 to %zu or -1.\n"),
res_list.size()-1); res_list.size()-1);
} }
} else { } else {
sdcv_pager pager(force); sdcv_pager pager(force);
fprintf(pager.get_stream(), _("Found %zu items, similar to %s.\n"), 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_ ? str : utf8_to_locale_ign_err(str).c_str());
for (const TSearchResult& search_res : res_list) for (const TSearchResult& search_res : res_list)
print_search_result(pager.get_stream(), search_res); print_search_result(pager.get_stream(), search_res);
} }
} else { } else {
std::string loc_str; std::string loc_str;
if (!utf8_output_) if (!utf8_output_)
loc_str = utf8_to_locale_ign_err(str); loc_str = utf8_to_locale_ign_err(str);
printf(_("Nothing similar to %s, sorry :(\n"), utf8_output_ ? str : loc_str.c_str()); printf(_("Nothing similar to %s, sorry :(\n"), utf8_output_ ? str : loc_str.c_str());
} }
g_free(str); g_free(str);