From 3812fad586acf7be1606a7c717687040adc6b6e9 Mon Sep 17 00:00:00 2001 From: Evgeniy Dushistov Date: Sat, 6 Jul 2013 22:44:11 +0000 Subject: [PATCH] c++11 for readline + libwrapper --- src/libwrapper.cpp | 107 ++++++++++++++++++++++----------------------- src/libwrapper.hpp | 11 ++--- src/readline.cpp | 104 ++++++++++++++++++++++--------------------- src/readline.hpp | 16 +++---- 4 files changed, 116 insertions(+), 122 deletions(-) diff --git a/src/libwrapper.cpp b/src/libwrapper.cpp index ddc35b0..b3c2d3d 100644 --- a/src/libwrapper.cpp +++ b/src/libwrapper.cpp @@ -67,7 +67,7 @@ static std::string xdxf2text(const char *p) res+=""; else if (name=="k") { const char *begin=next; - if ((next=strstr(begin, ""))!=NULL) + if ((next=strstr(begin, ""))!=nullptr) next+=sizeof("")-1-1; else next=begin; @@ -171,7 +171,7 @@ void Library::SimpleLookup(const string &str, TSearchResultList& res_list) { glong ind; res_list.reserve(ndicts()); - for (gint idict=0; idict match_res((MAX_MATCH_ITEM_PER_LIB) * ndicts()); - gint nfound=Libs::LookupWithRule(str.c_str(), &match_res[0]); + const gint nfound = Libs::LookupWithRule(str.c_str(), &match_res[0]); if (!nfound) return; - for (gint i=0; i > drl(ndicts()); if (!Libs::LookupData(str.c_str(), &drl[0])) return; - for (int idict=0; idict::size_type j=0; j::size_type j=0; j < drl[idict].size(); ++j) { SimpleLookup(drl[idict][j], res_list); g_free(drl[idict][j]); } @@ -237,51 +237,52 @@ void Library::print_search_result(FILE *out, const TSearchResult & res) utf8_output ? res.exp.c_str() : loc_exp.c_str()); } -class sdcv_pager { -public: - sdcv_pager(bool ignore_env=false) { - output=stdout; - if (ignore_env) - return; - const gchar *pager=g_getenv("SDCV_PAGER"); - if (pager && (output=popen(pager, "w"))==NULL) { - perror(_("popen failed")); - output=stdout; - } - } - ~sdcv_pager() { - if (output!=stdout) - fclose(output); - } - FILE *get_stream() { return output; } -private: - FILE *output; -}; +namespace { + class sdcv_pager { + public: + sdcv_pager(bool ignore_env=false) { + output = stdout; + if (ignore_env) + return; + const gchar *pager = g_getenv("SDCV_PAGER"); + if (pager && (output = popen(pager, "w")) == nullptr) { + perror(_("popen failed")); + output = stdout; + } + } + sdcv_pager(const sdcv_pager&) = delete; + sdcv_pager& operator=(const sdcv_pager&) = delete; + ~sdcv_pager() { + if (output != stdout) + fclose(output); + } + FILE *get_stream() { return output; } + private: + FILE *output; + }; +} -bool Library::process_phrase(const char *loc_str, read_line &io, bool force) +bool Library::process_phrase(const char *loc_str, IReadLine &io, bool force) { - if (NULL==loc_str) + if (nullptr==loc_str) return true; std::string query; - analyze_query(loc_str, query); if (!query.empty()) io.add_to_history(query.c_str()); - - gsize bytes_read; gsize bytes_written; - GError *err=NULL; - char *str=NULL; + GError *err = nullptr; + char *str = nullptr; if (!utf8_input) - str=g_locale_to_utf8(loc_str, -1, &bytes_read, &bytes_written, &err); + str = g_locale_to_utf8(loc_str, -1, &bytes_read, &bytes_written, &err); else - str=g_strdup(loc_str); + str = g_strdup(loc_str); - if (NULL==str) { + if (nullptr==str) { fprintf(stderr, _("Can not convert %s to utf8.\n"), loc_str); fprintf(stderr, "%s\n", err->message); g_error_free(err); @@ -290,11 +291,9 @@ bool Library::process_phrase(const char *loc_str, read_line &io, bool force) if (str[0]=='\0') return true; - TSearchResultList res_list; - switch (analyze_query(str, query)) { case qtFUZZY: LookupWithFuzzy(query, res_list); @@ -318,20 +317,19 @@ bool Library::process_phrase(const char *loc_str, read_line &io, bool force) /* try to be more clever, if there are one or zero results per dictionary show all */ - bool show_all_results=true; + bool show_all_results = true; typedef std::map< string, int, std::less > DictResMap; if (!force) { DictResMap res_per_dict; - for(TSearchResultList::iterator ptr=res_list.begin(); ptr!=res_list.end(); ++ptr){ - std::pair r = - res_per_dict.equal_range(ptr->bookname); + for (const TSearchResult& search_res : res_list) { + auto r = res_per_dict.equal_range(search_res.bookname); DictResMap tmp(r.first, r.second); if (tmp.empty()) //there are no yet such bookname in map - res_per_dict.insert(DictResMap::value_type(ptr->bookname, 1)); + res_per_dict.insert(DictResMap::value_type(search_res.bookname, 1)); else { ++((tmp.begin())->second); - if (tmp.begin()->second>1) { - show_all_results=false; + if (tmp.begin()->second > 1) { + show_all_results = false; break; } } @@ -341,25 +339,24 @@ bool Library::process_phrase(const char *loc_str, read_line &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()); - for (size_t i=0; i%s\n", i, utf8_output ? res_list[i].bookname.c_str() : loc_bookname.c_str(), utf8_output ? res_list[i].def.c_str() : loc_def.c_str()); } int choise; - std::auto_ptr choice_readline(create_readline_object()); + std::unique_ptr choice_readline(create_readline_object()); for (;;) { - string str_choise; + 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= 0 && choise < int(res_list.size())) { sdcv_pager pager; print_search_result(pager.get_stream(), res_list[choise]); break; - } else if (choise==-1){ + } else if (choise == -1){ break; } else printf(_("Invalid choice.\nIt must be from 0 to %zu or -1.\n"), @@ -369,8 +366,8 @@ bool Library::process_phrase(const char *loc_str, read_line &io, bool force) 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()); - for (PSearchResult ptr=res_list.begin(); ptr!=res_list.end(); ++ptr) - print_search_result(pager.get_stream(), *ptr); + for (const TSearchResult& search_res : res_list) + print_search_result(pager.get_stream(), search_res); } } else { diff --git a/src/libwrapper.hpp b/src/libwrapper.hpp index 76153ce..b3a215c 100644 --- a/src/libwrapper.hpp +++ b/src/libwrapper.hpp @@ -1,11 +1,10 @@ -#ifndef _LIBWRAPPER_HPP_ -#define _LIBWRAPPER_HPP_ +#pragma once #include #include #include "file.hpp" -#include "lib.h" +#include "lib.hpp" #include "readline.hpp" using std::string; @@ -24,8 +23,7 @@ struct TSearchResult { } }; -typedef vector TSearchResultList; -typedef TSearchResultList::iterator PSearchResult; +typedef std::vector TSearchResultList; //this class is wrapper around Dicts class for easy use //of it @@ -34,7 +32,7 @@ public: Library(bool uinput, bool uoutput) : utf8_input(uinput), utf8_output(uoutput) {} - bool process_phrase(const char *loc_str, read_line &io, bool force=false); + bool process_phrase(const char *loc_str, IReadLine &io, bool force=false); private: bool utf8_input, utf8_output; @@ -45,4 +43,3 @@ private: void print_search_result(FILE *out, const TSearchResult & res); }; -#endif//!_LIBWRAPPER_HPP_ diff --git a/src/readline.cpp b/src/readline.cpp index fdf311f..6b942be 100644 --- a/src/readline.cpp +++ b/src/readline.cpp @@ -35,65 +35,67 @@ #include "readline.hpp" #ifndef WITH_READLINE +namespace { + static bool stdio_getline(FILE *in, std::string & str) + { + str.clear(); + int ch; + while ((ch=fgetc(in)) != EOF && ch != '\n') + str += ch; -static bool stdio_getline(FILE *in, std::string & str) -{ - str.clear(); - int ch; - while ((ch=fgetc(in))!=EOF && ch!='\n') - str+=ch; - if (EOF==ch) - return false; - return true; + return EOF != ch; + } + + class dummy_readline : public IReadLine { + public: + bool read(const string& banner, string& line) override { + printf("%s", banner.c_str()); + return stdio_getline(stdin, line); + } + }; } - -class dummy_readline : public read_line { -public: - bool read(const string& banner, string& line) { - printf("%s", banner.c_str()); - return stdio_getline(stdin, line); - } -}; - #else -class real_readline : public read_line { +namespace { + class real_readline : public IReadLine { -public: - real_readline() - { - rl_readline_name = "sdcv"; - using_history(); - string histname=(string(g_get_home_dir())+G_DIR_SEPARATOR+".sdcv_history"); - read_history(histname.c_str());; - } - ~real_readline() - { - string histname=(string(g_get_home_dir())+G_DIR_SEPARATOR+".sdcv_history"); - write_history(histname.c_str()); - const gchar *hist_size_str=g_getenv("SDCV_HISTSIZE"); - int hist_size; - if (!hist_size_str || sscanf(hist_size_str, "%d", &hist_size)<1) - hist_size=2000; - history_truncate_file(histname.c_str(), hist_size); - } - bool read(const string &banner, string& line) - { - char *phrase=NULL; - phrase=readline(banner.c_str()); - if (phrase) { - line=phrase; - free(phrase); - return true; - } - return false; - } - void add_to_history(const std::string& phrase) { add_history(phrase.c_str()); } -}; + public: + 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() { + 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"); + int hist_size; + if (!hist_size_str || sscanf(hist_size_str, "%d", &hist_size)<1) + hist_size = 2000; + history_truncate_file(histname.c_str(), hist_size); + } + + bool read(const std::string &banner, std::string& line) override { + char *phrase = nullptr; + phrase = readline(banner.c_str()); + if (phrase) { + line = phrase; + free(phrase); + return true; + } + return false; + } + + void add_to_history(const std::string& phrase) override { + add_history(phrase.c_str()); + } + }; +} #endif//WITH_READLINE -read_line *create_readline_object() +IReadLine *create_readline_object() { #ifdef WITH_READLINE return new real_readline; diff --git a/src/readline.hpp b/src/readline.hpp index c782b4c..2911859 100644 --- a/src/readline.hpp +++ b/src/readline.hpp @@ -1,16 +1,14 @@ -#ifndef _READLINE_HPP_ -#define _READLINE_HPP_ +#pragma once #include -using std::string; -class read_line { +class IReadLine { public: - virtual ~read_line() {} - virtual bool read(const string &banner, string& line)=0; + virtual ~IReadLine() {} + virtual bool read(const std::string &banner, std::string& line) = 0; virtual void add_to_history(const std::string& phrase) {} }; -extern string sdcv_readline; -extern read_line *create_readline_object(); -#endif//!_READLINE_HPP_ +extern std::string sdcv_readline; +extern IReadLine *create_readline_object(); +