GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
dir-ops.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2026 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 "oct-error.h"
46#include "oct-sysdep.h"
47#include "str-vec.h"
48
51
52bool
53dir_entry::open (const std::string& n)
54{
55 if (! n.empty ())
56 m_name = n;
57
58 if (! m_name.empty ())
59 {
60 close ();
61
62 std::string fullname = sys::file_ops::tilde_expand (m_name);
63
64#if defined (OCTAVE_USE_WINDOWS_API)
65 std::string msg;
66
67 if (sys::dir_exists (fullname, msg))
68 // Never dereference this pointer!
69 // We just need something that is non-null.
70 m_dir = reinterpret_cast<void *> (1);
71 else
72 {
73 m_dir = nullptr;
74 m_errmsg = msg;
75 }
76#else
77 m_dir = octave_opendir_wrapper (fullname.c_str ());
78
79 if (! m_dir)
80 m_errmsg = std::strerror (errno);
81#endif
82 }
83 else
84 m_errmsg = "dir_entry::open: empty filename";
85
86 return m_dir != nullptr;
87}
88
91{
92 string_vector retval;
93
94 if (ok ())
95 {
96 std::list<std::string> dirlist;
97
98#if defined (OCTAVE_USE_WINDOWS_API)
99 WIN32_FIND_DATAW find_file_data;
100 std::wstring pattern = u8_to_wstring (m_name + "\\*");
101 std::string file_name;
102
103 HANDLE h_find_file = FindFirstFileW (pattern.c_str (), &find_file_data);
104 if (h_find_file != INVALID_HANDLE_VALUE)
105 {
106 do
107 {
108 file_name = u8_from_wstring (find_file_data.cFileName);
109 dirlist.push_back (file_name);
110 }
111 while (FindNextFileW (h_find_file, &find_file_data));
112
113 FindClose (h_find_file);
114 }
115#else
116 char *fname;
117
118 while ((fname = octave_readdir_wrapper (m_dir)))
119 dirlist.push_back (fname);
120#endif
121
122 retval = string_vector (dirlist);
123 }
124
125 return retval;
126}
127
128bool
130{
131 bool retval = true;
132
133#if defined (OCTAVE_USE_WINDOWS_API)
134 m_dir = nullptr;
135#else
136 if (m_dir)
137 {
138 retval = (octave_closedir_wrapper (m_dir) == 0);
139
140 m_dir = nullptr;
141 }
142#endif
143
144 return retval;
145}
146
147unsigned int
152
153OCTAVE_END_NAMESPACE(sys)
154OCTAVE_END_NAMESPACE(octave)
string_vector read()
Definition dir-ops.cc:90
bool ok() const
Definition dir-ops.h:81
static unsigned int max_name_length()
Definition dir-ops.cc:148
bool open(const std::string &="")
Definition dir-ops.cc:53
bool close()
Definition dir-ops.cc:129
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)
std::string u8_from_wstring(const std::wstring &wchar_string)
std::wstring u8_to_wstring(const std::string &utf8_string)