mirror of
https://github.com/Dushistov/sdcv.git
synced 2025-12-16 18:01:54 +00:00
sdcv.cpp: use c++11 where possible
This commit is contained in:
109
src/sdcv.cpp
109
src/sdcv.cpp
@@ -41,47 +41,15 @@
|
|||||||
|
|
||||||
static const char gVersion[] = VERSION;
|
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 {
|
namespace {
|
||||||
void free_str_array(gchar **arr)
|
static void free_str_array(gchar **arr)
|
||||||
{
|
{
|
||||||
gchar **p;
|
gchar **p;
|
||||||
|
|
||||||
for (p = arr; *p; ++p)
|
for (p = arr; *p; ++p)
|
||||||
g_free(*p);
|
g_free(*p);
|
||||||
g_free(arr);
|
g_free(arr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
namespace glib {
|
namespace glib {
|
||||||
typedef ResourceWrapper<gchar *, gchar *, free_str_array> StrArr;
|
typedef ResourceWrapper<gchar *, gchar *, free_str_array> StrArr;
|
||||||
@@ -105,22 +73,22 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
GOptionEntry entries[] = {
|
GOptionEntry entries[] = {
|
||||||
{"version", 'v', 0, G_OPTION_ARG_NONE, &show_version,
|
{"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,
|
{"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),
|
{"use-dict", 'u', 0, G_OPTION_ARG_STRING_ARRAY, get_addr(use_dict_list),
|
||||||
_("for search use only dictionary with this bookname"),
|
_("for search use only dictionary with this bookname"),
|
||||||
_("bookname")},
|
_("bookname")},
|
||||||
{"non-interactive", 'n', 0, G_OPTION_ARG_NONE, &non_interactive,
|
{"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,
|
{"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,
|
{"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),
|
{"data-dir", '2', 0, G_OPTION_ARG_STRING, get_addr(opt_data_dir),
|
||||||
_("use this directory as path to stardict data directory"),
|
_("use this directory as path to stardict data directory"),
|
||||||
_("path/to/dir")},
|
_("path/to/dir")},
|
||||||
{ NULL },
|
{ nullptr },
|
||||||
};
|
};
|
||||||
|
|
||||||
glib::Error error;
|
glib::Error error;
|
||||||
@@ -128,8 +96,8 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
context = g_option_context_new(_(" words"));
|
context = g_option_context_new(_(" words"));
|
||||||
g_option_context_set_help_enabled(context, TRUE);
|
g_option_context_set_help_enabled(context, TRUE);
|
||||||
g_option_context_add_main_entries(context, entries, NULL);
|
g_option_context_add_main_entries(context, entries, nullptr);
|
||||||
gboolean parse_res = g_option_context_parse(context, &argc, &argv, get_addr(error));
|
const gboolean parse_res = g_option_context_parse(context, &argc, &argv, get_addr(error));
|
||||||
g_option_context_free(context);
|
g_option_context_free(context);
|
||||||
if (!parse_res) {
|
if (!parse_res) {
|
||||||
fprintf(stderr, _("Invalid command line arguments: %s\n"),
|
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);
|
data_dir = get_impl(opt_data_dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
strlist_t dicts_dir_list;
|
|
||||||
|
|
||||||
const char *homedir = g_getenv ("HOME");
|
const char *homedir = g_getenv ("HOME");
|
||||||
if (!homedir)
|
if (!homedir)
|
||||||
homedir = g_get_home_dir ();
|
homedir = g_get_home_dir ();
|
||||||
|
|
||||||
dicts_dir_list.push_back(std::string(homedir)+G_DIR_SEPARATOR+
|
const strlist_t dicts_dir_list = {
|
||||||
".stardict"+G_DIR_SEPARATOR+"dic");
|
std::string(homedir) + G_DIR_SEPARATOR + ".stardict" + G_DIR_SEPARATOR + "dic",
|
||||||
dicts_dir_list.push_back(data_dir);
|
data_dir
|
||||||
|
};
|
||||||
|
|
||||||
if (show_list_dicts) {
|
if (show_list_dicts) {
|
||||||
printf(_("Dictionary's name Word count\n"));
|
printf(_("Dictionary's name Word count\n"));
|
||||||
PrintDictInfo print_dict_info;
|
|
||||||
strlist_t order_list, disable_list;
|
strlist_t order_list, disable_list;
|
||||||
for_each_file(dicts_dir_list, ".ifo", order_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;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
strlist_t disable_list;
|
strlist_t disable_list;
|
||||||
//DictInfoList dict_info_list;
|
|
||||||
|
|
||||||
if (use_dict_list) {
|
if (use_dict_list) {
|
||||||
strlist_t empty_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,
|
for_each_file(dicts_dir_list, ".ifo", empty_list, empty_list,
|
||||||
empty_list, create_disable_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);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const string conf_dir = string(g_get_home_dir())+G_DIR_SEPARATOR+".stardict";
|
||||||
string conf_dir = string(g_get_home_dir())+G_DIR_SEPARATOR+".stardict";
|
|
||||||
if (g_mkdir(conf_dir.c_str(), S_IRWXU)==-1 && errno!=EEXIST)
|
if (g_mkdir(conf_dir.c_str(), S_IRWXU)==-1 && errno!=EEXIST)
|
||||||
fprintf(stderr, _("g_mkdir failed: %s\n"), strerror(errno));
|
fprintf(stderr, _("g_mkdir failed: %s\n"), strerror(errno));
|
||||||
|
|
||||||
@@ -196,10 +174,9 @@ int main(int argc, char *argv[])
|
|||||||
strlist_t empty_list;
|
strlist_t empty_list;
|
||||||
lib.load(dicts_dir_list, empty_list, disable_list);
|
lib.load(dicts_dir_list, empty_list, disable_list);
|
||||||
|
|
||||||
|
std::unique_ptr<IReadLine> io(create_readline_object());
|
||||||
std::auto_ptr<read_line> io(create_readline_object());
|
|
||||||
if (optind < argc) {
|
if (optind < argc) {
|
||||||
for(int i=optind; i<argc; ++i)
|
for(int i = optind; i < argc; ++i)
|
||||||
if (!lib.process_phrase(argv[i], *io, non_interactive))
|
if (!lib.process_phrase(argv[i], *io, non_interactive))
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
} else if (!non_interactive) {
|
} else if (!non_interactive) {
|
||||||
@@ -215,7 +192,5 @@ int main(int argc, char *argv[])
|
|||||||
} else
|
} else
|
||||||
fprintf(stderr, _("There are no words/phrases to translate.\n"));
|
fprintf(stderr, _("There are no words/phrases to translate.\n"));
|
||||||
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user