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 |
|
78 str_llist_elt_type *dir; |
|
79 for (dir = *elt_dirs; dir; dir = STR_LLIST_NEXT (*dir)) |
|
80 { |
|
81 char *elt_dir = STR_LLIST (*dir); |
|
82 |
|
83 if (elt_dir) |
|
84 { |
|
85 if (count == nmax) |
|
86 nmax *= 2; |
|
87 |
|
88 retval.resize (nmax); |
|
89 |
|
90 retval[count++] = elt_dir; |
|
91 } |
|
92 } |
|
93 } |
|
94 |
|
95 retval.resize (count); |
|
96 } |
|
97 |
|
98 return retval; |
|
99 } |
|
100 |
|
101 string |
|
102 dir_path::find_first (const string& nm) |
|
103 { |
|
104 string retval; |
|
105 |
|
106 if (initialized) |
|
107 { |
3024
|
108 char *tmp = kpse_path_search (p.c_str (), nm.c_str (), true); |
1786
|
109 if (tmp) |
|
110 { |
|
111 retval = tmp; |
|
112 free (tmp); |
|
113 } |
|
114 } |
|
115 |
|
116 return retval; |
|
117 } |
|
118 |
|
119 string_vector |
|
120 dir_path::find_all (const string& nm) |
|
121 { |
|
122 string_vector retval; |
|
123 |
3174
|
124 if (initialized) |
1786
|
125 { |
3174
|
126 kpse_string *tmp = kpse_all_path_search (p.c_str (), nm.c_str ()); |
1786
|
127 |
3174
|
128 if (tmp) |
|
129 { |
|
130 int count = 0; |
|
131 kpse_string *ptr = tmp; |
|
132 while (*ptr++) |
|
133 count++; |
1786
|
134 |
3174
|
135 retval.resize (count); |
|
136 |
|
137 for (int i = 0; i < count; i++) |
|
138 retval[i] = tmp[i]; |
|
139 } |
1786
|
140 } |
|
141 |
|
142 return retval; |
|
143 } |
|
144 |
|
145 void |
3174
|
146 dir_path::set_program_name (const string& nm) |
3024
|
147 { |
3174
|
148 kpse_set_progname (nm.c_str ()); |
3024
|
149 } |
|
150 |
|
151 void |
1786
|
152 dir_path::init (void) |
|
153 { |
3174
|
154 if (! octave_kpathsea_initialized) |
1786
|
155 { |
|
156 char *s = getenv ("KPATHSEA_DEBUG"); |
|
157 |
|
158 if (s) |
|
159 kpathsea_debug |= atoi (s); |
3174
|
160 |
|
161 octave_kpathsea_initialized = true; |
1786
|
162 } |
|
163 |
3185
|
164 kpse_clear_dir_cache (); |
|
165 |
3196
|
166 char *t1 = 0; |
|
167 |
|
168 if (p_default.empty ()) |
|
169 t1 = kpse_path_expand (p_orig.c_str ()); |
|
170 else |
3174
|
171 { |
3196
|
172 char *t2 = kpse_expand_default (p_orig.c_str (), p_default.c_str ()); |
|
173 |
|
174 t1 = kpse_path_expand (t2); |
|
175 |
|
176 if (t2) |
|
177 free (t2); |
|
178 } |
|
179 |
|
180 if (t1) |
|
181 { |
|
182 p = t1; |
|
183 free (t1); |
3174
|
184 } |
|
185 else |
|
186 p = string (); |
|
187 |
1786
|
188 int count = 0; |
|
189 char *path_elt = kpse_path_element (p.c_str ()); |
|
190 while (path_elt) |
|
191 { |
|
192 path_elt = kpse_path_element (0); |
|
193 count++; |
|
194 } |
|
195 |
|
196 pv.resize (count); |
|
197 |
|
198 path_elt = kpse_path_element (p.c_str ()); |
|
199 |
|
200 for (int i = 0; i < count; i++) |
|
201 { |
|
202 pv[i] = path_elt; |
|
203 path_elt = kpse_path_element (0); |
|
204 } |
|
205 |
|
206 initialized = true; |
|
207 } |
|
208 |
|
209 /* |
|
210 ;;; Local Variables: *** |
|
211 ;;; mode: C++ *** |
|
212 ;;; End: *** |
|
213 */ |