1782
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1782
|
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 2, or (at your option) any |
|
10 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, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
1782
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <cerrno> |
2926
|
29 #include <cstdlib> |
1782
|
30 #include <cstring> |
|
31 |
|
32 #include "sysdir.h" |
|
33 |
|
34 #include "dir-ops.h" |
5871
|
35 #include "file-ops.h" |
2926
|
36 #include "lo-error.h" |
|
37 #include "lo-sysdep.h" |
1782
|
38 #include "str-vec.h" |
|
39 |
|
40 bool |
3504
|
41 dir_entry::open (const std::string& n) |
1782
|
42 { |
|
43 fail = true; |
|
44 |
|
45 if (! n.empty ()) |
|
46 name = n; |
|
47 |
|
48 if (! name.empty ()) |
|
49 { |
|
50 close (); |
5871
|
51 |
|
52 std::string fullname = file_ops::tilde_expand (name); |
1782
|
53 |
5871
|
54 dir = static_cast<void *> (opendir (fullname.c_str ())); |
1782
|
55 |
|
56 if (dir) |
|
57 fail = false; |
|
58 else |
3504
|
59 { |
|
60 using namespace std; |
|
61 errmsg = strerror (errno); |
|
62 } |
1782
|
63 } |
|
64 else |
|
65 errmsg = "dir_entry::open: empty file name"; |
|
66 |
|
67 return ! fail; |
|
68 } |
|
69 |
|
70 string_vector |
|
71 dir_entry::read (void) |
|
72 { |
6145
|
73 static octave_idx_type grow_size = 100; |
|
74 |
|
75 octave_idx_type len = 0; |
|
76 |
1782
|
77 string_vector dirlist; |
|
78 |
|
79 if (ok ()) |
|
80 { |
|
81 int count = 0; |
|
82 |
|
83 struct dirent *dir_ent; |
|
84 |
2800
|
85 while ((dir_ent = readdir (static_cast<DIR *> (dir)))) |
6145
|
86 { |
|
87 if (dir_ent) |
|
88 { |
|
89 if (count >= len) |
|
90 { |
|
91 len += grow_size; |
|
92 dirlist.resize (len); |
|
93 } |
1782
|
94 |
6145
|
95 dirlist[count] = dir_ent->d_name; |
1782
|
96 |
6145
|
97 count++; |
|
98 } |
1782
|
99 else |
|
100 break; |
|
101 } |
6145
|
102 |
|
103 dirlist.resize (count); |
1782
|
104 } |
|
105 |
|
106 return dirlist; |
|
107 } |
|
108 |
|
109 void |
|
110 dir_entry::close (void) |
|
111 { |
|
112 if (dir) |
2800
|
113 closedir (static_cast<DIR *> (dir)); |
1782
|
114 |
|
115 dir = 0; |
|
116 } |
|
117 |
|
118 void |
|
119 dir_entry::copy (const dir_entry& de) |
|
120 { |
|
121 name = de.name; |
|
122 dir = de.dir; |
|
123 fail = de.fail; |
|
124 errmsg = de.errmsg; |
|
125 } |
|
126 |
|
127 /* |
|
128 ;;; Local Variables: *** |
|
129 ;;; mode: C++ *** |
|
130 ;;; End: *** |
|
131 */ |