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