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 #ifdef HAVE_CONFIG_H 00024 #include <config.h> 00025 #endif 00026 00027 #include <cstdlib> 00028 00029 #include <string> 00030 00031 #include "lo-utils.h" 00032 #include "oct-env.h" 00033 #include "pathsearch.h" 00034 #include "singleton-cleanup.h" 00035 #include "str-vec.h" 00036 00037 #include "kpse.cc" 00038 00039 dir_path::static_members *dir_path::static_members::instance = 0; 00040 00041 dir_path::static_members::static_members (void) 00042 : xpath_sep_char (SEPCHAR), xpath_sep_str (SEPCHAR_STR) { } 00043 00044 bool 00045 dir_path::static_members::instance_ok (void) 00046 { 00047 bool retval = true; 00048 00049 if (! instance) 00050 { 00051 instance = new static_members (); 00052 00053 if (instance) 00054 singleton_cleanup_list::add (cleanup_instance); 00055 } 00056 00057 if (! instance) 00058 { 00059 (*current_liboctave_error_handler) 00060 ("unable to create dir_path::static_members object!"); 00061 00062 retval = false; 00063 } 00064 00065 return retval; 00066 } 00067 00068 string_vector 00069 dir_path::elements (void) 00070 { 00071 return initialized ? pv : string_vector (); 00072 } 00073 00074 string_vector 00075 dir_path::all_directories (void) 00076 { 00077 int count = 0; 00078 string_vector retval; 00079 00080 if (initialized) 00081 { 00082 int len = pv.length (); 00083 00084 int nmax = len > 32 ? len : 32; 00085 00086 retval.resize (len); 00087 00088 for (int i = 0; i < len; i++) 00089 { 00090 str_llist_type *elt_dirs = kpse_element_dirs (pv[i]); 00091 00092 if (elt_dirs) 00093 { 00094 str_llist_elt_type *dir; 00095 00096 for (dir = *elt_dirs; dir; dir = STR_LLIST_NEXT (*dir)) 00097 { 00098 const std::string elt_dir = STR_LLIST (*dir); 00099 00100 if (! elt_dir.empty ()) 00101 { 00102 if (count == nmax) 00103 nmax *= 2; 00104 00105 retval.resize (nmax); 00106 00107 retval[count++] = elt_dir; 00108 } 00109 } 00110 } 00111 } 00112 00113 retval.resize (count); 00114 } 00115 00116 return retval; 00117 } 00118 00119 std::string 00120 dir_path::find_first (const std::string& nm) 00121 { 00122 return initialized ? kpse_path_search (p, nm, true) : std::string (); 00123 } 00124 00125 string_vector 00126 dir_path::find_all (const std::string& nm) 00127 { 00128 return initialized ? kpse_all_path_search (p, nm) : string_vector (); 00129 } 00130 00131 std::string 00132 dir_path::find_first_of (const string_vector& names) 00133 { 00134 return initialized 00135 ? kpse_path_find_first_of (p, names, true) : std::string (); 00136 } 00137 00138 string_vector 00139 dir_path::find_all_first_of (const string_vector& names) 00140 { 00141 return initialized 00142 ? kpse_all_path_find_first_of (p, names) : string_vector (); 00143 } 00144 00145 void 00146 dir_path::init (void) 00147 { 00148 static bool octave_kpathsea_initialized = false; 00149 00150 if (! octave_kpathsea_initialized) 00151 { 00152 std::string val = octave_env::getenv ("KPATHSEA_DEBUG"); 00153 00154 if (! val.empty ()) 00155 kpathsea_debug |= atoi (val.c_str ()); 00156 00157 octave_kpathsea_initialized = true; 00158 } 00159 00160 p = kpse_path_expand (p_default.empty () 00161 ? p_orig : kpse_expand_default (p_orig, p_default)); 00162 00163 int count = 0; 00164 for (kpse_path_iterator pi (p); pi != std::string::npos; pi++) 00165 count++; 00166 00167 pv.resize (count); 00168 00169 kpse_path_iterator pi (p); 00170 00171 for (int i = 0; i < count; i++) 00172 pv[i] = *pi++; 00173 00174 initialized = true; 00175 }