5832
|
1 /* |
|
2 |
|
3 Copyright (C) 2006 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #if !defined (octave_load_path_h) |
|
25 #define octave_load_path_h 1 |
|
26 |
|
27 #include <iostream> |
|
28 #include <list> |
|
29 #include <map> |
|
30 #include <string> |
|
31 |
5835
|
32 #include "pathsearch.h" |
5832
|
33 #include "str-vec.h" |
|
34 |
|
35 class |
6241
|
36 OCTINTERP_API |
5832
|
37 load_path |
|
38 { |
|
39 protected: |
|
40 |
|
41 load_path (void) : dir_info_list (), fcn_map () { } |
|
42 |
|
43 public: |
|
44 |
|
45 typedef void (*hook_function_ptr) (const std::string& dir); |
|
46 |
|
47 ~load_path (void) { } |
|
48 |
6365
|
49 static void initialize (bool set_initial_path = false) |
5832
|
50 { |
|
51 if (instance_ok ()) |
6365
|
52 instance->do_initialize (set_initial_path); |
5832
|
53 } |
|
54 |
|
55 static void clear (void) |
|
56 { |
|
57 if (instance_ok ()) |
|
58 instance->do_clear (); |
|
59 } |
|
60 |
5867
|
61 static void set (const std::string& p, bool warn = false) |
5832
|
62 { |
|
63 if (instance_ok ()) |
5867
|
64 instance->do_set (p, warn); |
5832
|
65 } |
|
66 |
5867
|
67 static void append (const std::string& dir, bool warn = false) |
5832
|
68 { |
|
69 if (instance_ok ()) |
5867
|
70 instance->do_append (dir, warn); |
5832
|
71 } |
|
72 |
5867
|
73 static void prepend (const std::string& dir, bool warn = false) |
5832
|
74 { |
|
75 if (instance_ok ()) |
5867
|
76 instance->do_prepend (dir, warn); |
5832
|
77 } |
|
78 |
|
79 static bool remove (const std::string& dir) |
|
80 { |
|
81 return instance_ok () ? instance->do_remove (dir) : false; |
|
82 } |
|
83 |
|
84 static void update (void) |
|
85 { |
|
86 if (instance_ok ()) |
|
87 instance->do_update (); |
|
88 } |
|
89 |
|
90 static std::string find_fcn (const std::string& fcn) |
|
91 { |
|
92 return instance_ok () |
|
93 ? instance->do_find_fcn (fcn) : std::string (); |
|
94 } |
|
95 |
|
96 static std::string find_fcn_file (const std::string& fcn) |
|
97 { |
|
98 return instance_ok () ? |
|
99 instance->do_find_fcn (fcn, M_FILE) : std::string (); |
|
100 } |
|
101 |
|
102 static std::string find_oct_file (const std::string& fcn) |
|
103 { |
|
104 return instance_ok () ? |
|
105 instance->do_find_fcn (fcn, OCT_FILE) : std::string (); |
|
106 } |
|
107 |
5864
|
108 static std::string find_mex_file (const std::string& fcn) |
|
109 { |
|
110 return instance_ok () ? |
|
111 instance->do_find_fcn (fcn, MEX_FILE) : std::string (); |
|
112 } |
|
113 |
5832
|
114 static std::string find_file (const std::string& file) |
|
115 { |
|
116 return instance_ok () |
|
117 ? instance->do_find_file (file) : std::string (); |
|
118 } |
|
119 |
|
120 static std::string find_first_of (const string_vector& files) |
|
121 { |
|
122 return instance_ok () ? |
|
123 instance->do_find_first_of (files) : std::string (); |
|
124 } |
|
125 |
|
126 static string_vector find_all_first_of (const string_vector& files) |
|
127 { |
|
128 return instance_ok () ? |
|
129 instance->do_find_all_first_of (files) : string_vector (); |
|
130 } |
|
131 |
|
132 static string_vector dirs (void) |
|
133 { |
|
134 return instance_ok () ? instance->do_dirs () : string_vector (); |
|
135 } |
|
136 |
|
137 static std::list<std::string> dir_list (void) |
|
138 { |
|
139 return instance_ok () |
|
140 ? instance->do_dir_list () : std::list<std::string> (); |
|
141 } |
|
142 |
|
143 static string_vector files (const std::string& dir) |
|
144 { |
|
145 return instance_ok () ? instance->do_files (dir) : string_vector (); |
|
146 } |
|
147 |
|
148 static string_vector fcn_names (void) |
|
149 { |
|
150 return instance_ok () ? instance->do_fcn_names () : string_vector (); |
|
151 } |
|
152 |
|
153 static std::string path (void) |
|
154 { |
|
155 return instance_ok () ? instance->do_path () : std::string (); |
|
156 } |
|
157 |
|
158 static void display (std::ostream& os) |
|
159 { |
|
160 if (instance_ok ()) |
|
161 instance->do_display (os); |
|
162 } |
|
163 |
|
164 static void set_add_hook (hook_function_ptr f) { add_hook = f; } |
|
165 |
|
166 static void set_remove_hook (hook_function_ptr f) { remove_hook = f; } |
|
167 |
|
168 static void set_command_line_path (const std::string& p) |
|
169 { |
5835
|
170 if (command_line_path.empty ()) |
|
171 command_line_path = p; |
|
172 else |
|
173 command_line_path += dir_path::path_sep_str + p; |
5832
|
174 } |
|
175 |
6626
|
176 static std::string system_path (void) |
|
177 { |
|
178 return instance_ok () ? do_system_path () : std::string (); |
|
179 } |
|
180 |
5832
|
181 private: |
|
182 |
|
183 static const int M_FILE = 1; |
|
184 static const int OCT_FILE = 2; |
5864
|
185 static const int MEX_FILE = 4; |
5832
|
186 |
|
187 class dir_info |
|
188 { |
|
189 public: |
|
190 |
|
191 dir_info (const std::string& d) : dir_name (d) { initialize (); } |
|
192 |
|
193 dir_info (const dir_info& di) |
|
194 : dir_name (di.dir_name), is_relative (di.is_relative), |
|
195 dir_mtime (di.dir_mtime), all_files (di.all_files), |
|
196 fcn_files (di.fcn_files), |
|
197 private_function_map (di.private_function_map) { } |
|
198 |
|
199 ~dir_info (void) { } |
|
200 |
|
201 dir_info& operator = (const dir_info& di) |
|
202 { |
|
203 if (&di != this) |
|
204 { |
|
205 dir_name = di.dir_name; |
|
206 is_relative = di.is_relative; |
|
207 dir_mtime = di.dir_mtime; |
|
208 all_files = di.all_files; |
|
209 fcn_files = di.fcn_files; |
|
210 private_function_map = di.private_function_map; |
|
211 } |
|
212 |
|
213 return *this; |
|
214 } |
|
215 |
|
216 void update (void); |
|
217 |
|
218 std::string dir_name; |
|
219 bool is_relative; |
|
220 octave_time dir_mtime; |
|
221 string_vector all_files; |
|
222 string_vector fcn_files; |
|
223 std::map<std::string, int> private_function_map; |
|
224 |
|
225 private: |
|
226 |
|
227 void initialize (void); |
|
228 |
|
229 bool get_file_list (const std::string& d); |
|
230 |
|
231 void get_private_function_map (const std::string& d); |
|
232 }; |
|
233 |
|
234 class file_info |
|
235 { |
|
236 public: |
|
237 |
|
238 file_info (const std::string& d, int t) : dir_name (d), types (t) { } |
|
239 |
|
240 file_info (const file_info& fi) |
|
241 : dir_name (fi.dir_name), types (fi.types) { } |
|
242 |
|
243 ~file_info (void) { } |
|
244 |
|
245 file_info& operator = (const file_info& fi) |
|
246 { |
|
247 if (&fi != this) |
|
248 { |
|
249 dir_name = fi.dir_name; |
|
250 types = fi.types; |
|
251 } |
|
252 |
|
253 return *this; |
|
254 } |
|
255 |
|
256 std::string dir_name; |
|
257 int types; |
|
258 }; |
|
259 |
|
260 // We maintain two ways of looking at the same information. |
|
261 // |
|
262 // First, a list of directories and the set of "public" files and |
|
263 // private files (those found in the special "private" subdirectory) |
|
264 // in each directory. |
|
265 // |
|
266 // Second, a map from file names (the union of all "public" files for all |
|
267 // directories, but without filename exteinsions) to a list of |
|
268 // corresponding information (directory name and file types). This |
|
269 // way, we can quickly find shadowed file names and look up all |
|
270 // overloaded functions (in the "@" directories used to implement |
|
271 // classes). |
|
272 |
|
273 mutable std::list<dir_info> dir_info_list; |
|
274 |
|
275 mutable std::map<std::string, std::list<file_info> > fcn_map; |
|
276 |
|
277 static load_path *instance; |
|
278 |
|
279 static hook_function_ptr add_hook; |
|
280 |
|
281 static hook_function_ptr remove_hook; |
|
282 |
|
283 static std::string command_line_path; |
|
284 |
6626
|
285 static std::string sys_path; |
|
286 |
5832
|
287 static bool instance_ok (void); |
|
288 |
|
289 typedef std::list<dir_info>::const_iterator const_dir_info_list_iterator; |
|
290 typedef std::list<dir_info>::iterator dir_info_list_iterator; |
|
291 |
|
292 typedef std::map<std::string, std::list<file_info> >::const_iterator const_fcn_map_iterator; |
|
293 typedef std::map<std::string, std::list<file_info> >::iterator fcn_map_iterator; |
|
294 |
|
295 typedef std::list<file_info>::const_iterator const_file_info_list_iterator; |
|
296 typedef std::list<file_info>::iterator file_info_list_iterator; |
|
297 |
|
298 const_dir_info_list_iterator find_dir_info (const std::string& dir) const; |
|
299 dir_info_list_iterator find_dir_info (const std::string& dir); |
|
300 |
|
301 bool contains (const std::string& dir) const; |
|
302 |
|
303 void move (std::list<dir_info>::iterator i, bool at_end); |
|
304 |
6365
|
305 void do_initialize (bool set_initial_path); |
5832
|
306 |
|
307 void do_clear (void); |
|
308 |
5867
|
309 void do_set (const std::string& p, bool warn); |
|
310 |
|
311 void do_append (const std::string& dir, bool warn); |
5832
|
312 |
5867
|
313 void do_prepend (const std::string& dir, bool warn); |
5832
|
314 |
5867
|
315 void do_add (const std::string& dir, bool at_end, bool warn); |
5832
|
316 |
|
317 bool do_remove (const std::string& dir); |
|
318 |
|
319 void do_update (void) const; |
|
320 |
|
321 std::string do_find_fcn (const std::string& fcn, |
5864
|
322 int type = M_FILE | OCT_FILE | MEX_FILE) const; |
5832
|
323 |
|
324 std::string do_find_file (const std::string& file) const; |
|
325 |
|
326 std::string do_find_first_of (const string_vector& files) const; |
|
327 |
|
328 string_vector do_find_all_first_of (const string_vector& files) const; |
|
329 |
|
330 string_vector do_dirs (void) const; |
|
331 |
|
332 std::list<std::string> do_dir_list (void) const; |
|
333 |
|
334 string_vector do_files (const std::string& dir) const; |
|
335 |
|
336 string_vector do_fcn_names (void) const; |
|
337 |
|
338 std::string do_path (void) const; |
|
339 |
|
340 void do_display (std::ostream& os) const; |
|
341 |
6629
|
342 std::string do_system_path (void) const { return sys_path; } |
6626
|
343 |
5832
|
344 void add_to_fcn_map (const dir_info& di, bool at_end) const; |
|
345 }; |
|
346 |
|
347 extern std::string |
|
348 genpath (const std::string& dir, const string_vector& skip = "private"); |
|
349 |
|
350 extern void execute_pkg_add (const std::string& dir); |
|
351 extern void execute_pkg_del (const std::string& dir); |
|
352 |
|
353 #endif |
|
354 |
|
355 /* |
|
356 ;;; Local Variables: *** |
|
357 ;;; mode: C++ *** |
|
358 ;;; page-delimiter: "^/\\*" *** |
|
359 ;;; End: *** |
|
360 */ |