GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
pathsearch.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2025 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
42directory_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
50std::list<std::string>
52{
53 return m_initialized ? m_path_elements : std::list<std::string> ();
54}
55
56std::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
75std::string
76directory_path::find_first (const std::string& nm)
77{
78 return m_initialized ? kpse_path_search (m_expanded_path, nm) : "";
79}
80
81std::list<std::string>
82directory_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
89std::string
90directory_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
96std::list<std::string>
97directory_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
104void
105directory_path::init ()
106{
107 static bool octave_kpse_initialized = false;
108
109 if (! octave_kpse_initialized)
110 {
111 std::string env_val = sys::env::getenv ("KPATHSEA_DEBUG");
112
113 if (! env_val.empty ())
114 {
115 unsigned int env_debug_flags = 0;
116
117 try
118 {
119 unsigned long val = std::stoul (env_val);
120
121 if (val > std::numeric_limits<unsigned int>::max ())
122 (*current_liboctave_warning_with_id_handler)
123 ("Octave:kpathsea-debug-value-ignored", "directory_path::init: ignoring out of range KPATHSEA_DEBUG value '%s'", env_val.c_str ());
124 else
125 env_debug_flags = val;
126 }
127 catch (const std::invalid_argument&)
128 {
129 (*current_liboctave_warning_with_id_handler)
130 ("Octave:kpathsea-debug-value-ignored", "directory_path::init: ignoring invalid KPATHSEA_DEBUG value '%s'", env_val.c_str ());
131 }
132 catch (const std::out_of_range&)
133 {
134 (*current_liboctave_warning_with_id_handler)
135 ("Octave:kpathsea-debug-value-ignored", "directory_path::init: ignoring out of range KPATHSEA_DEBUG value '%s'", env_val.c_str ());
136 }
137
138 kpse_debug |= env_debug_flags;
139 }
140
141 octave_kpse_initialized = true;
142 }
143
144 m_expanded_path = kpse_path_expand (m_orig_path);
145
146 for (kpse_path_iterator pi (m_expanded_path); pi != std::string::npos; pi++)
147 m_path_elements.push_back (*pi);
148
149 m_initialized = true;
150}
151
152char
154{
155 return SEPCHAR;
156}
157
158std::string
160{
161 return SEPCHAR_STR;
162}
163
164OCTAVE_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()
static char path_sep_char()
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
unsigned int kpse_debug
Definition kpse.cc:91
std::list< std::string > kpse_all_path_find_first_of(const std::string &path, const std::list< std::string > &names)
Definition kpse.cc:746
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