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