From 9034e792b6edf0ae3300597a3f920737c11df354 Mon Sep 17 00:00:00 2001 From: Evgeniy Dushistov Date: Sun, 7 Jul 2013 17:05:55 +0000 Subject: [PATCH] code cleanups + use where possible get_uint32 instead of not safe cast --- src/libwrapper.cpp | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/src/libwrapper.cpp b/src/libwrapper.cpp index c50baaa..55164df 100644 --- a/src/libwrapper.cpp +++ b/src/libwrapper.cpp @@ -1,4 +1,4 @@ -/* +/* * This file part of sdcv - console version of Stardict program * http://sdcv.sourceforge.net * Copyright (C) 2005-2006 Evgeniy @@ -133,11 +133,11 @@ static std::string parse_data(const gchar *data, bool colorize_output) return ""; std::string res; - guint32 data_size, sec_size=0; + guint32 data_size, sec_size = 0; gchar *m_str; - const gchar *p=data; - data_size=*((guint32 *)p); - p+=sizeof(guint32); + const gchar *p = data; + data_size = get_uint32(p); + p += sizeof(guint32); while (guint32(p - data) match_res((MAX_MATCH_ITEM_PER_LIB) * ndicts()); const gint nfound = Libs::LookupWithRule(str.c_str(), &match_res[0]); - if (!nfound) + if (nfound == 0) return; 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 : "", utf8_output_ ? res.def.c_str() : loc_def.c_str(), 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 { @@ -322,7 +320,7 @@ bool Library::process_phrase(const char *loc_str, IReadLine &io, bool force) if (str[0]=='\0') return true; - + TSearchResultList res_list; switch (analyze_query(str, query)) { @@ -344,7 +342,7 @@ bool Library::process_phrase(const char *loc_str, IReadLine &io, bool force) /*nothing*/; } - if (!res_list.empty()) { + if (!res_list.empty()) { /* try to be more clever, if there are 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 (!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()); for (size_t i = 0; i < res_list.size(); ++i) { 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; choice_readline->read(_("Your choice[-1 to abort]: "), str_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; print_search_result(pager.get_stream(), res_list[choise]); break; } else if (choise == -1){ break; } 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); } } else { 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()); for (const TSearchResult& search_res : res_list) print_search_result(pager.get_stream(), search_res); } - + } else { std::string loc_str; if (!utf8_output_) loc_str = utf8_to_locale_ign_err(str); - + printf(_("Nothing similar to %s, sorry :(\n"), utf8_output_ ? str : loc_str.c_str()); } g_free(str);