2323
2424#include < algorithm>
2525#include < ctime>
26+ #include < filesystem>
2627#include < locale>
2728#include < sstream>
2829#include < sys/stat.h>
4546using namespace digidoc ;
4647using namespace digidoc ::util;
4748using namespace std ;
49+ namespace fs = filesystem;
4850
4951#ifdef _WIN32
5052#define f_stat _wstat64
@@ -58,77 +60,6 @@ using f_statbuf = struct stat;
5860using f_utimbuf = struct utimbuf ;
5961#endif
6062
61- #if !defined(_WIN32) && !defined(__APPLE__) && !defined(__ANDROID__)
62- #include < cerrno>
63- #include < iconv.h>
64- #include < cstdlib>
65- #include < cstring>
66- #include < langinfo.h>
67-
68- /* *
69- * Helper method for converting from non-UTF-8 encoded strings to UTF-8.
70- * Supported LANG values for Linux: see /usr/share/i18n/SUPPORTED.
71- * Supported encodings for libiconv: see iconv --list .
72- *
73- * Note! If non-ASCII characters are used we assume a proper LANG value!!!
74- *
75- * @param str_in The string to be converted.
76- * @return Returns the input string in UTF-8.
77- */
78- string File::convertUTF8 (string_view str_in, bool to_UTF)
79- {
80- string charset = nl_langinfo (CODESET );
81- // no conversion needed for UTF-8
82- if (charset == " UTF-8" || charset == " utf-8" )
83- return string (str_in);
84-
85- iconv_t ic_descr = iconv_t (-1 );
86- try
87- {
88- ic_descr = to_UTF ? iconv_open (" UTF-8" , charset.c_str ()) : iconv_open (charset.c_str (), " UTF-8" );
89- }
90- catch (exception &) {}
91-
92- if (ic_descr == iconv_t (-1 ))
93- return string (str_in);
94-
95- char * inptr = (char *)str_in.data ();
96- size_t inleft = str_in.size ();
97-
98- string out;
99- char outbuf[64 ];
100- char * outptr;
101- size_t outleft;
102-
103- while (inleft > 0 )
104- {
105- outbuf[0 ] = ' \0 ' ;
106- outptr = (char *)outbuf;
107- outleft = sizeof (outbuf) - sizeof (outbuf[0 ]);
108-
109- size_t result = iconv (ic_descr, &inptr, &inleft, &outptr, &outleft);
110- if (result == size_t (-1 ))
111- {
112- switch (errno)
113- {
114- case E2BIG : break ;
115- case EILSEQ :
116- case EINVAL :
117- default :
118- iconv_close (ic_descr);
119- return string (str_in);
120- break ;
121- }
122- }
123- *outptr = ' \0 ' ;
124- out += outbuf;
125- }
126- iconv_close (ic_descr);
127-
128- return out;
129- }
130- #endif
131-
13263stack<string> File::tempFiles;
13364
13465string File::confPath ()
@@ -167,23 +98,15 @@ File::f_string File::encodeName(string_view fileName)
16798{
16899 if (fileName.empty ())
169100 return {};
170- #if defined(_WIN32)
171- int len = MultiByteToWideChar (CP_UTF8 , 0 , fileName.data (), int (fileName.size ()), nullptr , 0 );
172- f_string out (size_t (len), 0 );
173- len = MultiByteToWideChar (CP_UTF8 , 0 , fileName.data (), int (fileName.size ()), out.data (), len);
174- #elif defined(__APPLE__)
175- CFMutableStringRef ref = CFStringCreateMutable (nullptr , 0 );
176- CFStringAppendCString (ref, fileName.data (), kCFStringEncodingUTF8 );
177- CFStringNormalize (ref, kCFStringNormalizationFormD );
178-
101+ #ifdef __APPLE__
102+ CFStringRef ref = CFStringCreateWithBytesNoCopy ({}, (UInt8 *)fileName.data (),
103+ CFIndex (fileName.size ()), kCFStringEncodingUTF8 , FALSE , kCFAllocatorNull );
179104 string out (fileName.size () * 2 , 0 );
180- CFStringGetCString (ref, out.data (), CFIndex (out.size ()), kCFStringEncodingUTF8 );
105+ CFStringGetFileSystemRepresentation (ref, out.data (), CFIndex (out.size ()));
181106 CFRelease (ref);
182107 out.resize (strlen (out.c_str ()));
183- #elif defined(__ANDROID__)
184- f_string out = string (fileName);
185108#else
186- f_string out = convertUTF8 (fileName, false );
109+ f_string out = fs::u8path (fileName);
187110#endif
188111 return out;
189112}
@@ -197,11 +120,7 @@ string File::decodeName(const f_string_view &localFileName)
197120{
198121 if (localFileName.empty ())
199122 return {};
200- #if defined(_WIN32)
201- int len = WideCharToMultiByte (CP_UTF8 , 0 , localFileName.data (), int (localFileName.size ()), nullptr , 0 , nullptr , nullptr );
202- string out (size_t (len), 0 );
203- WideCharToMultiByte (CP_UTF8 , 0 , localFileName.data (), int (localFileName.size ()), out.data (), len, nullptr , nullptr );
204- #elif defined(__APPLE__)
123+ #ifdef __APPLE__
205124 CFMutableStringRef ref = CFStringCreateMutable (nullptr , 0 );
206125 CFStringAppendCString (ref, localFileName.data (), kCFStringEncodingUTF8 );
207126 CFStringNormalize (ref, kCFStringNormalizationFormC );
@@ -210,10 +129,8 @@ string File::decodeName(const f_string_view &localFileName)
210129 CFStringGetCString (ref, out.data (), CFIndex (out.size ()), kCFStringEncodingUTF8 );
211130 CFRelease (ref);
212131 out.resize (strlen (out.c_str ()));
213- #elif defined(__ANDROID__)
214- string out = string (localFileName);
215132#else
216- string out = convertUTF8 (localFileName, true );
133+ string out = fs::path (localFileName). u8string ( );
217134#endif
218135 return out;
219136}
0 commit comments