annotate liboctave/system/lo-sysdep.cc @ 32086:3c608abd55f5

Move "same_file" from liboctinterp to liboctave (bug #63803). * libinterp/corefcn/utils.cc, utils.h (same_file): Deprecate function. * libinterp/corefcn/sysdep.cc, sysdep.h (same_file_internal): Remove function. * liboctave/system/lo-sysdep.cc, lo-sysdep.h (same_file): Move function from liboctinterp to liboctave. * libgui/src/m-editor/file-editor.cc (file_editor::find_tab_widget), libinterp/corefcn/fcn-info.cc (file_editor::find_tab_widget), libinterp/corefcn/interpreter.cc (interpreter::execute_startup_files), libinterp/corefcn/load-path.cc (load_path::append): Use function in updated namespace.
author Markus Mützel <markus.muetzel@gmx.de>
date Sat, 06 May 2023 17:48:27 +0200
parents ed3a18fe328a
children 85255746fc21
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ////////////////////////////////////////////////////////////////////////
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 //
31706
597f3ee61a48 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 31607
diff changeset
3 // Copyright (C) 1996-2023 The Octave Project Developers
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
4 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 // See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 // distribution or <https://octave.org/copyright/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
7 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
8 // This file is part of Octave.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
9 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
10 // Octave is free software: you can redistribute it and/or modify it
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
11 // under the terms of the GNU General Public License as published by
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
12 // the Free Software Foundation, either version 3 of the License, or
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
13 // (at your option) any later version.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
14 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
15 // Octave is distributed in the hope that it will be useful, but
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
18 // GNU General Public License for more details.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
19 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
20 // You should have received a copy of the GNU General Public License
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
21 // along with Octave; see the file COPYING. If not, see
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
22 // <https://www.gnu.org/licenses/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ////////////////////////////////////////////////////////////////////////
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
25
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21642
diff changeset
26 #if defined (HAVE_CONFIG_H)
21301
40de9f8f23a6 Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents: 21213
diff changeset
27 # include "config.h"
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
28 #endif
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
29
29141
f873857f5f86 system: Support executing commands with non-ASCII characters on Windows (bug #59572).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29016
diff changeset
30 #include <cstdlib>
29492
be4b78fb4411 Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
31 #include <locale>
be4b78fb4411 Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
32 #include <codecvt>
29141
f873857f5f86 system: Support executing commands with non-ASCII characters on Windows (bug #59572).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29016
diff changeset
33
25516
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
34 #include "dir-ops.h"
5872
44f24cf66b95 [project @ 2006-06-30 18:19:20 by jwe]
jwe
parents: 5307
diff changeset
35 #include "file-ops.h"
32077
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
36 #include "file-stat.h"
3069
db6d57d718f7 [project @ 1997-07-10 23:34:03 by jwe]
jwe
parents: 2926
diff changeset
37 #include "lo-error.h"
21910
4d723ba06b4a provide wrappers for some unistd.h functions
John W. Eaton <jwe@octave.org>
parents: 21828
diff changeset
38 #include "lo-sysdep.h"
28534
548598760b66 get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28472
diff changeset
39 #include "localcharset-wrapper.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
40 #include "putenv-wrapper.h"
21910
4d723ba06b4a provide wrappers for some unistd.h functions
John W. Eaton <jwe@octave.org>
parents: 21828
diff changeset
41 #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
42 #include "unsetenv-wrapper.h"
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
43
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 #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
45 # 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
46 # 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
47
28534
548598760b66 get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28472
diff changeset
48 # include "filepos-wrappers.h"
25532
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
49 # include "lo-hash.h"
28534
548598760b66 get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28472
diff changeset
50 # include "oct-locbuf.h"
29492
be4b78fb4411 Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
51 # include "uniconv-wrappers.h"
26531
481ac37b0e0e lo-sysdep.cc: Fix static analyzer detected issue (bug #55464).
Markus Mützel <markus.muetzel@gmx.de>
parents: 26506
diff changeset
52 # 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
53 #endif
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
54
31605
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30881
diff changeset
55 OCTAVE_BEGIN_NAMESPACE(octave)
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30881
diff changeset
56
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30881
diff changeset
57 OCTAVE_BEGIN_NAMESPACE(sys)
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30881
diff changeset
58
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
59 int
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
60 system (const std::string& cmd_str)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
61 {
29141
f873857f5f86 system: Support executing commands with non-ASCII characters on Windows (bug #59572).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29016
diff changeset
62 #if defined (OCTAVE_USE_WINDOWS_API)
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
63 const std::wstring wcmd_str = u8_to_wstring (cmd_str);
29141
f873857f5f86 system: Support executing commands with non-ASCII characters on Windows (bug #59572).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29016
diff changeset
64
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
65 return _wsystem (wcmd_str.c_str ());
29141
f873857f5f86 system: Support executing commands with non-ASCII characters on Windows (bug #59572).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29016
diff changeset
66 #else
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
67 return ::system (cmd_str.c_str ());
29141
f873857f5f86 system: Support executing commands with non-ASCII characters on Windows (bug #59572).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29016
diff changeset
68 #endif
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
69 }
29141
f873857f5f86 system: Support executing commands with non-ASCII characters on Windows (bug #59572).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29016
diff changeset
70
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
71 std::string
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31706
diff changeset
72 getcwd ()
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
73 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
74 std::string retval;
3069
db6d57d718f7 [project @ 1997-07-10 23:34:03 by jwe]
jwe
parents: 2926
diff changeset
75
28470
39c078e14824 Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28223
diff changeset
76 #if defined (OCTAVE_USE_WINDOWS_API)
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
77 wchar_t *tmp = _wgetcwd (nullptr, 0);
28470
39c078e14824 Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28223
diff changeset
78
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
79 if (! tmp)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
80 (*current_liboctave_error_handler) ("unable to find current directory");
28470
39c078e14824 Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28223
diff changeset
81
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
82 std::wstring tmp_wstr (tmp);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
83 free (tmp);
28470
39c078e14824 Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28223
diff changeset
84
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
85 std::string tmp_str = u8_from_wstring (tmp_wstr);
28470
39c078e14824 Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28223
diff changeset
86
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
87 retval = tmp_str;
28470
39c078e14824 Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28223
diff changeset
88
39c078e14824 Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28223
diff changeset
89 #else
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
90 // Using octave_getcwd_wrapper ensures that we have a getcwd that
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
91 // will allocate a buffer as large as necessary if buf and size are
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
92 // both 0.
3069
db6d57d718f7 [project @ 1997-07-10 23:34:03 by jwe]
jwe
parents: 2926
diff changeset
93
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
94 char *tmp = octave_getcwd_wrapper (nullptr, 0);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
95
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
96 if (! tmp)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
97 (*current_liboctave_error_handler) ("unable to find current directory");
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
98
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
99 retval = tmp;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
100 free (tmp);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
101 #endif
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
102
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
103 return retval;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
104 }
21738
a99c2407f930 use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
105
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
106 int
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
107 chdir (const std::string& path_arg)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
108 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
109 std::string path = sys::file_ops::tilde_expand (path_arg);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
110
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
111 #if defined (OCTAVE_USE_WINDOWS_API)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
112 if (path.length () == 2 && path[1] == ':')
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
113 path += '\\';
28470
39c078e14824 Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28223
diff changeset
114 #endif
21136
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 19864
diff changeset
115
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
116 return octave_chdir_wrapper (path.c_str ());
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
117 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
118
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
119 bool
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
120 get_dirlist (const std::string& dirname, string_vector& dirlist,
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
121 std::string& msg)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
122 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
123 dirlist = "";
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
124 msg = "";
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
125 #if defined (OCTAVE_USE_WINDOWS_API)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
126 _WIN32_FIND_DATAW ffd;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
127
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
128 std::string path_name (dirname);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
129 if (path_name.empty ())
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
130 return true;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
131
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
132 if (path_name.back () == '\\' || path_name.back () == '/')
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
133 path_name.push_back ('*');
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
134 else
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
135 path_name.append (R"(\*)");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
136
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
137 // Find first file in directory.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
138 std::wstring wpath_name = u8_to_wstring (path_name);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
139 HANDLE hFind = FindFirstFileW (wpath_name.c_str (), &ffd);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
140 if (INVALID_HANDLE_VALUE == hFind)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
141 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
142 DWORD errCode = GetLastError ();
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
143 char *errorText = nullptr;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
144 FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM |
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
145 FORMAT_MESSAGE_ALLOCATE_BUFFER |
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
146 FORMAT_MESSAGE_IGNORE_INSERTS,
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
147 nullptr, errCode,
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
148 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
149 reinterpret_cast <char *> (&errorText), 0, nullptr);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
150 if (errorText != nullptr)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
151 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
152 msg = std::string (errorText);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
153 LocalFree (errorText);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
154 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
155 return false;
10250
2d47356a7a1a use gnulib getcwd module
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
156 }
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents:
diff changeset
157
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
158 std::list<std::string> dirlist_str;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
159 do
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
160 dirlist_str.push_back (u8_from_wstring (ffd.cFileName));
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
161 while (FindNextFileW (hFind, &ffd) != 0);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
162
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
163 FindClose(hFind);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
164
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
165 dirlist = string_vector (dirlist_str);
5872
44f24cf66b95 [project @ 2006-06-30 18:19:20 by jwe]
jwe
parents: 5307
diff changeset
166
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
167 #else
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
168
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
169 dir_entry dir (dirname);
6244
8cab253171b6 [project @ 2007-01-17 21:55:49 by jwe]
jwe
parents: 6208
diff changeset
170
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
171 if (! dir)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
172 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
173 msg = dir.error ();
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
174 return false;
21738
a99c2407f930 use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
175 }
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
176
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
177 dirlist = dir.read ();
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
178
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
179 dir.close ();
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
180 #endif
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
181
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
182 return true;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
183 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
184
25516
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
185 #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
186
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
187 static bool check_fseek_ftell_workaround_needed (bool set_nonbuffered_mode)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
188 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
189 // To check whether the workaround is needed:
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
190 //
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
191 // * Create a tmp file with LF line endings only.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
192 //
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
193 // * Open that file for reading in text mode.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
194 //
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
195 // * Read a line.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
196 //
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
197 // * Use ftello to record the position of the beginning of the
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
198 // second line.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
199 //
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
200 // * Read and save the contents of the second line.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
201 //
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
202 // * Use fseeko to return to the saved position.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
203 //
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
204 // * Read the second line again and compare to the previously
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
205 // saved text.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
206 //
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
207 // * If the lines are different, we need to set non-buffered
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
208 // input mode for files opened in text mode.
25516
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
209
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
210 std::string tmpname = sys::tempnam ("", "oct-");
25516
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
211
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
212 if (tmpname.empty ())
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
213 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
214 (*current_liboctave_warning_handler)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
215 ("fseek/ftell bug check failed (tmp name creation)!");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
216 return false;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
217 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
218
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
219 std::FILE *fptr = std::fopen (tmpname.c_str (), "wb");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
220
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
221 if (! fptr)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
222 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
223 (*current_liboctave_warning_handler)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
224 ("fseek/ftell bug check failed (opening tmp file for writing)!");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
225 return false;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
226 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
227
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
228 fprintf (fptr, "%s", "foo\nbar\nbaz\n");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
229
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
230 std::fclose (fptr);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
231
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
232 fptr = std::fopen (tmpname.c_str (), "rt");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
233
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
234 if (! fptr)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
235 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
236 (*current_liboctave_warning_handler)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
237 ("fseek/ftell bug check failed (opening tmp file for reading)!");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
238 return false;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
239 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
240
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
241 unwind_action act ([=] ()
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
242 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
243 std::fclose (fptr);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
244 sys::unlink (tmpname);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
245 });
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
246
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
247 if (set_nonbuffered_mode)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
248 ::setvbuf (fptr, nullptr, _IONBF, 0);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
249
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
250 while (true)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
251 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
252 int c = fgetc (fptr);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
253
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
254 if (c == EOF)
25516
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
255 {
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
256 (*current_liboctave_warning_handler)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
257 ("fseek/ftell bug check failed (skipping first line)!");
25516
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
258 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
259 }
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
260
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
261 if (c == '\n')
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
262 break;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
263 }
25516
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
264
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
265 off_t pos = octave_ftello_wrapper (fptr);
25516
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
266
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
267 char buf1[8];
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
268 int i = 0;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
269 while (true)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
270 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
271 int c = fgetc (fptr);
25516
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
272
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
273 if (c == EOF)
25516
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
274 {
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
275 (*current_liboctave_warning_handler)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
276 ("fseek/ftell bug check failed (reading second line)!");
25516
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
277 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
278 }
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
279
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
280 if (c == '\n')
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
281 break;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
282
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
283 buf1[i++] = static_cast<char> (c);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
284 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
285 buf1[i] = '\0';
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
286
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
287 octave_fseeko_wrapper (fptr, pos, SEEK_SET);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
288
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
289 char buf2[8];
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
290 i = 0;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
291 while (true)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
292 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
293 int c = fgetc (fptr);
25516
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
294
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
295 if (c == EOF)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
296 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
297 (*current_liboctave_warning_handler)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
298 ("fseek/ftell bug check failed (reading after repositioning)!");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
299 return false;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
300 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
301
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
302 if (c == '\n')
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
303 break;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
304
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
305 buf2[i++] = static_cast<char> (c);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
306 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
307 buf2[i] = '\0';
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
308
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
309 return strcmp (buf1, buf2);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
310 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
311
32077
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
312 static std::string
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
313 get_formatted_last_error ()
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
314 {
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
315 std::string msg = "";
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
316
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
317 DWORD last_error = GetLastError ();
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
318
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
319 wchar_t *error_text = nullptr;
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
320 FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
321 FORMAT_MESSAGE_ALLOCATE_BUFFER |
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
322 FORMAT_MESSAGE_IGNORE_INSERTS,
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
323 nullptr, last_error,
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
324 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
325 reinterpret_cast <wchar_t *> (&error_text), 0, nullptr);
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
326
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
327 if (error_text != nullptr)
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
328 {
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
329 msg = u8_from_wstring (error_text);
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
330 LocalFree (error_text);
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
331 }
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
332 else
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
333 msg = "Unknown error.";
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
334
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
335 return msg;
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
336 }
25516
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
337 #endif
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25514
diff changeset
338
32077
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
339 bool
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
340 file_exists (const std::string& filename, bool is_dir)
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
341 {
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
342 // Check if a file with the given name exists on the file system. If is_dir
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
343 // is true (the default), also return true if filename refers to a directory.
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
344 #if defined (OCTAVE_USE_WINDOWS_API)
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
345 std::wstring w_fn = u8_to_wstring (filename);
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
346
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
347 DWORD f_attr = GetFileAttributesW (w_fn.c_str ());
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
348
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
349 return ((f_attr != INVALID_FILE_ATTRIBUTES)
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
350 && (is_dir || ! (f_attr & FILE_ATTRIBUTE_DIRECTORY)));
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
351
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
352 #else
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
353 file_stat fs (filename);
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
354
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
355 return (fs && (fs.is_reg () || (is_dir && fs.is_dir ())));
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
356
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
357 #endif
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
358 }
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
359
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
360 bool
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
361 file_exists (const std::string& filename, bool is_dir, std::string& msg)
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
362 {
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
363 // Check if a file with the given name exists on the file system. If is_dir
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
364 // is true (the default), also return true if filename refers to a directory.
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
365 #if defined (OCTAVE_USE_WINDOWS_API)
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
366 std::wstring w_fn = u8_to_wstring (filename);
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
367
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
368 DWORD f_attr = GetFileAttributesW (w_fn.c_str ());
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
369
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
370 if (f_attr == INVALID_FILE_ATTRIBUTES)
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
371 msg = get_formatted_last_error ();
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
372
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
373 return ((f_attr != INVALID_FILE_ATTRIBUTES)
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
374 && (is_dir || ! (f_attr & FILE_ATTRIBUTE_DIRECTORY)));
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
375
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
376 #else
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
377 file_stat fs (filename);
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
378
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
379 if (! fs)
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
380 msg = fs.error ();
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
381
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
382 return (fs && (fs.is_reg () || (is_dir && fs.is_dir ())));
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
383
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
384 #endif
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
385 }
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
386
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
387 bool
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
388 dir_exists (const std::string& dirname)
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
389 {
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
390 // Check if a directory with the given name exists on the file system.
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
391 #if defined (OCTAVE_USE_WINDOWS_API)
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
392 std::wstring w_dn = u8_to_wstring (dirname);
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
393
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
394 DWORD f_attr = GetFileAttributesW (w_dn.c_str ());
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
395
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
396 return ((f_attr != INVALID_FILE_ATTRIBUTES)
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
397 && (f_attr & FILE_ATTRIBUTE_DIRECTORY));
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
398
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
399 #else
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
400 file_stat fs (dirname);
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
401
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
402 return (fs && fs.is_dir ());
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
403
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
404 #endif
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
405 }
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
406
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
407 bool
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
408 dir_exists (const std::string& dirname, std::string& msg)
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
409 {
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
410 // Check if a directory with the given name exists on the file system.
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
411 #if defined (OCTAVE_USE_WINDOWS_API)
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
412 std::wstring w_dn = u8_to_wstring (dirname);
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
413
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
414 DWORD f_attr = GetFileAttributesW (w_dn.c_str ());
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
415
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
416 if (f_attr == INVALID_FILE_ATTRIBUTES)
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
417 msg = get_formatted_last_error ();
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
418
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
419 return ((f_attr != INVALID_FILE_ATTRIBUTES)
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
420 && (f_attr & FILE_ATTRIBUTE_DIRECTORY));
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
421
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
422 #else
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
423 file_stat fs (dirname);
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
424
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
425 if (! fs)
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
426 msg = fs.error ();
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
427
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
428 return (fs && fs.is_dir ());
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
429
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
430 #endif
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
431 }
ed3a18fe328a Add more efficient functions to check for files or directories on Windows (bug #59711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31771
diff changeset
432
32086
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
433 // Return TRUE if FILE1 and FILE2 refer to the same (physical) file.
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
434
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
435 bool same_file (const std::string& file1, const std::string& file2)
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
436 {
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
437 #if defined (OCTAVE_USE_WINDOWS_API)
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
438
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
439 // FIXME: When Octave switches to C++17, consider replacing this function
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
440 // by https://en.cppreference.com/w/cpp/filesystem/equivalent.
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
441
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
442 bool retval = false;
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
443
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
444 std::wstring file1w = sys::u8_to_wstring (file1);
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
445 std::wstring file2w = sys::u8_to_wstring (file2);
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
446 const wchar_t *f1 = file1w.c_str ();
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
447 const wchar_t *f2 = file2w.c_str ();
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
448
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
449 bool f1_is_dir = GetFileAttributesW (f1) & FILE_ATTRIBUTE_DIRECTORY;
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
450 bool f2_is_dir = GetFileAttributesW (f2) & FILE_ATTRIBUTE_DIRECTORY;
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
451
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
452 // Windows native code
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
453 // Reference: http://msdn2.microsoft.com/en-us/library/aa363788.aspx
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
454
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
455 DWORD share = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
456
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
457 HANDLE hfile1
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
458 = CreateFileW (f1, 0, share, 0, OPEN_EXISTING,
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
459 f1_is_dir ? FILE_FLAG_BACKUP_SEMANTICS : 0, 0);
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
460
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
461 if (hfile1 != INVALID_HANDLE_VALUE)
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
462 {
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
463 HANDLE hfile2
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
464 = CreateFileW (f2, 0, share, 0, OPEN_EXISTING,
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
465 f2_is_dir ? FILE_FLAG_BACKUP_SEMANTICS : 0, 0);
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
466
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
467 if (hfile2 != INVALID_HANDLE_VALUE)
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
468 {
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
469 BY_HANDLE_FILE_INFORMATION hfi1;
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
470 BY_HANDLE_FILE_INFORMATION hfi2;
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
471
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
472 if (GetFileInformationByHandle (hfile1, &hfi1)
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
473 && GetFileInformationByHandle (hfile2, &hfi2))
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
474 {
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
475 retval = (hfi1.dwVolumeSerialNumber == hfi2.dwVolumeSerialNumber
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
476 && hfi1.nFileIndexHigh == hfi2.nFileIndexHigh
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
477 && hfi1.nFileIndexLow == hfi2.nFileIndexLow
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
478 && hfi1.nFileSizeHigh == hfi2.nFileSizeHigh
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
479 && hfi1.nFileSizeLow == hfi2.nFileSizeLow
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
480 && hfi1.ftLastWriteTime.dwLowDateTime
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
481 == hfi2.ftLastWriteTime.dwLowDateTime
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
482 && hfi1.ftLastWriteTime.dwHighDateTime
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
483 == hfi2.ftLastWriteTime.dwHighDateTime);
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
484 }
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
485
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
486 CloseHandle (hfile2);
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
487 }
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
488
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
489 CloseHandle (hfile1);
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
490 }
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
491
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
492 return retval;
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
493
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
494 #else
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
495
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
496 // POSIX Code
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
497
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
498 sys::file_stat fs_file1 (file1);
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
499 sys::file_stat fs_file2 (file2);
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
500
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
501 return (fs_file1 && fs_file2
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
502 && fs_file1.ino () == fs_file2.ino ()
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
503 && fs_file1.dev () == fs_file2.dev ());
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
504
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
505 #endif
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
506 }
3c608abd55f5 Move "same_file" from liboctinterp to liboctave (bug #63803).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32077
diff changeset
507
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
508 std::FILE *
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
509 fopen (const std::string& filename, const std::string& mode)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
510 {
28220
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
511 #if defined (OCTAVE_USE_WINDOWS_API)
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
512
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
513 std::wstring wfilename = u8_to_wstring (filename);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
514 std::wstring wmode = u8_to_wstring (mode);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
515
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
516 std::FILE *fptr = _wfopen (wfilename.c_str (), wmode.c_str ());
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
517
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
518 static bool fseek_ftell_bug_workaround_needed = false;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
519 static bool fseek_ftell_bug_checked = false;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
520
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
521 if (! fseek_ftell_bug_checked && mode.find ('t') != std::string::npos)
28220
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
522 {
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
523 // FIXME: Is the following workaround needed for all files
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
524 // opened in text mode, or only for files opened for reading?
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
525
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
526 // Try to avoid fseek/ftell bug on Windows systems by setting
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
527 // non-buffered input mode for files opened in text mode, but
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
528 // only if it appears that the workaround is needed. See
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
529 // Octave bug #58055.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
530
28220
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
531 // To check whether the workaround is needed:
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
532 //
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
533 // * Create a tmp file with LF line endings only.
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
534 //
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
535 // * Open that file for reading in text mode.
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
536 //
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
537 // * Read a line.
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
538 //
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
539 // * Use ftello to record the position of the beginning of
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
540 // the second line.
28220
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
541 //
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
542 // * Read and save the contents of the second line.
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
543 //
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
544 // * Use fseeko to return to the saved position.
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
545 //
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
546 // * Read the second line again and compare to the
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
547 // previously saved text.
28220
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
548 //
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
549 // * If the lines are different, we need to set non-buffered
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
550 // input mode for files opened in text mode.
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
551 //
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
552 // * To verify that the workaround solves the problem,
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
553 // repeat the above test with non-buffered input mode. If
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
554 // that fails, warn that there may be trouble with
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
555 // ftell/fseek when reading files opened in text mode.
28220
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
556
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
557 if (check_fseek_ftell_workaround_needed (false))
28220
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
558 {
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
559 if (check_fseek_ftell_workaround_needed (true))
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
560 (*current_liboctave_warning_handler)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
561 ("fseek/ftell may fail for files opened in text mode");
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
562 else
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
563 fseek_ftell_bug_workaround_needed = true;
28220
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
564 }
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
565
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
566 fseek_ftell_bug_checked = true;
28220
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
567 }
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
568
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
569 if (fseek_ftell_bug_workaround_needed
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
570 && mode.find ('t') != std::string::npos)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
571 ::setvbuf (fptr, nullptr, _IONBF, 0);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
572
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
573 return fptr;
28220
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
574
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
575 #else
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
576 return std::fopen (filename.c_str (), mode.c_str ());
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
577 #endif
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
578 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
579
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
580 std::FILE *
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
581 fopen_tmp (const std::string& name, const std::string& mode)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
582 {
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
583 #if defined (OCTAVE_USE_WINDOWS_API)
28220
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
584
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
585 // Append "D" to the mode string to indicate that this is a temporary
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
586 // file that should be deleted when the last open handle is closed.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
587 std::string tmp_mode = mode + "D";
28220
6cccc3c82175 attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents: 28175
diff changeset
588
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
589 return std::fopen (name.c_str (), tmp_mode.c_str ());
30881
1921d9d0e62b Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents: 30565
diff changeset
590
1921d9d0e62b Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents: 30565
diff changeset
591 #else
1921d9d0e62b Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents: 30565
diff changeset
592
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
593 std::FILE *fptr = std::fopen (name.c_str (), mode.c_str ());
30881
1921d9d0e62b Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents: 30565
diff changeset
594
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
595 // From gnulib: This relies on the Unix semantics that a file is not
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
596 // really removed until it is closed.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
597 octave_unlink_wrapper (name.c_str ());
30881
1921d9d0e62b Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents: 30565
diff changeset
598
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
599 return fptr;
30881
1921d9d0e62b Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents: 30565
diff changeset
600
1921d9d0e62b Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents: 30565
diff changeset
601 #endif
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
602 }
30881
1921d9d0e62b Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents: 30565
diff changeset
603
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
604 std::fstream
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
605 fstream (const std::string& filename, const std::ios::openmode mode)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
606 {
29006
139df373c107 Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28470
diff changeset
607 #if defined (OCTAVE_USE_WINDOWS_API)
139df373c107 Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28470
diff changeset
608
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
609 std::wstring wfilename = u8_to_wstring (filename);
29006
139df373c107 Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28470
diff changeset
610
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
611 return std::fstream (wfilename.c_str (), mode);
29006
139df373c107 Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28470
diff changeset
612
139df373c107 Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28470
diff changeset
613 #else
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
614 return std::fstream (filename.c_str (), mode);
29006
139df373c107 Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28470
diff changeset
615 #endif
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
616 }
29006
139df373c107 Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28470
diff changeset
617
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
618 std::ifstream
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
619 ifstream (const std::string& filename, const std::ios::openmode mode)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
620 {
29006
139df373c107 Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28470
diff changeset
621 #if defined (OCTAVE_USE_WINDOWS_API)
139df373c107 Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28470
diff changeset
622
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
623 std::wstring wfilename = u8_to_wstring (filename);
29006
139df373c107 Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28470
diff changeset
624
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
625 return std::ifstream (wfilename.c_str (), mode);
29006
139df373c107 Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28470
diff changeset
626
139df373c107 Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28470
diff changeset
627 #else
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
628 return std::ifstream (filename.c_str (), mode);
29006
139df373c107 Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28470
diff changeset
629 #endif
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
630 }
29006
139df373c107 Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28470
diff changeset
631
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
632 std::ofstream
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
633 ofstream (const std::string& filename, const std::ios::openmode mode)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
634 {
29006
139df373c107 Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28470
diff changeset
635 #if defined (OCTAVE_USE_WINDOWS_API)
139df373c107 Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28470
diff changeset
636
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
637 std::wstring wfilename = u8_to_wstring (filename);
29006
139df373c107 Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28470
diff changeset
638
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
639 return std::ofstream (wfilename.c_str (), mode);
29006
139df373c107 Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28470
diff changeset
640
139df373c107 Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28470
diff changeset
641 #else
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
642 return std::ofstream (filename.c_str (), mode);
29006
139df373c107 Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28470
diff changeset
643 #endif
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
644 }
29006
139df373c107 Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28470
diff changeset
645
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
646 void
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
647 putenv_wrapper (const std::string& name, const std::string& value)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
648 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
649 std::string new_env = name + "=" + value;
25916
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
650
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
651 // FIXME: The malloc leaks memory, but so would a call to setenv.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
652 // Short of extreme measures to track memory, altering the environment
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
653 // always leaks memory, but the saving grace is that the leaks are small.
25916
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
654
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
655 // As far as I can see there's no way to distinguish between the
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
656 // various errors; putenv doesn't have errno values.
25916
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
657
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
658 #if defined (OCTAVE_USE_WINDOWS_API)
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
659 std::wstring new_wenv = u8_to_wstring (new_env);
29492
be4b78fb4411 Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
660
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
661 int len = (new_wenv.length () + 1) * sizeof (wchar_t);
29492
be4b78fb4411 Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
662
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
663 wchar_t *new_item = static_cast<wchar_t *> (std::malloc (len));
29492
be4b78fb4411 Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
664
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
665 wcscpy (new_item, new_wenv.c_str());
28852
fe0298edc04d Fix compilation error with lambda expression.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28851
diff changeset
666
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
667 if (_wputenv (new_item) < 0)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
668 (*current_liboctave_error_handler)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
669 ("putenv (%s) failed", new_env.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
670 #else
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
671 int len = new_env.length () + 1;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
672
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
673 char *new_item = static_cast<char *> (std::malloc (len));
28824
a98fd0657e35 Replace unwind_protect with more efficient constructs in liboctave (bug #59192).
Rik <rik@octave.org>
parents: 28534
diff changeset
674
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
675 std::strcpy (new_item, new_env.c_str());
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
676
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
677 if (octave_putenv_wrapper (new_item) < 0)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
678 (*current_liboctave_error_handler) ("putenv (%s) failed", new_item);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
679 #endif
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
680 }
29492
be4b78fb4411 Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
681
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
682 std::string
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
683 getenv_wrapper (const std::string& name)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
684 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
685 #if defined (OCTAVE_USE_WINDOWS_API)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
686 std::wstring wname = u8_to_wstring (name);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
687 wchar_t *env = _wgetenv (wname.c_str ());
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
688 return env ? u8_from_wstring (env) : "";
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
689 #else
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
690 char *env = ::getenv (name.c_str ());
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
691 return env ? env : "";
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
692 #endif
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
693 }
29492
be4b78fb4411 Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
694
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
695 int
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
696 unsetenv_wrapper (const std::string& name)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
697 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
698 #if defined (OCTAVE_USE_WINDOWS_API)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
699 putenv_wrapper (name, "");
25916
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
700
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
701 std::wstring wname = u8_to_wstring (name);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
702 return (SetEnvironmentVariableW (wname.c_str (), nullptr) ? 0 : -1);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
703 #else
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
704 return octave_unsetenv_wrapper (name.c_str ());
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
705 #endif
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
706 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
707
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
708 std::wstring
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
709 u8_to_wstring (const std::string& utf8_string)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
710 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
711 // convert multibyte UTF-8 string to wide character string
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
712 static std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t>
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
713 wchar_conv;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
714
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
715 std::wstring retval = L"";
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
716
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
717 try
25916
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
718 {
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
719 retval = wchar_conv.from_bytes (utf8_string);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
720 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
721 catch (const std::range_error& e)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
722 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
723 // What to do in case of error?
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
724 // error ("u8_to_wstring: converting from UTF-8 to wchar_t: %s",
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
725 // e.what ());
25916
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
726 }
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
727
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
728 return retval;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
729 }
25916
3f2282954600 Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25660
diff changeset
730
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
731 std::string
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
732 u8_from_wstring (const std::wstring& wchar_string)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
733 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
734 // convert wide character string to multibyte UTF-8 string
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
735 static std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t>
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
736 wchar_conv;
29492
be4b78fb4411 Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
737
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
738 std::string retval = "";
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
739
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
740 try
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
741 {
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
742 retval = wchar_conv.to_bytes (wchar_string);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
743 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
744 catch (const std::range_error& e)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
745 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
746 // What to do in case of error?
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
747 // error ("u8_from_wstring: converting from wchar_t to UTF-8: %s",
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
748 // e.what ());
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
749 }
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
750
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
751 return retval;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
752 }
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
753
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
754 // At quite a few places in the code we are passing file names as
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
755 // char arrays to external library functions.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
756
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
757 // When these functions try to locate the corresponding file on the
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
758 // disc, they need to use the wide character API on Windows to
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
759 // correctly open files with non-ASCII characters.
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
760
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
761 // But they have no way of knowing which encoding we are using for
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
762 // the passed string. So they have no way of reliably converting to
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
763 // a wchar_t array. (I.e. there is no possible fix for these
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
764 // functions with current C or C++.)
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
765
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
766 // To solve the dilemma, the function "get_ASCII_filename" first
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
767 // checks whether there are any non-ASCII characters in the passed
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
768 // file name. If there are not, it returns the original name.
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
769
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
770 // Otherwise, it optionally tries to convert the file name to the locale
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
771 // charset.
28534
548598760b66 get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28472
diff changeset
772
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
773 // If the file name contains characters that cannot be converted to the
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
774 // locale charset (or that step is skipped), it tries to obtain the short
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
775 // file name (8.3 naming scheme) which only consists of ASCII characters
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
776 // and are safe to pass. However, short file names can be disabled for
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
777 // performance reasons on the file system level with NTFS and they are not
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
778 // stored on other file systems (e.g. ExFAT). So there is no guarantee
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
779 // that these exist.
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
780
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
781 // If short file names are not stored, a hard link to the file is
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
782 // created. For this the path to the file is split at the deepest
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
783 // possible level that doesn't contain non-ASCII characters. At
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
784 // that level a hidden folder is created that holds the hard links.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
785 // That means we need to have write access on that location. A path
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
786 // to that hard link is returned.
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
787
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
788 // If the file system is FAT32, there are no hard links. But FAT32
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
789 // always stores short file names. So we are safe.
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
790
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
791 // ExFAT that is occasionally used on USB sticks and SD cards stores
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
792 // neither short file names nor does it support hard links. So for
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
793 // exFAT with this function, there is (currently) no way to generate
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
794 // a file name that is stripped from non-ASCII characters but still
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
795 // is valid.
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
796
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
797 // For Unixy systems, this function does nothing.
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
798
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
799 std::string
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
800 get_ASCII_filename (const std::string& orig_file_name,
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
801 const bool allow_locale)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
802 {
25532
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
803 #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
804
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
805 // Return file name that only contains ASCII characters that can
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
806 // be used to access the file orig_file_name. The original file
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
807 // must exist in the file system before calling this function.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
808 // This is useful for passing file names to functions that are not
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
809 // 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
810
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
811 // 0. Check whether filename contains non-ASCII (UTF-8) characters.
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
812
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
813 std::string::const_iterator first_non_ASCII
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
814 = std::find_if (orig_file_name.begin (), orig_file_name.end (),
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
815 [](char c) { return (c < 0 || c >= 128); });
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
816
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
817 if (first_non_ASCII == orig_file_name.end ())
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
818 return orig_file_name;
25532
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
819
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
820 // 1. Optionally, check if all characters in the path can be successfully
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
821 // converted to the locale charset
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
822 if (allow_locale)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
823 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
824 const char *locale = octave_locale_charset_wrapper ();
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
825 if (locale)
28534
548598760b66 get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28472
diff changeset
826 {
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
827 const uint8_t *name_u8 = reinterpret_cast<const uint8_t *>
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
828 (orig_file_name.c_str ());
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
829 std::size_t length = 0;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
830 char *name_locale = octave_u8_conv_to_encoding_strict
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
831 (locale, name_u8,
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
832 orig_file_name.length () + 1, &length);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
833 if (name_locale)
28534
548598760b66 get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28472
diff changeset
834 {
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
835 std::string file_name_locale (name_locale, length);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
836 free (name_locale);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
837 return file_name_locale;
28534
548598760b66 get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28472
diff changeset
838 }
548598760b66 get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28472
diff changeset
839 }
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
840 }
28534
548598760b66 get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28472
diff changeset
841
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
842 // 2. Check if file system stores short filenames (might be ASCII-only).
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
843
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
844 std::wstring w_orig_file_name_str = u8_to_wstring (orig_file_name);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
845 const wchar_t *w_orig_file_name = w_orig_file_name_str.c_str ();
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
846
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
847 // Get full path to file
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
848 wchar_t w_full_file_name[_MAX_PATH];
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
849 if (_wfullpath (w_full_file_name, w_orig_file_name, _MAX_PATH) == nullptr)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
850 return orig_file_name;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
851
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
852 std::wstring w_full_file_name_str = w_full_file_name;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
853
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
854 // Get short filename (8.3) from UTF-16 filename.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
855
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
856 long length = GetShortPathNameW (w_full_file_name, nullptr, 0);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
857
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
858 if (length > 0)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
859 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
860 // Dynamically allocate the correct size (terminating null char
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
861 // was included in length).
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
862
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
863 OCTAVE_LOCAL_BUFFER (wchar_t, w_short_file_name, length);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
864 GetShortPathNameW (w_full_file_name, w_short_file_name, length);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
865
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
866 std::wstring w_short_file_name_str
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
867 = std::wstring (w_short_file_name, length);
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
868
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
869 if (w_short_file_name_str.compare (0, length-1, w_full_file_name_str) != 0)
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
870 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
871 // Check whether short file name contains non-ASCII characters
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
872 std::string short_file_name
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
873 = u8_from_wstring (w_short_file_name_str);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
874 first_non_ASCII
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
875 = std::find_if (short_file_name.begin (),
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
876 short_file_name.end (),
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
877 [](char c) { return (c < 0 || c >= 128); });
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
878 if (first_non_ASCII == short_file_name.end ())
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
879 return short_file_name;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
880 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
881 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
882
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
883 // 3. Create hard link with only-ASCII characters.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
884 // Get longest possible part of path that only contains ASCII chars.
25660
4de7535ece11 Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25615
diff changeset
885
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
886 std::wstring::iterator w_first_non_ASCII
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
887 = std::find_if (w_full_file_name_str.begin (), w_full_file_name_str.end (),
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
888 [](wchar_t c) { return (c < 0 || c >= 128); });
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
889 std::wstring tmp_substr
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
890 = std::wstring (w_full_file_name_str.begin (), w_first_non_ASCII);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
891
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
892 std::size_t pos
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
893 = tmp_substr.find_last_of (u8_to_wstring (file_ops::dir_sep_chars ()));
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
894
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
895 std::string par_dir
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
896 = u8_from_wstring (w_full_file_name_str.substr (0, pos+1));
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
897
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
898 // Create .oct_ascii directory.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
899 // FIXME: We need to have write permission in this location.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
900
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
901 std::string oct_ascii_dir = par_dir + ".oct_ascii";
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
902 std::string test_dir = canonicalize_file_name (oct_ascii_dir);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
903
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
904 if (test_dir.empty ())
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
905 {
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
906 std::string msg;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
907 int status = sys::mkdir (oct_ascii_dir, 0777, msg);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
908
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
909 if (status < 0)
25660
4de7535ece11 Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25615
diff changeset
910 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
911
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
912 // Set hidden property.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
913 SetFileAttributesA (oct_ascii_dir.c_str (), FILE_ATTRIBUTE_HIDDEN);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
914 }
25532
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
915
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
916 // Create file from hash of full filename.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
917 std::string filename_hash
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
918 = (oct_ascii_dir + file_ops::dir_sep_str ()
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
919 + crypto::hash ("SHA1", 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
920
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
921 // FIXME: This is just to check if the file exists. Use a more efficient
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
922 // method.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
923 std::string abs_filename_hash = canonicalize_file_name (filename_hash);
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
924
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
925 if (! abs_filename_hash.empty ())
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
926 sys::unlink (filename_hash);
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
927
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
928 // At this point, we know that we have only ASCII characters.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
929 // So instead of converting, just copy the characters to std::wstring.
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
930 std::wstring w_filename_hash (filename_hash.begin (),
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
931 filename_hash.end ());
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
932
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
933 if (CreateHardLinkW (w_filename_hash.c_str (), w_orig_file_name, nullptr))
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
934 return filename_hash;
25532
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
935
28534
548598760b66 get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28472
diff changeset
936 #else
548598760b66 get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28472
diff changeset
937
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
938 octave_unused_parameter (allow_locale);
28534
548598760b66 get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28472
diff changeset
939
25615
ffc858064239 style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents: 25609
diff changeset
940 #endif
25532
7dad5fa7e88e Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25531
diff changeset
941
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
942 return orig_file_name;
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
943 }
31605
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30881
diff changeset
944
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30881
diff changeset
945 OCTAVE_END_NAMESPACE(sys)
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30881
diff changeset
946 OCTAVE_END_NAMESPACE(octave)