1 /*
2  * This file part of sdcv - console version of Stardict program
3  * http://sdcv.sourceforge.net
4  * Copyright (C) 2005-2006 Evgeniy <dushistov@mail.ru>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include <algorithm>
26 #include <cstdio>
27 #include <cstdlib>
28 #include <glib.h>
29 #include <glib/gi18n.h>
30 #include <iomanip>
31 #include <sstream>
32 
33 #include "utils.hpp"
34 
utf8_to_locale_ign_err(const std::string & utf8_str)35 std::string utf8_to_locale_ign_err(const std::string &utf8_str)
36 {
37     std::string res;
38 
39     const char *charset;
40     if (g_get_charset(&charset))
41         res = utf8_str;
42     else {
43         gsize bytes_read, bytes_written;
44         glib::Error err;
45         glib::CharStr tmp(g_convert_with_fallback(utf8_str.c_str(), -1, charset, "UTF-8", nullptr,
46                                                   &bytes_read, &bytes_written, get_addr(err)));
47         if (nullptr == get_impl(tmp)) {
48             fprintf(stderr, _("Can not convert %s to current locale.\n"), utf8_str.c_str());
49             fprintf(stderr, "%s\n", err->message);
50             exit(EXIT_FAILURE);
51         }
52         res = get_impl(tmp);
53     }
54 
55     return res;
56 }
57 
__for_each_file(const std::string & dirname,const std::string & suff,const std::list<std::string> & order_list,const std::list<std::string> & disable_list,const std::function<void (const std::string &,bool)> & f)58 static void __for_each_file(const std::string &dirname, const std::string &suff,
59                             const std::list<std::string> &order_list, const std::list<std::string> &disable_list,
60                             const std::function<void(const std::string &, bool)> &f)
61 {
62     GDir *dir = g_dir_open(dirname.c_str(), 0, nullptr);
63     if (dir) {
64         const gchar *filename;
65 
66         while ((filename = g_dir_read_name(dir)) != nullptr) {
67             const std::string fullfilename(dirname + G_DIR_SEPARATOR_S + filename);
68             if (g_file_test(fullfilename.c_str(), G_FILE_TEST_IS_DIR))
69                 __for_each_file(fullfilename, suff, order_list, disable_list, f);
70             else if (g_str_has_suffix(filename, suff.c_str()) && std::find(order_list.begin(), order_list.end(), fullfilename) == order_list.end()) {
71                 const bool disable = std::find(disable_list.begin(),
72                                                disable_list.end(),
73                                                fullfilename)
74                                      != disable_list.end();
75                 f(fullfilename, disable);
76             }
77         }
78         g_dir_close(dir);
79     }
80 }
81 
for_each_file(const std::list<std::string> & dirs_list,const std::string & suff,const std::list<std::string> & order_list,const std::list<std::string> & disable_list,const std::function<void (const std::string &,bool)> & f)82 void for_each_file(const std::list<std::string> &dirs_list, const std::string &suff,
83                    const std::list<std::string> &order_list, const std::list<std::string> &disable_list,
84                    const std::function<void(const std::string &, bool)> &f)
85 {
86     for (const std::string &item : order_list) {
87         const bool disable = std::find(disable_list.begin(), disable_list.end(), item) != disable_list.end();
88         f(item, disable);
89     }
90     for (const std::string &item : dirs_list)
91         __for_each_file(item, suff, order_list, disable_list, f);
92 }
93 
94 // based on https://stackoverflow.com/questions/7724448/simple-json-string-escape-for-c/33799784#33799784
json_escape_string(const std::string & s)95 std::string json_escape_string(const std::string &s)
96 {
97     std::ostringstream o;
98     for (auto c = s.cbegin(); c != s.cend(); c++) {
99         switch (*c) {
100         case '"':
101             o << "\\\"";
102             break;
103         case '\\':
104             o << "\\\\";
105             break;
106         case '\b':
107             o << "\\b";
108             break;
109         case '\f':
110             o << "\\f";
111             break;
112         case '\n':
113             o << "\\n";
114             break;
115         case '\r':
116             o << "\\r";
117             break;
118         case '\t':
119             o << "\\t";
120             break;
121         default:
122             if ('\x00' <= *c && *c <= '\x1f') {
123                 o << "\\u"
124                   << std::hex << std::setw(4) << std::setfill('0') << (int)*c;
125             } else {
126                 o << *c;
127             }
128         }
129     }
130     return o.str();
131 }
132