GNU Octave  4.0.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-2015 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 }
static string_vector kpse_all_path_search(const std::string &path, const std::string &name)
Definition: kpse.cc:952
string_vector pv
Definition: pathsearch.h:119
std::string find_first_of(const string_vector &names)
Definition: pathsearch.cc:132
string_vector find_all(const std::string &)
Definition: pathsearch.cc:126
bool initialized
Definition: pathsearch.h:112
static unsigned int kpathsea_debug
Definition: kpse.cc:234
static string_vector names(const map_type &lst)
Definition: help.cc:782
std::string p
Definition: pathsearch.h:116
void init(void)
Definition: pathsearch.cc:146
static string_vector kpse_all_path_find_first_of(const std::string &path, const string_vector &names)
Definition: kpse.cc:1173
static static_members * instance
Definition: pathsearch.h:154
static std::string kpse_path_find_first_of(const std::string &path, const string_vector &names, bool must_exist)
Definition: kpse.cc:1157
static std::string kpse_expand_default(const std::string &path, const std::string &dflt)
Definition: kpse.cc:1985
#define STR_LLIST(sl)
Definition: kpse.cc:326
static void add(fptr f)
void resize(octave_idx_type n, const std::string &rfv=std::string())
Definition: str-vec.h:91
static std::string getenv(const std::string &name)
Definition: oct-env.cc:238
static str_llist_type * kpse_element_dirs(const std::string &elt)
Definition: kpse.cc:2388
string_vector elements(void)
Definition: pathsearch.cc:69
string_vector all_directories(void)
Definition: pathsearch.cc:75
octave_idx_type length(void) const
Number of elements in the array.
Definition: Array.h:267
string_vector find_all_first_of(const string_vector &names)
Definition: pathsearch.cc:139
std::string p_orig
Definition: pathsearch.h:105
#define STR_LLIST_NEXT(sl)
Definition: kpse.cc:328
static std::string kpse_path_expand(const std::string &path)
Definition: kpse.cc:1406
static bool instance_ok(void)
Definition: pathsearch.cc:45
std::string p_default
Definition: pathsearch.h:109
std::string find_first(const std::string &)
Definition: pathsearch.cc:120
static std::string kpse_path_search(const std::string &path, const std::string &name, bool must_exist)
Definition: kpse.cc:937