comparison src/fn-cache.cc @ 2232:834eab508368

[project @ 1996-05-17 17:11:40 by jwe] Initial revision
author jwe
date Fri, 17 May 1996 17:11:40 +0000
parents
children 0da2c91573d9
comparison
equal deleted inserted replaced
2231:cd86f3e5e318 2232:834eab508368
1 /*
2
3 Copyright (C) 1996 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 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
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string>
28
29 #include "file-ops.h"
30 #include "str-vec.h"
31
32 #include "defaults.h"
33 #include "dir-ops.h"
34 #include "dirfns.h"
35 #include "error.h"
36 #include "fn-cache.h"
37 #include "pathsearch.h"
38
39 octave_fcn_file_name_cache *octave_fcn_file_name_cache::instance = 0;
40
41 // Update the cache. Returns TRUE if something needed to be updated.
42
43 // XXX FIXME XXX -- I suppose we could also keep track of the load
44 // path. Then if a directory is deleted from the load path, we could
45 // also delete it from the cache. Currently, we just accumulate all
46 // directories ever referenced in the cache.
47
48 bool
49 octave_fcn_file_name_cache::update (void)
50 {
51 bool retval = false;
52
53 dir_path p (Vload_path);
54
55 string_vector dirs = p.all_directories ();
56
57 int len = dirs.length ();
58
59 for (int i = 0; i < len; i++)
60 {
61 string d = dirs[i];
62
63 if (cache.contains (d))
64 {
65 if (cache[d].update (d))
66 retval = true;
67 }
68 else
69 {
70 cache[d] = file_name_cache_elt (d);
71 retval = true;
72 }
73 }
74
75 return retval;
76 }
77
78 // Check to see if any of the elements in the cache need to be
79 // updated, then return the list of names in the cache.
80
81 string_vector
82 octave_fcn_file_name_cache::do_list (bool no_suffix)
83 {
84 // Only recompute the cache if something has changed.
85
86 if (update ())
87 {
88 int total_len = 0;
89
90 dir_path p (Vload_path);
91
92 string_vector dirs = p.all_directories ();
93
94 int ndirs = dirs.length ();
95
96 for (int i = 0; i < ndirs; i++)
97 {
98 string d = dirs[i];
99
100 total_len += cache[d].length ();
101 }
102
103 fcn_file_names.resize (total_len);
104 fcn_file_names_no_suffix.resize (total_len);
105
106 int k = 0;
107
108 for (int j = 0; j < ndirs; j++)
109 {
110 string d = dirs[j];
111
112 file_name_cache_elt elt = cache[d];
113
114 int len = elt.length ();
115
116 string_vector ffn = elt.fcn_file_names;
117 string_vector ffnns = elt.fcn_file_names_no_suffix;
118
119 for (int i = 0; i < len; i++)
120 {
121 fcn_file_names[k] = ffn[i];
122 fcn_file_names_no_suffix[k] = ffnns[i];
123
124 k++;
125 }
126 }
127 }
128
129 return no_suffix ? fcn_file_names_no_suffix : fcn_file_names;
130 }
131
132 string_vector
133 octave_fcn_file_name_cache::list (bool no_suffix = false)
134 {
135 string_vector retval;
136
137 if (! instance)
138 instance = new octave_fcn_file_name_cache ();
139
140 if (instance)
141 retval = instance->do_list (no_suffix);
142 else
143 panic_impossible ();
144
145 return retval;
146 }
147
148 // Create a list of the function names in a given directory. Returns
149 // TRUE if the cache element was out of date.
150
151 bool
152 file_name_cache_elt::update (const string& dir_name)
153 {
154 bool retval = false;
155
156 file_stat file (dir_name);
157
158 // If the directory doesn't exist, delete the names in the cache.
159 // If it does exist,read it only if it is out of date.
160
161 if (file)
162 {
163 if (file.is_newer (timestamp))
164 {
165 retval = true;
166
167 timestamp = file.mtime ();
168
169 dir_entry dir (dir_name);
170
171 if (dir)
172 {
173 string_vector tmp = dir.read ();
174
175 int max_len = tmp.length ();
176
177 fcn_file_names.resize (max_len);
178 fcn_file_names_no_suffix.resize (max_len);
179
180 int k = 0;
181 int i;
182 for (i = 0; i < max_len; i++)
183 {
184 string entry = tmp[i];
185
186 int len = entry.length ();
187
188 #if defined (WITH_DYNAMIC_LINKING)
189 if ((len > 2
190 && entry[len-2] == '.' && entry[len-1] == 'm')
191 || (len > 4
192 && entry[len-4] == '.' && entry[len-3] == 'o'
193 && entry[len-2] == 'c' && entry[len-1] == 't'))
194 #else
195 if (len > 2
196 && entry[len-2] == '.' && entry[len-1] == 'm')
197 #endif
198 {
199 fcn_file_names[k] = entry;
200
201 fcn_file_names_no_suffix[k] = (entry[len-1] == 'm')
202 ? string (entry, 0, len-2)
203 : string (entry, 0, len-4);
204
205 k++;
206 }
207 }
208
209 fcn_file_names.resize (k);
210 fcn_file_names_no_suffix.resize (k);
211 }
212 else
213 {
214 fcn_file_names.resize (0);
215 fcn_file_names_no_suffix.resize (0);
216 }
217 }
218 }
219 else
220 {
221 fcn_file_names.resize (0);
222 fcn_file_names_no_suffix.resize (0);
223 }
224
225 return retval;
226 }
227
228 /*
229 ;;; Local Variables: ***
230 ;;; mode: C++ ***
231 ;;; End: ***
232 */