mirror of
https://github.com/Dushistov/sdcv.git
synced 2025-12-16 01:41:55 +00:00
cleanups for specify "dictionary order by"
This commit is contained in:
@@ -34,18 +34,19 @@
|
|||||||
|
|
||||||
#include "readline.hpp"
|
#include "readline.hpp"
|
||||||
|
|
||||||
|
bool stdio_getline(FILE *in, std::string & str)
|
||||||
|
{
|
||||||
|
assert(in != nullptr);
|
||||||
|
str.clear();
|
||||||
|
int ch;
|
||||||
|
while ((ch=fgetc(in)) != EOF && ch != '\n')
|
||||||
|
str += ch;
|
||||||
|
|
||||||
|
return EOF != ch;
|
||||||
|
}
|
||||||
|
|
||||||
#ifndef WITH_READLINE
|
#ifndef WITH_READLINE
|
||||||
namespace {
|
namespace {
|
||||||
static bool stdio_getline(FILE *in, std::string & str)
|
|
||||||
{
|
|
||||||
str.clear();
|
|
||||||
int ch;
|
|
||||||
while ((ch=fgetc(in)) != EOF && ch != '\n')
|
|
||||||
str += ch;
|
|
||||||
|
|
||||||
return EOF != ch;
|
|
||||||
}
|
|
||||||
|
|
||||||
class dummy_readline : public IReadLine {
|
class dummy_readline : public IReadLine {
|
||||||
public:
|
public:
|
||||||
bool read(const string& banner, string& line) override {
|
bool read(const string& banner, string& line) override {
|
||||||
|
|||||||
@@ -11,4 +11,4 @@ public:
|
|||||||
|
|
||||||
extern std::string sdcv_readline;
|
extern std::string sdcv_readline;
|
||||||
extern IReadLine *create_readline_object();
|
extern IReadLine *create_readline_object();
|
||||||
|
extern bool stdio_getline(FILE *in, std::string &str);
|
||||||
|
|||||||
104
src/sdcv.cpp
104
src/sdcv.cpp
@@ -31,6 +31,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
#include <glib/gi18n.h>
|
#include <glib/gi18n.h>
|
||||||
@@ -55,9 +56,11 @@ static void free_str_array(gchar **arr)
|
|||||||
}
|
}
|
||||||
namespace glib
|
namespace glib
|
||||||
{
|
{
|
||||||
typedef ResourceWrapper<gchar *, gchar *, free_str_array> StrArr;
|
using StrArr = ResourceWrapper<gchar *, gchar *, free_str_array>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void list_dicts(const std::list<std::string> &dicts_dir_list);
|
||||||
|
|
||||||
int main(int argc, char *argv[]) try {
|
int main(int argc, char *argv[]) try {
|
||||||
setlocale(LC_ALL, "");
|
setlocale(LC_ALL, "");
|
||||||
#if ENABLE_NLS
|
#if ENABLE_NLS
|
||||||
@@ -96,7 +99,7 @@ int main(int argc, char *argv[]) try {
|
|||||||
_("path/to/dir") },
|
_("path/to/dir") },
|
||||||
{ "color", 'c', 0, G_OPTION_ARG_NONE, &colorize,
|
{ "color", 'c', 0, G_OPTION_ARG_NONE, &colorize,
|
||||||
_("colorize the output"), nullptr },
|
_("colorize the output"), nullptr },
|
||||||
{ nullptr },
|
{},
|
||||||
};
|
};
|
||||||
|
|
||||||
glib::Error error;
|
glib::Error error;
|
||||||
@@ -137,72 +140,57 @@ int main(int argc, char *argv[]) try {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (show_list_dicts) {
|
if (show_list_dicts) {
|
||||||
printf(_("Dictionary's name Word count\n"));
|
list_dicts(dicts_dir_list);
|
||||||
std::list<std::string> order_list, disable_list;
|
|
||||||
for_each_file(dicts_dir_list, ".ifo", order_list,
|
|
||||||
disable_list, [](const std::string &filename, bool) -> void {
|
|
||||||
DictInfo dict_info;
|
|
||||||
if (dict_info.load_from_ifo_file(filename, false)) {
|
|
||||||
const std::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;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::list<std::string> disable_list;
|
std::list<std::string> disable_list;
|
||||||
std::list<std::string> order_list, bookname_list;
|
|
||||||
|
|
||||||
|
std::map<std::string, std::string> bookname_to_ifo;
|
||||||
|
for_each_file(dicts_dir_list, ".ifo", std::list<std::string>(), std::list<std::string>(),
|
||||||
|
[&bookname_to_ifo](const std::string &fname, bool) {
|
||||||
|
DictInfo dict_info;
|
||||||
|
const bool load_ok = dict_info.load_from_ifo_file(fname, false);
|
||||||
|
if (!load_ok)
|
||||||
|
return;
|
||||||
|
bookname_to_ifo[dict_info.bookname] = dict_info.ifo_file_name;
|
||||||
|
});
|
||||||
|
|
||||||
|
std::list<std::string> order_list;
|
||||||
if (use_dict_list) {
|
if (use_dict_list) {
|
||||||
std::list<std::string> empty_list;
|
for (auto &&x : bookname_to_ifo) {
|
||||||
|
gchar **p = get_impl(use_dict_list);
|
||||||
for_each_file(dicts_dir_list, ".ifo", empty_list, empty_list,
|
for (; *p != nullptr; ++p)
|
||||||
[&disable_list, &use_dict_list](const std::string &filename, bool) -> void {
|
if (x.first.compare(*p) == 0) {
|
||||||
DictInfo dict_info;
|
break;
|
||||||
const bool load_ok = dict_info.load_from_ifo_file(filename, false);
|
}
|
||||||
if (!load_ok)
|
if (*p == nullptr) {
|
||||||
return;
|
disable_list.push_back(x.second);
|
||||||
|
}
|
||||||
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);
|
|
||||||
});
|
|
||||||
|
|
||||||
// add bookname to list
|
// add bookname to list
|
||||||
gchar **p = get_impl(use_dict_list);
|
gchar **p = get_impl(use_dict_list);
|
||||||
while (*p) {
|
while (*p) {
|
||||||
bookname_list.push_back(*p);
|
order_list.push_back(bookname_to_ifo.at(*p));
|
||||||
++p;
|
++p;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
FILE *ordering_file;
|
const std::string odering_cfg_file = std::string(homedir) + G_DIR_SEPARATOR_S ".sdcv_ordering";
|
||||||
char bookname[1024];
|
FILE *ordering_file = fopen(odering_cfg_file.c_str(), "r");
|
||||||
if ((ordering_file = fopen((std::string(homedir)+G_DIR_SEPARATOR+".sdcv_ordering").c_str(), "r"))) {
|
if (ordering_file != nullptr) {
|
||||||
while (fgets(bookname, 1023, ordering_file))
|
std::string line;
|
||||||
bookname_list.push_back(bookname);
|
while (stdio_getline(ordering_file, line)) {
|
||||||
|
order_list.push_back(bookname_to_ifo.at(line));
|
||||||
|
}
|
||||||
fclose(ordering_file);
|
fclose(ordering_file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// translation from bookname to filename
|
|
||||||
for (std::list<std::string>::const_iterator bookname=bookname_list.begin();
|
|
||||||
bookname!=bookname_list.end(); ++bookname) {
|
|
||||||
std::list<std::string> empty_list;
|
|
||||||
for_each_file(dicts_dir_list, ".ifo", empty_list, empty_list,
|
|
||||||
[&order_list, &bookname](const std::string &filename, bool) -> void {
|
|
||||||
DictInfo dict_info;
|
|
||||||
if (dict_info.load_from_ifo_file(filename, false) &&
|
|
||||||
bookname->compare(0, dict_info.bookname.size(), dict_info.bookname) == 0 &&
|
|
||||||
std::find(order_list.begin(), order_list.end(), filename)==order_list.end())
|
|
||||||
order_list.push_back(filename);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string conf_dir = std::string(g_get_home_dir()) + G_DIR_SEPARATOR + ".stardict";
|
const std::string conf_dir = std::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));
|
||||||
|
}
|
||||||
|
|
||||||
Library lib(utf8_input, utf8_output, colorize);
|
Library lib(utf8_input, utf8_output, colorize);
|
||||||
lib.load(dicts_dir_list, order_list, disable_list);
|
lib.load(dicts_dir_list, order_list, disable_list);
|
||||||
@@ -210,8 +198,9 @@ int main(int argc, char *argv[]) try {
|
|||||||
std::unique_ptr<IReadLine> io(create_readline_object());
|
std::unique_ptr<IReadLine> 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) {
|
||||||
|
|
||||||
std::string phrase;
|
std::string phrase;
|
||||||
@@ -230,3 +219,18 @@ int main(int argc, char *argv[]) try {
|
|||||||
fprintf(stderr, "Internal error: %s\n", ex.what());
|
fprintf(stderr, "Internal error: %s\n", ex.what());
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void list_dicts(const std::list<std::string> &dicts_dir_list)
|
||||||
|
{
|
||||||
|
printf(_("Dictionary's name Word count\n"));
|
||||||
|
std::list<std::string> order_list, disable_list;
|
||||||
|
for_each_file(dicts_dir_list, ".ifo", order_list,
|
||||||
|
disable_list, [](const std::string &filename, bool) -> void {
|
||||||
|
DictInfo dict_info;
|
||||||
|
if (dict_info.load_from_ifo_file(filename, false)) {
|
||||||
|
const std::string bookname = utf8_to_locale_ign_err(dict_info.bookname);
|
||||||
|
printf("%s %d\n", bookname.c_str(), dict_info.wordcount);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user