1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2021 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 
45 namespace octave
46 {
47   namespace sys
48   {
49     bool
open(const std::string & n)50     dir_entry::open (const std::string& n)
51     {
52       if (! n.empty ())
53         name = n;
54 
55       if (! name.empty ())
56         {
57           close ();
58 
59           std::string fullname = sys::file_ops::tilde_expand (name);
60 
61           dir = octave_opendir_wrapper (fullname.c_str ());
62 
63           if (! dir)
64             errmsg = std::strerror (errno);
65         }
66       else
67         errmsg = "dir_entry::open: empty filename";
68 
69       return dir != nullptr;
70     }
71 
72     string_vector
read(void)73     dir_entry::read (void)
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 (dir)))
84             dirlist.push_back (fname);
85 
86           retval = string_vector (dirlist);
87         }
88 
89       return retval;
90     }
91 
92     bool
close(void)93     dir_entry::close (void)
94     {
95       bool retval = true;
96 
97       if (dir)
98         {
99           retval = (octave_closedir_wrapper (dir) == 0);
100 
101           dir = nullptr;
102         }
103 
104       return retval;
105     }
106 
107     unsigned int
max_name_length(void)108     dir_entry::max_name_length (void)
109     {
110       return octave_name_max_wrapper ();
111     }
112   }
113 }
114