Debian Codegraph @

src/Base/Tools.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2009 Werner Mayer <wmayer[at]users.sourceforge.net> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Library General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Library General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Library General Public *
19 * License along with this library; see the file COPYING.LIB. If not, *
20 * write to the Free Software Foundation, Inc., 59 Temple Place, *
21 * Suite 330, Boston, MA 02111-1307, USA *
22 * *
23 ***************************************************************************/
24
25 #include <unicode/unistr.h>
26 #include <unicode/uchar.h>
27 #include <vector>
28 #include <string>
29 #include <sstream>
30 #include <QDateTime>
31 #include <QTimeZone>
32
33 #include "PyExport.h"
34 #include "Interpreter.h"
35 #include "Tools.h"
36
37 namespace
38 {
39 constexpr auto underscore = static_cast<UChar32>(U'_');
40
41 bool isValidFirstChar(UChar32 c)
42 {
43 auto category = static_cast<UCharCategory>(u_charType(c));
44
45 return (c == underscore)
46 || (category == U_UPPERCASE_LETTER || category == U_LOWERCASE_LETTER
47 || category == U_TITLECASE_LETTER || category == U_MODIFIER_LETTER
48 || category == U_OTHER_LETTER || category == U_LETTER_NUMBER);
49 }
50
51 bool isValidSubsequentChar(UChar32 c)
52 {
53 auto category = static_cast<UCharCategory>(u_charType(c));
54 return (c == underscore)
55 || (category == U_UPPERCASE_LETTER || category == U_LOWERCASE_LETTER
56 || category == U_TITLECASE_LETTER || category == U_MODIFIER_LETTER
57 || category == U_OTHER_LETTER || category == U_LETTER_NUMBER
58 || category == U_DECIMAL_DIGIT_NUMBER || category == U_NON_SPACING_MARK
59 || category == U_COMBINING_SPACING_MARK || category == U_CONNECTOR_PUNCTUATION);
60 }
61
62 std::string unicodeCharToStdString(UChar32 c)
63 {
64 icu::UnicodeString uChar(c);
65 std::string utf8Char;
66 return uChar.toUTF8String(utf8Char);
67 }
68
69 }; // namespace
70
71 std::string Base::Tools::getIdentifier(const std::string& name)
72 {
73 if (name.empty()) {
74 return "_";
75 }
76
77 icu::UnicodeString uName = icu::UnicodeString::fromUTF8(name);
78 std::stringstream result;
79
80 // Handle the first character independently, prepending an underscore if it is not a valid
81 // first character, but *is* a valid later character
82 UChar32 firstChar = uName.char32At(0);
83 const int32_t firstCharLength = U16_LENGTH(firstChar);
84 if (!isValidFirstChar(firstChar)) {
85 result << "_";
86 if (isValidSubsequentChar(firstChar)) {
87 result << unicodeCharToStdString(firstChar);
88 }
89 }
90 else {
91 result << unicodeCharToStdString(firstChar);
92 }
93
94 for (int32_t i = firstCharLength; i < uName.length(); /* will increment by char length */) {
95 UChar32 c = uName.char32At(i);
96 int32_t charLength = U16_LENGTH(c);
97 i += charLength;
98
99 if (isValidSubsequentChar(c)) {
100 result << unicodeCharToStdString(c);
101 }
102 else {
103 result << "_";
104 }
105 }
106
107 return result.str();
108 }
109
110 std::wstring Base::Tools::widen(const std::string& str)
111 {
112 std::wostringstream wstm;
113 const std::ctype<wchar_t>& ctfacet = std::use_facet<std::ctype<wchar_t>>(wstm.getloc());
114 for (char i : str) {
115 wstm << ctfacet.widen(i);
116 }
117 return wstm.str();
118 }
119
120 std::string Base::Tools::narrow(const std::wstring& str)
121 {
122 std::ostringstream stm;
123 const std::ctype<char>& ctfacet = std::use_facet<std::ctype<char>>(stm.getloc());
124 for (wchar_t i : str) {
125 stm << ctfacet.narrow(i, 0);
126 }
127 return stm.str();
128 }
129
130 std::string Base::Tools::escapedUnicodeFromUtf8(const char* s)
131 {
132 Base::PyGILStateLocker lock;
133 std::string escapedstr;
134
135 PyObject* unicode = PyUnicode_FromString(s);
136 if (!unicode) {
137 return escapedstr;
138 }
139
140 PyObject* escaped = PyUnicode_AsUnicodeEscapeString(unicode);
141 if (escaped) {
142 escapedstr = std::string(PyBytes_AsString(escaped));
143 Py_DECREF(escaped);
144 }
145
146 Py_DECREF(unicode);
147 return escapedstr;
148 }
149
150 std::string Base::Tools::escapedUnicodeToUtf8(const std::string& s)
151 {
152 Base::PyGILStateLocker lock;
153 std::string string;
154
155 PyObject* unicode
156 = PyUnicode_DecodeUnicodeEscape(s.c_str(), static_cast<Py_ssize_t>(s.size()), "strict");
157 if (!unicode) {
158 return string;
159 }
160 if (PyUnicode_Check(unicode)) {
161 string = PyUnicode_AsUTF8(unicode);
162 }
163 Py_DECREF(unicode);
164 return string;
165 }
166
167 std::string Base::Tools::escapeQuotesFromString(const std::string& s)
168 {
169 std::string result;
170 size_t len = s.size();
171 for (size_t i = 0; i < len; ++i) {
172 switch (s.at(i)) {
173 case '\"':
174 result += "\\\"";
175 break;
176 case '\'':
177 result += "\\\'";
178 break;
179 default:
180 result += s.at(i);
181 break;
182 }
183 }
184 return result;
185 }
186
187 QString Base::Tools::escapeEncodeString(const QString& s)
188 {
189 QString result;
190 const int len = s.length();
191 result.reserve(int(len * 1.1));
192 for (int i = 0; i < len; ++i) {
193 if (s.at(i) == QLatin1Char('\\')) {
194 result += QLatin1String("\\\\");
195 }
196 else if (s.at(i) == QLatin1Char('\"')) {
197 result += QLatin1String("\\\"");
198 }
199 else if (s.at(i) == QLatin1Char('\'')) {
200 result += QLatin1String("\\\'");
201 }
202 else {
203 result += s.at(i);
204 }
205 }
206 result.squeeze();
207 return result;
208 }
209
210 std::string Base::Tools::escapeEncodeString(const std::string& s)
211 {
212 std::string result;
213 size_t len = s.size();
214 for (size_t i = 0; i < len; ++i) {
215 switch (s.at(i)) {
216 case '\\':
217 result += "\\\\";
218 break;
219 case '\"':
220 result += "\\\"";
221 break;
222 case '\'':
223 result += "\\\'";
224 break;
225 case '\n':
226 result += "\\n";
227 break;
228 case '\r':
229 result += "\\r";
230 break;
231 default:
232 result += s.at(i);
233 break;
234 }
235 }
236 return result;
237 }
238
239 QString Base::Tools::escapeEncodeFilename(const QString& s)
240 {
241 QString result;
242 const int len = s.length();
243 result.reserve(int(len * 1.1));
244 for (int i = 0; i < len; ++i) {
245 if (s.at(i) == QLatin1Char('\"')) {
246 result += QLatin1String("\\\"");
247 }
248 else if (s.at(i) == QLatin1Char('\'')) {
249 result += QLatin1String("\\\'");
250 }
251 else {
252 result += s.at(i);
253 }
254 }
255 result.squeeze();
256 return result;
257 }
258
259 std::string Base::Tools::escapeEncodeFilename(const std::string& s)
260 {
261 std::string result;
262 size_t len = s.size();
263 for (size_t i = 0; i < len; ++i) {
264 switch (s.at(i)) {
265 case '\"':
266 result += "\\\"";
267 break;
268 case '\'':
269 result += "\\\'";
270 break;
271 default:
272 result += s.at(i);
273 break;
274 }
275 }
276 return result;
277 }
278
279 std::string Base::Tools::quoted(const char* name)
280 {
281 std::stringstream str;
282 str << "\"" << name << "\"";
283 return str.str();
284 }
285
286 std::string Base::Tools::quoted(const std::string& name)
287 {
288 std::stringstream str;
289 str << "\"" << name << "\"";
290 return str.str();
291 }
292
293 std::string Base::Tools::joinList(const std::vector<std::string>& vec, const std::string& sep)
294 {
295 std::stringstream str;
296 for (const auto& it : vec) {
297 str << it << sep;
298 }
299 return str.str();
300 }
301
302 std::string Base::Tools::currentDateTimeString()
303 {
304 return QDateTime::currentDateTime()
305 .toTimeZone(QTimeZone::systemTimeZone())
306 .toString(Qt::ISODate)
307 .toStdString();
308 }
309
310 std::vector<std::string> Base::Tools::splitSubName(const std::string& subname)
311 {
312 // Turns 'Part.Part001.Body.Pad.Edge1'
313 // Into ['Part', 'Part001', 'Body', 'Pad', 'Edge1']
314 std::vector<std::string> subNames;
315 std::string subName;
316 std::istringstream subNameStream(subname);
317 while (std::getline(subNameStream, subName, '.')) {
318 subNames.push_back(subName);
319 }
320
321 // Check if the last character of the input string is the delimiter.
322 // If so, add an empty string to the subNames vector.
323 // Because the last subname is the element name and can be empty.
324 if (!subname.empty() && subname.back() == '.') {
325 subNames.push_back(""); // Append empty string for trailing dot.
326 }
327
328 return subNames;
329 }
330
331 // ------------------------------------------------------------------------------------------------
332
333 void Base::ZipTools::rewrite(const std::string& source, const std::string& target)
334 {
335 Base::PyGILStateLocker lock;
336 PyObject* module = PyImport_ImportModule("freecad.utils_zip");
337 if (!module) {
338 throw Py::Exception();
339 }
340
341 Py::Module commands(module, true);
342 commands.callMemberFunction("rewrite", Py::TupleN(Py::String(source), Py::String(target)));
343 }