Mercurial > hg > octave-nkf
annotate src/load-path.cc @ 8586:31ab3b83bc8a
savepath: Respect cmd-line and env paths.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Sat, 24 Jan 2009 14:53:44 -0500 |
parents | a835406e02dd |
children | 4e39b00218d3 |
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 |
7336 | 906 dir_name = std::string (); |
5832 | 907 |
908 const_fcn_map_iterator p = fcn_map.find (fcn); | |
909 | |
910 if (p != fcn_map.end ()) | |
911 { | |
7336 | 912 const file_info_list_type& file_info_list = p->second; |
5832 | 913 |
914 for (const_file_info_list_iterator i = file_info_list.begin (); | |
915 i != file_info_list.end (); | |
916 i++) | |
917 { | |
918 const file_info& fi = *i; | |
919 | |
7272 | 920 retval = file_ops::concat (fi.dir_name, fcn); |
5832 | 921 |
7336 | 922 if (check_file_type (retval, type, fi.types, |
923 fcn, "load_path::do_find_fcn")) | |
5832 | 924 { |
7336 | 925 dir_name = fi.dir_name; |
926 break; | |
5832 | 927 } |
7336 | 928 else |
929 retval = std::string (); | |
930 } | |
931 } | |
932 | |
933 return retval; | |
934 } | |
935 | |
936 std::string | |
937 load_path::do_find_private_fcn (const std::string& dir, | |
938 const std::string& fcn, int type) const | |
939 { | |
940 std::string retval; | |
941 | |
942 // update (); | |
943 | |
944 const_private_fcn_map_iterator q = private_fcn_map.find (dir); | |
945 | |
946 if (q != private_fcn_map.end ()) | |
947 { | |
948 const dir_info::fcn_file_map_type& m = q->second; | |
949 | |
950 dir_info::const_fcn_file_map_iterator p = m.find (fcn); | |
951 | |
952 if (p != m.end ()) | |
953 { | |
954 std::string fname | |
955 = file_ops::concat (file_ops::concat (dir, "private"), fcn); | |
956 | |
957 if (check_file_type (fname, type, p->second, fcn, | |
958 "load_path::find_private_fcn")) | |
959 retval = fname; | |
960 } | |
961 } | |
962 | |
963 return retval; | |
964 } | |
965 | |
966 std::string | |
967 load_path::do_find_method (const std::string& class_name, | |
968 const std::string& meth, | |
969 std::string& dir_name, int type) const | |
970 { | |
971 std::string retval; | |
972 | |
973 // update (); | |
974 | |
975 dir_name = std::string (); | |
976 | |
977 const_method_map_iterator q = method_map.find (class_name); | |
978 | |
979 if (q != method_map.end ()) | |
980 { | |
981 const fcn_map_type& m = q->second; | |
982 | |
983 const_fcn_map_iterator p = m.find (meth); | |
984 | |
985 if (p != m.end ()) | |
986 { | |
987 const file_info_list_type& file_info_list = p->second; | |
988 | |
989 for (const_file_info_list_iterator i = file_info_list.begin (); | |
990 i != file_info_list.end (); | |
991 i++) | |
5864 | 992 { |
7336 | 993 const file_info& fi = *i; |
994 | |
995 retval = file_ops::concat (fi.dir_name, meth); | |
996 | |
997 bool found = check_file_type (retval, type, fi.types, | |
998 meth, "load_path::do_find_method"); | |
999 | |
1000 if (found) | |
5864 | 1001 { |
7336 | 1002 dir_name = fi.dir_name; |
5832 | 1003 break; |
1004 } | |
7336 | 1005 else |
1006 retval = std::string (); | |
5864 | 1007 } |
5832 | 1008 } |
1009 } | |
1010 | |
1011 return retval; | |
1012 } | |
1013 | |
7336 | 1014 std::list<std::string> |
1015 load_path::do_methods (const std::string& class_name) const | |
1016 { | |
1017 std::list<std::string> retval; | |
1018 | |
1019 // update (); | |
1020 | |
1021 const_method_map_iterator q = method_map.find (class_name); | |
1022 | |
1023 if (q != method_map.end ()) | |
1024 { | |
1025 const fcn_map_type& m = q->second; | |
1026 | |
1027 for (const_fcn_map_iterator p = m.begin (); p != m.end (); p++) | |
1028 retval.push_back (p->first); | |
1029 } | |
1030 | |
1031 if (! retval.empty ()) | |
1032 retval.sort (); | |
1033 | |
1034 return retval; | |
1035 } | |
1036 | |
5832 | 1037 std::string |
1038 load_path::do_find_file (const std::string& file) const | |
1039 { | |
1040 std::string retval; | |
1041 | |
8021 | 1042 if (file.find_first_of (file_ops::dir_sep_chars ()) != std::string::npos) |
5832 | 1043 { |
6838 | 1044 if (octave_env::absolute_pathname (file) |
1045 || octave_env::rooted_relative_pathname (file)) | |
1046 { | |
1047 file_stat fs (file); | |
5832 | 1048 |
6838 | 1049 if (fs.exists ()) |
1050 return file; | |
1051 } | |
1052 else | |
1053 { | |
1054 for (const_dir_info_list_iterator p = dir_info_list.begin (); | |
1055 p != dir_info_list.end (); | |
1056 p++) | |
1057 { | |
7272 | 1058 std::string tfile = file_ops::concat (p->dir_name, file); |
5832 | 1059 |
6838 | 1060 file_stat fs (tfile); |
6159 | 1061 |
6838 | 1062 if (fs.exists ()) |
1063 return tfile; | |
5832 | 1064 } |
1065 } | |
1066 } | |
6838 | 1067 else |
1068 { | |
1069 for (const_dir_info_list_iterator p = dir_info_list.begin (); | |
1070 p != dir_info_list.end (); | |
1071 p++) | |
1072 { | |
1073 string_vector all_files = p->all_files; | |
6159 | 1074 |
6838 | 1075 octave_idx_type len = all_files.length (); |
1076 | |
1077 for (octave_idx_type i = 0; i < len; i++) | |
1078 { | |
1079 if (all_files[i] == file) | |
7272 | 1080 return file_ops::concat (p->dir_name, file); |
6838 | 1081 } |
1082 } | |
1083 } | |
5832 | 1084 |
1085 return retval; | |
1086 } | |
1087 | |
1088 std::string | |
8041
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1089 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
|
1090 { |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1091 std::string retval; |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1092 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1093 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
|
1094 && (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
|
1095 || 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
|
1096 { |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1097 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
|
1098 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1099 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
|
1100 return dir; |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1101 } |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1102 else |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1103 { |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1104 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
|
1105 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
|
1106 p++) |
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 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
|
1109 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1110 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
|
1111 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1112 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
|
1113 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
|
1114 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1115 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
|
1116 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1117 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
|
1118 && 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
|
1119 && 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
|
1120 { |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1121 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
|
1122 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1123 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
|
1124 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
|
1125 } |
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 } |
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 return retval; |
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 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1132 std::string |
5832 | 1133 load_path::do_find_first_of (const string_vector& flist) const |
1134 { | |
1135 std::string retval; | |
1136 | |
1137 std::string dir_name; | |
1138 std::string file_name; | |
1139 | |
1140 octave_idx_type flen = flist.length (); | |
1141 octave_idx_type rel_flen = 0; | |
1142 | |
1143 string_vector rel_flist (flen); | |
1144 | |
1145 for (octave_idx_type i = 0; i < flen; i++) | |
1146 { | |
1147 if (octave_env::absolute_pathname (flist[i])) | |
1148 { | |
1149 file_stat fs (flist[i]); | |
1150 | |
1151 if (fs.exists ()) | |
1152 return flist[i]; | |
1153 } | |
1154 else | |
1155 rel_flist[rel_flen++] = flist[i]; | |
1156 } | |
1157 | |
1158 rel_flist.resize (rel_flen); | |
1159 | |
1160 for (const_dir_info_list_iterator p = dir_info_list.begin (); | |
1161 p != dir_info_list.end (); | |
1162 p++) | |
1163 { | |
1164 string_vector all_files = p->all_files; | |
1165 | |
1166 octave_idx_type len = all_files.length (); | |
1167 | |
1168 for (octave_idx_type i = 0; i < len; i++) | |
1169 { | |
1170 for (octave_idx_type j = 0; j < rel_flen; j++) | |
1171 { | |
1172 if (all_files[i] == rel_flist[j]) | |
1173 { | |
1174 dir_name = p->dir_name; | |
1175 file_name = rel_flist[j]; | |
6159 | 1176 |
1177 goto done; | |
5832 | 1178 } |
1179 } | |
1180 } | |
1181 } | |
1182 | |
6159 | 1183 done: |
1184 | |
5832 | 1185 if (! dir_name.empty ()) |
7272 | 1186 retval = file_ops::concat (dir_name, file_name); |
5832 | 1187 |
1188 return retval; | |
1189 } | |
1190 | |
1191 string_vector | |
1192 load_path::do_find_all_first_of (const string_vector& flist) const | |
1193 { | |
1194 std::list<std::string> retlist; | |
1195 | |
1196 std::string dir_name; | |
1197 std::string file_name; | |
1198 | |
1199 octave_idx_type flen = flist.length (); | |
1200 octave_idx_type rel_flen = 0; | |
1201 | |
1202 string_vector rel_flist (flen); | |
1203 | |
1204 for (octave_idx_type i = 0; i < flen; i++) | |
1205 { | |
1206 if (octave_env::absolute_pathname (flist[i])) | |
1207 { | |
1208 file_stat fs (flist[i]); | |
1209 | |
1210 if (fs.exists ()) | |
1211 retlist.push_back (flist[i]); | |
1212 } | |
1213 else | |
1214 rel_flist[rel_flen++] = flist[i]; | |
1215 } | |
1216 | |
1217 rel_flist.resize (rel_flen); | |
1218 | |
1219 for (const_dir_info_list_iterator p = dir_info_list.begin (); | |
1220 p != dir_info_list.end (); | |
1221 p++) | |
1222 { | |
1223 string_vector all_files = p->all_files; | |
1224 | |
1225 octave_idx_type len = all_files.length (); | |
1226 | |
1227 for (octave_idx_type i = 0; i < len; i++) | |
1228 { | |
1229 for (octave_idx_type j = 0; j < rel_flen; j++) | |
1230 { | |
1231 if (all_files[i] == rel_flist[j]) | |
1232 retlist.push_back | |
7272 | 1233 (file_ops::concat (p->dir_name, rel_flist[j])); |
5832 | 1234 } |
1235 } | |
1236 } | |
1237 | |
1238 size_t retsize = retlist.size (); | |
1239 | |
1240 string_vector retval (retsize); | |
1241 | |
1242 for (size_t i = 0; i < retsize; i++) | |
1243 { | |
1244 retval[i] = retlist.front (); | |
1245 | |
1246 retlist.pop_front (); | |
1247 } | |
1248 | |
1249 return retval; | |
1250 } | |
1251 | |
1252 string_vector | |
1253 load_path::do_dirs (void) const | |
1254 { | |
1255 size_t len = dir_info_list.size (); | |
1256 | |
1257 string_vector retval (len); | |
1258 | |
1259 octave_idx_type k = 0; | |
1260 | |
1261 for (const_dir_info_list_iterator i = dir_info_list.begin (); | |
1262 i != dir_info_list.end (); | |
1263 i++) | |
1264 retval[k++] = i->dir_name; | |
1265 | |
1266 return retval; | |
1267 } | |
1268 | |
1269 std::list<std::string> | |
1270 load_path::do_dir_list (void) const | |
1271 { | |
1272 std::list<std::string> retval; | |
1273 | |
1274 for (const_dir_info_list_iterator i = dir_info_list.begin (); | |
1275 i != dir_info_list.end (); | |
1276 i++) | |
1277 retval.push_back (i->dir_name); | |
1278 | |
1279 return retval; | |
1280 } | |
1281 | |
1282 string_vector | |
1283 load_path::do_files (const std::string& dir) const | |
1284 { | |
1285 string_vector retval; | |
1286 | |
1287 const_dir_info_list_iterator i = find_dir_info (dir); | |
1288 | |
1289 if (i != dir_info_list.end ()) | |
1290 retval = i->fcn_files; | |
1291 | |
1292 return retval; | |
1293 } | |
1294 | |
1295 string_vector | |
1296 load_path::do_fcn_names (void) const | |
1297 { | |
1298 size_t len = fcn_map.size (); | |
1299 | |
1300 string_vector retval (len); | |
1301 | |
1302 octave_idx_type count = 0; | |
1303 | |
1304 for (const_fcn_map_iterator p = fcn_map.begin (); | |
1305 p != fcn_map.end (); | |
1306 p++) | |
1307 retval[count++] = p->first; | |
1308 | |
1309 return retval; | |
1310 } | |
1311 | |
1312 std::string | |
1313 load_path::do_path (void) const | |
1314 { | |
1315 std::string xpath; | |
1316 | |
1317 string_vector xdirs = load_path::dirs (); | |
1318 | |
1319 octave_idx_type len = xdirs.length (); | |
1320 | |
1321 if (len > 0) | |
1322 xpath = xdirs[0]; | |
1323 | |
1324 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
|
1325 xpath += dir_path::path_sep_str () + xdirs[i]; |
5832 | 1326 |
1327 return xpath; | |
1328 } | |
1329 | |
1330 void | |
7336 | 1331 print_types (std::ostream& os, int types) |
1332 { | |
1333 bool printed_type = false; | |
1334 | |
1335 if (types & load_path::OCT_FILE) | |
1336 { | |
1337 os << "oct"; | |
1338 printed_type = true; | |
1339 } | |
1340 | |
1341 if (types & load_path::MEX_FILE) | |
1342 { | |
1343 if (printed_type) | |
1344 os << "|"; | |
1345 os << "mex"; | |
1346 printed_type = true; | |
1347 } | |
1348 | |
1349 if (types & load_path::M_FILE) | |
1350 { | |
1351 if (printed_type) | |
1352 os << "|"; | |
1353 os << "m"; | |
1354 printed_type = true; | |
1355 } | |
1356 } | |
1357 | |
1358 void | |
1359 print_fcn_list (std::ostream& os, | |
1360 const load_path::dir_info::fcn_file_map_type& lst) | |
1361 { | |
1362 for (load_path::dir_info::const_fcn_file_map_iterator p = lst.begin (); | |
1363 p != lst.end (); | |
1364 p++) | |
1365 { | |
1366 os << " " << p->first << " ("; | |
1367 | |
1368 print_types (os, p->second); | |
1369 | |
1370 os << ")\n"; | |
1371 } | |
1372 } | |
1373 | |
1374 string_vector | |
1375 get_file_list (const load_path::dir_info::fcn_file_map_type& lst) | |
1376 { | |
1377 octave_idx_type n = lst.size (); | |
1378 | |
1379 string_vector retval (n); | |
1380 | |
1381 octave_idx_type count = 0; | |
1382 | |
1383 for (load_path::dir_info::const_fcn_file_map_iterator p = lst.begin (); | |
1384 p != lst.end (); | |
1385 p++) | |
1386 { | |
1387 std::string nm = p->first; | |
1388 | |
1389 int types = p->second; | |
1390 | |
1391 if (types & load_path::OCT_FILE) | |
1392 nm += ".oct"; | |
1393 else if (types & load_path::MEX_FILE) | |
1394 nm += ".mex"; | |
1395 else | |
1396 nm += ".m"; | |
1397 | |
1398 retval[count++] = nm; | |
1399 } | |
1400 | |
1401 return retval; | |
1402 } | |
1403 | |
1404 void | |
5832 | 1405 load_path::do_display (std::ostream& os) const |
1406 { | |
1407 for (const_dir_info_list_iterator i = dir_info_list.begin (); | |
1408 i != dir_info_list.end (); | |
1409 i++) | |
1410 { | |
1411 string_vector fcn_files = i->fcn_files; | |
1412 | |
1413 if (! fcn_files.empty ()) | |
1414 { | |
1415 os << "\n*** function files in " << i->dir_name << ":\n\n"; | |
1416 | |
1417 fcn_files.list_in_columns (os); | |
1418 } | |
1419 | |
7336 | 1420 const dir_info::method_file_map_type& method_file_map |
1421 = i->method_file_map; | |
5832 | 1422 |
7336 | 1423 if (! method_file_map.empty ()) |
5832 | 1424 { |
7336 | 1425 for (dir_info::const_method_file_map_iterator p = method_file_map.begin (); |
1426 p != method_file_map.end (); | |
5832 | 1427 p++) |
1428 { | |
7336 | 1429 os << "\n*** methods in " << i->dir_name |
1430 << "/@" << p->first << ":\n\n"; | |
5832 | 1431 |
7971
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1432 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
|
1433 |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1434 string_vector method_files = get_file_list (ci.method_file_map); |
5832 | 1435 |
7336 | 1436 method_files.list_in_columns (os); |
1437 } | |
1438 } | |
1439 } | |
5864 | 1440 |
7336 | 1441 for (const_private_fcn_map_iterator i = private_fcn_map.begin (); |
1442 i != private_fcn_map.end (); i++) | |
1443 { | |
1444 os << "\n*** private functions in " | |
1445 << file_ops::concat (i->first, "private") << ":\n\n"; | |
5832 | 1446 |
7336 | 1447 print_fcn_list (os, i->second); |
5832 | 1448 } |
1449 | |
1450 #if defined (DEBUG_LOAD_PATH) | |
1451 | |
1452 for (const_fcn_map_iterator i = fcn_map.begin (); | |
1453 i != fcn_map.end (); | |
1454 i++) | |
1455 { | |
1456 os << i->first << ":\n"; | |
1457 | |
7336 | 1458 const file_info_list_type& file_info_list = i->second; |
5832 | 1459 |
1460 for (const_file_info_list_iterator p = file_info_list.begin (); | |
1461 p != file_info_list.end (); | |
1462 p++) | |
1463 { | |
1464 os << " " << p->dir_name << " ("; | |
1465 | |
7336 | 1466 print_types (os, p->types); |
5832 | 1467 |
1468 os << ")\n"; | |
1469 } | |
1470 } | |
1471 | |
7336 | 1472 for (const_method_map_iterator i = method_map.begin (); |
1473 i != method_map.end (); | |
1474 i++) | |
1475 { | |
1476 os << "CLASS " << i->first << ":\n"; | |
1477 | |
1478 const fcn_map_type& fm = i->second; | |
1479 | |
1480 for (const_fcn_map_iterator q = fm.begin (); | |
1481 q != fm.end (); | |
1482 q++) | |
1483 { | |
1484 os << " " << q->first << ":\n"; | |
1485 | |
1486 const file_info_list_type& file_info_list = q->second; | |
1487 | |
1488 for (const_file_info_list_iterator p = file_info_list.begin (); | |
1489 p != file_info_list.end (); | |
1490 p++) | |
1491 { | |
1492 os << " " << p->dir_name << " ("; | |
1493 | |
1494 print_types (os, p->types); | |
1495 | |
1496 os << ")\n"; | |
1497 } | |
1498 } | |
1499 } | |
1500 | |
5832 | 1501 os << "\n"; |
1502 | |
1503 #endif | |
1504 } | |
1505 | |
1506 void | |
1507 load_path::add_to_fcn_map (const dir_info& di, bool at_end) const | |
1508 { | |
1509 std::string dir_name = di.dir_name; | |
1510 | |
1511 string_vector fcn_files = di.fcn_files; | |
1512 | |
1513 octave_idx_type len = fcn_files.length (); | |
1514 | |
1515 for (octave_idx_type i = 0; i < len; i++) | |
1516 { | |
1517 std::string fname = fcn_files[i]; | |
1518 | |
1519 std::string ext; | |
1520 std::string base = fname; | |
1521 | |
1522 size_t pos = fname.rfind ('.'); | |
1523 | |
8021 | 1524 if (pos != std::string::npos) |
5832 | 1525 { |
1526 base = fname.substr (0, pos); | |
1527 ext = fname.substr (pos); | |
1528 } | |
1529 | |
7336 | 1530 file_info_list_type& file_info_list = fcn_map[base]; |
5832 | 1531 |
1532 file_info_list_iterator p = file_info_list.begin (); | |
1533 | |
1534 while (p != file_info_list.end ()) | |
1535 { | |
1536 if (p->dir_name == dir_name) | |
1537 break; | |
1538 | |
1539 p++; | |
1540 } | |
1541 | |
1542 int t = 0; | |
1543 if (ext == ".m") | |
1544 t = load_path::M_FILE; | |
1545 else if (ext == ".oct") | |
1546 t = load_path::OCT_FILE; | |
5864 | 1547 else if (ext == ".mex") |
1548 t = load_path::MEX_FILE; | |
5832 | 1549 |
1550 if (p == file_info_list.end ()) | |
1551 { | |
1552 file_info fi (dir_name, t); | |
1553 | |
1554 if (at_end) | |
1555 file_info_list.push_back (fi); | |
1556 else | |
1557 file_info_list.push_front (fi); | |
1558 } | |
1559 else | |
1560 { | |
1561 file_info& fi = *p; | |
1562 | |
1563 fi.types |= t; | |
1564 } | |
1565 } | |
1566 } | |
1567 | |
7336 | 1568 void |
1569 load_path::add_to_private_fcn_map (const dir_info& di) const | |
1570 { | |
1571 dir_info::fcn_file_map_type private_file_map = di.private_file_map; | |
1572 | |
1573 if (! private_file_map.empty ()) | |
1574 private_fcn_map[di.dir_name] = private_file_map; | |
1575 } | |
1576 | |
1577 void | |
1578 load_path::add_to_method_map (const dir_info& di, bool at_end) const | |
1579 { | |
1580 std::string dir_name = di.dir_name; | |
1581 | |
7971
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1582 // <CLASS_NAME, CLASS_INFO> |
7336 | 1583 dir_info::method_file_map_type method_file_map = di.method_file_map; |
1584 | |
1585 for (dir_info::const_method_file_map_iterator q = method_file_map.begin (); | |
1586 q != method_file_map.end (); | |
1587 q++) | |
1588 { | |
1589 std::string class_name = q->first; | |
1590 | |
1591 fcn_map_type& fm = method_map[class_name]; | |
1592 | |
1593 std::string full_dir_name | |
1594 = file_ops::concat (dir_name, "@" + class_name); | |
1595 | |
7971
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1596 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
|
1597 |
7336 | 1598 // <FCN_NAME, TYPES> |
7971
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1599 const dir_info::fcn_file_map_type& m = ci.method_file_map; |
7336 | 1600 |
1601 for (dir_info::const_fcn_file_map_iterator p = m.begin (); | |
1602 p != m.end (); | |
1603 p++) | |
1604 { | |
1605 std::string base = p->first; | |
1606 | |
1607 int types = p->second; | |
1608 | |
1609 file_info_list_type& file_info_list = fm[base]; | |
1610 | |
1611 file_info_list_iterator p2 = file_info_list.begin (); | |
1612 | |
1613 while (p2 != file_info_list.end ()) | |
1614 { | |
1615 if (p2->dir_name == full_dir_name) | |
1616 break; | |
1617 | |
1618 p2++; | |
1619 } | |
1620 | |
1621 if (p2 == file_info_list.end ()) | |
1622 { | |
1623 file_info fi (full_dir_name, types); | |
1624 | |
1625 if (at_end) | |
1626 file_info_list.push_back (fi); | |
1627 else | |
1628 file_info_list.push_front (fi); | |
1629 } | |
1630 else | |
1631 { | |
1632 // FIXME -- is this possible? | |
1633 | |
1634 file_info& fi = *p2; | |
1635 | |
1636 fi.types = types; | |
1637 } | |
1638 } | |
7971
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1639 |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1640 // <FCN_NAME, TYPES> |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1641 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
|
1642 |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1643 if (! private_file_map.empty ()) |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7786
diff
changeset
|
1644 private_fcn_map[full_dir_name] = private_file_map; |
7336 | 1645 } |
1646 } | |
1647 | |
5832 | 1648 std::string |
1649 genpath (const std::string& dirname, const string_vector& skip) | |
1650 { | |
1651 std::string retval; | |
1652 | |
5871 | 1653 dir_entry dir (dirname); |
5832 | 1654 |
1655 if (dir) | |
1656 { | |
1657 retval = dirname; | |
1658 | |
1659 string_vector dirlist = dir.read (); | |
1660 | |
1661 octave_idx_type len = dirlist.length (); | |
1662 | |
1663 for (octave_idx_type i = 0; i < len; i++) | |
1664 { | |
1665 std::string elt = dirlist[i]; | |
1666 | |
1667 // FIXME -- the caller should be able to specify the list of | |
7336 | 1668 // directories to skip in addition to ".", "..", and |
1669 // directories beginning with "@". | |
5832 | 1670 |
7336 | 1671 bool skip_p = (elt == "." || elt == ".." || elt[0] == '@'); |
5832 | 1672 |
1673 if (! skip_p) | |
1674 { | |
1675 for (octave_idx_type j = 0; j < skip.length (); j++) | |
1676 { | |
1677 skip_p = (elt == skip[j]); | |
1678 if (skip_p) | |
1679 break; | |
1680 } | |
1681 | |
1682 if (! skip_p) | |
1683 { | |
7272 | 1684 std::string nm = file_ops::concat (dirname, elt); |
5832 | 1685 |
1686 file_stat fs (nm); | |
1687 | |
1688 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
|
1689 retval += dir_path::path_sep_str () + genpath (nm); |
5832 | 1690 } |
1691 } | |
1692 } | |
1693 } | |
1694 | |
1695 return retval; | |
1696 } | |
1697 | |
1698 static void | |
1699 execute_pkg_add_or_del (const std::string& dir, | |
1700 const std::string& script_file) | |
1701 { | |
1702 if (! octave_interpreter_ready) | |
1703 return; | |
1704 | |
1705 unwind_protect::begin_frame ("execute_pkg_add_or_del"); | |
1706 | |
1707 unwind_protect_bool (input_from_startup_file); | |
1708 | |
1709 input_from_startup_file = true; | |
1710 | |
7272 | 1711 std::string file = file_ops::concat (dir, script_file); |
5832 | 1712 |
5978 | 1713 file_stat fs (file); |
5832 | 1714 |
1715 if (fs.exists ()) | |
5975 | 1716 source_file (file, "base"); |
5832 | 1717 |
1718 unwind_protect::run_frame ("execute_pkg_add_or_del"); | |
1719 } | |
1720 | |
1721 void | |
1722 execute_pkg_add (const std::string& dir) | |
1723 { | |
1724 execute_pkg_add_or_del (dir, "PKG_ADD"); | |
1725 } | |
1726 | |
1727 void | |
1728 execute_pkg_del (const std::string& dir) | |
1729 { | |
1730 execute_pkg_add_or_del (dir, "PKG_DEL"); | |
1731 } | |
1732 | |
1733 DEFUN (genpath, args, , | |
1734 "-*- texinfo -*-\n\ | |
1735 @deftypefn {Built-in Function} {} genpath (@var{dir})\n\ | |
8325
b93ac0586e4b
spelling corrections
Brian Gough<bjg@network-theory.co.uk>
parents:
8141
diff
changeset
|
1736 Return a path constructed from @var{dir} and all its subdirectories.\n\ |
5832 | 1737 @end deftypefn") |
1738 { | |
1739 octave_value retval; | |
1740 | |
1741 if (args.length () == 1) | |
1742 { | |
1743 std::string dirname = args(0).string_value (); | |
1744 | |
1745 if (! error_state) | |
1746 retval = genpath (dirname); | |
1747 else | |
1748 error ("genpath: expecting argument to be a character string"); | |
1749 } | |
1750 else | |
1751 print_usage (); | |
1752 | |
1753 return retval; | |
1754 } | |
1755 | |
1756 DEFUN (rehash, , , | |
1757 "-*- texinfo -*-\n\ | |
1758 @deftypefn {Built-in Function} {} rehash ()\n\ | |
6644 | 1759 Reinitialize Octave's load path directory cache.\n\ |
5832 | 1760 @end deftypefn") |
1761 { | |
1762 octave_value_list retval; | |
1763 | |
1764 load_path::update (); | |
1765 | |
1766 // FIXME -- maybe we should rename this variable since it is being | |
1767 // used for more than keeping track of the prompt time. | |
1768 | |
1769 // This will force updated functions to be found. | |
1770 Vlast_prompt_time.stamp (); | |
1771 | |
1772 return retval; | |
1773 } | |
1774 | |
8586
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
8511
diff
changeset
|
1775 DEFUN (commandlinepath, , , |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
8511
diff
changeset
|
1776 "-*- texinfo -*-\n\ |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
8511
diff
changeset
|
1777 @deftypefn {Built-in Function} {} commandlinepath (@dots{})\n\ |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
8511
diff
changeset
|
1778 Return the command line path variable.\n\ |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
8511
diff
changeset
|
1779 \n\ |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
8511
diff
changeset
|
1780 @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
|
1781 @end deftypefn") |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
8511
diff
changeset
|
1782 { |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
8511
diff
changeset
|
1783 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
|
1784 } |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
8511
diff
changeset
|
1785 |
7391 | 1786 DEFUN (restoredefaultpath, , , |
1787 "-*- texinfo -*-\n\ | |
1788 @deftypefn {Built-in Function} {} restoredefaultpath (@dots{})\n\ | |
1789 Restore Octave's path to it's initial state at startup.\n\ | |
1790 \n\ | |
1791 @seealso{path, addpath, rmpath, genpath, pathdef, savepath, pathsep}\n\ | |
1792 @end deftypefn") | |
1793 { | |
1794 load_path::initialize (true); | |
1795 | |
1796 return octave_value (load_path::system_path ()); | |
1797 } | |
1798 | |
1799 DEFUN (__pathorig__, , , | |
5832 | 1800 "-*- texinfo -*-\n\ |
7391 | 1801 @deftypefn {Built-in Function} {@var{val} =} __pathorig__ ()\n\ |
1802 Return Octave's original default list of directories in which to search\n\ | |
1803 for function files. This corresponds to the path that exists prior to\n\ | |
1804 running the system's @file{octaverc}, or the users' @file{~/.octaverc}\n\ | |
1805 @seealso{path, addpath, rmpath, genpath, savepath, pathsep, \n\ | |
1806 restoredefaultpath}\n\ | |
5832 | 1807 @end deftypefn") |
1808 { | |
6630 | 1809 return octave_value (load_path::system_path ()); |
5832 | 1810 } |
1811 | |
1812 DEFUN (path, args, nargout, | |
1813 "-*- texinfo -*-\n\ | |
6678 | 1814 @deftypefn {Built-in Function} {} path (@dots{})\n\ |
6644 | 1815 Modify or display Octave's load path.\n\ |
5832 | 1816 \n\ |
1817 If @var{nargin} and @var{nargout} are zero, display the elements of\n\ | |
6644 | 1818 Octave's load path in an easy to read format.\n\ |
5832 | 1819 \n\ |
1820 If @var{nargin} is zero and nargout is greater than zero, return the\n\ | |
6644 | 1821 current load path.\n\ |
5832 | 1822 \n\ |
1823 If @var{nargin} is greater than zero, concatenate the arguments,\n\ | |
1824 separating them with @code{pathsep()}. Set the internal search path\n\ | |
1825 to the result and return it.\n\ | |
1826 \n\ | |
1827 No checks are made for duplicate elements.\n\ | |
1828 @seealso{addpath, rmpath, genpath, pathdef, savepath, pathsep}\n\ | |
1829 @end deftypefn") | |
1830 { | |
1831 octave_value retval; | |
1832 | |
1833 int argc = args.length () + 1; | |
1834 | |
1835 string_vector argv = args.make_argv ("path"); | |
1836 | |
1837 if (! error_state) | |
1838 { | |
1839 if (argc > 1) | |
1840 { | |
1841 std::string path = argv[1]; | |
1842 | |
1843 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
|
1844 path += dir_path::path_sep_str () + argv[i]; |
5832 | 1845 |
5867 | 1846 load_path::set (path, true); |
5832 | 1847 } |
1848 | |
1849 if (nargout > 0) | |
1850 retval = load_path::path (); | |
1851 else if (argc == 1 && nargout == 0) | |
1852 { | |
1853 octave_stdout << "\nOctave's search path contains the following directories:\n\n"; | |
1854 | |
1855 string_vector dirs = load_path::dirs (); | |
1856 | |
1857 dirs.list_in_columns (octave_stdout); | |
1858 | |
1859 octave_stdout << "\n"; | |
1860 } | |
1861 } | |
1862 | |
1863 return retval; | |
1864 } | |
1865 | |
1866 DEFCMD (addpath, args, nargout, | |
1867 "-*- texinfo -*-\n\ | |
6678 | 1868 @deftypefn {Built-in Function} {} addpath (@var{dir1}, @dots{})\n\ |
1869 @deftypefnx {Built-in Function} {} addpath (@var{dir1}, @dots{}, @var{option})\n\ | |
5832 | 1870 Add @var{dir1}, @dots{} to the current function search path. If\n\ |
1871 @var{option} is @samp{\"-begin\"} or 0 (the default), prepend the\n\ | |
1872 directory name to the current path. If @var{option} is @samp{\"-end\"}\n\ | |
1873 or 1, append the directory name to the current path.\n\ | |
1874 Directories added to the path must exist.\n\ | |
1875 @seealso{path, rmpath, genpath, pathdef, savepath, pathsep}\n\ | |
1876 @end deftypefn") | |
1877 { | |
1878 octave_value retval; | |
1879 | |
1880 // Originally written by Bill Denney and Etienne Grossman. Heavily | |
1881 // modified and translated to C++ by jwe. | |
1882 | |
1883 if (nargout > 0) | |
1884 retval = load_path::path (); | |
1885 | |
1886 int nargin = args.length (); | |
1887 | |
1888 if (nargin > 0) | |
1889 { | |
1890 bool append = false; | |
1891 | |
1892 octave_value option_arg = args(nargin-1); | |
1893 | |
1894 if (option_arg.is_string ()) | |
1895 { | |
1896 std::string option = option_arg.string_value (); | |
1897 | |
1898 if (option == "-end") | |
1899 { | |
1900 append = true; | |
1901 nargin--; | |
1902 } | |
1903 else if (option == "-begin") | |
1904 nargin--; | |
1905 } | |
1906 else if (option_arg.is_numeric_type ()) | |
1907 { | |
1908 int val = option_arg.int_value (); | |
1909 | |
1910 if (! error_state) | |
1911 { | |
1912 if (val == 0) | |
1913 append = false; | |
1914 else if (val == 1) | |
1915 append = true; | |
1916 else | |
1917 { | |
1918 error ("addpath: expecting final argument to be 1 or 0"); | |
1919 return retval; | |
1920 } | |
1921 } | |
1922 else | |
1923 { | |
1924 error ("addpath: expecting final argument to be 1 or 0"); | |
1925 return retval; | |
1926 } | |
1927 } | |
1928 | |
1929 for (int i = 0; i < nargin; i++) | |
1930 { | |
1931 std::string arg = args(i).string_value (); | |
1932 | |
1933 if (! error_state) | |
1934 { | |
1935 std::list<std::string> dir_elts = split_path (arg); | |
1936 | |
1937 for (std::list<std::string>::const_iterator p = dir_elts.begin (); | |
1938 p != dir_elts.end (); | |
1939 p++) | |
1940 { | |
1941 std::string dir = *p; | |
1942 | |
1943 //dir = regexprep (dir_elts{j}, "//+", "/"); | |
1944 //dir = regexprep (dir, "/$", ""); | |
1945 | |
5867 | 1946 if (append) |
1947 load_path::append (dir, true); | |
5832 | 1948 else |
5867 | 1949 load_path::prepend (dir, true); |
5832 | 1950 } |
1951 } | |
1952 else | |
1953 error ("addpath: expecting all args to be character strings"); | |
1954 } | |
1955 } | |
1956 else | |
1957 print_usage (); | |
1958 | |
1959 return retval; | |
1960 } | |
1961 | |
1962 DEFCMD (rmpath, args, nargout, | |
1963 "-*- texinfo -*-\n\ | |
6678 | 1964 @deftypefn {Built-in Function} {} rmpath (@var{dir1}, @dots{})\n\ |
5832 | 1965 Remove @var{dir1}, @dots{} from the current function search path.\n\ |
1966 \n\ | |
1967 @seealso{path, addpath, genpath, pathdef, savepath, pathsep}\n\ | |
1968 @end deftypefn") | |
1969 { | |
1970 // Originally by Etienne Grossmann. Heavily modified and translated | |
1971 // to C++ by jwe. | |
1972 | |
1973 octave_value retval; | |
1974 | |
1975 if (nargout > 0) | |
1976 retval = load_path::path (); | |
1977 | |
1978 int nargin = args.length (); | |
1979 | |
1980 if (nargin > 0) | |
1981 { | |
1982 for (int i = 0; i < nargin; i++) | |
1983 { | |
1984 std::string arg = args(i).string_value (); | |
1985 | |
1986 if (! error_state) | |
1987 { | |
1988 std::list<std::string> dir_elts = split_path (arg); | |
1989 | |
1990 for (std::list<std::string>::const_iterator p = dir_elts.begin (); | |
1991 p != dir_elts.end (); | |
1992 p++) | |
1993 { | |
1994 std::string dir = *p; | |
1995 | |
1996 //dir = regexprep (dir_elts{j}, "//+", "/"); | |
1997 //dir = regexprep (dir, "/$", ""); | |
1998 | |
1999 if (! load_path::remove (dir)) | |
2000 warning ("rmpath: %s: not found", dir.c_str ()); | |
2001 } | |
2002 } | |
2003 else | |
2004 error ("addpath: expecting all args to be character strings"); | |
2005 } | |
2006 } | |
2007 else | |
2008 print_usage (); | |
2009 | |
2010 return retval; | |
2011 } | |
2012 | |
2013 /* | |
2014 ;;; Local Variables: *** | |
2015 ;;; mode: C++ *** | |
2016 ;;; End: *** | |
2017 */ |