GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
pathsearch.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <cstdlib>
31 
32 #include <string>
33 
34 #include "kpse.h"
35 #include "lo-error.h"
36 #include "lo-utils.h"
37 #include "oct-env.h"
38 #include "pathsearch.h"
39 
41 
42 directory_path::directory_path (const std::string& s)
43  : m_orig_path (s), m_initialized (false), m_expanded_path (),
44  m_path_elements ()
45 {
46  if (! m_orig_path.empty ())
47  init ();
48 }
49 
50 std::list<std::string>
52 {
53  return m_initialized ? m_path_elements : std::list<std::string> ();
54 }
55 
56 std::list<std::string>
58 {
59  std::list<std::string> retval;
60 
61  if (m_initialized)
62  {
63  for (const auto& elt : m_path_elements)
64  {
65  std::string elt_dir = kpse_element_dir (elt);
66 
67  if (! elt_dir.empty ())
68  retval.push_back (elt_dir);
69  }
70  }
71 
72  return retval;
73 }
74 
75 std::string
76 directory_path::find_first (const std::string& nm)
77 {
78  return m_initialized ? kpse_path_search (m_expanded_path, nm) : "";
79 }
80 
81 std::list<std::string>
82 directory_path::find_all (const std::string& nm)
83 {
84  return (m_initialized
85  ? kpse_all_path_search (m_expanded_path, nm)
86  : std::list<std::string> ());
87 }
88 
89 std::string
90 directory_path::find_first_of (const std::list<std::string>& names)
91 {
92  return (m_initialized
93  ? kpse_path_find_first_of (m_expanded_path, names) : "");
94 }
95 
96 std::list<std::string>
97 directory_path::find_all_first_of (const std::list<std::string>& names)
98 {
99  return (m_initialized
100  ? kpse_all_path_find_first_of (m_expanded_path, names)
101  : std::list<std::string> ());
102 }
103 
104 void
105 directory_path::init ()
106 {
107  static bool octave_kpse_initialized = false;
108 
109  if (! octave_kpse_initialized)
110  {
111  std::string val = sys::env::getenv ("KPATHSEA_DEBUG");
112 
113  if (! val.empty ())
114  kpse_debug |= atoi (val.c_str ());
115 
116  octave_kpse_initialized = true;
117  }
118 
119  m_expanded_path = kpse_path_expand (m_orig_path);
120 
121  for (kpse_path_iterator pi (m_expanded_path); pi != std::string::npos; pi++)
122  m_path_elements.push_back (*pi);
123 
124  m_initialized = true;
125 }
126 
127 char
129 {
130  return SEPCHAR;
131 }
132 
133 std::string
135 {
136  return SEPCHAR_STR;
137 }
138 
139 OCTAVE_END_NAMESPACE(octave)
std::list< std::string > find_all(const std::string &)
Definition: pathsearch.cc:82
std::list< std::string > all_directories()
Definition: pathsearch.cc:57
std::string find_first_of(const std::list< std::string > &names)
Definition: pathsearch.cc:90
std::string find_first(const std::string &)
Definition: pathsearch.cc:76
std::list< std::string > find_all_first_of(const std::list< std::string > &names)
Definition: pathsearch.cc:97
static std::string path_sep_str()
Definition: pathsearch.cc:134
static char path_sep_char()
Definition: pathsearch.cc:128
std::list< std::string > elements()
Definition: pathsearch.cc:51
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
std::string kpse_path_find_first_of(const std::string &path, const std::list< std::string > &names)
Definition: kpse.cc:730
std::string kpse_element_dir(const std::string &elt)
Definition: kpse.cc:1025
std::string kpse_path_expand(const std::string &path)
Definition: kpse.cc:757
std::list< std::string > kpse_all_path_find_first_of(const std::string &path, const std::list< std::string > &names)
Definition: kpse.cc:746
unsigned int kpse_debug
Definition: kpse.cc:91
std::list< std::string > kpse_all_path_search(const std::string &path, const std::string &name)
Definition: kpse.cc:566
std::string kpse_path_search(const std::string &path, const std::string &name)
Definition: kpse.cc:555