Mercurial > octave
annotate liboctave/system/lo-sysdep.cc @ 26163:d478d5d1c8e9
Fix potential segfault on Windows.
* lo-sysdep.cc (get_dirlist): Fix potential segfault with FormatMessageA. Use
C++ syntax.
author | Markus Mützel <markus.muetzel@gmx.de> |
---|---|
date | Mon, 03 Dec 2018 19:32:31 +0100 |
parents | 3f2282954600 |
children | 00f796120a6d |
rev | line source |
---|---|
2926 | 1 /* |
2 | |
25054
6652d3823428
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
3 Copyright (C) 1996-2018 John W. Eaton |
2926 | 4 |
5 This file is part of Octave. | |
6 | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23804
diff
changeset
|
7 Octave is free software: you can redistribute it and/or modify it |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
8 under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23804
diff
changeset
|
9 the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
10 (at your option) any later version. |
2926 | 11 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
12 Octave is distributed in the hope that it will be useful, but |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
13 WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
15 GNU General Public License for more details. |
2926 | 16 |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23804
diff
changeset
|
19 <https://www.gnu.org/licenses/>. |
2926 | 20 |
21 */ | |
22 | |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21642
diff
changeset
|
23 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21213
diff
changeset
|
24 # include "config.h" |
2926 | 25 #endif |
26 | |
27 #include <string> | |
7695
eacf87a24f55
lo-sysdep.cc: include windows.h if windows and not cygwin
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
28 |
25516
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
29 #include "dir-ops.h" |
5872 | 30 #include "file-ops.h" |
3069 | 31 #include "lo-error.h" |
21910
4d723ba06b4a
provide wrappers for some unistd.h functions
John W. Eaton <jwe@octave.org>
parents:
21828
diff
changeset
|
32 #include "lo-sysdep.h" |
25916
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
33 #include "putenv-wrapper.h" |
25514
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
34 #include "uniconv-wrappers.h" |
21910
4d723ba06b4a
provide wrappers for some unistd.h functions
John W. Eaton <jwe@octave.org>
parents:
21828
diff
changeset
|
35 #include "unistd-wrappers.h" |
25916
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
36 #include "unsetenv-wrapper.h" |
2926 | 37 |
25516
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
38 #if defined (OCTAVE_USE_WINDOWS_API) |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
39 # include <windows.h> |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
40 # include <wchar.h> |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
41 |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
42 # include "lo-hash.h" |
25516
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
43 #endif |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
44 |
21738
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
45 namespace octave |
2926 | 46 { |
21738
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
47 namespace sys |
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
48 { |
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
49 std::string |
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
50 getcwd (void) |
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
51 { |
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
52 std::string retval; |
3069 | 53 |
21910
4d723ba06b4a
provide wrappers for some unistd.h functions
John W. Eaton <jwe@octave.org>
parents:
21828
diff
changeset
|
54 // Using octave_getcwd_wrapper ensures that we have a getcwd that |
21738
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
55 // will allocate a buffer as large as necessary if buf and size are |
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
56 // both 0. |
3069 | 57 |
23795
980f39c3ab90
Use C++11 nullptr rather than 0 in code (bug #51565).
Rik <rik@octave.org>
parents:
23696
diff
changeset
|
58 char *tmp = octave_getcwd_wrapper (nullptr, 0); |
2926 | 59 |
21738
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
60 if (! tmp) |
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
61 (*current_liboctave_error_handler) ("unable to find current directory"); |
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
62 |
10250 | 63 retval = tmp; |
64 free (tmp); | |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
19864
diff
changeset
|
65 |
21738
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
66 return retval; |
10250 | 67 } |
2926 | 68 |
21738
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
69 int |
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
70 chdir (const std::string& path_arg) |
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
71 { |
23696
08036a7f3660
remove octave:: namespace tag from symbols used inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
72 std::string path = sys::file_ops::tilde_expand (path_arg); |
5872 | 73 |
21979
d04da18a407a
use OCTAVE_USE_WINDOWS_API more consistently
John W. Eaton <jwe@octave.org>
parents:
21949
diff
changeset
|
74 #if defined (OCTAVE_USE_WINDOWS_API) |
21738
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
75 if (path.length () == 2 && path[1] == ':') |
23804
8057d3f0673d
Use C++11 raw string literals to avoid escaping backslashes in strings.
Rik <rik@octave.org>
parents:
23795
diff
changeset
|
76 path += '\\'; |
6244 | 77 #endif |
78 | |
21910
4d723ba06b4a
provide wrappers for some unistd.h functions
John W. Eaton <jwe@octave.org>
parents:
21828
diff
changeset
|
79 return octave_chdir_wrapper (path.c_str ()); |
21738
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
80 } |
25514
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
81 |
25516
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
82 bool |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
83 get_dirlist (const std::string& dirname, string_vector& dirlist, std::string& msg) |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
84 { |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
85 dirlist = ""; |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
86 msg = ""; |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
87 #if defined (OCTAVE_USE_WINDOWS_API) |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
88 _WIN32_FIND_DATAW ffd; |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
89 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
90 std::string path_name (dirname); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
91 if (path_name.empty ()) |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
92 return true; |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
93 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
94 if (path_name.back () == '\\' || path_name.back () == '/') |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
95 path_name.push_back ('*'); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
96 else |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
97 path_name.append (R"(\*)"); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
98 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
99 // Find first file in directory. |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
100 HANDLE hFind = FindFirstFileW (u8_to_wstring (path_name).c_str (), |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
101 &ffd); |
25609
506419b5f817
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
25532
diff
changeset
|
102 if (INVALID_HANDLE_VALUE == hFind) |
25516
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
103 { |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
104 DWORD errCode = GetLastError (); |
26163
d478d5d1c8e9
Fix potential segfault on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25916
diff
changeset
|
105 char *errorText = nullptr; |
25516
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
106 FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM | |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
107 FORMAT_MESSAGE_ALLOCATE_BUFFER | |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
108 FORMAT_MESSAGE_IGNORE_INSERTS, |
26163
d478d5d1c8e9
Fix potential segfault on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25916
diff
changeset
|
109 nullptr, errCode, |
25516
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
110 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), |
26163
d478d5d1c8e9
Fix potential segfault on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25916
diff
changeset
|
111 reinterpret_cast <char *> (&errorText), 0, nullptr); |
d478d5d1c8e9
Fix potential segfault on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25916
diff
changeset
|
112 if (errorText != nullptr) |
25516
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
113 { |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
114 msg = std::string (errorText); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
115 LocalFree (errorText); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
116 } |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
117 return false; |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
118 } |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
119 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
120 std::list<std::string> dirlist_str; |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
121 do |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
122 dirlist_str.push_back (u8_from_wstring (ffd.cFileName)); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
123 while (FindNextFileW (hFind, &ffd) != 0); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
124 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
125 FindClose(hFind); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
126 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
127 dirlist = string_vector (dirlist_str); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
128 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
129 #else |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
130 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
131 dir_entry dir (dirname); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
132 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
133 if (! dir) |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
134 { |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
135 msg = dir.error (); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
136 return false; |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
137 } |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
138 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
139 dirlist = dir.read (); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
140 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
141 dir.close (); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
142 #endif |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
143 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
144 return true; |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
145 } |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
146 |
25517
7fbc39a46be8
Add wrapper to fopen for files with non-ASCII chars in path on Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25516
diff
changeset
|
147 std::FILE * |
7fbc39a46be8
Add wrapper to fopen for files with non-ASCII chars in path on Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25516
diff
changeset
|
148 fopen (const std::string& filename, const std::string& mode) |
7fbc39a46be8
Add wrapper to fopen for files with non-ASCII chars in path on Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25516
diff
changeset
|
149 { |
7fbc39a46be8
Add wrapper to fopen for files with non-ASCII chars in path on Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25516
diff
changeset
|
150 #if defined (OCTAVE_USE_WINDOWS_API) |
7fbc39a46be8
Add wrapper to fopen for files with non-ASCII chars in path on Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25516
diff
changeset
|
151 return _wfopen (u8_to_wstring (filename).c_str (), |
7fbc39a46be8
Add wrapper to fopen for files with non-ASCII chars in path on Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25516
diff
changeset
|
152 u8_to_wstring (mode).c_str ()); |
7fbc39a46be8
Add wrapper to fopen for files with non-ASCII chars in path on Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25516
diff
changeset
|
153 #else |
7fbc39a46be8
Add wrapper to fopen for files with non-ASCII chars in path on Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25516
diff
changeset
|
154 return std::fopen (filename.c_str (), mode.c_str ()); |
7fbc39a46be8
Add wrapper to fopen for files with non-ASCII chars in path on Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25516
diff
changeset
|
155 #endif |
7fbc39a46be8
Add wrapper to fopen for files with non-ASCII chars in path on Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25516
diff
changeset
|
156 } |
7fbc39a46be8
Add wrapper to fopen for files with non-ASCII chars in path on Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25516
diff
changeset
|
157 |
25916
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
158 void |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
159 putenv_wrapper (const std::string& name, const std::string& value) |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
160 { |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
161 // This function was adapted from xputenv from Karl Berry's kpathsearch |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
162 // library. |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
163 // FIXME: make this do the right thing if we don't have a SMART_PUTENV. |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
164 |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
165 int new_len = name.length () + value.length () + 2; |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
166 |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
167 // FIXME: This leaks memory, but so would a call to setenv. |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
168 // Short of extreme measures to track memory, altering the environment |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
169 // always leaks memory, but the saving grace is that the leaks are small. |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
170 |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
171 char *new_item = static_cast<char *> (std::malloc (new_len)); |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
172 |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
173 sprintf (new_item, "%s=%s", name.c_str (), value.c_str ()); |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
174 |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
175 // As far as I can see there's no way to distinguish between the |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
176 // various errors; putenv doesn't have errno values. |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
177 |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
178 #if defined (OCTAVE_USE_WINDOWS_API) |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
179 wchar_t *wnew_item = u8_to_wchar (new_item); |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
180 std::free (static_cast<void *> (new_item)); |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
181 if (_wputenv (wnew_item) < 0) |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
182 (*current_liboctave_error_handler) ("putenv (%s) failed", new_item); |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
183 #else |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
184 if (octave_putenv_wrapper (new_item) < 0) |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
185 (*current_liboctave_error_handler) ("putenv (%s) failed", new_item); |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
186 #endif |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
187 } |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
188 |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
189 std::string |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
190 getenv_wrapper (const std::string& name) |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
191 { |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
192 #if defined (OCTAVE_USE_WINDOWS_API) |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
193 wchar_t *env = _wgetenv (u8_to_wstring (name).c_str ()); |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
194 return env ? u8_from_wstring (env) : ""; |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
195 #else |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
196 char *env = ::getenv (name.c_str ()); |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
197 return env ? env : ""; |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
198 #endif |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
199 } |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
200 |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
201 int |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
202 unsetenv_wrapper (const std::string& name) |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
203 { |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
204 #if defined (OCTAVE_USE_WINDOWS_API) |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
205 putenv_wrapper (name, ""); |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
206 |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
207 std::wstring wname = u8_to_wstring (name); |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
208 return (SetEnvironmentVariableW (wname.c_str (), nullptr) ? 0 : -1); |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
209 #else |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
210 return octave_unsetenv_wrapper (name.c_str ()); |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
211 #endif |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
212 } |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
213 |
25514
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
214 std::wstring |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
215 u8_to_wstring (const std::string& utf8_string) |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
216 { |
25531
c2dc2fb6359b
Fix C++ conversion functions between UTF-8 and wchar_t.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25517
diff
changeset
|
217 size_t srclen = utf8_string.length (); |
c2dc2fb6359b
Fix C++ conversion functions between UTF-8 and wchar_t.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25517
diff
changeset
|
218 const uint8_t *src = reinterpret_cast<const uint8_t *> |
c2dc2fb6359b
Fix C++ conversion functions between UTF-8 and wchar_t.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25517
diff
changeset
|
219 (utf8_string.c_str ()); |
25514
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
220 |
25531
c2dc2fb6359b
Fix C++ conversion functions between UTF-8 and wchar_t.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25517
diff
changeset
|
221 size_t length = 0; |
c2dc2fb6359b
Fix C++ conversion functions between UTF-8 and wchar_t.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25517
diff
changeset
|
222 wchar_t *wchar = reinterpret_cast<wchar_t *> |
c2dc2fb6359b
Fix C++ conversion functions between UTF-8 and wchar_t.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25517
diff
changeset
|
223 (octave_u8_conv_to_encoding ("wchar_t", src, srclen, |
c2dc2fb6359b
Fix C++ conversion functions between UTF-8 and wchar_t.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25517
diff
changeset
|
224 &length)); |
25514
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
225 |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
226 std::wstring retval = L""; |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
227 if (wchar != nullptr) |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
228 { |
25531
c2dc2fb6359b
Fix C++ conversion functions between UTF-8 and wchar_t.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25517
diff
changeset
|
229 retval = std::wstring (wchar, length / sizeof (wchar_t)); |
25514
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
230 free (static_cast<void *> (wchar)); |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
231 } |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
232 |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
233 return retval; |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
234 } |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
235 |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
236 std::string |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
237 u8_from_wstring (const std::wstring& wchar_string) |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
238 { |
25531
c2dc2fb6359b
Fix C++ conversion functions between UTF-8 and wchar_t.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25517
diff
changeset
|
239 size_t srclen = wchar_string.length () * sizeof (wchar_t); |
c2dc2fb6359b
Fix C++ conversion functions between UTF-8 and wchar_t.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25517
diff
changeset
|
240 const char *src = reinterpret_cast<const char *> (wchar_string.c_str ()); |
25514
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
241 |
25531
c2dc2fb6359b
Fix C++ conversion functions between UTF-8 and wchar_t.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25517
diff
changeset
|
242 size_t length = 0; |
c2dc2fb6359b
Fix C++ conversion functions between UTF-8 and wchar_t.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25517
diff
changeset
|
243 char *mbchar = reinterpret_cast<char *> |
c2dc2fb6359b
Fix C++ conversion functions between UTF-8 and wchar_t.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25517
diff
changeset
|
244 (octave_u8_conv_from_encoding ("wchar_t", src, srclen, |
c2dc2fb6359b
Fix C++ conversion functions between UTF-8 and wchar_t.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25517
diff
changeset
|
245 &length)); |
25514
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
246 |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
247 std::string retval = ""; |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
248 if (mbchar != nullptr) |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
249 { |
25531
c2dc2fb6359b
Fix C++ conversion functions between UTF-8 and wchar_t.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25517
diff
changeset
|
250 retval = std::string (mbchar, length); |
25514
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
251 free (static_cast<void *> (mbchar)); |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
252 } |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
253 |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
254 return retval; |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
255 } |
25517
7fbc39a46be8
Add wrapper to fopen for files with non-ASCII chars in path on Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25516
diff
changeset
|
256 |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
257 // At quite a few places in the code we are passing file names as |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
258 // char arrays to external library functions. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
259 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
260 // When these functions try to locate the corresponding file on the |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
261 // disc, they need to use the wide character API on Windows to |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
262 // correctly open files with non-ASCII characters. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
263 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
264 // But they have no way of knowing which encoding we are using for |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
265 // the passed string. So they have no way of reliably converting to |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
266 // a wchar_t array. (I.e. there is no possible fix for these |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
267 // functions with current C or C++.) |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
268 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
269 // To solve the dilemma, the function "get_ASCII_filename" first |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
270 // checks whether there are any non-ASCII characters in the passed |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
271 // file name. If there are not, it returns the original name. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
272 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
273 // Otherwise, it tries to obtain the short file name (8.3 naming |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
274 // scheme) which only consists of ASCII characters and are safe to |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
275 // pass. However, short file names can be disabled for performance |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
276 // reasons on the file system level with NTFS. So there is no |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
277 // guarantee that these exist. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
278 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
279 // If short file names are not stored, a hard link to the file is |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
280 // created. For this the path to the file is split at the deepest |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
281 // possible level that doesn't contain non-ASCII characters. At |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
282 // that level a hidden folder is created that holds the hard links. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
283 // That means we need to have write access on that location. A path |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
284 // to that hard link is returned. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
285 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
286 // If the file system is FAT32, there are no hard links. But FAT32 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
287 // always stores short file names. So we are safe. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
288 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
289 // ExFAT that is occasionally used on USB sticks and SD cards stores |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
290 // neither short file names nor does it support hard links. So for |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
291 // exFAT with this function, there is (currently) no way to generate |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
292 // a file name that is stripped from non-ASCII characters but still |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
293 // is valid. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
294 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
295 // For Unixy systems, this function does nothing. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
296 |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
297 std::string |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
298 get_ASCII_filename (const std::string& orig_file_name) |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
299 { |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
300 #if defined (OCTAVE_USE_WINDOWS_API) |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
301 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
302 // Return file name that only contains ASCII characters that can |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
303 // be used to access the file orig_file_name. The original file |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
304 // must exist in the file system before calling this function. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
305 // This is useful for passing file names to functions that are not |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
306 // aware of the character encoding we are using. |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
307 |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
308 // 1. Check whether filename contains non-ASCII (UTF-8) characters. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
309 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
310 std::string::const_iterator first_non_ASCII |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
311 = std::find_if (orig_file_name.begin (), orig_file_name.end (), |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
312 [](char c) { return (c < 0 || c >= 128); }); |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
313 |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
314 if (first_non_ASCII == orig_file_name.end ()) |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
315 return orig_file_name; |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
316 |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
317 // 2. Check if file system stores short filenames (always |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
318 // ASCII-only). |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
319 |
25660
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
320 std::wstring w_orig_file_name_str = u8_to_wstring (orig_file_name); |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
321 const wchar_t *w_orig_file_name = w_orig_file_name_str.c_str (); |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
322 |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
323 // Get full path to file |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
324 wchar_t w_full_file_name[_MAX_PATH]; |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
325 if (_wfullpath (w_full_file_name, w_orig_file_name, _MAX_PATH) == nullptr) |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
326 return orig_file_name; |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
327 |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
328 std::wstring w_full_file_name_str = w_full_file_name; |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
329 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
330 // Get short filename (8.3) from UTF-16 filename. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
331 |
25660
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
332 long length = GetShortPathNameW (w_full_file_name, nullptr, 0); |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
333 |
25660
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
334 if (length > 0) |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
335 { |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
336 // Dynamically allocate the correct size (terminating null char |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
337 // was included in length). |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
338 |
25660
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
339 wchar_t *w_short_file_name = new wchar_t[length]; |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
340 GetShortPathNameW (w_full_file_name, w_short_file_name, length); |
25609
506419b5f817
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
25532
diff
changeset
|
341 |
25660
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
342 std::wstring w_short_file_name_str |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
343 = std::wstring (w_short_file_name, length); |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
344 std::string short_file_name = u8_from_wstring (w_short_file_name_str); |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
345 |
25660
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
346 if (w_short_file_name_str.compare (0, length-1, w_full_file_name_str) != 0) |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
347 return short_file_name; |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
348 } |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
349 |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
350 // 3. Create hard link with only-ASCII characters. |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
351 // Get longest possible part of path that only contains ASCII chars. |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
352 |
25660
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
353 std::wstring::iterator w_first_non_ASCII |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
354 = std::find_if (w_full_file_name_str.begin (), w_full_file_name_str.end (), |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
355 [](wchar_t c) { return (c < 0 || c >= 128); }); |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
356 std::wstring tmp_substr |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
357 = std::wstring (w_full_file_name_str.begin (), w_first_non_ASCII); |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
358 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
359 size_t pos |
25660
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
360 = tmp_substr.find_last_of (u8_to_wstring (file_ops::dir_sep_chars ())); |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
361 |
25660
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
362 std::string par_dir |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
363 = u8_from_wstring (w_full_file_name_str.substr (0, pos+1)); |
25609
506419b5f817
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
25532
diff
changeset
|
364 |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
365 // Create .oct_ascii directory. |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
366 // FIXME: We need to have write permission in this location. |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
367 |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
368 std::string oct_ascii_dir = par_dir + ".oct_ascii"; |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
369 std::string test_dir = canonicalize_file_name (oct_ascii_dir); |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
370 |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
371 if (test_dir.empty ()) |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
372 { |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
373 std::string msg; |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
374 int status = octave::sys::mkdir (oct_ascii_dir, 0777, msg); |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
375 |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
376 if (status < 0) |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
377 return orig_file_name; |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
378 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
379 // Set hidden property. |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
380 SetFileAttributesA (oct_ascii_dir.c_str (), FILE_ATTRIBUTE_HIDDEN); |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
381 } |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
382 |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
383 // Create file from hash of full filename. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
384 std::string filename_hash |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
385 = (oct_ascii_dir + file_ops::dir_sep_str () |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
386 + octave::crypto::hash ("SHA1", orig_file_name)); |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
387 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
388 std::string abs_filename_hash = canonicalize_file_name (filename_hash); |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
389 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
390 if (! abs_filename_hash.empty ()) |
25660
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
391 octave::sys::unlink (filename_hash); |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
392 |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
393 wchar_t w_filename_hash[filename_hash.length ()+1] = {0}; |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
394 |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
395 for (size_t i=0; i < filename_hash.length (); i++) |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
396 w_filename_hash[i] = filename_hash.at (i); |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
397 |
25660
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
398 if (CreateHardLinkW (w_filename_hash, w_orig_file_name, nullptr)) |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
399 return filename_hash; |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
400 |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
401 #endif |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
402 |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
403 return orig_file_name; |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
404 } |
21738
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
405 } |
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
406 } |