00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #if !defined (octave_load_path_h)
00024 #define octave_load_path_h 1
00025
00026 #include <iosfwd>
00027 #include <list>
00028 #include <map>
00029 #include <string>
00030
00031 #include "pathsearch.h"
00032 #include "str-vec.h"
00033
00034 class
00035 OCTINTERP_API
00036 load_path
00037 {
00038 protected:
00039
00040 load_path (void)
00041 : dir_info_list (), fcn_map (), method_map () { }
00042
00043 public:
00044
00045 typedef void (*hook_fcn_ptr) (const std::string& dir);
00046
00047 ~load_path (void) { }
00048
00049 static void initialize (bool set_initial_path = false)
00050 {
00051 if (instance_ok ())
00052 instance->do_initialize (set_initial_path);
00053 }
00054
00055 static void clear (void)
00056 {
00057 if (instance_ok ())
00058 instance->do_clear ();
00059 }
00060
00061 static void set (const std::string& p, bool warn = false)
00062 {
00063 if (instance_ok ())
00064 instance->do_set (p, warn);
00065 }
00066
00067 static void append (const std::string& dir, bool warn = false)
00068 {
00069 if (instance_ok ())
00070 instance->do_append (dir, warn);
00071 }
00072
00073 static void prepend (const std::string& dir, bool warn = false)
00074 {
00075 if (instance_ok ())
00076 instance->do_prepend (dir, warn);
00077 }
00078
00079 static bool remove (const std::string& dir)
00080 {
00081 return instance_ok () ? instance->do_remove (dir) : false;
00082 }
00083
00084 static void update (void)
00085 {
00086 if (instance_ok ())
00087 instance->do_update ();
00088 }
00089
00090 static std::string find_method (const std::string& class_name,
00091 const std::string& meth,
00092 std::string& dir_name)
00093 {
00094 return instance_ok ()
00095 ? instance->do_find_method (class_name, meth, dir_name) : std::string ();
00096 }
00097
00098 static std::string find_method (const std::string& class_name,
00099 const std::string& meth)
00100 {
00101 std::string dir_name;
00102 return find_method (class_name, meth, dir_name);
00103 }
00104
00105 static std::list<std::string> methods (const std::string& class_name)
00106 {
00107 return instance_ok ()
00108 ? instance->do_methods (class_name) : std::list<std::string> ();
00109 }
00110
00111 static bool any_class_method (const std::string& meth)
00112 {
00113 return instance_ok ()
00114 ? instance->do_any_class_method (meth) : false;
00115 }
00116
00117 static std::string find_fcn (const std::string& fcn, std::string& dir_name)
00118 {
00119 return instance_ok ()
00120 ? instance->do_find_fcn (fcn, dir_name) : std::string ();
00121 }
00122
00123 static std::string find_fcn (const std::string& fcn)
00124 {
00125 std::string dir_name;
00126 return find_fcn (fcn, dir_name);
00127 }
00128
00129 static std::string find_private_fcn (const std::string& dir,
00130 const std::string& fcn)
00131 {
00132 return instance_ok ()
00133 ? instance->do_find_private_fcn (dir, fcn) : std::string ();
00134 }
00135
00136 static std::string find_fcn_file (const std::string& fcn)
00137 {
00138 std::string dir_name;
00139
00140 return instance_ok () ?
00141 instance->do_find_fcn (fcn, dir_name, M_FILE) : std::string ();
00142 }
00143
00144 static std::string find_oct_file (const std::string& fcn)
00145 {
00146 std::string dir_name;
00147
00148 return instance_ok () ?
00149 instance->do_find_fcn (fcn, dir_name, OCT_FILE) : std::string ();
00150 }
00151
00152 static std::string find_mex_file (const std::string& fcn)
00153 {
00154 std::string dir_name;
00155
00156 return instance_ok () ?
00157 instance->do_find_fcn (fcn, dir_name, MEX_FILE) : std::string ();
00158 }
00159
00160 static std::string find_file (const std::string& file)
00161 {
00162 return instance_ok ()
00163 ? instance->do_find_file (file) : std::string ();
00164 }
00165
00166 static std::string find_dir (const std::string& dir)
00167 {
00168 return instance_ok ()
00169 ? instance->do_find_dir (dir) : std::string ();
00170 }
00171
00172 static std::string find_first_of (const string_vector& files)
00173 {
00174 return instance_ok () ?
00175 instance->do_find_first_of (files) : std::string ();
00176 }
00177
00178 static string_vector find_all_first_of (const string_vector& files)
00179 {
00180 return instance_ok () ?
00181 instance->do_find_all_first_of (files) : string_vector ();
00182 }
00183
00184 static string_vector dirs (void)
00185 {
00186 return instance_ok () ? instance->do_dirs () : string_vector ();
00187 }
00188
00189 static std::list<std::string> dir_list (void)
00190 {
00191 return instance_ok ()
00192 ? instance->do_dir_list () : std::list<std::string> ();
00193 }
00194
00195 static string_vector files (const std::string& dir, bool omit_exts = false)
00196 {
00197 return instance_ok ()
00198 ? instance->do_files (dir, omit_exts) : string_vector ();
00199 }
00200
00201 static string_vector fcn_names (void)
00202 {
00203 return instance_ok () ? instance->do_fcn_names () : string_vector ();
00204 }
00205
00206 static std::string path (void)
00207 {
00208 return instance_ok () ? instance->do_path () : std::string ();
00209 }
00210
00211 static void display (std::ostream& os)
00212 {
00213 if (instance_ok ())
00214 instance->do_display (os);
00215 }
00216
00217 static void set_add_hook (hook_fcn_ptr f) { add_hook = f; }
00218
00219 static void set_remove_hook (hook_fcn_ptr f) { remove_hook = f; }
00220
00221 static void set_command_line_path (const std::string& p)
00222 {
00223 if (command_line_path.empty ())
00224 command_line_path = p;
00225 else
00226 command_line_path += dir_path::path_sep_str () + p;
00227 }
00228
00229 static std::string get_command_line_path (void)
00230 {
00231 return instance_ok () ? instance->do_get_command_line_path () : std::string ();
00232 }
00233
00234 static std::string system_path (void)
00235 {
00236 return instance_ok () ? instance->do_system_path () : std::string ();
00237 }
00238
00239 private:
00240
00241 static const int M_FILE = 1;
00242 static const int OCT_FILE = 2;
00243 static const int MEX_FILE = 4;
00244
00245 class dir_info
00246 {
00247 public:
00248
00249
00250 typedef std::map<std::string, int> fcn_file_map_type;
00251
00252 typedef fcn_file_map_type::const_iterator const_fcn_file_map_iterator;
00253 typedef fcn_file_map_type::iterator fcn_file_map_iterator;
00254
00255 struct class_info
00256 {
00257 fcn_file_map_type method_file_map;
00258 fcn_file_map_type private_file_map;
00259 };
00260
00261
00262 typedef std::map<std::string, class_info> method_file_map_type;
00263
00264 typedef method_file_map_type::const_iterator const_method_file_map_iterator;
00265 typedef method_file_map_type::iterator method_file_map_iterator;
00266
00267
00268
00269
00270 dir_info (void) { }
00271
00272 dir_info (const std::string& d) : dir_name (d) { initialize (); }
00273
00274 dir_info (const dir_info& di)
00275 : dir_name (di.dir_name), abs_dir_name (di.abs_dir_name),
00276 is_relative (di.is_relative),
00277 dir_mtime (di.dir_mtime), all_files (di.all_files),
00278 fcn_files (di.fcn_files),
00279 private_file_map (di.private_file_map),
00280 method_file_map (di.method_file_map) { }
00281
00282 ~dir_info (void) { }
00283
00284 dir_info& operator = (const dir_info& di)
00285 {
00286 if (&di != this)
00287 {
00288 dir_name = di.dir_name;
00289 abs_dir_name = di.abs_dir_name;
00290 is_relative = di.is_relative;
00291 dir_mtime = di.dir_mtime;
00292 all_files = di.all_files;
00293 fcn_files = di.fcn_files;
00294 private_file_map = di.private_file_map;
00295 method_file_map = di.method_file_map;
00296 }
00297
00298 return *this;
00299 }
00300
00301 void update (void);
00302
00303 std::string dir_name;
00304 std::string abs_dir_name;
00305 bool is_relative;
00306 octave_time dir_mtime;
00307 string_vector all_files;
00308 string_vector fcn_files;
00309 fcn_file_map_type private_file_map;
00310 method_file_map_type method_file_map;
00311
00312 private:
00313
00314 void initialize (void);
00315
00316 void get_file_list (const std::string& d);
00317
00318 void get_private_file_map (const std::string& d);
00319
00320 void get_method_file_map (const std::string& d,
00321 const std::string& class_name);
00322
00323 friend fcn_file_map_type get_fcn_files (const std::string& d);
00324 };
00325
00326 class file_info
00327 {
00328 public:
00329
00330 file_info (const std::string& d, int t) : dir_name (d), types (t) { }
00331
00332 file_info (const file_info& fi)
00333 : dir_name (fi.dir_name), types (fi.types) { }
00334
00335 ~file_info (void) { }
00336
00337 file_info& operator = (const file_info& fi)
00338 {
00339 if (&fi != this)
00340 {
00341 dir_name = fi.dir_name;
00342 types = fi.types;
00343 }
00344
00345 return *this;
00346 }
00347
00348 std::string dir_name;
00349 int types;
00350 };
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365 typedef std::list<dir_info> dir_info_list_type;
00366
00367 typedef dir_info_list_type::const_iterator const_dir_info_list_iterator;
00368 typedef dir_info_list_type::iterator dir_info_list_iterator;
00369
00370 typedef std::map<std::string, dir_info> abs_dir_cache_type;
00371
00372 typedef abs_dir_cache_type::const_iterator const_abs_dir_cache_iterator;
00373 typedef abs_dir_cache_type::iterator abs_dir_cache_iterator;
00374
00375 typedef std::list<file_info> file_info_list_type;
00376
00377 typedef file_info_list_type::const_iterator const_file_info_list_iterator;
00378 typedef file_info_list_type::iterator file_info_list_iterator;
00379
00380
00381 typedef std::map<std::string, file_info_list_type> fcn_map_type;
00382
00383 typedef fcn_map_type::const_iterator const_fcn_map_iterator;
00384 typedef fcn_map_type::iterator fcn_map_iterator;
00385
00386
00387 typedef std::map<std::string, dir_info::fcn_file_map_type> private_fcn_map_type;
00388
00389 typedef private_fcn_map_type::const_iterator const_private_fcn_map_iterator;
00390 typedef private_fcn_map_type::iterator private_fcn_map_iterator;
00391
00392
00393 typedef std::map<std::string, fcn_map_type> method_map_type;
00394
00395 typedef method_map_type::const_iterator const_method_map_iterator;
00396 typedef method_map_type::iterator method_map_iterator;
00397
00398 mutable dir_info_list_type dir_info_list;
00399
00400 mutable fcn_map_type fcn_map;
00401
00402 mutable private_fcn_map_type private_fcn_map;
00403
00404 mutable method_map_type method_map;
00405
00406 static load_path *instance;
00407
00408 static hook_fcn_ptr add_hook;
00409
00410 static hook_fcn_ptr remove_hook;
00411
00412 static std::string command_line_path;
00413
00414 static std::string sys_path;
00415
00416 static abs_dir_cache_type abs_dir_cache;
00417
00418 static bool instance_ok (void);
00419
00420 const_dir_info_list_iterator find_dir_info (const std::string& dir) const;
00421 dir_info_list_iterator find_dir_info (const std::string& dir);
00422
00423 bool contains (const std::string& dir) const;
00424
00425 void move_fcn_map (const std::string& dir,
00426 const string_vector& fcn_files, bool at_end);
00427
00428 void move_method_map (const std::string& dir, bool at_end);
00429
00430 void move (std::list<dir_info>::iterator i, bool at_end);
00431
00432 void do_initialize (bool set_initial_path);
00433
00434 void do_clear (void);
00435
00436 void do_set (const std::string& p, bool warn);
00437
00438 void do_append (const std::string& dir, bool warn);
00439
00440 void do_prepend (const std::string& dir, bool warn);
00441
00442 void do_add (const std::string& dir, bool at_end, bool warn);
00443
00444 void remove_fcn_map (const std::string& dir, const string_vector& fcn_files);
00445
00446 void remove_private_fcn_map (const std::string& dir);
00447
00448 void remove_method_map (const std::string& dir);
00449
00450 bool do_remove (const std::string& dir);
00451
00452 void do_update (void) const;
00453
00454 static bool
00455 check_file_type (std::string& fname, int type, int possible_types,
00456 const std::string& fcn, const char *who);
00457
00458 std::string do_find_fcn (const std::string& fcn,
00459 std::string& dir_name,
00460 int type = M_FILE | OCT_FILE | MEX_FILE) const;
00461
00462 std::string do_find_private_fcn (const std::string& dir,
00463 const std::string& fcn,
00464 int type = M_FILE | OCT_FILE | MEX_FILE) const;
00465
00466 std::string do_find_method (const std::string& class_name,
00467 const std::string& meth,
00468 std::string& dir_name,
00469 int type = M_FILE | OCT_FILE | MEX_FILE) const;
00470
00471 std::list<std::string> do_methods (const std::string& class_name) const;
00472
00473 bool do_any_class_method (const std::string& meth) const;
00474
00475 std::string do_find_file (const std::string& file) const;
00476
00477 std::string do_find_dir (const std::string& dir) const;
00478
00479 std::string do_find_first_of (const string_vector& files) const;
00480
00481 string_vector do_find_all_first_of (const string_vector& files) const;
00482
00483 string_vector do_dirs (void) const;
00484
00485 std::list<std::string> do_dir_list (void) const;
00486
00487 string_vector do_files (const std::string& dir, bool omit_exts) const;
00488
00489 string_vector do_fcn_names (void) const;
00490
00491 std::string do_path (void) const;
00492
00493 friend void print_types (std::ostream& os, int types);
00494
00495 friend string_vector get_file_list (const dir_info::fcn_file_map_type& lst);
00496
00497 friend void
00498 print_fcn_list (std::ostream& os, const dir_info::fcn_file_map_type& lst);
00499
00500 void do_display (std::ostream& os) const;
00501
00502 std::string do_system_path (void) const { return sys_path; }
00503
00504 std::string do_get_command_line_path (void) const { return command_line_path; }
00505
00506 void add_to_fcn_map (const dir_info& di, bool at_end) const;
00507
00508 void add_to_private_fcn_map (const dir_info& di) const;
00509
00510 void add_to_method_map (const dir_info& di, bool at_end) const;
00511
00512 friend dir_info::fcn_file_map_type get_fcn_files (const std::string& d);
00513 };
00514
00515 extern std::string
00516 genpath (const std::string& dir, const string_vector& skip = "private");
00517
00518 extern void execute_pkg_add (const std::string& dir);
00519 extern void execute_pkg_del (const std::string& dir);
00520
00521 #endif
00522
00523
00524
00525
00526
00527
00528