pathsearch.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_pathsearch_h)
00024 #define octave_pathsearch_h 1
00025 
00026 #include <string>
00027 
00028 #include "str-vec.h"
00029 
00030 class
00031 OCTAVE_API
00032 dir_path
00033 {
00034 public:
00035 
00036   dir_path (const std::string& s = std::string (),
00037             const std::string& d = std::string ())
00038     : p_orig (s), p_default (d), initialized (false), p (), pv ()
00039     {
00040       if (! p_orig.empty ())
00041         init ();
00042     }
00043 
00044   dir_path (const dir_path& dp)
00045     : p_orig (dp.p_orig), p_default (dp.p_default),
00046       initialized (dp.initialized), p (dp.p), pv (dp.pv)
00047   { }
00048 
00049   dir_path& operator = (const dir_path& dp)
00050     {
00051       p_orig = dp.p_orig;
00052       p_default = dp.p_default;
00053       initialized = dp.initialized;
00054       p = dp.p;
00055       pv = dp.pv;
00056       return *this;
00057     }
00058 
00059   ~dir_path (void) { }
00060 
00061   void set (const std::string& s)
00062     {
00063       initialized = false;
00064       p_orig = s;
00065       init ();
00066     }
00067 
00068   string_vector elements (void);
00069   string_vector all_directories (void);
00070 
00071   std::string find_first (const std::string&);
00072   std::string find (const std::string& nm) { return find_first (nm); }
00073 
00074   string_vector find_all (const std::string&);
00075 
00076   std::string find_first_of (const string_vector& names);
00077   string_vector find_all_first_of (const string_vector& names);
00078 
00079   void rehash (void)
00080     {
00081       initialized = false;
00082       init ();
00083     }
00084 
00085   static char path_sep_char (void)
00086   {
00087     return static_members::path_sep_char ();
00088   }
00089 
00090   static void path_sep_char (char c)
00091   {
00092     static_members::path_sep_char (c);
00093   }
00094 
00095   static std::string path_sep_str (void)
00096   {
00097     return static_members::path_sep_str ();
00098   }
00099 
00100   static bool is_path_sep (char c) { return c == path_sep_char (); }
00101 
00102 private:
00103 
00104   // The colon separated list that we were given.
00105   std::string p_orig;
00106 
00107   // The default path.  If specified, replaces leading, trailing, or
00108   // doubled colons in p_orig.
00109   std::string p_default;
00110 
00111   // TRUE means we've unpacked p.
00112   bool initialized;
00113 
00114   // A version of the colon separate list on which we have performed
00115   // tilde, variable, and possibly default path expansion.
00116   std::string p;
00117 
00118   // The elements of the list.
00119   string_vector pv;
00120 
00121   void init (void);
00122 
00123   // Use a singleton class for these data members instead of just
00124   // making them static members of the dir_path class so that we can
00125   // ensure proper initialization.
00126 
00127   class OCTAVE_API static_members
00128   {
00129   public:
00130 
00131     static_members (void);
00132 
00133     static char path_sep_char (void)
00134     {
00135       return instance_ok () ? instance->xpath_sep_char : 0;
00136     }
00137 
00138     static void path_sep_char (char c)
00139     {
00140       if (instance_ok ())
00141         {
00142           instance->xpath_sep_char = c;
00143           instance->xpath_sep_str = std::string (1, c);
00144         }
00145     }
00146 
00147     static std::string path_sep_str (void)
00148     {
00149       return instance_ok () ? instance->xpath_sep_str : std::string ();
00150     }
00151 
00152   private:
00153 
00154     static static_members *instance;
00155 
00156     static void cleanup_instance (void) { delete instance; instance = 0; }
00157 
00158     static bool instance_ok (void);
00159 
00160     // No copying!
00161 
00162     static_members (const static_members&);
00163 
00164     static_members& operator = (const static_members&);
00165 
00166     char xpath_sep_char;
00167 
00168     std::string xpath_sep_str;
00169   };
00170 };
00171 
00172 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines