sdcv.cpp: use c++11 where possible

This commit is contained in:
Evgeniy Dushistov
2013-07-06 22:37:42 +00:00
parent 0bd3912672
commit edfee97db3

View File

@@ -41,40 +41,8 @@
static const char gVersion[] = VERSION;
struct PrintDictInfo {
void operator()(const std::string& filename, bool) {
DictInfo dict_info;
if (dict_info.load_from_ifo_file(filename, false)) {
string bookname=utf8_to_locale_ign_err(dict_info.bookname);
printf("%s %d\n", bookname.c_str(), dict_info.wordcount);
}
}
};
struct CreateDisableList {
CreateDisableList(gchar **use_dist_list, strlist_t& disable_list_) :
disable_list(disable_list_)
{
gchar **p = use_dist_list;
while (*p) {
enable_list.push_back(*p);
++p;
}
}
void operator()(const std::string& filename, bool) {
DictInfo dict_info;
if (dict_info.load_from_ifo_file(filename, false) &&
std::find(enable_list.begin(), enable_list.end(),
dict_info.bookname)==enable_list.end())
disable_list.push_back(dict_info.ifo_file_name);
}
private:
strlist_t enable_list;
strlist_t& disable_list;
};
namespace {
void free_str_array(gchar **arr)
static void free_str_array(gchar **arr)
{
gchar **p;
@@ -105,22 +73,22 @@ int main(int argc, char *argv[])
GOptionEntry entries[] = {
{"version", 'v', 0, G_OPTION_ARG_NONE, &show_version,
_("display version information and exit"), NULL },
_("display version information and exit"), nullptr },
{"list-dicts", 'l', 0, G_OPTION_ARG_NONE, &show_list_dicts,
_("display list of available dictionaries and exit"), NULL},
_("display list of available dictionaries and exit"), nullptr},
{"use-dict", 'u', 0, G_OPTION_ARG_STRING_ARRAY, get_addr(use_dict_list),
_("for search use only dictionary with this bookname"),
_("bookname")},
{"non-interactive", 'n', 0, G_OPTION_ARG_NONE, &non_interactive,
_("for use in scripts"), NULL},
_("for use in scripts"), nullptr},
{"utf8-output", '0', 0, G_OPTION_ARG_NONE, &utf8_output,
_("output must be in utf8"), NULL},
_("output must be in utf8"), nullptr},
{"utf8-input", '1', 0, G_OPTION_ARG_NONE, &utf8_input,
_("input of sdcv in utf8"), NULL},
_("input of sdcv in utf8"), nullptr},
{"data-dir", '2', 0, G_OPTION_ARG_STRING, get_addr(opt_data_dir),
_("use this directory as path to stardict data directory"),
_("path/to/dir")},
{ NULL },
{ nullptr },
};
glib::Error error;
@@ -128,8 +96,8 @@ int main(int argc, char *argv[])
context = g_option_context_new(_(" words"));
g_option_context_set_help_enabled(context, TRUE);
g_option_context_add_main_entries(context, entries, NULL);
gboolean parse_res = g_option_context_parse(context, &argc, &argv, get_addr(error));
g_option_context_add_main_entries(context, entries, nullptr);
const gboolean parse_res = g_option_context_parse(context, &argc, &argv, get_addr(error));
g_option_context_free(context);
if (!parse_res) {
fprintf(stderr, _("Invalid command line arguments: %s\n"),
@@ -154,40 +122,50 @@ int main(int argc, char *argv[])
data_dir = get_impl(opt_data_dir);
}
strlist_t dicts_dir_list;
const char *homedir = g_getenv ("HOME");
if (!homedir)
homedir = g_get_home_dir ();
dicts_dir_list.push_back(std::string(homedir)+G_DIR_SEPARATOR+
".stardict"+G_DIR_SEPARATOR+"dic");
dicts_dir_list.push_back(data_dir);
const strlist_t dicts_dir_list = {
std::string(homedir) + G_DIR_SEPARATOR + ".stardict" + G_DIR_SEPARATOR + "dic",
data_dir
};
if (show_list_dicts) {
printf(_("Dictionary's name Word count\n"));
PrintDictInfo print_dict_info;
strlist_t order_list, disable_list;
for_each_file(dicts_dir_list, ".ifo", order_list,
disable_list, print_dict_info);
disable_list, [](const std::string& filename, bool) -> void {
DictInfo dict_info;
if (dict_info.load_from_ifo_file(filename, false)) {
const string bookname = utf8_to_locale_ign_err(dict_info.bookname);
printf("%s %d\n", bookname.c_str(), dict_info.wordcount);
}
});
return EXIT_SUCCESS;
}
strlist_t disable_list;
//DictInfoList dict_info_list;
if (use_dict_list) {
strlist_t empty_list;
CreateDisableList create_disable_list(get_impl(use_dict_list), disable_list);
for_each_file(dicts_dir_list, ".ifo", empty_list,
empty_list, create_disable_list);
for_each_file(dicts_dir_list, ".ifo", empty_list, empty_list,
[&disable_list, &use_dict_list](const std::string& filename, bool) -> void {
DictInfo dict_info;
const bool load_ok = dict_info.load_from_ifo_file(filename, false);
if (!load_ok)
return;
for (gchar **p = get_impl(use_dict_list); *p != nullptr; ++p)
if (strcmp(*p, dict_info.bookname.c_str()) == 0)
return;
disable_list.push_back(dict_info.ifo_file_name);
});
}
string conf_dir = string(g_get_home_dir())+G_DIR_SEPARATOR+".stardict";
const string conf_dir = string(g_get_home_dir())+G_DIR_SEPARATOR+".stardict";
if (g_mkdir(conf_dir.c_str(), S_IRWXU)==-1 && errno!=EEXIST)
fprintf(stderr, _("g_mkdir failed: %s\n"), strerror(errno));
@@ -196,8 +174,7 @@ int main(int argc, char *argv[])
strlist_t empty_list;
lib.load(dicts_dir_list, empty_list, disable_list);
std::auto_ptr<read_line> io(create_readline_object());
std::unique_ptr<IReadLine> io(create_readline_object());
if (optind < argc) {
for(int i = optind; i < argc; ++i)
if (!lib.process_phrase(argv[i], *io, non_interactive))
@@ -215,7 +192,5 @@ int main(int argc, char *argv[])
} else
fprintf(stderr, _("There are no words/phrases to translate.\n"));
return EXIT_SUCCESS;
}