1786
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1786
|
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 2, or (at your option) any |
|
10 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, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
2021
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
1786
|
27 #include <cstdlib> |
|
28 |
|
29 #include <string> |
|
30 |
3024
|
31 #include "lo-utils.h" |
1786
|
32 #include "pathsearch.h" |
|
33 #include "str-vec.h" |
3024
|
34 #include "str-vec.h" |
1786
|
35 |
|
36 extern "C" |
|
37 { |
|
38 // Have to redefine these because they conflict with new C++ keywords |
|
39 // or otherwise cause trouble... |
|
40 #define string kpse_string |
|
41 |
3196
|
42 #include <kpathsea/default.h> |
|
43 #include <kpathsea/expand.h> |
1786
|
44 #include <kpathsea/pathsearch.h> |
3024
|
45 #include <kpathsea/progname.h> |
1786
|
46 |
|
47 extern unsigned int kpathsea_debug; |
|
48 |
|
49 #undef string |
|
50 } |
|
51 |
3174
|
52 static bool octave_kpathsea_initialized = false; |
1786
|
53 |
|
54 string_vector |
|
55 dir_path::elements (void) |
|
56 { |
|
57 return initialized ? pv : string_vector (); |
|
58 } |
|
59 |
|
60 string_vector |
|
61 dir_path::all_directories (void) |
|
62 { |
|
63 int count = 0; |
|
64 string_vector retval; |
|
65 |
|
66 if (initialized) |
|
67 { |
|
68 int len = pv.length (); |
|
69 |
|
70 int nmax = len > 32 ? len : 32; |
|
71 |
|
72 retval.resize (len); |
|
73 |
|
74 for (int i = 0; i < len; i++) |
|
75 { |
|
76 str_llist_type *elt_dirs = kpse_element_dirs (pv[i].c_str ()); |
|
77 |
3415
|
78 if (elt_dirs) |
1786
|
79 { |
3415
|
80 str_llist_elt_type *dir; |
1786
|
81 |
3415
|
82 for (dir = *elt_dirs; dir; dir = STR_LLIST_NEXT (*dir)) |
1786
|
83 { |
3415
|
84 char *elt_dir = STR_LLIST (*dir); |
1786
|
85 |
3415
|
86 if (elt_dir) |
|
87 { |
|
88 if (count == nmax) |
|
89 nmax *= 2; |
1786
|
90 |
3415
|
91 retval.resize (nmax); |
|
92 |
|
93 retval[count++] = elt_dir; |
|
94 } |
1786
|
95 } |
|
96 } |
|
97 } |
|
98 |
|
99 retval.resize (count); |
|
100 } |
|
101 |
|
102 return retval; |
|
103 } |
|
104 |
3504
|
105 std::string |
|
106 dir_path::find_first (const std::string& nm) |
1786
|
107 { |
3504
|
108 std::string retval; |
1786
|
109 |
|
110 if (initialized) |
|
111 { |
3024
|
112 char *tmp = kpse_path_search (p.c_str (), nm.c_str (), true); |
1786
|
113 if (tmp) |
|
114 { |
|
115 retval = tmp; |
|
116 free (tmp); |
|
117 } |
|
118 } |
|
119 |
|
120 return retval; |
|
121 } |
|
122 |
|
123 string_vector |
3504
|
124 dir_path::find_all (const std::string& nm) |
1786
|
125 { |
|
126 string_vector retval; |
|
127 |
3174
|
128 if (initialized) |
1786
|
129 { |
3174
|
130 kpse_string *tmp = kpse_all_path_search (p.c_str (), nm.c_str ()); |
1786
|
131 |
3174
|
132 if (tmp) |
|
133 { |
|
134 int count = 0; |
|
135 kpse_string *ptr = tmp; |
|
136 while (*ptr++) |
|
137 count++; |
1786
|
138 |
3174
|
139 retval.resize (count); |
|
140 |
|
141 for (int i = 0; i < count; i++) |
|
142 retval[i] = tmp[i]; |
|
143 } |
1786
|
144 } |
|
145 |
|
146 return retval; |
|
147 } |
|
148 |
|
149 void |
3504
|
150 dir_path::set_program_name (const std::string& nm) |
3024
|
151 { |
3174
|
152 kpse_set_progname (nm.c_str ()); |
3024
|
153 } |
|
154 |
|
155 void |
1786
|
156 dir_path::init (void) |
|
157 { |
3174
|
158 if (! octave_kpathsea_initialized) |
1786
|
159 { |
|
160 char *s = getenv ("KPATHSEA_DEBUG"); |
|
161 |
|
162 if (s) |
|
163 kpathsea_debug |= atoi (s); |
3174
|
164 |
|
165 octave_kpathsea_initialized = true; |
1786
|
166 } |
|
167 |
3185
|
168 kpse_clear_dir_cache (); |
|
169 |
3196
|
170 char *t1 = 0; |
|
171 |
|
172 if (p_default.empty ()) |
|
173 t1 = kpse_path_expand (p_orig.c_str ()); |
|
174 else |
3174
|
175 { |
3196
|
176 char *t2 = kpse_expand_default (p_orig.c_str (), p_default.c_str ()); |
|
177 |
|
178 t1 = kpse_path_expand (t2); |
|
179 |
|
180 if (t2) |
|
181 free (t2); |
|
182 } |
|
183 |
|
184 if (t1) |
|
185 { |
|
186 p = t1; |
|
187 free (t1); |
3174
|
188 } |
|
189 else |
3504
|
190 p = std::string (); |
3174
|
191 |
1786
|
192 int count = 0; |
|
193 char *path_elt = kpse_path_element (p.c_str ()); |
|
194 while (path_elt) |
|
195 { |
|
196 path_elt = kpse_path_element (0); |
|
197 count++; |
|
198 } |
|
199 |
|
200 pv.resize (count); |
|
201 |
|
202 path_elt = kpse_path_element (p.c_str ()); |
|
203 |
|
204 for (int i = 0; i < count; i++) |
|
205 { |
|
206 pv[i] = path_elt; |
|
207 path_elt = kpse_path_element (0); |
|
208 } |
|
209 |
|
210 initialized = true; |
|
211 } |
|
212 |
|
213 /* |
|
214 ;;; Local Variables: *** |
|
215 ;;; mode: C++ *** |
|
216 ;;; End: *** |
|
217 */ |