load-path.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-2012 John W. Eaton
00004 Copyright (C) 2010 VZLU Prague
00005 
00006 This file is part of Octave.
00007 
00008 Octave is free software; you can redistribute it and/or modify it
00009 under the terms of the GNU General Public License as published by the
00010 Free Software Foundation; either version 3 of the License, or (at your
00011 option) any later version.
00012 
00013 Octave is distributed in the hope that it will be useful, but WITHOUT
00014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00015 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00016 for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with Octave; see the file COPYING.  If not, see
00020 <http://www.gnu.org/licenses/>.
00021 
00022 */
00023 
00024 #if !defined (octave_load_path_h)
00025 #define octave_load_path_h 1
00026 
00027 #include <iosfwd>
00028 #include <list>
00029 #include <map>
00030 #include <string>
00031 
00032 #include "pathsearch.h"
00033 #include "str-vec.h"
00034 
00035 class
00036 OCTINTERP_API
00037 load_path
00038 {
00039 protected:
00040 
00041   load_path (void)
00042     : dir_info_list (), fcn_map (), private_fcn_map (), method_map () { }
00043 
00044 public:
00045 
00046   typedef void (*hook_fcn_ptr) (const std::string& dir);
00047 
00048   ~load_path (void) { }
00049 
00050   static void initialize (bool set_initial_path = false)
00051   {
00052     if (instance_ok ())
00053       instance->do_initialize (set_initial_path);
00054   }
00055 
00056   static void clear (void)
00057   {
00058     if (instance_ok ())
00059       instance->do_clear ();
00060   }
00061 
00062   static void set (const std::string& p, bool warn = false)
00063   {
00064     if (instance_ok ())
00065       instance->do_set (p, warn);
00066   }
00067 
00068   static void append (const std::string& dir, bool warn = false)
00069   {
00070     if (instance_ok ())
00071       instance->do_append (dir, warn);
00072   }
00073 
00074   static void prepend (const std::string& dir, bool warn = false)
00075   {
00076     if (instance_ok ())
00077       instance->do_prepend (dir, warn);
00078   }
00079 
00080   static bool remove (const std::string& dir)
00081   {
00082     return instance_ok () ? instance->do_remove (dir) : false;
00083   }
00084 
00085   static void update (void)
00086   {
00087     if (instance_ok ())
00088       instance->do_update ();
00089   }
00090 
00091   static std::string find_method (const std::string& class_name,
00092                                   const std::string& meth,
00093                                   std::string& dir_name)
00094   {
00095     return instance_ok ()
00096       ? instance->do_find_method (class_name, meth, dir_name) : std::string ();
00097   }
00098 
00099   static std::string find_method (const std::string& class_name,
00100                                   const std::string& meth)
00101   {
00102     std::string dir_name;
00103     return find_method (class_name, meth, dir_name);
00104   }
00105 
00106   static std::list<std::string> methods (const std::string& class_name)
00107   {
00108     return instance_ok ()
00109       ? instance->do_methods (class_name) : std::list<std::string> ();
00110   }
00111 
00112   static std::list<std::string> overloads (const std::string& meth)
00113   {
00114     return instance_ok ()
00115       ? instance->do_overloads (meth) : std::list<std::string> ();
00116   }
00117 
00118   static std::string find_fcn (const std::string& fcn, std::string& dir_name)
00119   {
00120     return instance_ok ()
00121       ? instance->do_find_fcn (fcn, dir_name) : std::string ();
00122   }
00123 
00124   static std::string find_fcn (const std::string& fcn)
00125   {
00126     std::string dir_name;
00127     return find_fcn (fcn, dir_name);
00128   }
00129 
00130   static std::string find_private_fcn (const std::string& dir,
00131                                        const std::string& fcn)
00132   {
00133     return instance_ok ()
00134       ? instance->do_find_private_fcn (dir, fcn) : std::string ();
00135   }
00136 
00137   static std::string find_fcn_file (const std::string& fcn)
00138   {
00139     std::string dir_name;
00140 
00141     return instance_ok () ?
00142       instance->do_find_fcn (fcn, dir_name, M_FILE) : std::string ();
00143   }
00144 
00145   static std::string find_oct_file (const std::string& fcn)
00146   {
00147     std::string dir_name;
00148 
00149     return instance_ok () ?
00150       instance->do_find_fcn (fcn, dir_name, OCT_FILE) : std::string ();
00151   }
00152 
00153   static std::string find_mex_file (const std::string& fcn)
00154   {
00155     std::string dir_name;
00156 
00157     return instance_ok () ?
00158       instance->do_find_fcn (fcn, dir_name, MEX_FILE) : std::string ();
00159   }
00160 
00161   static std::string find_file (const std::string& file)
00162   {
00163     return instance_ok ()
00164       ? instance->do_find_file (file) : std::string ();
00165   }
00166 
00167   static std::string find_dir (const std::string& dir)
00168   {
00169     return instance_ok ()
00170       ? instance->do_find_dir (dir) : std::string ();
00171   }
00172 
00173   static string_vector find_matching_dirs (const std::string& dir)
00174   {
00175     return instance_ok ()
00176       ? instance->do_find_matching_dirs (dir) : string_vector ();
00177   }
00178 
00179   static std::string find_first_of (const string_vector& files)
00180   {
00181     return instance_ok () ?
00182       instance->do_find_first_of (files) : std::string ();
00183   }
00184 
00185   static string_vector find_all_first_of (const string_vector& files)
00186   {
00187     return instance_ok () ?
00188       instance->do_find_all_first_of (files) : string_vector ();
00189   }
00190 
00191   static string_vector dirs (void)
00192   {
00193     return instance_ok () ? instance->do_dirs () : string_vector ();
00194   }
00195 
00196   static std::list<std::string> dir_list (void)
00197   {
00198     return instance_ok ()
00199       ? instance->do_dir_list () : std::list<std::string> ();
00200   }
00201 
00202   static string_vector files (const std::string& dir, bool omit_exts = false)
00203   {
00204     return instance_ok ()
00205       ? instance->do_files (dir, omit_exts) : string_vector ();
00206   }
00207 
00208   static string_vector fcn_names (void)
00209   {
00210     return instance_ok () ? instance->do_fcn_names () : string_vector ();
00211   }
00212 
00213   static std::string path (void)
00214   {
00215     return instance_ok () ? instance->do_path () : std::string ();
00216   }
00217 
00218   static void display (std::ostream& os)
00219   {
00220     if (instance_ok ())
00221       instance->do_display (os);
00222   }
00223 
00224   static void set_add_hook (hook_fcn_ptr f) { add_hook = f; }
00225 
00226   static void set_remove_hook (hook_fcn_ptr f) { remove_hook = f; }
00227 
00228   static void set_command_line_path (const std::string& p)
00229   {
00230     if (command_line_path.empty ())
00231       command_line_path = p;
00232     else
00233       command_line_path += dir_path::path_sep_str () + p;
00234   }
00235 
00236   static std::string get_command_line_path (void)
00237   {
00238     return instance_ok () ? instance->do_get_command_line_path () : std::string ();
00239   }
00240 
00241   static std::string system_path (void)
00242   {
00243     return instance_ok () ? instance->do_system_path () : std::string ();
00244   }
00245 
00246 private:
00247 
00248   static const int M_FILE = 1;
00249   static const int OCT_FILE = 2;
00250   static const int MEX_FILE = 4;
00251 
00252   class dir_info
00253   {
00254   public:
00255 
00256     // <FCN_NAME, TYPE>
00257     typedef std::map<std::string, int> fcn_file_map_type;
00258 
00259     typedef fcn_file_map_type::const_iterator const_fcn_file_map_iterator;
00260     typedef fcn_file_map_type::iterator fcn_file_map_iterator;
00261 
00262     struct class_info
00263     {
00264       class_info (void) : method_file_map (), private_file_map () { }
00265 
00266       class_info (const class_info& ci)
00267         : method_file_map (ci.method_file_map),
00268           private_file_map (ci.private_file_map) { }
00269 
00270       class_info& operator = (const class_info& ci)
00271       {
00272         if (this != &ci)
00273           {
00274             method_file_map = ci.method_file_map;
00275             private_file_map = ci.private_file_map;
00276           }
00277         return *this;
00278       }
00279 
00280       ~class_info (void) { }
00281 
00282       fcn_file_map_type method_file_map;
00283       fcn_file_map_type private_file_map;
00284     };
00285 
00286     // <CLASS_NAME, CLASS_INFO>
00287     typedef std::map<std::string, class_info> method_file_map_type;
00288 
00289     typedef method_file_map_type::const_iterator const_method_file_map_iterator;
00290     typedef method_file_map_type::iterator method_file_map_iterator;
00291 
00292     // This default constructor is only provided so we can create a
00293     // std::map of dir_info objects.  You should not use this
00294     // constructor for any other purpose.
00295     dir_info (void)
00296       : dir_name (), abs_dir_name (), is_relative (false),
00297         dir_mtime (), dir_time_last_checked (), all_files (),
00298         fcn_files (), private_file_map (), method_file_map ()
00299       { }
00300 
00301     dir_info (const std::string& d)
00302       : dir_name (d), abs_dir_name (), is_relative (false),
00303         dir_mtime (), dir_time_last_checked (), all_files (),
00304         fcn_files (), private_file_map (), method_file_map ()
00305     {
00306       initialize ();
00307     }
00308 
00309     dir_info (const dir_info& di)
00310       : dir_name (di.dir_name), abs_dir_name (di.abs_dir_name),
00311         is_relative (di.is_relative),
00312         dir_mtime (di.dir_mtime),
00313         dir_time_last_checked (di.dir_time_last_checked),
00314         all_files (di.all_files), fcn_files (di.fcn_files),
00315         private_file_map (di.private_file_map),
00316         method_file_map (di.method_file_map) { }
00317 
00318     ~dir_info (void) { }
00319 
00320     dir_info& operator = (const dir_info& di)
00321     {
00322       if (&di != this)
00323         {
00324           dir_name = di.dir_name;
00325           abs_dir_name = di.abs_dir_name;
00326           is_relative = di.is_relative;
00327           dir_mtime = di.dir_mtime;
00328           dir_time_last_checked = di.dir_time_last_checked;
00329           all_files = di.all_files;
00330           fcn_files = di.fcn_files;
00331           private_file_map = di.private_file_map;
00332           method_file_map = di.method_file_map;
00333         }
00334 
00335       return *this;
00336     }
00337 
00338     void update (void);
00339 
00340     std::string dir_name;
00341     std::string abs_dir_name;
00342     bool is_relative;
00343     octave_time dir_mtime;
00344     octave_time dir_time_last_checked;
00345     string_vector all_files;
00346     string_vector fcn_files;
00347     fcn_file_map_type private_file_map;
00348     method_file_map_type method_file_map;
00349 
00350   private:
00351 
00352     void initialize (void);
00353 
00354     void get_file_list (const std::string& d);
00355 
00356     void get_private_file_map (const std::string& d);
00357 
00358     void get_method_file_map (const std::string& d,
00359                               const std::string& class_name);
00360 
00361     friend fcn_file_map_type get_fcn_files (const std::string& d);
00362   };
00363 
00364   class file_info
00365   {
00366   public:
00367 
00368     file_info (const std::string& d, int t) : dir_name (d), types (t) { }
00369 
00370     file_info (const file_info& fi)
00371       : dir_name (fi.dir_name), types (fi.types) { }
00372 
00373     ~file_info (void) { }
00374 
00375     file_info& operator = (const file_info& fi)
00376     {
00377       if (&fi != this)
00378         {
00379           dir_name = fi.dir_name;
00380           types = fi.types;
00381         }
00382 
00383       return *this;
00384     }
00385 
00386     std::string dir_name;
00387     int types;
00388   };
00389 
00390   // We maintain two ways of looking at the same information.
00391   //
00392   // First, a list of directories and the set of "public" files and
00393   // private files (those found in the special "private" subdirectory)
00394   // in each directory.
00395   //
00396   // Second, a map from file names (the union of all "public" files for all
00397   // directories, but without filename extensions) to a list of
00398   // corresponding information (directory name and file types).  This
00399   // way, we can quickly find shadowed file names and look up all
00400   // overloaded functions (in the "@" directories used to implement
00401   // classes).
00402 
00403   typedef std::list<dir_info> dir_info_list_type;
00404 
00405   typedef dir_info_list_type::const_iterator const_dir_info_list_iterator;
00406   typedef dir_info_list_type::iterator dir_info_list_iterator;
00407 
00408   typedef std::map<std::string, dir_info> abs_dir_cache_type;
00409 
00410   typedef abs_dir_cache_type::const_iterator const_abs_dir_cache_iterator;
00411   typedef abs_dir_cache_type::iterator abs_dir_cache_iterator;
00412 
00413   typedef std::list<file_info> file_info_list_type;
00414 
00415   typedef file_info_list_type::const_iterator const_file_info_list_iterator;
00416   typedef file_info_list_type::iterator file_info_list_iterator;
00417 
00418   // <FCN_NAME, FILE_INFO_LIST>
00419   typedef std::map<std::string, file_info_list_type> fcn_map_type;
00420 
00421   typedef fcn_map_type::const_iterator const_fcn_map_iterator;
00422   typedef fcn_map_type::iterator fcn_map_iterator;
00423 
00424   // <DIR_NAME, <FCN_NAME, TYPE>>
00425   typedef std::map<std::string, dir_info::fcn_file_map_type> private_fcn_map_type;
00426 
00427   typedef private_fcn_map_type::const_iterator const_private_fcn_map_iterator;
00428   typedef private_fcn_map_type::iterator private_fcn_map_iterator;
00429 
00430   // <CLASS_NAME, <FCN_NAME, FILE_INFO_LIST>>
00431   typedef std::map<std::string, fcn_map_type> method_map_type;
00432 
00433   typedef method_map_type::const_iterator const_method_map_iterator;
00434   typedef method_map_type::iterator method_map_iterator;
00435 
00436   mutable dir_info_list_type dir_info_list;
00437 
00438   mutable fcn_map_type fcn_map;
00439 
00440   mutable private_fcn_map_type private_fcn_map;
00441 
00442   mutable method_map_type method_map;
00443 
00444   static load_path *instance;
00445 
00446   static void cleanup_instance (void) { delete instance; instance = 0; }
00447 
00448   static hook_fcn_ptr add_hook;
00449 
00450   static hook_fcn_ptr remove_hook;
00451 
00452   static std::string command_line_path;
00453 
00454   static std::string sys_path;
00455 
00456   static abs_dir_cache_type abs_dir_cache;
00457 
00458   static bool instance_ok (void);
00459 
00460   const_dir_info_list_iterator find_dir_info (const std::string& dir) const;
00461   dir_info_list_iterator find_dir_info (const std::string& dir);
00462 
00463   bool contains (const std::string& dir) const;
00464 
00465   void move_fcn_map (const std::string& dir,
00466                      const string_vector& fcn_files, bool at_end);
00467 
00468   void move_method_map (const std::string& dir, bool at_end);
00469 
00470   void move (std::list<dir_info>::iterator i, bool at_end);
00471 
00472   void do_initialize (bool set_initial_path);
00473 
00474   void do_clear (void);
00475 
00476   void do_set (const std::string& p, bool warn);
00477 
00478   void do_append (const std::string& dir, bool warn);
00479 
00480   void do_prepend (const std::string& dir, bool warn);
00481 
00482   void do_add (const std::string& dir, bool at_end, bool warn);
00483 
00484   void remove_fcn_map (const std::string& dir, const string_vector& fcn_files);
00485 
00486   void remove_private_fcn_map (const std::string& dir);
00487 
00488   void remove_method_map (const std::string& dir);
00489 
00490   bool do_remove (const std::string& dir);
00491 
00492   void do_update (void) const;
00493 
00494   static bool
00495   check_file_type (std::string& fname, int type, int possible_types,
00496                    const std::string& fcn, const char *who);
00497 
00498   std::string do_find_fcn (const std::string& fcn,
00499                            std::string& dir_name,
00500                            int type = M_FILE | OCT_FILE | MEX_FILE) const;
00501 
00502   std::string do_find_private_fcn (const std::string& dir,
00503                                    const std::string& fcn,
00504                                    int type = M_FILE | OCT_FILE | MEX_FILE) const;
00505 
00506   std::string do_find_method (const std::string& class_name,
00507                               const std::string& meth,
00508                               std::string& dir_name,
00509                               int type = M_FILE | OCT_FILE | MEX_FILE) const;
00510 
00511   std::list<std::string> do_methods (const std::string& class_name) const;
00512 
00513   std::list<std::string> do_overloads (const std::string& meth) const;
00514 
00515   std::string do_find_file (const std::string& file) const;
00516 
00517   std::string do_find_dir (const std::string& dir) const;
00518 
00519   string_vector do_find_matching_dirs (const std::string& dir) const;
00520 
00521   std::string do_find_first_of (const string_vector& files) const;
00522 
00523   string_vector do_find_all_first_of (const string_vector& files) const;
00524 
00525   string_vector do_dirs (void) const;
00526 
00527   std::list<std::string> do_dir_list (void) const;
00528 
00529   string_vector do_files (const std::string& dir, bool omit_exts) const;
00530 
00531   string_vector do_fcn_names (void) const;
00532 
00533   std::string do_path (void) const;
00534 
00535   friend void print_types (std::ostream& os, int types);
00536 
00537   friend string_vector get_file_list (const dir_info::fcn_file_map_type& lst);
00538 
00539   friend void
00540   print_fcn_list (std::ostream& os, const dir_info::fcn_file_map_type& lst);
00541 
00542   void do_display (std::ostream& os) const;
00543 
00544   std::string do_system_path (void) const { return sys_path; }
00545 
00546   std::string do_get_command_line_path (void) const { return command_line_path; }
00547 
00548   void add_to_fcn_map (const dir_info& di, bool at_end) const;
00549 
00550   void add_to_private_fcn_map (const dir_info& di) const;
00551 
00552   void add_to_method_map (const dir_info& di, bool at_end) const;
00553 
00554   friend dir_info::fcn_file_map_type get_fcn_files (const std::string& d);
00555 };
00556 
00557 extern std::string
00558 genpath (const std::string& dir, const string_vector& skip = "private");
00559 
00560 extern void execute_pkg_add (const std::string& dir);
00561 extern void execute_pkg_del (const std::string& dir);
00562 
00563 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines