GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
pathsearch.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2013 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 3 of the License, or (at your
10 option) any 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, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <cstdlib>
28 
29 #include <string>
30 
31 #include "lo-utils.h"
32 #include "oct-env.h"
33 #include "pathsearch.h"
34 #include "singleton-cleanup.h"
35 #include "str-vec.h"
36 
37 #include "kpse.cc"
38 
40 
42  : xpath_sep_char (SEPCHAR), xpath_sep_str (SEPCHAR_STR) { }
43 
44 bool
46 {
47  bool retval = true;
48 
49  if (! instance)
50  {
51  instance = new static_members ();
52 
53  if (instance)
54  singleton_cleanup_list::add (cleanup_instance);
55  }
56 
57  if (! instance)
58  {
59  (*current_liboctave_error_handler)
60  ("unable to create dir_path::static_members object!");
61 
62  retval = false;
63  }
64 
65  return retval;
66 }
67 
70 {
71  return initialized ? pv : string_vector ();
72 }
73 
76 {
77  int count = 0;
78  string_vector retval;
79 
80  if (initialized)
81  {
82  int len = pv.length ();
83 
84  int nmax = len > 32 ? len : 32;
85 
86  retval.resize (len);
87 
88  for (int i = 0; i < len; i++)
89  {
90  str_llist_type *elt_dirs = kpse_element_dirs (pv[i]);
91 
92  if (elt_dirs)
93  {
94  str_llist_elt_type *dir;
95 
96  for (dir = *elt_dirs; dir; dir = STR_LLIST_NEXT (*dir))
97  {
98  const std::string elt_dir = STR_LLIST (*dir);
99 
100  if (! elt_dir.empty ())
101  {
102  if (count == nmax)
103  nmax *= 2;
104 
105  retval.resize (nmax);
106 
107  retval[count++] = elt_dir;
108  }
109  }
110  }
111  }
112 
113  retval.resize (count);
114  }
115 
116  return retval;
117 }
118 
119 std::string
120 dir_path::find_first (const std::string& nm)
121 {
122  return initialized ? kpse_path_search (p, nm, true) : std::string ();
123 }
124 
126 dir_path::find_all (const std::string& nm)
127 {
128  return initialized ? kpse_all_path_search (p, nm) : string_vector ();
129 }
130 
131 std::string
133 {
134  return initialized
135  ? kpse_path_find_first_of (p, names, true) : std::string ();
136 }
137 
140 {
141  return initialized
143 }
144 
145 void
147 {
148  static bool octave_kpathsea_initialized = false;
149 
150  if (! octave_kpathsea_initialized)
151  {
152  std::string val = octave_env::getenv ("KPATHSEA_DEBUG");
153 
154  if (! val.empty ())
155  kpathsea_debug |= atoi (val.c_str ());
156 
157  octave_kpathsea_initialized = true;
158  }
159 
160  p = kpse_path_expand (p_default.empty ()
162 
163  int count = 0;
164  for (kpse_path_iterator pi (p); pi != std::string::npos; pi++)
165  count++;
166 
167  pv.resize (count);
168 
169  kpse_path_iterator pi (p);
170 
171  for (int i = 0; i < count; i++)
172  pv[i] = *pi++;
173 
174  initialized = true;
175 }