Mercurial > hg > octave-nkf
annotate src/load-path.cc @ 8870:eea0e1b45ec0
optimize string manipulation in lookfor
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 25 Feb 2009 10:21:33 +0100 |
parents | 7d48766c21a5 |
children | eb63fbe60fab |
rev | line source |
---|---|
5832 | 1 /* |
2 | |
7017 | 3 Copyright (C) 2006, 2007 John W. Eaton |
5832 | 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 | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
5832 | 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 | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
5832 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 | |
27 #include <algorithm> | |
28 | |
29 #include "dir-ops.h" | |
30 #include "file-ops.h" | |
31 #include "file-stat.h" | |
32 #include "oct-env.h" | |
33 #include "pathsearch.h" | |
34 | |
35 #include "defaults.h" | |
36 #include "defun.h" | |
37 #include "input.h" | |
38 #include "load-path.h" | |
39 #include "pager.h" | |
40 #include "parse.h" | |
41 #include "toplev.h" | |
42 #include "unwind-prot.h" | |
43 #include "utils.h" | |
44 | |
45 load_path *load_path::instance = 0; | |
7336 | 46 load_path::hook_fcn_ptr load_path::add_hook = execute_pkg_add; |
47 load_path::hook_fcn_ptr load_path::remove_hook = execute_pkg_del; | |
5832 | 48 std::string load_path::command_line_path; |
6630 | 49 std::string load_path::sys_path; |
8329
c91b59532f32
load-path.cc (load_path::dir_info::update): smarter handling of relative names
John W. Eaton <jwe@octave.org>
parents:
8325
diff
changeset
|
50 load_path::abs_dir_cache_type load_path::abs_dir_cache; |
5832 | 51 |
52 void | |
53 load_path::dir_info::update (void) | |
54 { | |
8330
8303f0e912bc
load-path.cc (load_path::dir_info::update): simplify previous change
John W. Eaton <jwe@octave.org>
parents:
8329
diff
changeset
|
55 file_stat fs (dir_name); |
8303f0e912bc
load-path.cc (load_path::dir_info::update): simplify previous change
John W. Eaton <jwe@octave.org>
parents:
8329
diff
changeset
|
56 |
8303f0e912bc
load-path.cc (load_path::dir_info::update): simplify previous change
John W. Eaton <jwe@octave.org>
parents:
8329
diff
changeset
|
57 if (fs) |
8329
c91b59532f32
load-path.cc (load_path::dir_info::update): smarter handling of relative names
John W. Eaton <jwe@octave.org>
parents:
8325
diff
changeset
|
58 { |
8330
8303f0e912bc
load-path.cc (load_path::dir_info::update): simplify previous change
John W. Eaton <jwe@octave.org>
parents:
8329
diff
changeset
|
59 if (is_relative) |
8329
c91b59532f32
load-path.cc (load_path::dir_info::update): smarter handling of relative names
John W. Eaton <jwe@octave.org>
parents:
8325
diff
changeset
|
60 { |
8704
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
61 try |
8330
8303f0e912bc
load-path.cc (load_path::dir_info::update): simplify previous change
John W. Eaton <jwe@octave.org>
parents:
8329
diff
changeset
|
62 { |
8704
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
63 std::string abs_name |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
64 = octave_env::make_absolute (dir_name, octave_env::getcwd ()); |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
65 |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
66 abs_dir_cache_iterator p = abs_dir_cache.find (abs_name); |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
67 |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
68 if (p != abs_dir_cache.end ()) |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
69 { |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
70 // The directory is in the cache of all directories |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
71 // we have visited (indexed by its absolute name). |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
72 // If it is out of date, initialize it. Otherwise, |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
73 // copy the info from the cache. By doing that, we |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
74 // avoid unnecessary calls to stat that can slow |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
75 // things down tremendously for large directories. |
8329
c91b59532f32
load-path.cc (load_path::dir_info::update): smarter handling of relative names
John W. Eaton <jwe@octave.org>
parents:
8325
diff
changeset
|
76 |
8704
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
77 const dir_info& di = p->second; |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
78 |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
79 if (fs.mtime () != di.dir_mtime) |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
80 initialize (); |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
81 else |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
82 *this = di; |
8329
c91b59532f32
load-path.cc (load_path::dir_info::update): smarter handling of relative names
John W. Eaton <jwe@octave.org>
parents:
8325
diff
changeset
|
83 |
8704
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
84 return; |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
85 } |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
86 } |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
87 catch (octave_execution_exception) |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
88 { |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
89 // Skip updating if we don't know where we are, but |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
90 // don't treat it as an error. |
8329
c91b59532f32
load-path.cc (load_path::dir_info::update): smarter handling of relative names
John W. Eaton <jwe@octave.org>
parents:
8325
diff
changeset
|
91 |
8704
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
92 error_state = 0; |
8329
c91b59532f32
load-path.cc (load_path::dir_info::update): smarter handling of relative names
John W. Eaton <jwe@octave.org>
parents:
8325
diff
changeset
|
93 } |
c91b59532f32
load-path.cc (load_path::dir_info::update): smarter handling of relative names
John W. Eaton <jwe@octave.org>
parents:
8325
diff
changeset
|
94 } |
c91b59532f32
load-path.cc (load_path::dir_info::update): smarter handling of relative names
John W. Eaton <jwe@octave.org>
parents:
8325
diff
changeset
|
95 |
c91b59532f32
load-path.cc (load_path::dir_info::update): smarter handling of relative names
John W. Eaton <jwe@octave.org>
parents:
8325
diff
changeset
|
96 if (fs.mtime () != dir_mtime) |
c91b59532f32
load-path.cc (load_path::dir_info::update): smarter handling of relative names
John W. Eaton <jwe@octave.org>
parents:
8325
diff
changeset
|
97 initialize (); |
c91b59532f32
load-path.cc (load_path::dir_info::update): smarter handling of relative names
John W. Eaton <jwe@octave.org>
parents:
8325
diff
changeset
|
98 } |
5832 | 99 else |
100 { | |
8329
c91b59532f32
load-path.cc (load_path::dir_info::update): smarter handling of relative names
John W. Eaton <jwe@octave.org>
parents:
8325
diff
changeset
|
101 std::string msg = fs.error (); |
c91b59532f32
load-path.cc (load_path::dir_info::update): smarter handling of relative names
John W. Eaton <jwe@octave.org>
parents:
8325
diff
changeset
|
102 warning ("load_path: %s: %s", dir_name.c_str (), msg.c_str ()); |
5832 | 103 } |
104 } | |
105 | |
106 void | |
107 load_path::dir_info::initialize (void) | |
108 { | |
109 is_relative = ! octave_env::absolute_pathname (dir_name); | |
110 | |
111 file_stat fs (dir_name); | |
112 | |
113 if (fs) | |
114 { | |
115 dir_mtime = fs.mtime (); | |
116 | |
7336 | 117 get_file_list (dir_name); |
8329
c91b59532f32
load-path.cc (load_path::dir_info::update): smarter handling of relative names
John W. Eaton <jwe@octave.org>
parents:
8325
diff
changeset
|
118 |
8704
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
119 try |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
120 { |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
121 std::string abs_name |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
122 = octave_env::make_absolute (dir_name, octave_env::getcwd ()); |
8329
c91b59532f32
load-path.cc (load_path::dir_info::update): smarter handling of relative names
John W. Eaton <jwe@octave.org>
parents:
8325
diff
changeset
|
123 |
8704
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
124 // FIXME -- nothing is ever removed from this cache of |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
125 // directory information, so there could be some resource |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
126 // problems. Perhaps it should be pruned from time to time. |
8331 | 127 |
8704
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
128 abs_dir_cache[abs_name] = *this; |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
129 } |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
130 catch (octave_execution_exception) |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
131 { |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
132 // Skip updating if we don't know where we are. |
236ff50db90f
load-path.cc: catch execution exception in getcwd
John W. Eaton <jwe@octave.org>
parents:
8609
diff
changeset
|
133 } |
5832 | 134 } |
135 else | |
136 { | |
137 std::string msg = fs.error (); | |
138 warning ("load_path: %s: %s", dir_name.c_str (), msg.c_str ()); | |
139 } | |
140 } | |
141 | |
7336 | 142 void |
5832 | 143 load_path::dir_info::get_file_list (const std::string& d) |
144 { | |
145 dir_entry dir (d); | |
146 | |
147 if (dir) | |
148 { | |
149 string_vector flist = dir.read (); | |
150 | |
151 octave_idx_type len = flist.length (); | |
152 | |
153 all_files.resize (len); | |
154 fcn_files.resize (len); | |
155 | |
156 octave_idx_type all_files_count = 0; | |
157 octave_idx_type fcn_files_count = 0; | |
158 | |
159 for (octave_idx_type i = 0; i < len; i++) | |
160 { | |
161 std::string fname = flist[i]; | |
162 | |
7272 | 163 std::string full_name = file_ops::concat (d, fname); |
5832 | 164 |
165 file_stat fs (full_name); | |
166 | |
167 if (fs) | |
168 { | |
169 if (fs.is_dir ()) | |
170 { | |
7336 | 171 if (fname == "private") |
172 get_private_file_map (full_name); | |
173 else if (fname[0] == '@') | |
174 get_method_file_map (full_name, fname.substr (1)); | |
5832 | 175 } |
176 else | |
177 { | |
178 all_files[all_files_count++] = fname; | |
179 | |
180 size_t pos = fname.rfind ('.'); | |
181 | |
8021 | 182 if (pos != std::string::npos) |
5832 | 183 { |
184 std::string ext = fname.substr (pos); | |
185 | |
5864 | 186 if (ext == ".m" || ext == ".oct" || ext == ".mex") |
5832 | 187 { |
188 std::string base = fname.substr (0, pos); | |
189 | |
190 if (valid_identifier (base)) | |
191 fcn_files[fcn_files_count++] = fname; | |
192 } | |
193 } | |
194 } | |
195 } | |
196 } | |
197 | |
198 all_files.resize (all_files_count); | |
199 fcn_files.resize (fcn_files_count); | |
200 } | |
201 else | |
202 { | |
203 std::string msg = dir.error (); | |
204 warning ("load_path: %s: %s", d.c_str (), msg.c_str ()); | |
205 } | |
206 } | |
207 | |
7336 | 208 load_path::dir_info::fcn_file_map_type |
209 get_fcn_files (const std::string& d) | |
5832 | 210 { |
7336 | 211 load_path::dir_info::fcn_file_map_type retval; |
212 | |
5832 | 213 dir_entry dir (d); |
214 | |
215 if (dir) | |
216 { | |
217 string_vector flist = dir.read (); | |
218 | |
219 octave_idx_type len = flist.length (); | |
220 | |
221 for (octave_idx_type i = 0; i < len; i++) | |
222 { | |
223 std::string fname = flist[i]; | |
224 | |
225 std::string ext; | |
226 std::string base = fname; | |
227 | |
228 size_t pos = fname.rfind ('.'); | |
229 | |
8021 | 230 if (pos != std::string::npos) |
5832 | 231 { |
232 base = fname.substr (0, pos); | |
233 ext = fname.substr (pos); | |
234 | |
235 if (valid_identifier (base)) | |
236 { | |
237 int t = 0; | |
238 | |
239 if (ext == ".m") | |
240 t = load_path::M_FILE; | |
241 else if (ext == ".oct") | |
242 t = load_path::OCT_FILE; | |
5864 | 243 else if (ext == ".mex") |
244 t = load_path::MEX_FILE; | |
5832 | 245 |
7336 | 246 retval[base] |= t; |
5832 | 247 } |
248 } | |
249 } | |
250 } | |
251 else | |
252 { | |
253 std::string msg = dir.error (); | |
254 warning ("load_path: %s: %s", d.c_str (), msg.c_str ()); | |
255 } | |
7336 | 256 |
257 return retval; | |
258 } | |
259 | |
260 void | |
261 load_path::dir_info::get_private_file_map (const std::string& d) | |
262 { | |
263 private_file_map = get_fcn_files (d); | |
264 } | |
265 | |
266 void | |
267 load_path::dir_info::get_method_file_map (const std::string& d, | |
268 const std::string& class_name) | |
269 { | |
7971
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
270 method_file_map[class_name].method_file_map = get_fcn_files (d); |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
271 |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
272 std::string pd = file_ops::concat (d, "private"); |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
273 |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
274 file_stat fs (pd); |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
275 |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
276 if (fs && fs.is_dir ()) |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
277 method_file_map[class_name].private_file_map = get_fcn_files (pd); |
5832 | 278 } |
279 | |
280 bool | |
281 load_path::instance_ok (void) | |
282 { | |
283 bool retval = true; | |
284 | |
285 if (! instance) | |
286 instance = new load_path (); | |
287 | |
288 if (! instance) | |
289 { | |
290 ::error ("unable to create load path object!"); | |
291 | |
292 retval = false; | |
293 } | |
294 | |
295 return retval; | |
296 } | |
297 | |
7336 | 298 // FIXME -- maybe we should also maintain a map to speed up this |
299 // method of access. | |
300 | |
5832 | 301 load_path::const_dir_info_list_iterator |
5919 | 302 load_path::find_dir_info (const std::string& dir_arg) const |
5832 | 303 { |
5919 | 304 std::string dir = file_ops::tilde_expand (dir_arg); |
305 | |
5832 | 306 const_dir_info_list_iterator retval = dir_info_list.begin (); |
307 | |
308 while (retval != dir_info_list.end ()) | |
309 { | |
310 if (retval->dir_name == dir) | |
311 break; | |
312 | |
313 retval++; | |
314 } | |
315 | |
316 return retval; | |
317 } | |
318 | |
319 load_path::dir_info_list_iterator | |
5919 | 320 load_path::find_dir_info (const std::string& dir_arg) |
5832 | 321 { |
5919 | 322 std::string dir = file_ops::tilde_expand (dir_arg); |
323 | |
5832 | 324 dir_info_list_iterator retval = dir_info_list.begin (); |
325 | |
326 while (retval != dir_info_list.end ()) | |
327 { | |
328 if (retval->dir_name == dir) | |
329 break; | |
330 | |
331 retval++; | |
332 } | |
333 | |
334 return retval; | |
335 } | |
336 | |
337 bool | |
338 load_path::contains (const std::string& dir) const | |
339 { | |
340 return find_dir_info (dir) != dir_info_list.end (); | |
341 } | |
342 | |
343 void | |
7336 | 344 load_path::move_fcn_map (const std::string& dir_name, |
345 const string_vector& fcn_files, bool at_end) | |
5832 | 346 { |
7336 | 347 octave_idx_type len = fcn_files.length (); |
348 | |
349 for (octave_idx_type k = 0; k < len; k++) | |
5832 | 350 { |
7336 | 351 std::string fname = fcn_files[k]; |
5832 | 352 |
7336 | 353 std::string ext; |
354 std::string base = fname; | |
355 | |
356 size_t pos = fname.rfind ('.'); | |
5832 | 357 |
8021 | 358 if (pos != std::string::npos) |
7336 | 359 { |
360 base = fname.substr (0, pos); | |
361 ext = fname.substr (pos); | |
362 } | |
5832 | 363 |
7336 | 364 file_info_list_type& file_info_list = fcn_map[base]; |
5832 | 365 |
7336 | 366 if (file_info_list.size () == 1) |
367 continue; | |
368 else | |
5832 | 369 { |
7336 | 370 for (file_info_list_iterator p = file_info_list.begin (); |
371 p != file_info_list.end (); | |
372 p++) | |
373 { | |
374 if (p->dir_name == dir_name) | |
375 { | |
376 file_info fi = *p; | |
5832 | 377 |
7336 | 378 file_info_list.erase (p); |
379 | |
380 if (at_end) | |
381 file_info_list.push_back (fi); | |
382 else | |
383 file_info_list.push_front (fi); | |
5832 | 384 |
7336 | 385 break; |
386 } | |
387 } | |
388 } | |
389 } | |
390 } | |
5832 | 391 |
7336 | 392 void |
393 load_path::move_method_map (const std::string& dir_name, bool at_end) | |
394 { | |
395 for (method_map_iterator i = method_map.begin (); | |
396 i != method_map.end (); | |
397 i++) | |
398 { | |
399 std::string class_name = i->first; | |
5832 | 400 |
7336 | 401 fcn_map_type& fm = i->second; |
402 | |
403 std::string full_dir_name | |
404 = file_ops::concat (dir_name, "@" + class_name); | |
405 | |
406 for (fcn_map_iterator q = fm.begin (); q != fm.end (); q++) | |
407 { | |
408 file_info_list_type& file_info_list = q->second; | |
5832 | 409 |
410 if (file_info_list.size () == 1) | |
411 continue; | |
412 else | |
413 { | |
7336 | 414 for (file_info_list_iterator p = file_info_list.begin (); |
415 p != file_info_list.end (); | |
416 p++) | |
5832 | 417 { |
7336 | 418 if (p->dir_name == full_dir_name) |
5832 | 419 { |
6149 | 420 file_info fi = *p; |
5832 | 421 |
422 file_info_list.erase (p); | |
423 | |
424 if (at_end) | |
425 file_info_list.push_back (fi); | |
426 else | |
427 file_info_list.push_front (fi); | |
428 | |
429 break; | |
430 } | |
431 } | |
432 } | |
433 } | |
434 } | |
435 } | |
436 | |
7336 | 437 void |
438 load_path::move (dir_info_list_iterator i, bool at_end) | |
439 { | |
440 if (dir_info_list.size () > 1) | |
441 { | |
442 dir_info di = *i; | |
443 | |
444 dir_info_list.erase (i); | |
445 | |
446 if (at_end) | |
447 dir_info_list.push_back (di); | |
448 else | |
449 dir_info_list.push_front (di); | |
450 | |
451 std::string dir_name = di.dir_name; | |
452 | |
453 move_fcn_map (dir_name, di.fcn_files, at_end); | |
454 | |
455 // No need to move elements of private function map. | |
456 | |
457 move_method_map (dir_name, at_end); | |
458 } | |
459 } | |
460 | |
5832 | 461 static void |
462 maybe_add_path_elts (std::string& path, const std::string& dir) | |
463 { | |
464 std::string tpath = genpath (dir); | |
465 | |
466 if (! tpath.empty ()) | |
7374 | 467 { |
468 if (path.empty ()) | |
469 path = tpath; | |
470 else | |
8008
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
471 path += dir_path::path_sep_str () + tpath; |
7374 | 472 } |
5832 | 473 } |
474 | |
475 void | |
6365 | 476 load_path::do_initialize (bool set_initial_path) |
5832 | 477 { |
7374 | 478 sys_path = ""; |
5832 | 479 |
6365 | 480 if (set_initial_path) |
481 { | |
6626 | 482 maybe_add_path_elts (sys_path, Vlocal_ver_oct_file_dir); |
483 maybe_add_path_elts (sys_path, Vlocal_api_oct_file_dir); | |
484 maybe_add_path_elts (sys_path, Vlocal_oct_file_dir); | |
485 maybe_add_path_elts (sys_path, Vlocal_ver_fcn_file_dir); | |
486 maybe_add_path_elts (sys_path, Vlocal_api_fcn_file_dir); | |
487 maybe_add_path_elts (sys_path, Vlocal_fcn_file_dir); | |
488 maybe_add_path_elts (sys_path, Voct_file_dir); | |
489 maybe_add_path_elts (sys_path, Vfcn_file_dir); | |
6365 | 490 } |
5832 | 491 |
492 std::string tpath = load_path::command_line_path; | |
493 | |
494 if (tpath.empty ()) | |
8141
31d7885d9869
load-path.cc (load_path::do_initialize): look for OCTAVE_PATH in the environment, not OCTAVE_LOADPATH
John W. Eaton <jwe@octave.org>
parents:
8041
diff
changeset
|
495 tpath = octave_env::getenv ("OCTAVE_PATH"); |
5832 | 496 |
497 std::string xpath = "."; | |
498 | |
499 if (! tpath.empty ()) | |
8008
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
500 xpath += dir_path::path_sep_str () + tpath; |
5832 | 501 |
7374 | 502 if (! sys_path.empty ()) |
8008
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
503 xpath += dir_path::path_sep_str () + sys_path; |
5832 | 504 |
6119 | 505 do_set (xpath, false); |
5832 | 506 } |
507 | |
508 void | |
509 load_path::do_clear (void) | |
510 { | |
511 dir_info_list.clear (); | |
512 fcn_map.clear (); | |
7336 | 513 private_fcn_map.clear (); |
514 method_map.clear (); | |
5867 | 515 |
516 do_append (".", false); | |
5832 | 517 } |
518 | |
519 static std::list<std::string> | |
520 split_path (const std::string& p) | |
521 { | |
522 std::list<std::string> retval; | |
523 | |
524 size_t beg = 0; | |
8008
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
525 size_t end = p.find (dir_path::path_sep_char ()); |
5832 | 526 |
527 size_t len = p.length (); | |
528 | |
8021 | 529 while (end != std::string::npos) |
5832 | 530 { |
531 std::string elt = p.substr (beg, end-beg); | |
532 | |
533 if (! elt.empty ()) | |
534 retval.push_back (elt); | |
535 | |
536 beg = end + 1; | |
537 | |
538 if (beg == len) | |
539 break; | |
540 | |
8008
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
541 end = p.find (dir_path::path_sep_char (), beg); |
5832 | 542 } |
543 | |
544 std::string elt = p.substr (beg); | |
545 | |
546 if (! elt.empty ()) | |
547 retval.push_back (elt); | |
548 | |
549 return retval; | |
550 } | |
551 | |
552 void | |
5867 | 553 load_path::do_set (const std::string& p, bool warn) |
5832 | 554 { |
555 std::list<std::string> elts = split_path (p); | |
556 | |
557 // Temporarily disable add hook. | |
558 | |
5854 | 559 unwind_protect_fptr (add_hook); |
5832 | 560 |
561 add_hook = 0; | |
562 | |
8511
a835406e02dd
load_path::do_set: call clear after disabling add_hook
John W. Eaton <jwe@octave.org>
parents:
8331
diff
changeset
|
563 do_clear (); |
a835406e02dd
load_path::do_set: call clear after disabling add_hook
John W. Eaton <jwe@octave.org>
parents:
8331
diff
changeset
|
564 |
5832 | 565 for (std::list<std::string>::const_iterator i = elts.begin (); |
566 i != elts.end (); | |
567 i++) | |
5867 | 568 do_append (*i, warn); |
5832 | 569 |
570 // Restore add hook and execute for all newly added directories. | |
571 | |
572 unwind_protect::run (); | |
573 | |
574 for (dir_info_list_iterator i = dir_info_list.begin (); | |
575 i != dir_info_list.end (); | |
576 i++) | |
577 { | |
578 if (add_hook) | |
579 add_hook (i->dir_name); | |
580 } | |
581 } | |
582 | |
583 void | |
5867 | 584 load_path::do_append (const std::string& dir, bool warn) |
5832 | 585 { |
586 if (! dir.empty ()) | |
5867 | 587 do_add (dir, true, warn); |
588 } | |
5832 | 589 |
5867 | 590 void |
591 load_path::do_prepend (const std::string& dir, bool warn) | |
592 { | |
593 if (! dir.empty ()) | |
594 do_add (dir, false, warn); | |
5832 | 595 } |
596 | |
597 void | |
5919 | 598 load_path::do_add (const std::string& dir_arg, bool at_end, bool warn) |
5832 | 599 { |
5919 | 600 size_t len = dir_arg.length (); |
5911 | 601 |
5919 | 602 if (len > 1 && dir_arg.substr (len-2) == "//") |
5911 | 603 warning_with_id ("Octave:recursive-path-search", |
604 "trailing `//' is no longer special in search path elements"); | |
605 | |
5919 | 606 std::string dir = file_ops::tilde_expand (dir_arg); |
607 | |
5867 | 608 dir_info_list_iterator i = find_dir_info (dir); |
609 | |
610 if (i != dir_info_list.end ()) | |
611 move (i, at_end); | |
612 else | |
5832 | 613 { |
5867 | 614 file_stat fs (dir); |
5832 | 615 |
5867 | 616 if (fs) |
5832 | 617 { |
5867 | 618 if (fs.is_dir ()) |
619 { | |
620 dir_info di (dir); | |
5832 | 621 |
5867 | 622 if (! error_state) |
623 { | |
624 if (at_end) | |
625 dir_info_list.push_back (di); | |
626 else | |
627 dir_info_list.push_front (di); | |
628 | |
629 add_to_fcn_map (di, true); | |
5832 | 630 |
7336 | 631 add_to_private_fcn_map (di); |
632 | |
633 add_to_method_map (di, true); | |
634 | |
5867 | 635 if (add_hook) |
636 add_hook (dir); | |
637 } | |
5832 | 638 } |
5867 | 639 else if (warn) |
5919 | 640 warning ("addpath: %s: not a directory", dir_arg.c_str ()); |
5832 | 641 } |
5867 | 642 else if (warn) |
643 { | |
644 std::string msg = fs.error (); | |
5919 | 645 warning ("addpath: %s: %s", dir_arg.c_str (), msg.c_str ()); |
5867 | 646 } |
647 } | |
5832 | 648 |
5867 | 649 // FIXME -- is there a better way to do this? |
5832 | 650 |
5867 | 651 i = find_dir_info ("."); |
5832 | 652 |
5867 | 653 if (i != dir_info_list.end ()) |
5871 | 654 move (i, false); |
5867 | 655 else |
656 panic_impossible (); | |
5832 | 657 } |
658 | |
7336 | 659 void |
660 load_path::remove_fcn_map (const std::string& dir, | |
661 const string_vector& fcn_files) | |
662 { | |
663 octave_idx_type len = fcn_files.length (); | |
664 | |
665 for (octave_idx_type k = 0; k < len; k++) | |
666 { | |
667 std::string fname = fcn_files[k]; | |
668 | |
669 std::string ext; | |
670 std::string base = fname; | |
671 | |
672 size_t pos = fname.rfind ('.'); | |
673 | |
8021 | 674 if (pos != std::string::npos) |
7336 | 675 { |
676 base = fname.substr (0, pos); | |
677 ext = fname.substr (pos); | |
678 } | |
679 | |
680 file_info_list_type& file_info_list = fcn_map[base]; | |
681 | |
682 for (file_info_list_iterator p = file_info_list.begin (); | |
683 p != file_info_list.end (); | |
684 p++) | |
685 { | |
686 if (p->dir_name == dir) | |
687 { | |
688 file_info_list.erase (p); | |
689 | |
690 if (file_info_list.empty ()) | |
691 fcn_map.erase (fname); | |
692 | |
693 break; | |
694 } | |
695 } | |
696 } | |
697 } | |
698 | |
699 void | |
700 load_path::remove_private_fcn_map (const std::string& dir) | |
701 { | |
702 private_fcn_map_iterator p = private_fcn_map.find (dir); | |
703 | |
704 if (p != private_fcn_map.end ()) | |
705 private_fcn_map.erase (p); | |
706 } | |
707 | |
708 void | |
709 load_path::remove_method_map (const std::string& dir) | |
710 { | |
711 for (method_map_iterator i = method_map.begin (); | |
712 i != method_map.end (); | |
713 i++) | |
714 { | |
715 std::string class_name = i->first; | |
716 | |
717 fcn_map_type& fm = i->second; | |
718 | |
719 std::string full_dir_name = file_ops::concat (dir, "@" + class_name); | |
720 | |
721 for (fcn_map_iterator q = fm.begin (); q != fm.end (); q++) | |
722 { | |
723 file_info_list_type& file_info_list = q->second; | |
724 | |
725 if (file_info_list.size () == 1) | |
726 continue; | |
727 else | |
728 { | |
729 for (file_info_list_iterator p = file_info_list.begin (); | |
730 p != file_info_list.end (); | |
731 p++) | |
732 { | |
733 if (p->dir_name == full_dir_name) | |
734 { | |
735 file_info_list.erase (p); | |
736 | |
737 // FIXME -- if there are no other elements, we | |
738 // should remove this element of fm but calling | |
739 // erase here would invalidate the iterator q. | |
740 | |
741 break; | |
742 } | |
743 } | |
744 } | |
745 } | |
746 } | |
747 } | |
748 | |
5832 | 749 bool |
5919 | 750 load_path::do_remove (const std::string& dir_arg) |
5832 | 751 { |
752 bool retval = false; | |
753 | |
5919 | 754 if (! dir_arg.empty ()) |
5832 | 755 { |
5919 | 756 if (dir_arg == ".") |
5867 | 757 { |
758 warning ("rmpath: can't remove \".\" from path"); | |
5832 | 759 |
5867 | 760 // Avoid additional warnings. |
5832 | 761 retval = true; |
5867 | 762 } |
763 else | |
764 { | |
5919 | 765 std::string dir = file_ops::tilde_expand (dir_arg); |
766 | |
5867 | 767 dir_info_list_iterator i = find_dir_info (dir); |
5832 | 768 |
5867 | 769 if (i != dir_info_list.end ()) |
770 { | |
771 retval = true; | |
5832 | 772 |
6825 | 773 if (remove_hook) |
774 remove_hook (dir); | |
775 | |
5867 | 776 string_vector fcn_files = i->fcn_files; |
777 | |
778 dir_info_list.erase (i); | |
5832 | 779 |
7336 | 780 remove_fcn_map (dir, fcn_files); |
5867 | 781 |
7336 | 782 remove_private_fcn_map (dir); |
5832 | 783 |
7336 | 784 remove_method_map (dir); |
5832 | 785 } |
786 } | |
787 } | |
788 | |
789 return retval; | |
790 } | |
791 | |
792 void | |
793 load_path::do_update (void) const | |
794 { | |
795 // I don't see a better way to do this because we need to | |
796 // preserve the correct directory ordering for new files that | |
797 // have appeared. | |
798 | |
799 fcn_map.clear (); | |
800 | |
7336 | 801 private_fcn_map.clear (); |
802 | |
803 method_map.clear (); | |
804 | |
5832 | 805 for (dir_info_list_iterator p = dir_info_list.begin (); |
806 p != dir_info_list.end (); | |
807 p++) | |
808 { | |
809 dir_info& di = *p; | |
810 | |
811 di.update (); | |
812 | |
813 add_to_fcn_map (di, true); | |
7336 | 814 |
815 add_to_private_fcn_map (di); | |
816 | |
817 add_to_method_map (di, true); | |
5832 | 818 } |
819 } | |
820 | |
7336 | 821 bool |
822 load_path::check_file_type (std::string& fname, int type, int possible_types, | |
823 const std::string& fcn, const char *who) | |
824 { | |
825 bool retval = false; | |
826 | |
827 if (type == load_path::OCT_FILE) | |
828 { | |
829 if ((type & possible_types) == load_path::OCT_FILE) | |
830 { | |
831 fname += ".oct"; | |
832 retval = true; | |
833 } | |
834 } | |
835 else if (type == load_path::M_FILE) | |
836 { | |
837 if ((type & possible_types) == load_path::M_FILE) | |
838 { | |
839 fname += ".m"; | |
840 retval = true; | |
841 } | |
842 } | |
843 else if (type == load_path::MEX_FILE) | |
844 { | |
845 if ((type & possible_types) == load_path::MEX_FILE) | |
846 { | |
847 fname += ".mex"; | |
848 retval = true; | |
849 } | |
850 } | |
851 else if (type == (load_path::M_FILE | load_path::OCT_FILE)) | |
852 { | |
853 if (possible_types & load_path::OCT_FILE) | |
854 { | |
855 fname += ".oct"; | |
856 retval = true; | |
857 } | |
858 else if (possible_types & load_path::M_FILE) | |
859 { | |
860 fname += ".m"; | |
861 retval = true; | |
862 } | |
863 } | |
864 else if (type == (load_path::M_FILE | load_path::MEX_FILE)) | |
865 { | |
866 if (possible_types & load_path::MEX_FILE) | |
867 { | |
868 fname += ".mex"; | |
869 retval = true; | |
870 } | |
871 else if (possible_types & load_path::M_FILE) | |
872 { | |
873 fname += ".m"; | |
874 retval = true; | |
875 } | |
876 } | |
877 else if (type == (load_path::OCT_FILE | load_path::MEX_FILE)) | |
878 { | |
879 if (possible_types & load_path::OCT_FILE) | |
880 { | |
881 fname += ".oct"; | |
882 retval = true; | |
883 } | |
884 else if (possible_types & load_path::MEX_FILE) | |
885 { | |
886 fname += ".mex"; | |
887 retval = true; | |
888 } | |
889 } | |
890 else if (type == (load_path::M_FILE | load_path::OCT_FILE | |
891 | load_path::MEX_FILE)) | |
892 { | |
893 if (possible_types & load_path::OCT_FILE) | |
894 { | |
895 fname += ".oct"; | |
896 retval = true; | |
897 } | |
898 else if (possible_types & load_path::MEX_FILE) | |
899 { | |
900 fname += ".mex"; | |
901 retval = true; | |
902 } | |
903 else if (possible_types & load_path::M_FILE) | |
904 { | |
905 fname += ".m"; | |
906 retval = true; | |
907 } | |
908 } | |
909 else | |
910 error ("%s: %s: invalid type code = %d", who, fcn.c_str (), type); | |
911 | |
912 return retval; | |
913 } | |
914 | |
5832 | 915 std::string |
7336 | 916 load_path::do_find_fcn (const std::string& fcn, std::string& dir_name, |
917 int type) const | |
5832 | 918 { |
919 std::string retval; | |
7336 | 920 |
921 // update (); | |
5832 | 922 |
8593
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
923 if (fcn.length () > 0 && fcn[0] == '@') |
5832 | 924 { |
8593
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
925 size_t pos = fcn.find ('/'); |
5832 | 926 |
8593
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
927 if (pos != std::string::npos) |
5832 | 928 { |
8593
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
929 std::string class_name = fcn.substr (1, pos-1); |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
930 std::string meth = fcn.substr (pos+1); |
5832 | 931 |
8593
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
932 retval = do_find_method (class_name, meth, dir_name); |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
933 } |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
934 else |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
935 retval = std::string (); |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
936 } |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
937 else |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
938 { |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
939 dir_name = std::string (); |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
940 |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
941 const_fcn_map_iterator p = fcn_map.find (fcn); |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
942 |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
943 if (p != fcn_map.end ()) |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
944 { |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
945 const file_info_list_type& file_info_list = p->second; |
5832 | 946 |
8593
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
947 for (const_file_info_list_iterator i = file_info_list.begin (); |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
948 i != file_info_list.end (); |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
949 i++) |
5832 | 950 { |
8593
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
951 const file_info& fi = *i; |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
952 |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
953 retval = file_ops::concat (fi.dir_name, fcn); |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
954 |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
955 if (check_file_type (retval, type, fi.types, |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
956 fcn, "load_path::do_find_fcn")) |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
957 { |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
958 dir_name = fi.dir_name; |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
959 break; |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
960 } |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
961 else |
4e39b00218d3
load-path.cc (load_path::do_find_fcn): handle @foo/bar
John W. Eaton <jwe@octave.org>
parents:
8586
diff
changeset
|
962 retval = std::string (); |
5832 | 963 } |
7336 | 964 } |
965 } | |
966 | |
967 return retval; | |
968 } | |
969 | |
970 std::string | |
971 load_path::do_find_private_fcn (const std::string& dir, | |
972 const std::string& fcn, int type) const | |
973 { | |
974 std::string retval; | |
975 | |
976 // update (); | |
977 | |
978 const_private_fcn_map_iterator q = private_fcn_map.find (dir); | |
979 | |
980 if (q != private_fcn_map.end ()) | |
981 { | |
982 const dir_info::fcn_file_map_type& m = q->second; | |
983 | |
984 dir_info::const_fcn_file_map_iterator p = m.find (fcn); | |
985 | |
986 if (p != m.end ()) | |
987 { | |
988 std::string fname | |
989 = file_ops::concat (file_ops::concat (dir, "private"), fcn); | |
990 | |
991 if (check_file_type (fname, type, p->second, fcn, | |
992 "load_path::find_private_fcn")) | |
993 retval = fname; | |
994 } | |
995 } | |
996 | |
997 return retval; | |
998 } | |
999 | |
1000 std::string | |
1001 load_path::do_find_method (const std::string& class_name, | |
1002 const std::string& meth, | |
1003 std::string& dir_name, int type) const | |
1004 { | |
1005 std::string retval; | |
1006 | |
1007 // update (); | |
1008 | |
1009 dir_name = std::string (); | |
1010 | |
1011 const_method_map_iterator q = method_map.find (class_name); | |
1012 | |
1013 if (q != method_map.end ()) | |
1014 { | |
1015 const fcn_map_type& m = q->second; | |
1016 | |
1017 const_fcn_map_iterator p = m.find (meth); | |
1018 | |
1019 if (p != m.end ()) | |
1020 { | |
1021 const file_info_list_type& file_info_list = p->second; | |
1022 | |
1023 for (const_file_info_list_iterator i = file_info_list.begin (); | |
1024 i != file_info_list.end (); | |
1025 i++) | |
5864 | 1026 { |
7336 | 1027 const file_info& fi = *i; |
1028 | |
1029 retval = file_ops::concat (fi.dir_name, meth); | |
1030 | |
1031 bool found = check_file_type (retval, type, fi.types, | |
1032 meth, "load_path::do_find_method"); | |
1033 | |
1034 if (found) | |
5864 | 1035 { |
7336 | 1036 dir_name = fi.dir_name; |
5832 | 1037 break; |
1038 } | |
7336 | 1039 else |
1040 retval = std::string (); | |
5864 | 1041 } |
5832 | 1042 } |
1043 } | |
1044 | |
1045 return retval; | |
1046 } | |
1047 | |
7336 | 1048 std::list<std::string> |
1049 load_path::do_methods (const std::string& class_name) const | |
1050 { | |
1051 std::list<std::string> retval; | |
1052 | |
1053 // update (); | |
1054 | |
1055 const_method_map_iterator q = method_map.find (class_name); | |
1056 | |
1057 if (q != method_map.end ()) | |
1058 { | |
1059 const fcn_map_type& m = q->second; | |
1060 | |
1061 for (const_fcn_map_iterator p = m.begin (); p != m.end (); p++) | |
1062 retval.push_back (p->first); | |
1063 } | |
1064 | |
1065 if (! retval.empty ()) | |
1066 retval.sort (); | |
1067 | |
1068 return retval; | |
1069 } | |
1070 | |
5832 | 1071 std::string |
1072 load_path::do_find_file (const std::string& file) const | |
1073 { | |
1074 std::string retval; | |
1075 | |
8021 | 1076 if (file.find_first_of (file_ops::dir_sep_chars ()) != std::string::npos) |
5832 | 1077 { |
6838 | 1078 if (octave_env::absolute_pathname (file) |
1079 || octave_env::rooted_relative_pathname (file)) | |
1080 { | |
1081 file_stat fs (file); | |
5832 | 1082 |
6838 | 1083 if (fs.exists ()) |
1084 return file; | |
1085 } | |
1086 else | |
1087 { | |
1088 for (const_dir_info_list_iterator p = dir_info_list.begin (); | |
1089 p != dir_info_list.end (); | |
1090 p++) | |
1091 { | |
7272 | 1092 std::string tfile = file_ops::concat (p->dir_name, file); |
5832 | 1093 |
6838 | 1094 file_stat fs (tfile); |
6159 | 1095 |
6838 | 1096 if (fs.exists ()) |
1097 return tfile; | |
5832 | 1098 } |
1099 } | |
1100 } | |
6838 | 1101 else |
1102 { | |
1103 for (const_dir_info_list_iterator p = dir_info_list.begin (); | |
1104 p != dir_info_list.end (); | |
1105 p++) | |
1106 { | |
1107 string_vector all_files = p->all_files; | |
6159 | 1108 |
6838 | 1109 octave_idx_type len = all_files.length (); |
1110 | |
1111 for (octave_idx_type i = 0; i < len; i++) | |
1112 { | |
1113 if (all_files[i] == file) | |
7272 | 1114 return file_ops::concat (p->dir_name, file); |
6838 | 1115 } |
1116 } | |
1117 } | |
5832 | 1118 |
1119 return retval; | |
1120 } | |
1121 | |
1122 std::string | |
8041
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1123 load_path::do_find_dir (const std::string& dir) const |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1124 { |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1125 std::string retval; |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1126 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1127 if (dir.find_first_of (file_ops::dir_sep_chars ()) != std::string::npos |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1128 && (octave_env::absolute_pathname (dir) |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1129 || octave_env::rooted_relative_pathname (dir))) |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1130 { |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1131 file_stat fs (dir); |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1132 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1133 if (fs.exists () && fs.is_dir ()) |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1134 return dir; |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1135 } |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1136 else |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1137 { |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1138 for (const_dir_info_list_iterator p = dir_info_list.begin (); |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1139 p != dir_info_list.end (); |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1140 p++) |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1141 { |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1142 std::string dname = p->dir_name; |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1143 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1144 size_t dname_len = dname.length (); |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1145 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1146 if (dname.substr (dname_len - 1) == file_ops::dir_sep_str ()) |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1147 dname = dname.substr (0, dname_len - 1); |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1148 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1149 size_t dir_len = dir.length (); |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1150 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1151 if (dname_len >= dir_len |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1152 && file_ops::is_dir_sep (dname[dname_len - dir_len - 1]) |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1153 && dir.compare (dname.substr (dname_len - dir_len)) == 0) |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1154 { |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1155 file_stat fs (p->dir_name); |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1156 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1157 if (fs.exists () && fs.is_dir ()) |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1158 return p->dir_name; |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1159 } |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1160 } |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1161 } |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1162 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1163 return retval; |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1164 } |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1165 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1166 std::string |
5832 | 1167 load_path::do_find_first_of (const string_vector& flist) const |
1168 { | |
1169 std::string retval; | |
1170 | |
1171 std::string dir_name; | |
1172 std::string file_name; | |
1173 | |
1174 octave_idx_type flen = flist.length (); | |
1175 octave_idx_type rel_flen = 0; | |
1176 | |
1177 string_vector rel_flist (flen); | |
1178 | |
1179 for (octave_idx_type i = 0; i < flen; i++) | |
1180 { | |
1181 if (octave_env::absolute_pathname (flist[i])) | |
1182 { | |
1183 file_stat fs (flist[i]); | |
1184 | |
1185 if (fs.exists ()) | |
1186 return flist[i]; | |
1187 } | |
1188 else | |
1189 rel_flist[rel_flen++] = flist[i]; | |
1190 } | |
1191 | |
1192 rel_flist.resize (rel_flen); | |
1193 | |
1194 for (const_dir_info_list_iterator p = dir_info_list.begin (); | |
1195 p != dir_info_list.end (); | |
1196 p++) | |
1197 { | |
1198 string_vector all_files = p->all_files; | |
1199 | |
1200 octave_idx_type len = all_files.length (); | |
1201 | |
1202 for (octave_idx_type i = 0; i < len; i++) | |
1203 { | |
1204 for (octave_idx_type j = 0; j < rel_flen; j++) | |
1205 { | |
1206 if (all_files[i] == rel_flist[j]) | |
1207 { | |
1208 dir_name = p->dir_name; | |
1209 file_name = rel_flist[j]; | |
6159 | 1210 |
1211 goto done; | |
5832 | 1212 } |
1213 } | |
1214 } | |
1215 } | |
1216 | |
6159 | 1217 done: |
1218 | |
5832 | 1219 if (! dir_name.empty ()) |
7272 | 1220 retval = file_ops::concat (dir_name, file_name); |
5832 | 1221 |
1222 return retval; | |
1223 } | |
1224 | |
1225 string_vector | |
1226 load_path::do_find_all_first_of (const string_vector& flist) const | |
1227 { | |
1228 std::list<std::string> retlist; | |
1229 | |
1230 std::string dir_name; | |
1231 std::string file_name; | |
1232 | |
1233 octave_idx_type flen = flist.length (); | |
1234 octave_idx_type rel_flen = 0; | |
1235 | |
1236 string_vector rel_flist (flen); | |
1237 | |
1238 for (octave_idx_type i = 0; i < flen; i++) | |
1239 { | |
1240 if (octave_env::absolute_pathname (flist[i])) | |
1241 { | |
1242 file_stat fs (flist[i]); | |
1243 | |
1244 if (fs.exists ()) | |
1245 retlist.push_back (flist[i]); | |
1246 } | |
1247 else | |
1248 rel_flist[rel_flen++] = flist[i]; | |
1249 } | |
1250 | |
1251 rel_flist.resize (rel_flen); | |
1252 | |
1253 for (const_dir_info_list_iterator p = dir_info_list.begin (); | |
1254 p != dir_info_list.end (); | |
1255 p++) | |
1256 { | |
1257 string_vector all_files = p->all_files; | |
1258 | |
1259 octave_idx_type len = all_files.length (); | |
1260 | |
1261 for (octave_idx_type i = 0; i < len; i++) | |
1262 { | |
1263 for (octave_idx_type j = 0; j < rel_flen; j++) | |
1264 { | |
1265 if (all_files[i] == rel_flist[j]) | |
1266 retlist.push_back | |
7272 | 1267 (file_ops::concat (p->dir_name, rel_flist[j])); |
5832 | 1268 } |
1269 } | |
1270 } | |
1271 | |
1272 size_t retsize = retlist.size (); | |
1273 | |
1274 string_vector retval (retsize); | |
1275 | |
1276 for (size_t i = 0; i < retsize; i++) | |
1277 { | |
1278 retval[i] = retlist.front (); | |
1279 | |
1280 retlist.pop_front (); | |
1281 } | |
1282 | |
1283 return retval; | |
1284 } | |
1285 | |
1286 string_vector | |
1287 load_path::do_dirs (void) const | |
1288 { | |
1289 size_t len = dir_info_list.size (); | |
1290 | |
1291 string_vector retval (len); | |
1292 | |
1293 octave_idx_type k = 0; | |
1294 | |
1295 for (const_dir_info_list_iterator i = dir_info_list.begin (); | |
1296 i != dir_info_list.end (); | |
1297 i++) | |
1298 retval[k++] = i->dir_name; | |
1299 | |
1300 return retval; | |
1301 } | |
1302 | |
1303 std::list<std::string> | |
1304 load_path::do_dir_list (void) const | |
1305 { | |
1306 std::list<std::string> retval; | |
1307 | |
1308 for (const_dir_info_list_iterator i = dir_info_list.begin (); | |
1309 i != dir_info_list.end (); | |
1310 i++) | |
1311 retval.push_back (i->dir_name); | |
1312 | |
1313 return retval; | |
1314 } | |
1315 | |
1316 string_vector | |
1317 load_path::do_files (const std::string& dir) const | |
1318 { | |
1319 string_vector retval; | |
1320 | |
1321 const_dir_info_list_iterator i = find_dir_info (dir); | |
1322 | |
1323 if (i != dir_info_list.end ()) | |
1324 retval = i->fcn_files; | |
1325 | |
1326 return retval; | |
1327 } | |
1328 | |
1329 string_vector | |
1330 load_path::do_fcn_names (void) const | |
1331 { | |
1332 size_t len = fcn_map.size (); | |
1333 | |
1334 string_vector retval (len); | |
1335 | |
1336 octave_idx_type count = 0; | |
1337 | |
1338 for (const_fcn_map_iterator p = fcn_map.begin (); | |
1339 p != fcn_map.end (); | |
1340 p++) | |
1341 retval[count++] = p->first; | |
1342 | |
1343 return retval; | |
1344 } | |
1345 | |
1346 std::string | |
1347 load_path::do_path (void) const | |
1348 { | |
1349 std::string xpath; | |
1350 | |
1351 string_vector xdirs = load_path::dirs (); | |
1352 | |
1353 octave_idx_type len = xdirs.length (); | |
1354 | |
1355 if (len > 0) | |
1356 xpath = xdirs[0]; | |
1357 | |
1358 for (octave_idx_type i = 1; i < len; i++) | |
8008
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
1359 xpath += dir_path::path_sep_str () + xdirs[i]; |
5832 | 1360 |
1361 return xpath; | |
1362 } | |
1363 | |
1364 void | |
7336 | 1365 print_types (std::ostream& os, int types) |
1366 { | |
1367 bool printed_type = false; | |
1368 | |
1369 if (types & load_path::OCT_FILE) | |
1370 { | |
1371 os << "oct"; | |
1372 printed_type = true; | |
1373 } | |
1374 | |
1375 if (types & load_path::MEX_FILE) | |
1376 { | |
1377 if (printed_type) | |
1378 os << "|"; | |
1379 os << "mex"; | |
1380 printed_type = true; | |
1381 } | |
1382 | |
1383 if (types & load_path::M_FILE) | |
1384 { | |
1385 if (printed_type) | |
1386 os << "|"; | |
1387 os << "m"; | |
1388 printed_type = true; | |
1389 } | |
1390 } | |
1391 | |
1392 void | |
1393 print_fcn_list (std::ostream& os, | |
1394 const load_path::dir_info::fcn_file_map_type& lst) | |
1395 { | |
1396 for (load_path::dir_info::const_fcn_file_map_iterator p = lst.begin (); | |
1397 p != lst.end (); | |
1398 p++) | |
1399 { | |
1400 os << " " << p->first << " ("; | |
1401 | |
1402 print_types (os, p->second); | |
1403 | |
1404 os << ")\n"; | |
1405 } | |
1406 } | |
1407 | |
1408 string_vector | |
1409 get_file_list (const load_path::dir_info::fcn_file_map_type& lst) | |
1410 { | |
1411 octave_idx_type n = lst.size (); | |
1412 | |
1413 string_vector retval (n); | |
1414 | |
1415 octave_idx_type count = 0; | |
1416 | |
1417 for (load_path::dir_info::const_fcn_file_map_iterator p = lst.begin (); | |
1418 p != lst.end (); | |
1419 p++) | |
1420 { | |
1421 std::string nm = p->first; | |
1422 | |
1423 int types = p->second; | |
1424 | |
1425 if (types & load_path::OCT_FILE) | |
1426 nm += ".oct"; | |
1427 else if (types & load_path::MEX_FILE) | |
1428 nm += ".mex"; | |
1429 else | |
1430 nm += ".m"; | |
1431 | |
1432 retval[count++] = nm; | |
1433 } | |
1434 | |
1435 return retval; | |
1436 } | |
1437 | |
1438 void | |
5832 | 1439 load_path::do_display (std::ostream& os) const |
1440 { | |
1441 for (const_dir_info_list_iterator i = dir_info_list.begin (); | |
1442 i != dir_info_list.end (); | |
1443 i++) | |
1444 { | |
1445 string_vector fcn_files = i->fcn_files; | |
1446 | |
1447 if (! fcn_files.empty ()) | |
1448 { | |
1449 os << "\n*** function files in " << i->dir_name << ":\n\n"; | |
1450 | |
1451 fcn_files.list_in_columns (os); | |
1452 } | |
1453 | |
7336 | 1454 const dir_info::method_file_map_type& method_file_map |
1455 = i->method_file_map; | |
5832 | 1456 |
7336 | 1457 if (! method_file_map.empty ()) |
5832 | 1458 { |
7336 | 1459 for (dir_info::const_method_file_map_iterator p = method_file_map.begin (); |
1460 p != method_file_map.end (); | |
5832 | 1461 p++) |
1462 { | |
7336 | 1463 os << "\n*** methods in " << i->dir_name |
1464 << "/@" << p->first << ":\n\n"; | |
5832 | 1465 |
7971
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1466 const dir_info::class_info& ci = p->second; |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1467 |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1468 string_vector method_files = get_file_list (ci.method_file_map); |
5832 | 1469 |
7336 | 1470 method_files.list_in_columns (os); |
1471 } | |
1472 } | |
1473 } | |
5864 | 1474 |
7336 | 1475 for (const_private_fcn_map_iterator i = private_fcn_map.begin (); |
1476 i != private_fcn_map.end (); i++) | |
1477 { | |
1478 os << "\n*** private functions in " | |
1479 << file_ops::concat (i->first, "private") << ":\n\n"; | |
5832 | 1480 |
7336 | 1481 print_fcn_list (os, i->second); |
5832 | 1482 } |
1483 | |
1484 #if defined (DEBUG_LOAD_PATH) | |
1485 | |
1486 for (const_fcn_map_iterator i = fcn_map.begin (); | |
1487 i != fcn_map.end (); | |
1488 i++) | |
1489 { | |
1490 os << i->first << ":\n"; | |
1491 | |
7336 | 1492 const file_info_list_type& file_info_list = i->second; |
5832 | 1493 |
1494 for (const_file_info_list_iterator p = file_info_list.begin (); | |
1495 p != file_info_list.end (); | |
1496 p++) | |
1497 { | |
1498 os << " " << p->dir_name << " ("; | |
1499 | |
7336 | 1500 print_types (os, p->types); |
5832 | 1501 |
1502 os << ")\n"; | |
1503 } | |
1504 } | |
1505 | |
7336 | 1506 for (const_method_map_iterator i = method_map.begin (); |
1507 i != method_map.end (); | |
1508 i++) | |
1509 { | |
1510 os << "CLASS " << i->first << ":\n"; | |
1511 | |
1512 const fcn_map_type& fm = i->second; | |
1513 | |
1514 for (const_fcn_map_iterator q = fm.begin (); | |
1515 q != fm.end (); | |
1516 q++) | |
1517 { | |
1518 os << " " << q->first << ":\n"; | |
1519 | |
1520 const file_info_list_type& file_info_list = q->second; | |
1521 | |
1522 for (const_file_info_list_iterator p = file_info_list.begin (); | |
1523 p != file_info_list.end (); | |
1524 p++) | |
1525 { | |
1526 os << " " << p->dir_name << " ("; | |
1527 | |
1528 print_types (os, p->types); | |
1529 | |
1530 os << ")\n"; | |
1531 } | |
1532 } | |
1533 } | |
1534 | |
5832 | 1535 os << "\n"; |
1536 | |
1537 #endif | |
1538 } | |
1539 | |
1540 void | |
1541 load_path::add_to_fcn_map (const dir_info& di, bool at_end) const | |
1542 { | |
1543 std::string dir_name = di.dir_name; | |
1544 | |
1545 string_vector fcn_files = di.fcn_files; | |
1546 | |
1547 octave_idx_type len = fcn_files.length (); | |
1548 | |
1549 for (octave_idx_type i = 0; i < len; i++) | |
1550 { | |
1551 std::string fname = fcn_files[i]; | |
1552 | |
1553 std::string ext; | |
1554 std::string base = fname; | |
1555 | |
1556 size_t pos = fname.rfind ('.'); | |
1557 | |
8021 | 1558 if (pos != std::string::npos) |
5832 | 1559 { |
1560 base = fname.substr (0, pos); | |
1561 ext = fname.substr (pos); | |
1562 } | |
1563 | |
7336 | 1564 file_info_list_type& file_info_list = fcn_map[base]; |
5832 | 1565 |
1566 file_info_list_iterator p = file_info_list.begin (); | |
1567 | |
1568 while (p != file_info_list.end ()) | |
1569 { | |
1570 if (p->dir_name == dir_name) | |
1571 break; | |
1572 | |
1573 p++; | |
1574 } | |
1575 | |
1576 int t = 0; | |
1577 if (ext == ".m") | |
1578 t = load_path::M_FILE; | |
1579 else if (ext == ".oct") | |
1580 t = load_path::OCT_FILE; | |
5864 | 1581 else if (ext == ".mex") |
1582 t = load_path::MEX_FILE; | |
5832 | 1583 |
1584 if (p == file_info_list.end ()) | |
1585 { | |
1586 file_info fi (dir_name, t); | |
1587 | |
1588 if (at_end) | |
1589 file_info_list.push_back (fi); | |
1590 else | |
1591 file_info_list.push_front (fi); | |
1592 } | |
1593 else | |
1594 { | |
1595 file_info& fi = *p; | |
1596 | |
1597 fi.types |= t; | |
1598 } | |
1599 } | |
1600 } | |
1601 | |
7336 | 1602 void |
1603 load_path::add_to_private_fcn_map (const dir_info& di) const | |
1604 { | |
1605 dir_info::fcn_file_map_type private_file_map = di.private_file_map; | |
1606 | |
1607 if (! private_file_map.empty ()) | |
1608 private_fcn_map[di.dir_name] = private_file_map; | |
1609 } | |
1610 | |
1611 void | |
1612 load_path::add_to_method_map (const dir_info& di, bool at_end) const | |
1613 { | |
1614 std::string dir_name = di.dir_name; | |
1615 | |
7971
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1616 // <CLASS_NAME, CLASS_INFO> |
7336 | 1617 dir_info::method_file_map_type method_file_map = di.method_file_map; |
1618 | |
1619 for (dir_info::const_method_file_map_iterator q = method_file_map.begin (); | |
1620 q != method_file_map.end (); | |
1621 q++) | |
1622 { | |
1623 std::string class_name = q->first; | |
1624 | |
1625 fcn_map_type& fm = method_map[class_name]; | |
1626 | |
1627 std::string full_dir_name | |
1628 = file_ops::concat (dir_name, "@" + class_name); | |
1629 | |
7971
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1630 const dir_info::class_info& ci = q->second; |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1631 |
7336 | 1632 // <FCN_NAME, TYPES> |
7971
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1633 const dir_info::fcn_file_map_type& m = ci.method_file_map; |
7336 | 1634 |
1635 for (dir_info::const_fcn_file_map_iterator p = m.begin (); | |
1636 p != m.end (); | |
1637 p++) | |
1638 { | |
1639 std::string base = p->first; | |
1640 | |
1641 int types = p->second; | |
1642 | |
1643 file_info_list_type& file_info_list = fm[base]; | |
1644 | |
1645 file_info_list_iterator p2 = file_info_list.begin (); | |
1646 | |
1647 while (p2 != file_info_list.end ()) | |
1648 { | |
1649 if (p2->dir_name == full_dir_name) | |
1650 break; | |
1651 | |
1652 p2++; | |
1653 } | |
1654 | |
1655 if (p2 == file_info_list.end ()) | |
1656 { | |
1657 file_info fi (full_dir_name, types); | |
1658 | |
1659 if (at_end) | |
1660 file_info_list.push_back (fi); | |
1661 else | |
1662 file_info_list.push_front (fi); | |
1663 } | |
1664 else | |
1665 { | |
1666 // FIXME -- is this possible? | |
1667 | |
1668 file_info& fi = *p2; | |
1669 | |
1670 fi.types = types; | |
1671 } | |
1672 } | |
7971
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1673 |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1674 // <FCN_NAME, TYPES> |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1675 dir_info::fcn_file_map_type private_file_map = ci.private_file_map; |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1676 |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1677 if (! private_file_map.empty ()) |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1678 private_fcn_map[full_dir_name] = private_file_map; |
7336 | 1679 } |
1680 } | |
1681 | |
5832 | 1682 std::string |
1683 genpath (const std::string& dirname, const string_vector& skip) | |
1684 { | |
1685 std::string retval; | |
1686 | |
5871 | 1687 dir_entry dir (dirname); |
5832 | 1688 |
1689 if (dir) | |
1690 { | |
1691 retval = dirname; | |
1692 | |
1693 string_vector dirlist = dir.read (); | |
1694 | |
1695 octave_idx_type len = dirlist.length (); | |
1696 | |
1697 for (octave_idx_type i = 0; i < len; i++) | |
1698 { | |
1699 std::string elt = dirlist[i]; | |
1700 | |
1701 // FIXME -- the caller should be able to specify the list of | |
7336 | 1702 // directories to skip in addition to ".", "..", and |
1703 // directories beginning with "@". | |
5832 | 1704 |
7336 | 1705 bool skip_p = (elt == "." || elt == ".." || elt[0] == '@'); |
5832 | 1706 |
1707 if (! skip_p) | |
1708 { | |
1709 for (octave_idx_type j = 0; j < skip.length (); j++) | |
1710 { | |
1711 skip_p = (elt == skip[j]); | |
1712 if (skip_p) | |
1713 break; | |
1714 } | |
1715 | |
1716 if (! skip_p) | |
1717 { | |
7272 | 1718 std::string nm = file_ops::concat (dirname, elt); |
5832 | 1719 |
1720 file_stat fs (nm); | |
1721 | |
1722 if (fs && fs.is_dir ()) | |
8008
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
1723 retval += dir_path::path_sep_str () + genpath (nm); |
5832 | 1724 } |
1725 } | |
1726 } | |
1727 } | |
1728 | |
1729 return retval; | |
1730 } | |
1731 | |
1732 static void | |
1733 execute_pkg_add_or_del (const std::string& dir, | |
1734 const std::string& script_file) | |
1735 { | |
1736 if (! octave_interpreter_ready) | |
1737 return; | |
1738 | |
1739 unwind_protect::begin_frame ("execute_pkg_add_or_del"); | |
1740 | |
1741 unwind_protect_bool (input_from_startup_file); | |
1742 | |
1743 input_from_startup_file = true; | |
1744 | |
7272 | 1745 std::string file = file_ops::concat (dir, script_file); |
5832 | 1746 |
5978 | 1747 file_stat fs (file); |
5832 | 1748 |
1749 if (fs.exists ()) | |
5975 | 1750 source_file (file, "base"); |
5832 | 1751 |
1752 unwind_protect::run_frame ("execute_pkg_add_or_del"); | |
1753 } | |
1754 | |
1755 void | |
1756 execute_pkg_add (const std::string& dir) | |
1757 { | |
1758 execute_pkg_add_or_del (dir, "PKG_ADD"); | |
1759 } | |
1760 | |
1761 void | |
1762 execute_pkg_del (const std::string& dir) | |
1763 { | |
1764 execute_pkg_add_or_del (dir, "PKG_DEL"); | |
1765 } | |
1766 | |
1767 DEFUN (genpath, args, , | |
1768 "-*- texinfo -*-\n\ | |
1769 @deftypefn {Built-in Function} {} genpath (@var{dir})\n\ | |
8325
b93ac0586e4b
spelling corrections
Brian Gough<bjg@network-theory.co.uk>
parents:
8141
diff
changeset
|
1770 Return a path constructed from @var{dir} and all its subdirectories.\n\ |
5832 | 1771 @end deftypefn") |
1772 { | |
1773 octave_value retval; | |
1774 | |
1775 if (args.length () == 1) | |
1776 { | |
1777 std::string dirname = args(0).string_value (); | |
1778 | |
1779 if (! error_state) | |
1780 retval = genpath (dirname); | |
1781 else | |
1782 error ("genpath: expecting argument to be a character string"); | |
1783 } | |
1784 else | |
1785 print_usage (); | |
1786 | |
1787 return retval; | |
1788 } | |
1789 | |
1790 DEFUN (rehash, , , | |
1791 "-*- texinfo -*-\n\ | |
1792 @deftypefn {Built-in Function} {} rehash ()\n\ | |
6644 | 1793 Reinitialize Octave's load path directory cache.\n\ |
5832 | 1794 @end deftypefn") |
1795 { | |
1796 octave_value_list retval; | |
1797 | |
1798 load_path::update (); | |
1799 | |
1800 // FIXME -- maybe we should rename this variable since it is being | |
1801 // used for more than keeping track of the prompt time. | |
1802 | |
1803 // This will force updated functions to be found. | |
1804 Vlast_prompt_time.stamp (); | |
1805 | |
1806 return retval; | |
1807 } | |
1808 | |
8609
fcf762ba66cf
load-path.cc (Fcommand_line_path): rename from Fcommandlinepath
John W. Eaton <jwe@octave.org>
parents:
8593
diff
changeset
|
1809 DEFUN (command_line_path, , , |
8586
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
8511
diff
changeset
|
1810 "-*- texinfo -*-\n\ |
8609
fcf762ba66cf
load-path.cc (Fcommand_line_path): rename from Fcommandlinepath
John W. Eaton <jwe@octave.org>
parents:
8593
diff
changeset
|
1811 @deftypefn {Built-in Function} {} command_line_path (@dots{})\n\ |
8586
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
8511
diff
changeset
|
1812 Return the command line path variable.\n\ |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
8511
diff
changeset
|
1813 \n\ |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
8511
diff
changeset
|
1814 @seealso{path, addpath, rmpath, genpath, pathdef, savepath, pathsep}\n\ |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
8511
diff
changeset
|
1815 @end deftypefn") |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
8511
diff
changeset
|
1816 { |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
8511
diff
changeset
|
1817 return octave_value (load_path::get_command_line_path ()); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
8511
diff
changeset
|
1818 } |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
8511
diff
changeset
|
1819 |
7391 | 1820 DEFUN (restoredefaultpath, , , |
1821 "-*- texinfo -*-\n\ | |
1822 @deftypefn {Built-in Function} {} restoredefaultpath (@dots{})\n\ | |
1823 Restore Octave's path to it's initial state at startup.\n\ | |
1824 \n\ | |
1825 @seealso{path, addpath, rmpath, genpath, pathdef, savepath, pathsep}\n\ | |
1826 @end deftypefn") | |
1827 { | |
1828 load_path::initialize (true); | |
1829 | |
1830 return octave_value (load_path::system_path ()); | |
1831 } | |
1832 | |
8812
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
8746
diff
changeset
|
1833 // Return Octave's original default list of directories in which to |
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
8746
diff
changeset
|
1834 // search for function files. This corresponds to the path that |
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
8746
diff
changeset
|
1835 // exists prior to running the system's octaverc file or the user's |
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
8746
diff
changeset
|
1836 // ~/.octaverc file |
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
8746
diff
changeset
|
1837 |
7391 | 1838 DEFUN (__pathorig__, , , |
5832 | 1839 "-*- texinfo -*-\n\ |
7391 | 1840 @deftypefn {Built-in Function} {@var{val} =} __pathorig__ ()\n\ |
8812
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
8746
diff
changeset
|
1841 Undocumented internal function.\n\ |
5832 | 1842 @end deftypefn") |
1843 { | |
6630 | 1844 return octave_value (load_path::system_path ()); |
5832 | 1845 } |
1846 | |
1847 DEFUN (path, args, nargout, | |
1848 "-*- texinfo -*-\n\ | |
6678 | 1849 @deftypefn {Built-in Function} {} path (@dots{})\n\ |
6644 | 1850 Modify or display Octave's load path.\n\ |
5832 | 1851 \n\ |
1852 If @var{nargin} and @var{nargout} are zero, display the elements of\n\ | |
6644 | 1853 Octave's load path in an easy to read format.\n\ |
5832 | 1854 \n\ |
1855 If @var{nargin} is zero and nargout is greater than zero, return the\n\ | |
6644 | 1856 current load path.\n\ |
5832 | 1857 \n\ |
1858 If @var{nargin} is greater than zero, concatenate the arguments,\n\ | |
1859 separating them with @code{pathsep()}. Set the internal search path\n\ | |
1860 to the result and return it.\n\ | |
1861 \n\ | |
1862 No checks are made for duplicate elements.\n\ | |
1863 @seealso{addpath, rmpath, genpath, pathdef, savepath, pathsep}\n\ | |
1864 @end deftypefn") | |
1865 { | |
1866 octave_value retval; | |
1867 | |
1868 int argc = args.length () + 1; | |
1869 | |
1870 string_vector argv = args.make_argv ("path"); | |
1871 | |
1872 if (! error_state) | |
1873 { | |
1874 if (argc > 1) | |
1875 { | |
1876 std::string path = argv[1]; | |
1877 | |
1878 for (int i = 2; i < argc; i++) | |
8008
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
1879 path += dir_path::path_sep_str () + argv[i]; |
5832 | 1880 |
5867 | 1881 load_path::set (path, true); |
5832 | 1882 } |
1883 | |
1884 if (nargout > 0) | |
1885 retval = load_path::path (); | |
1886 else if (argc == 1 && nargout == 0) | |
1887 { | |
1888 octave_stdout << "\nOctave's search path contains the following directories:\n\n"; | |
1889 | |
1890 string_vector dirs = load_path::dirs (); | |
1891 | |
1892 dirs.list_in_columns (octave_stdout); | |
1893 | |
1894 octave_stdout << "\n"; | |
1895 } | |
1896 } | |
1897 | |
1898 return retval; | |
1899 } | |
1900 | |
8746
5dd06f19e9be
handle commands in the lexer
John W. Eaton <jwe@octave.org>
parents:
8704
diff
changeset
|
1901 DEFUN (addpath, args, nargout, |
5832 | 1902 "-*- texinfo -*-\n\ |
6678 | 1903 @deftypefn {Built-in Function} {} addpath (@var{dir1}, @dots{})\n\ |
1904 @deftypefnx {Built-in Function} {} addpath (@var{dir1}, @dots{}, @var{option})\n\ | |
5832 | 1905 Add @var{dir1}, @dots{} to the current function search path. If\n\ |
1906 @var{option} is @samp{\"-begin\"} or 0 (the default), prepend the\n\ | |
1907 directory name to the current path. If @var{option} is @samp{\"-end\"}\n\ | |
1908 or 1, append the directory name to the current path.\n\ | |
1909 Directories added to the path must exist.\n\ | |
1910 @seealso{path, rmpath, genpath, pathdef, savepath, pathsep}\n\ | |
1911 @end deftypefn") | |
1912 { | |
1913 octave_value retval; | |
1914 | |
1915 // Originally written by Bill Denney and Etienne Grossman. Heavily | |
1916 // modified and translated to C++ by jwe. | |
1917 | |
1918 if (nargout > 0) | |
1919 retval = load_path::path (); | |
1920 | |
1921 int nargin = args.length (); | |
1922 | |
1923 if (nargin > 0) | |
1924 { | |
1925 bool append = false; | |
1926 | |
1927 octave_value option_arg = args(nargin-1); | |
1928 | |
1929 if (option_arg.is_string ()) | |
1930 { | |
1931 std::string option = option_arg.string_value (); | |
1932 | |
1933 if (option == "-end") | |
1934 { | |
1935 append = true; | |
1936 nargin--; | |
1937 } | |
1938 else if (option == "-begin") | |
1939 nargin--; | |
1940 } | |
1941 else if (option_arg.is_numeric_type ()) | |
1942 { | |
1943 int val = option_arg.int_value (); | |
1944 | |
1945 if (! error_state) | |
1946 { | |
1947 if (val == 0) | |
1948 append = false; | |
1949 else if (val == 1) | |
1950 append = true; | |
1951 else | |
1952 { | |
1953 error ("addpath: expecting final argument to be 1 or 0"); | |
1954 return retval; | |
1955 } | |
1956 } | |
1957 else | |
1958 { | |
1959 error ("addpath: expecting final argument to be 1 or 0"); | |
1960 return retval; | |
1961 } | |
1962 } | |
1963 | |
1964 for (int i = 0; i < nargin; i++) | |
1965 { | |
1966 std::string arg = args(i).string_value (); | |
1967 | |
1968 if (! error_state) | |
1969 { | |
1970 std::list<std::string> dir_elts = split_path (arg); | |
1971 | |
1972 for (std::list<std::string>::const_iterator p = dir_elts.begin (); | |
1973 p != dir_elts.end (); | |
1974 p++) | |
1975 { | |
1976 std::string dir = *p; | |
1977 | |
1978 //dir = regexprep (dir_elts{j}, "//+", "/"); | |
1979 //dir = regexprep (dir, "/$", ""); | |
1980 | |
5867 | 1981 if (append) |
1982 load_path::append (dir, true); | |
5832 | 1983 else |
5867 | 1984 load_path::prepend (dir, true); |
5832 | 1985 } |
1986 } | |
1987 else | |
1988 error ("addpath: expecting all args to be character strings"); | |
1989 } | |
1990 } | |
1991 else | |
1992 print_usage (); | |
1993 | |
1994 return retval; | |
1995 } | |
1996 | |
8746
5dd06f19e9be
handle commands in the lexer
John W. Eaton <jwe@octave.org>
parents:
8704
diff
changeset
|
1997 DEFUN (rmpath, args, nargout, |
5832 | 1998 "-*- texinfo -*-\n\ |
6678 | 1999 @deftypefn {Built-in Function} {} rmpath (@var{dir1}, @dots{})\n\ |
5832 | 2000 Remove @var{dir1}, @dots{} from the current function search path.\n\ |
2001 \n\ | |
2002 @seealso{path, addpath, genpath, pathdef, savepath, pathsep}\n\ | |
2003 @end deftypefn") | |
2004 { | |
2005 // Originally by Etienne Grossmann. Heavily modified and translated | |
2006 // to C++ by jwe. | |
2007 | |
2008 octave_value retval; | |
2009 | |
2010 if (nargout > 0) | |
2011 retval = load_path::path (); | |
2012 | |
2013 int nargin = args.length (); | |
2014 | |
2015 if (nargin > 0) | |
2016 { | |
2017 for (int i = 0; i < nargin; i++) | |
2018 { | |
2019 std::string arg = args(i).string_value (); | |
2020 | |
2021 if (! error_state) | |
2022 { | |
2023 std::list<std::string> dir_elts = split_path (arg); | |
2024 | |
2025 for (std::list<std::string>::const_iterator p = dir_elts.begin (); | |
2026 p != dir_elts.end (); | |
2027 p++) | |
2028 { | |
2029 std::string dir = *p; | |
2030 | |
2031 //dir = regexprep (dir_elts{j}, "//+", "/"); | |
2032 //dir = regexprep (dir, "/$", ""); | |
2033 | |
2034 if (! load_path::remove (dir)) | |
2035 warning ("rmpath: %s: not found", dir.c_str ()); | |
2036 } | |
2037 } | |
2038 else | |
2039 error ("addpath: expecting all args to be character strings"); | |
2040 } | |
2041 } | |
2042 else | |
2043 print_usage (); | |
2044 | |
2045 return retval; | |
2046 } | |
2047 | |
2048 /* | |
2049 ;;; Local Variables: *** | |
2050 ;;; mode: C++ *** | |
2051 ;;; End: *** | |
2052 */ |