GNU Octave 7.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-2022 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#include "dirent-wrappers.h"
38
39#include "dir-ops.h"
40#include "file-ops.h"
41#include "lo-error.h"
42#include "lo-sysdep.h"
43#include "str-vec.h"
44
45namespace octave
46{
47 namespace sys
48 {
49 bool
50 dir_entry::open (const std::string& n)
51 {
52 if (! n.empty ())
53 m_name = n;
54
55 if (! m_name.empty ())
56 {
57 close ();
58
59 std::string fullname = sys::file_ops::tilde_expand (m_name);
60
61 m_dir = octave_opendir_wrapper (fullname.c_str ());
62
63 if (! m_dir)
64 m_errmsg = std::strerror (errno);
65 }
66 else
67 m_errmsg = "dir_entry::open: empty filename";
68
69 return m_dir != nullptr;
70 }
71
74 {
75 string_vector retval;
76
77 if (ok ())
78 {
79 std::list<std::string> dirlist;
80
81 char *fname;
82
83 while ((fname = octave_readdir_wrapper (m_dir)))
84 dirlist.push_back (fname);
85
86 retval = string_vector (dirlist);
87 }
88
89 return retval;
90 }
91
92 bool
94 {
95 bool retval = true;
96
97 if (m_dir)
98 {
99 retval = (octave_closedir_wrapper (m_dir) == 0);
100
101 m_dir = nullptr;
102 }
103
104 return retval;
105 }
106
107 unsigned int
109 {
110 return octave_name_max_wrapper ();
111 }
112 }
113}
std::string m_errmsg
Definition: dir-ops.h:106
std::string m_name
Definition: dir-ops.h:95
bool close(void)
Definition: dir-ops.cc:93
bool open(const std::string &="")
Definition: dir-ops.cc:50
string_vector read(void)
Definition: dir-ops.cc:73
static unsigned int max_name_length(void)
Definition: dir-ops.cc:108
bool ok(void) const
Definition: dir-ops.h:84
void * m_dir
Definition: dir-ops.h:100
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 tilde_expand(const std::string &name)
Definition: file-ops.cc:281