GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
dir-ops.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2015 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <cerrno>
28 #include <cstdlib>
29 #include <cstring>
30 
31 #include <list>
32 #include <string>
33 
34 #include "sysdir.h"
35 
36 #include "dir-ops.h"
37 #include "file-ops.h"
38 #include "lo-error.h"
39 #include "lo-sysdep.h"
40 #include "str-vec.h"
41 
42 bool
43 dir_entry::open (const std::string& n)
44 {
45  fail = true;
46 
47  if (! n.empty ())
48  name = n;
49 
50  if (! name.empty ())
51  {
52  close ();
53 
54  std::string fullname = file_ops::tilde_expand (name);
55 
56  dir = static_cast<void *> (gnulib::opendir (fullname.c_str ()));
57 
58  if (dir)
59  fail = false;
60  else
61  errmsg = gnulib::strerror (errno);
62  }
63  else
64  errmsg = "dir_entry::open: empty file name";
65 
66  return ! fail;
67 }
68 
71 {
72  string_vector retval;
73 
74  if (ok ())
75  {
76  std::list<std::string> dirlist;
77 
78  struct dirent *dir_ent;
79 
80  while ((dir_ent = gnulib::readdir (static_cast<DIR *> (dir))))
81  {
82  if (dir_ent)
83  dirlist.push_back (dir_ent->d_name);
84  else
85  break;
86  }
87 
88  retval = string_vector (dirlist);
89  }
90 
91  return retval;
92 }
93 
94 void
96 {
97  if (dir)
98  gnulib::closedir (static_cast<DIR *> (dir));
99 
100  dir = 0;
101 }
string_vector read(void)
Definition: dir-ops.cc:70
std::string errmsg
Definition: dir-ops.h:87
bool ok(void) const
Definition: dir-ops.h:67
std::string name
Definition: dir-ops.h:76
static std::string tilde_expand(const std::string &)
Definition: file-ops.cc:286
bool fail
Definition: dir-ops.h:84
#define dirent
Definition: sysdir.h:37
void * dir
Definition: dir-ops.h:81
void close(void)
Definition: dir-ops.cc:95
bool open(const std::string &=std::string())
Definition: dir-ops.cc:43