1786
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2005, 2006, |
|
4 2007 John W. Eaton |
1786
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
1786
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
1786
|
21 |
|
22 */ |
|
23 |
2021
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
1786
|
28 #include <cstdlib> |
|
29 |
|
30 #include <string> |
|
31 |
3024
|
32 #include "lo-utils.h" |
3833
|
33 #include "oct-env.h" |
1786
|
34 #include "pathsearch.h" |
|
35 #include "str-vec.h" |
3024
|
36 #include "str-vec.h" |
1786
|
37 |
4399
|
38 #include "kpse.cc" |
4378
|
39 |
5777
|
40 char dir_path::path_sep_char = SEPCHAR; |
|
41 |
|
42 std::string dir_path::path_sep_str (SEPCHAR_STR); |
|
43 |
3174
|
44 static bool octave_kpathsea_initialized = false; |
1786
|
45 |
|
46 string_vector |
|
47 dir_path::elements (void) |
|
48 { |
|
49 return initialized ? pv : string_vector (); |
|
50 } |
|
51 |
|
52 string_vector |
|
53 dir_path::all_directories (void) |
|
54 { |
|
55 int count = 0; |
|
56 string_vector retval; |
|
57 |
|
58 if (initialized) |
|
59 { |
|
60 int len = pv.length (); |
|
61 |
|
62 int nmax = len > 32 ? len : 32; |
|
63 |
|
64 retval.resize (len); |
|
65 |
|
66 for (int i = 0; i < len; i++) |
|
67 { |
4394
|
68 str_llist_type *elt_dirs = kpse_element_dirs (pv[i]); |
1786
|
69 |
3415
|
70 if (elt_dirs) |
1786
|
71 { |
3415
|
72 str_llist_elt_type *dir; |
1786
|
73 |
3415
|
74 for (dir = *elt_dirs; dir; dir = STR_LLIST_NEXT (*dir)) |
1786
|
75 { |
4390
|
76 const std::string elt_dir = STR_LLIST (*dir); |
1786
|
77 |
4390
|
78 if (! elt_dir.empty ()) |
3415
|
79 { |
|
80 if (count == nmax) |
|
81 nmax *= 2; |
1786
|
82 |
3415
|
83 retval.resize (nmax); |
|
84 |
|
85 retval[count++] = elt_dir; |
|
86 } |
1786
|
87 } |
|
88 } |
|
89 } |
|
90 |
|
91 retval.resize (count); |
|
92 } |
|
93 |
|
94 return retval; |
|
95 } |
|
96 |
3504
|
97 std::string |
|
98 dir_path::find_first (const std::string& nm) |
1786
|
99 { |
4390
|
100 return initialized ? kpse_path_search (p, nm, true) : std::string (); |
4242
|
101 } |
|
102 |
|
103 string_vector |
|
104 dir_path::find_all (const std::string& nm) |
|
105 { |
4390
|
106 return initialized ? kpse_all_path_search (p, nm) : string_vector (); |
4242
|
107 } |
|
108 |
|
109 std::string |
|
110 dir_path::find_first_of (const string_vector& names) |
|
111 { |
4390
|
112 return initialized |
|
113 ? kpse_path_find_first_of (p, names, true) : std::string (); |
1786
|
114 } |
|
115 |
|
116 string_vector |
4242
|
117 dir_path::find_all_first_of (const string_vector& names) |
1786
|
118 { |
4390
|
119 return initialized |
|
120 ? kpse_all_path_find_first_of (p, names) : string_vector (); |
1786
|
121 } |
|
122 |
|
123 void |
|
124 dir_path::init (void) |
|
125 { |
3174
|
126 if (! octave_kpathsea_initialized) |
1786
|
127 { |
4394
|
128 std::string val = octave_env::getenv ("KPATHSEA_DEBUG"); |
1786
|
129 |
4394
|
130 if (! val.empty ()) |
|
131 kpathsea_debug |= atoi (val.c_str ()); |
3174
|
132 |
|
133 octave_kpathsea_initialized = true; |
1786
|
134 } |
|
135 |
4395
|
136 p = kpse_path_expand (p_default.empty () |
|
137 ? p_orig : kpse_expand_default (p_orig, p_default)); |
3196
|
138 |
1786
|
139 int count = 0; |
4394
|
140 for (kpse_path_iterator pi (p); pi != NPOS; pi++) |
|
141 count++; |
1786
|
142 |
|
143 pv.resize (count); |
|
144 |
4394
|
145 kpse_path_iterator pi (p); |
1786
|
146 |
|
147 for (int i = 0; i < count; i++) |
4398
|
148 pv[i] = *pi++; |
1786
|
149 |
|
150 initialized = true; |
|
151 } |
|
152 |
|
153 /* |
|
154 ;;; Local Variables: *** |
|
155 ;;; mode: C++ *** |
|
156 ;;; End: *** |
|
157 */ |