annotate liboctave/system/lo-sysdep.cc @ 26531:481ac37b0e0e stable

lo-sysdep.cc: Fix static analyzer detected issue (bug #55464). * lo-sysdep.cc (putenv_wrapper): Fix use after free.
author Markus Mützel <markus.muetzel@gmx.de>
date Sun, 13 Jan 2019 14:31:16 +0100
parents 9cf999911dfe
children 84ff9953faa1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
1 /*
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
2
26376
00f796120a6d maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents: 26163
diff changeset
3 Copyright (C) 1996-2019 John W. Eaton
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
4
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
5 This file is part of Octave.
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
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
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
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
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
16
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6321
diff changeset
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
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
20
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
21 */
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
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
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
25 #endif
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
26
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
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
44f24cf66b95 [project @ 2006-06-30 18:19:20 by jwe]
jwe
parents: 5307
diff changeset
30 #include "file-ops.h"
3069
db6d57d718f7 [project @ 1997-07-10 23:34:03 by jwe]
jwe
parents: 2926
diff changeset
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
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
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"
26531
481ac37b0e0e lo-sysdep.cc: Fix static analyzer detected issue (bug #55464).
Markus Mützel <markus.muetzel@gmx.de>
parents: 26506
diff changeset
43 # include "unwind-prot.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
44 #endif
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
45
21738
a99c2407f930 use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
46 namespace octave
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
47 {
21738
a99c2407f930 use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
48 namespace sys
a99c2407f930 use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
49 {
a99c2407f930 use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
50 std::string
a99c2407f930 use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
51 getcwd (void)
a99c2407f930 use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
52 {
a99c2407f930 use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
53 std::string retval;
3069
db6d57d718f7 [project @ 1997-07-10 23:34:03 by jwe]
jwe
parents: 2926
diff changeset
54
21910
4d723ba06b4a provide wrappers for some unistd.h functions
John W. Eaton <jwe@octave.org>
parents: 21828
diff changeset
55 // 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
56 // 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
57 // both 0.
3069
db6d57d718f7 [project @ 1997-07-10 23:34:03 by jwe]
jwe
parents: 2926
diff changeset
58
23795
980f39c3ab90 Use C++11 nullptr rather than 0 in code (bug #51565).
Rik <rik@octave.org>
parents: 23696
diff changeset
59 char *tmp = octave_getcwd_wrapper (nullptr, 0);
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
60
21738
a99c2407f930 use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
61 if (! tmp)
a99c2407f930 use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
62 (*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
63
10250
2d47356a7a1a use gnulib getcwd module
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
64 retval = tmp;
2d47356a7a1a use gnulib getcwd module
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
65 free (tmp);
21136
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 19864
diff changeset
66
21738
a99c2407f930 use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
67 return retval;
10250
2d47356a7a1a use gnulib getcwd module
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
68 }
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
69
21738
a99c2407f930 use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
70 int
a99c2407f930 use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
71 chdir (const std::string& path_arg)
a99c2407f930 use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
72 {
23696
08036a7f3660 remove octave:: namespace tag from symbols used inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
73 std::string path = sys::file_ops::tilde_expand (path_arg);
5872
44f24cf66b95 [project @ 2006-06-30 18:19:20 by jwe]
jwe
parents: 5307
diff changeset
74
21979
d04da18a407a use OCTAVE_USE_WINDOWS_API more consistently
John W. Eaton <jwe@octave.org>
parents: 21949
diff changeset
75 #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
76 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
77 path += '\\';
6244
8cab253171b6 [project @ 2007-01-17 21:55:49 by jwe]
jwe
parents: 6208
diff changeset
78 #endif
8cab253171b6 [project @ 2007-01-17 21:55:49 by jwe]
jwe
parents: 6208
diff changeset
79
21910
4d723ba06b4a provide wrappers for some unistd.h functions
John W. Eaton <jwe@octave.org>
parents: 21828
diff changeset
80 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
81 }
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
82
25516
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
83 bool
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
84 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
85 {
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
86 dirlist = "";
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
87 msg = "";
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
88 #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
89 _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
90
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
91 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
92 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
93 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
94
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
95 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
96 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
97 else
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
98 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
99
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
100 // 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
101 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
102 &ffd);
25609
506419b5f817 maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents: 25532
diff changeset
103 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
104 {
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
105 DWORD errCode = GetLastError ();
26163
d478d5d1c8e9 Fix potential segfault on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25916
diff changeset
106 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
107 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
108 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
109 FORMAT_MESSAGE_IGNORE_INSERTS,
26163
d478d5d1c8e9 Fix potential segfault on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25916
diff changeset
110 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
111 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
26163
d478d5d1c8e9 Fix potential segfault on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25916
diff changeset
112 reinterpret_cast <char *> (&errorText), 0, nullptr);
d478d5d1c8e9 Fix potential segfault on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25916
diff changeset
113 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
114 {
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
115 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
116 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
117 }
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
118 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
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
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
121 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
122 do
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
123 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
124 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
125
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
126 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
127
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
128 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
129
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
130 #else
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
131
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
132 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
133
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
134 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
135 {
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
136 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
137 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
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
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
140 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
141
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
142 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
143 #endif
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
144
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
145 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
146 }
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
147
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
148 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
149 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
150 {
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 #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
152 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
153 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
154 #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
155 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
156 #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
157 }
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
158
25916
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
159 void
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
160 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
161 {
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
162 // 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
163 // library.
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
164 // 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
165
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
166 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
167
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
168 // 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
169 // 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
170 // 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
171
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
172 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
173
26506
9cf999911dfe lo-sysdep.cc: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents: 26376
diff changeset
174 if (new_item)
9cf999911dfe lo-sysdep.cc: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents: 26376
diff changeset
175 sprintf (new_item, "%s=%s", name.c_str (), value.c_str ());
25916
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
176
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
177 // 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
178 // 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
179
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
180 #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
181 wchar_t *wnew_item = u8_to_wchar (new_item);
26531
481ac37b0e0e lo-sysdep.cc: Fix static analyzer detected issue (bug #55464).
Markus Mützel <markus.muetzel@gmx.de>
parents: 26506
diff changeset
182 octave::unwind_protect frame;
481ac37b0e0e lo-sysdep.cc: Fix static analyzer detected issue (bug #55464).
Markus Mützel <markus.muetzel@gmx.de>
parents: 26506
diff changeset
183 frame.add_fcn (std::free, static_cast<void *> (new_item));
25916
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 (_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
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 #else
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
187 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
188 (*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
189 #endif
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
190 }
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 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
193 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
194 {
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
195 #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
196 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
197 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
198 #else
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
199 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
200 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
201 #endif
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
202 }
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 int
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
205 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
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 #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
208 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
209
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
210 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
211 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
212 #else
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
213 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
214 #endif
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
215 }
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
216
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
217 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
218 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
219 {
25531
c2dc2fb6359b Fix C++ conversion functions between UTF-8 and wchar_t.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25517
diff changeset
220 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
221 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
222 (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
223
25531
c2dc2fb6359b Fix C++ conversion functions between UTF-8 and wchar_t.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25517
diff changeset
224 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
225 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
226 (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
227 &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
228
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
229 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
230 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
231 {
25531
c2dc2fb6359b Fix C++ conversion functions between UTF-8 and wchar_t.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25517
diff changeset
232 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
233 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
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 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
237 }
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
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
239 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
240 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
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 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
243 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
244
25531
c2dc2fb6359b Fix C++ conversion functions between UTF-8 and wchar_t.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25517
diff changeset
245 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
246 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
247 (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
248 &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
249
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
250 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
251 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
252 {
25531
c2dc2fb6359b Fix C++ conversion functions between UTF-8 and wchar_t.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25517
diff changeset
253 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
254 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
255 }
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
256
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
257 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
258 }
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
259
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
260 // 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
261 // 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
262
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
263 // 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
264 // 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
265 // 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
266
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
267 // 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
268 // 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
269 // 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
270 // 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
271
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
272 // 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
273 // 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
274 // 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
275
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
276 // 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
277 // 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
278 // 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
279 // 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
280 // 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
281
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
282 // 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
283 // 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
284 // 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
285 // 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
286 // 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
287 // 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
288
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
289 // 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
290 // 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
291
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
292 // 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
293 // 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
294 // 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
295 // 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
296 // is valid.
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
297
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
298 // 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
299
25532
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
300 std::string
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
301 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
302 {
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
303 #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
304
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
305 // 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
306 // 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
307 // 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
308 // 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
309 // 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
310
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
311 // 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
312
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
313 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
314 = 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
315 [](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
316
25532
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
317 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
318 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
319
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
320 // 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
321 // ASCII-only).
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
322
25660
4de7535ece11 Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25615
diff changeset
323 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
324 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
325
4de7535ece11 Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25615
diff changeset
326 // 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
327 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
328 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
329 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
330
4de7535ece11 Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25615
diff changeset
331 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
332
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
333 // 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
334
25660
4de7535ece11 Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25615
diff changeset
335 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
336
25660
4de7535ece11 Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25615
diff changeset
337 if (length > 0)
4de7535ece11 Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25615
diff changeset
338 {
4de7535ece11 Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25615
diff changeset
339 // 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
340 // 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
341
25660
4de7535ece11 Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25615
diff changeset
342 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
343 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
344
25660
4de7535ece11 Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25615
diff changeset
345 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
346 = 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
347 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
348
25660
4de7535ece11 Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25615
diff changeset
349 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
350 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
351 }
25532
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
352
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
353 // 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
354 // 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
355
25660
4de7535ece11 Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25615
diff changeset
356 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
357 = 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
358 [](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
359 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
360 = 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
361
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
362 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
363 = 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
364
25660
4de7535ece11 Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25615
diff changeset
365 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
366 = 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
367
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
368 // 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
369 // 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
370
25532
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
371 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
372 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
373
25532
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
374 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
375 {
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
376 std::string msg;
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
377 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
378
25532
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
379 if (status < 0)
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
380 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
381
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
382 // 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
383 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
384 }
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
385
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
386 // 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
387 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
388 = (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
389 + 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
390
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
391 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
392
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
393 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
394 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
395
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
396 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
397
25532
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
398 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
399 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
400
25660
4de7535ece11 Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25615
diff changeset
401 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
402 return filename_hash;
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
403
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
404 #endif
25532
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
405
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
406 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
407 }
21738
a99c2407f930 use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
408 }
a99c2407f930 use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
409 }