annotate liboctave/system/dir-ops.h @ 27703:12a53552db92

Use Unicode-aware functions when initializing load-path (bug #57235). * load-path.cc (load_path::dir_info::get_file_list): The "dir_entry" class cannot handle non-ASCII characters in paths cross-platform. Use "get_dirlist" instead. * dir-ops.h: Add note that using this class can cause issues on Windows.
author Markus Mützel <markus.muetzel@gmx.de>
date Sat, 16 Nov 2019 17:24:41 +0100
parents 00f796120a6d
children b442ec6dda5c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1782
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
1 /*
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
2
26376
00f796120a6d maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents: 25147
diff changeset
3 Copyright (C) 1996-2019 John W. Eaton
1782
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
4
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
5 This file is part of Octave.
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
6
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23795
diff changeset
7 Octave is free software: you can redistribute it and/or modify it
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22402
diff changeset
8 under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23795
diff changeset
9 the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22402
diff changeset
10 (at your option) any later version.
1782
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
11
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22402
diff changeset
12 Octave is distributed in the hope that it will be useful, but
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22402
diff changeset
13 WITHOUT ANY WARRANTY; without even the implied warranty of
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22402
diff changeset
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22402
diff changeset
15 GNU General Public License for more details.
1782
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
16
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6108
diff changeset
18 along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23795
diff changeset
19 <https://www.gnu.org/licenses/>.
1782
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
20
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
21 */
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
22
20791
f7084eae3318 maint: Use Octave coding conventions for #if statements.
Rik <rik@octave.org>
parents: 19697
diff changeset
23 #if ! defined (octave_dir_ops_h)
1782
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
24 #define octave_dir_ops_h 1
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
25
21244
1473547f50f5 include octave-config.h in public header files
John W. Eaton <jwe@octave.org>
parents: 21017
diff changeset
26 #include "octave-config.h"
1473547f50f5 include octave-config.h in public header files
John W. Eaton <jwe@octave.org>
parents: 21017
diff changeset
27
1782
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
28 #include <string>
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
29
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
30 #include "str-vec.h"
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
31
21735
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
32 namespace octave
1782
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
33 {
21735
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
34 namespace sys
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
35 {
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
36 class
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
37 OCTAVE_API
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
38 dir_entry
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
39 {
27703
12a53552db92 Use Unicode-aware functions when initializing load-path (bug #57235).
Markus Mützel <markus.muetzel@gmx.de>
parents: 26376
diff changeset
40
12a53552db92 Use Unicode-aware functions when initializing load-path (bug #57235).
Markus Mützel <markus.muetzel@gmx.de>
parents: 26376
diff changeset
41 // NOTE: This class cannot be used safely cross-platform (Windows) with
12a53552db92 Use Unicode-aware functions when initializing load-path (bug #57235).
Markus Mützel <markus.muetzel@gmx.de>
parents: 26376
diff changeset
42 // non-ASCII characters in paths.
12a53552db92 Use Unicode-aware functions when initializing load-path (bug #57235).
Markus Mützel <markus.muetzel@gmx.de>
parents: 26376
diff changeset
43 // Consider replacing the implementation using std::filesystem (C++ 17).
12a53552db92 Use Unicode-aware functions when initializing load-path (bug #57235).
Markus Mützel <markus.muetzel@gmx.de>
parents: 26376
diff changeset
44 // In the meantime, consider using octave::sys::get_dirlist instead.
12a53552db92 Use Unicode-aware functions when initializing load-path (bug #57235).
Markus Mützel <markus.muetzel@gmx.de>
parents: 26376
diff changeset
45
21735
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
46 public:
1782
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
47
21735
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
48 dir_entry (const std::string& n = "")
23795
980f39c3ab90 Use C++11 nullptr rather than 0 in code (bug #51565).
Rik <rik@octave.org>
parents: 23615
diff changeset
49 : name (n), dir (nullptr), fail (false), errmsg ()
22402
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
50 {
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
51 if (! name.empty ())
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
52 open ();
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
53 }
1782
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
54
21735
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
55 dir_entry (const dir_entry& d)
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
56 : name (d.name), dir (d.dir), fail (d.fail), errmsg (d.errmsg) { }
1782
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
57
21735
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
58 dir_entry& operator = (const dir_entry& d)
22402
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
59 {
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
60 if (this != &d)
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
61 {
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
62 name = d.name;
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
63 dir = d.dir;
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
64 fail = d.fail;
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
65 errmsg = d.errmsg;
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
66 }
1929
908f5b6676d7 [project @ 1996-02-11 22:05:08 by jwe]
jwe
parents: 1879
diff changeset
67
22402
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
68 return *this;
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
69 }
21735
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
70
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
71 ~dir_entry (void) { close (); }
1782
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
72
21735
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
73 bool open (const std::string& = "");
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
74
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
75 string_vector read (void);
1782
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
76
21926
24215a16f3b2 hide dirent.h header
John W. Eaton <jwe@octave.org>
parents: 21752
diff changeset
77 bool close (void);
1782
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
78
21735
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
79 bool ok (void) const { return dir && ! fail; }
1782
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
80
21735
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
81 operator bool () const { return ok (); }
1782
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
82
21735
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
83 std::string error (void) const { return ok () ? "" : errmsg; }
1782
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
84
21926
24215a16f3b2 hide dirent.h header
John W. Eaton <jwe@octave.org>
parents: 21752
diff changeset
85 static unsigned int max_name_length (void);
24215a16f3b2 hide dirent.h header
John W. Eaton <jwe@octave.org>
parents: 21752
diff changeset
86
21735
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
87 private:
1782
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
88
21735
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
89 // Name of the directory.
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
90 std::string name;
1782
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
91
21735
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
92 // A pointer to the contents of the directory. We use void here to
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
93 // avoid possible conflicts with the way some systems declare the
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
94 // type DIR.
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
95 void *dir;
1782
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
96
21735
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
97 // TRUE means the open for this directory failed.
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
98 bool fail;
1782
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
99
21735
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
100 // If a failure occurs, this contains the system error text.
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
101 std::string errmsg;
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
102 };
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
103 }
60212efcd9cb use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents: 21244
diff changeset
104 }
1782
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
105
b17d014b6926 [project @ 1996-01-24 08:09:17 by jwe]
jwe
parents:
diff changeset
106 #endif