Mercurial > octave
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 |
rev | line source |
---|---|
1782 | 1 /* |
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 | 4 |
5 This file is part of Octave. | |
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 | 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 | 16 |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23795
diff
changeset
|
19 <https://www.gnu.org/licenses/>. |
1782 | 20 |
21 */ | |
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 | 24 #define octave_dir_ops_h 1 |
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 | 28 #include <string> |
29 | |
30 #include "str-vec.h" | |
31 | |
21735
60212efcd9cb
use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents:
21244
diff
changeset
|
32 namespace octave |
1782 | 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 | 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 | 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 | 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 | 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 | 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 | 76 |
21926 | 77 bool close (void); |
1782 | 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 | 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 | 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 | 84 |
21926 | 85 static unsigned int max_name_length (void); |
86 | |
21735
60212efcd9cb
use namespace for dir_entry class
John W. Eaton <jwe@octave.org>
parents:
21244
diff
changeset
|
87 private: |
1782 | 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 | 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 | 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 | 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 | 105 |
106 #endif |