2232
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2232
|
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. |
2232
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <string> |
|
29 |
2926
|
30 #include "file-stat.h" |
2232
|
31 #include "str-vec.h" |
|
32 |
2492
|
33 #include <defaults.h> |
2232
|
34 #include "dir-ops.h" |
|
35 #include "dirfns.h" |
|
36 #include "error.h" |
|
37 #include "fn-cache.h" |
|
38 #include "pathsearch.h" |
|
39 |
|
40 octave_fcn_file_name_cache *octave_fcn_file_name_cache::instance = 0; |
|
41 |
|
42 // Update the cache. Returns TRUE if something needed to be updated. |
|
43 |
2233
|
44 // We just accumulate all directories ever referenced in the cache and |
|
45 // we don't delete any old ones. |
2232
|
46 |
|
47 bool |
3523
|
48 octave_fcn_file_name_cache::update (const std::string& path) |
2232
|
49 { |
2286
|
50 bool something_changed = false; |
2232
|
51 |
3195
|
52 dir_path p = path.empty () ? Vload_path_dir_path : dir_path (path); |
2232
|
53 |
|
54 string_vector dirs = p.all_directories (); |
|
55 |
|
56 int len = dirs.length (); |
|
57 |
|
58 for (int i = 0; i < len; i++) |
|
59 { |
3523
|
60 std::string d = dirs[i]; |
2232
|
61 |
4215
|
62 if (cache.find (d) != cache.end ()) |
2232
|
63 { |
|
64 if (cache[d].update (d)) |
2286
|
65 something_changed = true; |
2232
|
66 } |
|
67 else |
|
68 { |
|
69 cache[d] = file_name_cache_elt (d); |
2286
|
70 something_changed = true; |
2232
|
71 } |
|
72 } |
|
73 |
2286
|
74 return something_changed; |
2232
|
75 } |
|
76 |
2370
|
77 string_vector |
3523
|
78 octave_fcn_file_name_cache::list (const std::string& path, bool no_suffix) |
2370
|
79 { |
|
80 string_vector retval; |
|
81 |
|
82 if (! instance) |
|
83 instance = new octave_fcn_file_name_cache (); |
|
84 |
|
85 if (instance) |
|
86 retval = instance->do_list (path, no_suffix); |
|
87 else |
2926
|
88 error ("unable to create file name cache object!"); |
2370
|
89 |
|
90 return retval; |
|
91 } |
|
92 |
2232
|
93 // Check to see if any of the elements in the cache need to be |
|
94 // updated, then return the list of names in the cache. |
|
95 |
|
96 string_vector |
3523
|
97 octave_fcn_file_name_cache::do_list (const std::string& path, bool no_suffix) |
2232
|
98 { |
2286
|
99 update (path); |
2232
|
100 |
2286
|
101 string_vector fcn_file_names; |
|
102 string_vector fcn_file_names_no_suffix; |
|
103 |
|
104 // For now, always generate the list of function files on each |
|
105 // call. |
2232
|
106 |
2286
|
107 // XXX FIXME XXX -- this could probably be improved by keeping lists |
|
108 // of all the function files for the current load path and only |
|
109 // updating that when the load path changes. Have to be careful to |
|
110 // return the right thing when we are only looking for a subset of |
|
111 // all the files in the load path. |
|
112 |
|
113 int total_len = 0; |
2232
|
114 |
3195
|
115 dir_path p = path.empty () ? Vload_path_dir_path : dir_path (path); |
2286
|
116 |
|
117 string_vector dirs = p.all_directories (); |
2232
|
118 |
2286
|
119 int ndirs = dirs.length (); |
2232
|
120 |
2286
|
121 if (ndirs > 1) |
|
122 { |
2232
|
123 for (int i = 0; i < ndirs; i++) |
|
124 { |
3523
|
125 std::string d = dirs[i]; |
2232
|
126 |
|
127 total_len += cache[d].length (); |
|
128 } |
|
129 |
|
130 fcn_file_names.resize (total_len); |
|
131 fcn_file_names_no_suffix.resize (total_len); |
|
132 |
|
133 int k = 0; |
|
134 |
|
135 for (int j = 0; j < ndirs; j++) |
|
136 { |
3523
|
137 std::string d = dirs[j]; |
2232
|
138 |
|
139 file_name_cache_elt elt = cache[d]; |
|
140 |
|
141 int len = elt.length (); |
|
142 |
|
143 string_vector ffn = elt.fcn_file_names; |
|
144 string_vector ffnns = elt.fcn_file_names_no_suffix; |
|
145 |
|
146 for (int i = 0; i < len; i++) |
|
147 { |
|
148 fcn_file_names[k] = ffn[i]; |
|
149 fcn_file_names_no_suffix[k] = ffnns[i]; |
|
150 |
|
151 k++; |
|
152 } |
|
153 } |
|
154 } |
2286
|
155 else if (ndirs == 1) |
|
156 { |
3523
|
157 std::string d = dirs[0]; |
2286
|
158 |
|
159 file_name_cache_elt elt = cache[d]; |
|
160 |
|
161 fcn_file_names = elt.fcn_file_names; |
|
162 fcn_file_names_no_suffix = elt.fcn_file_names_no_suffix; |
|
163 } |
2232
|
164 |
|
165 return no_suffix ? fcn_file_names_no_suffix : fcn_file_names; |
|
166 } |
|
167 |
|
168 // Create a list of the function names in a given directory. Returns |
|
169 // TRUE if the cache element was out of date. |
|
170 |
|
171 bool |
3523
|
172 file_name_cache_elt::update (const std::string& dir_name) |
2232
|
173 { |
|
174 bool retval = false; |
|
175 |
|
176 file_stat file (dir_name); |
|
177 |
|
178 // If the directory doesn't exist, delete the names in the cache. |
|
179 // If it does exist,read it only if it is out of date. |
|
180 |
|
181 if (file) |
|
182 { |
|
183 if (file.is_newer (timestamp)) |
|
184 { |
|
185 retval = true; |
|
186 |
|
187 timestamp = file.mtime (); |
|
188 |
|
189 dir_entry dir (dir_name); |
|
190 |
|
191 if (dir) |
|
192 { |
|
193 string_vector tmp = dir.read (); |
|
194 |
|
195 int max_len = tmp.length (); |
|
196 |
|
197 fcn_file_names.resize (max_len); |
|
198 fcn_file_names_no_suffix.resize (max_len); |
|
199 |
|
200 int k = 0; |
|
201 int i; |
|
202 for (i = 0; i < max_len; i++) |
|
203 { |
3523
|
204 std::string entry = tmp[i]; |
2232
|
205 |
|
206 int len = entry.length (); |
|
207 |
4128
|
208 #if defined (ENABLE_DYNAMIC_LINKING) |
2232
|
209 if ((len > 2 |
|
210 && entry[len-2] == '.' && entry[len-1] == 'm') |
|
211 || (len > 4 |
|
212 && entry[len-4] == '.' && entry[len-3] == 'o' |
|
213 && entry[len-2] == 'c' && entry[len-1] == 't')) |
|
214 #else |
|
215 if (len > 2 |
|
216 && entry[len-2] == '.' && entry[len-1] == 'm') |
|
217 #endif |
|
218 { |
|
219 fcn_file_names[k] = entry; |
|
220 |
|
221 fcn_file_names_no_suffix[k] = (entry[len-1] == 'm') |
3523
|
222 ? std::string (entry, 0, len-2) |
|
223 : std::string (entry, 0, len-4); |
2232
|
224 |
|
225 k++; |
|
226 } |
|
227 } |
|
228 |
|
229 fcn_file_names.resize (k); |
|
230 fcn_file_names_no_suffix.resize (k); |
|
231 } |
|
232 else |
|
233 { |
|
234 fcn_file_names.resize (0); |
|
235 fcn_file_names_no_suffix.resize (0); |
|
236 } |
|
237 } |
|
238 } |
|
239 else |
|
240 { |
|
241 fcn_file_names.resize (0); |
|
242 fcn_file_names_no_suffix.resize (0); |
|
243 } |
|
244 |
|
245 return retval; |
|
246 } |
|
247 |
|
248 /* |
|
249 ;;; Local Variables: *** |
|
250 ;;; mode: C++ *** |
|
251 ;;; End: *** |
|
252 */ |