GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
dir-ops.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_dir_ops_h)
27 #define octave_dir_ops_h 1
28 
29 #include "octave-config.h"
30 
31 #include <string>
32 
33 #include "str-vec.h"
34 
36 
38 
39 class
42 {
43 
44  // NOTE: This class cannot be used safely cross-platform (Windows) with
45  // non-ASCII characters in paths.
46  // Consider replacing the implementation using std::filesystem (C++ 17).
47  // In the meantime, consider using sys::get_dirlist instead.
48 
49 public:
50 
51  dir_entry (const std::string& n = "")
52  : m_name (n), m_dir (nullptr), m_fail (false), m_errmsg ()
53  {
54  if (! m_name.empty ())
55  open ();
56  }
57 
59  : m_name (d.m_name), m_dir (d.m_dir), m_fail (d.m_fail),
60  m_errmsg (d.m_errmsg)
61  { }
62 
63  dir_entry& operator = (const dir_entry& d)
64  {
65  if (this != &d)
66  {
67  m_name = d.m_name;
68  m_dir = d.m_dir;
69  m_fail = d.m_fail;
70  m_errmsg = d.m_errmsg;
71  }
72 
73  return *this;
74  }
75 
76  ~dir_entry () { close (); }
77 
78  bool open (const std::string& = "");
79 
80  string_vector read ();
81 
82  bool close ();
83 
84  bool ok () const { return m_dir && ! m_fail; }
85 
86  operator bool () const { return ok (); }
87 
88  std::string error () const { return ok () ? "" : m_errmsg; }
89 
90  static unsigned int max_name_length ();
91 
92 private:
93 
94  // Name of the directory.
95  std::string m_name;
96 
97  // A pointer to the contents of the directory. We use void here to
98  // avoid possible conflicts with the way some systems declare the
99  // type DIR.
100  void *m_dir;
101 
102  // TRUE means the open for this directory failed.
103  bool m_fail;
104 
105  // If a failure occurs, this contains the system error text.
106  std::string m_errmsg;
107 };
108 
109 OCTAVE_END_NAMESPACE(sys)
110 OCTAVE_END_NAMESPACE(octave)
111 
112 #endif
Definition: dir-ops.h:42
~dir_entry()
Definition: dir-ops.h:76
dir_entry(const std::string &n="")
Definition: dir-ops.h:51
bool ok() const
Definition: dir-ops.h:84
std::string error() const
Definition: dir-ops.h:88
dir_entry(const dir_entry &d)
Definition: dir-ops.h:58
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
#define OCTAVE_API
Definition: main.cc:55
octave_idx_type n
Definition: mx-inlines.cc:761