GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
dir-ops.cc
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 (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <cerrno>
31 #include <cstdlib>
32 #include <cstring>
33 
34 #include <list>
35 #include <string>
36 
37 #if defined (OCTAVE_USE_WINDOWS_API)
38 # include "windows.h"
39 #endif
40 
41 #include "dirent-wrappers.h"
42 
43 #include "dir-ops.h"
44 #include "file-ops.h"
45 #include "lo-error.h"
46 #include "lo-sysdep.h"
47 #include "str-vec.h"
48 
50 
52 
53 bool
54 dir_entry::open (const std::string& n)
55 {
56  if (! n.empty ())
57  m_name = n;
58 
59  if (! m_name.empty ())
60  {
61  close ();
62 
63  std::string fullname = sys::file_ops::tilde_expand (m_name);
64 
65 #if defined (OCTAVE_USE_WINDOWS_API)
66  std::string msg;
67 
68  if (sys::dir_exists (fullname, msg))
69  // Never dereference this pointer!
70  // We just need something that is non-null.
71  m_dir = reinterpret_cast<void *> (1);
72  else
73  {
74  m_dir = nullptr;
75  m_errmsg = msg;
76  }
77 #else
78  m_dir = octave_opendir_wrapper (fullname.c_str ());
79 
80  if (! m_dir)
81  m_errmsg = std::strerror (errno);
82 #endif
83  }
84  else
85  m_errmsg = "dir_entry::open: empty filename";
86 
87  return m_dir != nullptr;
88 }
89 
92 {
93  string_vector retval;
94 
95  if (ok ())
96  {
97  std::list<std::string> dirlist;
98 
99 #if defined (OCTAVE_USE_WINDOWS_API)
100  WIN32_FIND_DATAW find_file_data;
101  std::wstring pattern = u8_to_wstring (m_name + "\\*");
102  std::string file_name;
103 
104  HANDLE h_find_file = FindFirstFileW (pattern.c_str (), &find_file_data);
105  if (h_find_file != INVALID_HANDLE_VALUE)
106  {
107  do
108  {
109  file_name = u8_from_wstring (find_file_data.cFileName);
110  dirlist.push_back (file_name);
111  }
112  while (FindNextFileW (h_find_file, &find_file_data));
113 
114  FindClose (h_find_file);
115  }
116 #else
117  char *fname;
118 
119  while ((fname = octave_readdir_wrapper (m_dir)))
120  dirlist.push_back (fname);
121 #endif
122 
123  retval = string_vector (dirlist);
124  }
125 
126  return retval;
127 }
128 
129 bool
131 {
132  bool retval = true;
133 
134 #if defined (OCTAVE_USE_WINDOWS_API)
135  m_dir = nullptr;
136 #else
137  if (m_dir)
138  {
139  retval = (octave_closedir_wrapper (m_dir) == 0);
140 
141  m_dir = nullptr;
142  }
143 #endif
144 
145  return retval;
146 }
147 
148 unsigned int
150 {
151  return octave_name_max_wrapper ();
152 }
153 
154 OCTAVE_END_NAMESPACE(sys)
155 OCTAVE_END_NAMESPACE(octave)
string_vector read()
Definition: dir-ops.cc:91
bool ok() const
Definition: dir-ops.h:84
static unsigned int max_name_length()
Definition: dir-ops.cc:149
bool open(const std::string &="")
Definition: dir-ops.cc:54
bool close()
Definition: dir-ops.cc:130
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
unsigned int octave_name_max_wrapper(void)
char * octave_readdir_wrapper(void *dir)
void * octave_opendir_wrapper(const char *dname)
int octave_closedir_wrapper(void *dir)
bool dir_exists(const std::string &dirname)
Definition: lo-sysdep.cc:389
std::string u8_from_wstring(const std::wstring &wchar_string)
Definition: lo-sysdep.cc:746
std::wstring u8_to_wstring(const std::string &utf8_string)
Definition: lo-sysdep.cc:723
octave_idx_type n
Definition: mx-inlines.cc:761
std::string tilde_expand(const std::string &name)
Definition: file-ops.cc:289