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