1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2021 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 
40 namespace octave
41 {
directory_path(const std::string & s)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 
elements(void)50   std::list<std::string> directory_path::elements (void)
51   {
52     return m_initialized ? m_path_elements : std::list<std::string> ();
53   }
54 
all_directories(void)55   std::list<std::string> directory_path::all_directories (void)
56   {
57     std::list<std::string> retval;
58 
59     if (m_initialized)
60       {
61         for (const auto& elt : m_path_elements)
62           {
63             std::string elt_dir = kpse_element_dir (elt);
64 
65             if (! elt_dir.empty ())
66               retval.push_back (elt_dir);
67           }
68       }
69 
70     return retval;
71   }
72 
find_first(const std::string & nm)73   std::string directory_path::find_first (const std::string& nm)
74   {
75     return m_initialized ? kpse_path_search (m_expanded_path, nm) : "";
76   }
77 
find_all(const std::string & nm)78   std::list<std::string> directory_path::find_all (const std::string& nm)
79   {
80     return (m_initialized
81             ? kpse_all_path_search (m_expanded_path, nm)
82             : std::list<std::string> ());
83   }
84 
85   std::string
find_first_of(const std::list<std::string> & names)86   directory_path::find_first_of (const std::list<std::string>& names)
87   {
88     return (m_initialized
89             ? kpse_path_find_first_of (m_expanded_path, names) : "");
90   }
91 
92   std::list<std::string>
find_all_first_of(const std::list<std::string> & names)93   directory_path::find_all_first_of (const std::list<std::string>& names)
94   {
95     return (m_initialized
96             ? kpse_all_path_find_first_of (m_expanded_path, names)
97             : std::list<std::string> ());
98   }
99 
init(void)100   void directory_path::init (void)
101   {
102     static bool octave_kpse_initialized = false;
103 
104     if (! octave_kpse_initialized)
105       {
106         std::string val = sys::env::getenv ("KPATHSEA_DEBUG");
107 
108         if (! val.empty ())
109           kpse_debug |= atoi (val.c_str ());
110 
111         octave_kpse_initialized = true;
112       }
113 
114     m_expanded_path = kpse_path_expand (m_orig_path);
115 
116     for (kpse_path_iterator pi (m_expanded_path); pi != std::string::npos; pi++)
117       m_path_elements.push_back (*pi);
118 
119     m_initialized = true;
120   }
121 
path_sep_char(void)122   char directory_path::path_sep_char (void)
123   {
124     return SEPCHAR;
125   }
126 
path_sep_str(void)127   std::string directory_path::path_sep_str (void)
128   {
129     return SEPCHAR_STR;
130   }
131 }
132