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" |
3505
|
32 #include "oct-kpse.h" |
1786
|
33 #include "pathsearch.h" |
|
34 #include "str-vec.h" |
3024
|
35 #include "str-vec.h" |
1786
|
36 |
3174
|
37 static bool octave_kpathsea_initialized = false; |
1786
|
38 |
|
39 string_vector |
|
40 dir_path::elements (void) |
|
41 { |
|
42 return initialized ? pv : string_vector (); |
|
43 } |
|
44 |
|
45 string_vector |
|
46 dir_path::all_directories (void) |
|
47 { |
|
48 int count = 0; |
|
49 string_vector retval; |
|
50 |
|
51 if (initialized) |
|
52 { |
|
53 int len = pv.length (); |
|
54 |
|
55 int nmax = len > 32 ? len : 32; |
|
56 |
|
57 retval.resize (len); |
|
58 |
|
59 for (int i = 0; i < len; i++) |
|
60 { |
3505
|
61 str_llist_type *elt_dirs |
|
62 = ::octave_kpse_element_dirs (pv[i].c_str ()); |
1786
|
63 |
3415
|
64 if (elt_dirs) |
1786
|
65 { |
3415
|
66 str_llist_elt_type *dir; |
1786
|
67 |
3415
|
68 for (dir = *elt_dirs; dir; dir = STR_LLIST_NEXT (*dir)) |
1786
|
69 { |
3415
|
70 char *elt_dir = STR_LLIST (*dir); |
1786
|
71 |
3415
|
72 if (elt_dir) |
|
73 { |
|
74 if (count == nmax) |
|
75 nmax *= 2; |
1786
|
76 |
3415
|
77 retval.resize (nmax); |
|
78 |
|
79 retval[count++] = elt_dir; |
|
80 } |
1786
|
81 } |
|
82 } |
|
83 } |
|
84 |
|
85 retval.resize (count); |
|
86 } |
|
87 |
|
88 return retval; |
|
89 } |
|
90 |
3504
|
91 std::string |
|
92 dir_path::find_first (const std::string& nm) |
1786
|
93 { |
3504
|
94 std::string retval; |
1786
|
95 |
|
96 if (initialized) |
|
97 { |
3505
|
98 char *tmp = ::octave_kpse_path_search (p.c_str (), nm.c_str (), true); |
1786
|
99 if (tmp) |
|
100 { |
|
101 retval = tmp; |
|
102 free (tmp); |
|
103 } |
|
104 } |
|
105 |
|
106 return retval; |
|
107 } |
|
108 |
|
109 string_vector |
3504
|
110 dir_path::find_all (const std::string& nm) |
1786
|
111 { |
|
112 string_vector retval; |
|
113 |
3174
|
114 if (initialized) |
1786
|
115 { |
3505
|
116 char **tmp = ::octave_kpse_all_path_search (p.c_str (), nm.c_str ()); |
1786
|
117 |
3174
|
118 if (tmp) |
|
119 { |
|
120 int count = 0; |
3505
|
121 char **ptr = tmp; |
3174
|
122 while (*ptr++) |
|
123 count++; |
1786
|
124 |
3174
|
125 retval.resize (count); |
|
126 |
|
127 for (int i = 0; i < count; i++) |
|
128 retval[i] = tmp[i]; |
|
129 } |
1786
|
130 } |
|
131 |
|
132 return retval; |
|
133 } |
|
134 |
|
135 void |
3504
|
136 dir_path::set_program_name (const std::string& nm) |
3024
|
137 { |
3505
|
138 ::octave_kpse_set_progname (nm.c_str ()); |
3024
|
139 } |
|
140 |
|
141 void |
1786
|
142 dir_path::init (void) |
|
143 { |
3174
|
144 if (! octave_kpathsea_initialized) |
1786
|
145 { |
|
146 char *s = getenv ("KPATHSEA_DEBUG"); |
|
147 |
|
148 if (s) |
|
149 kpathsea_debug |= atoi (s); |
3174
|
150 |
|
151 octave_kpathsea_initialized = true; |
1786
|
152 } |
|
153 |
3505
|
154 ::octave_kpse_clear_dir_cache (); |
3185
|
155 |
3196
|
156 char *t1 = 0; |
|
157 |
|
158 if (p_default.empty ()) |
3505
|
159 t1 = ::octave_kpse_path_expand (p_orig.c_str ()); |
3196
|
160 else |
3174
|
161 { |
3505
|
162 char *t2 |
|
163 = ::octave_kpse_expand_default (p_orig.c_str (), p_default.c_str ()); |
3196
|
164 |
3505
|
165 t1 = ::octave_kpse_path_expand (t2); |
3196
|
166 |
|
167 if (t2) |
|
168 free (t2); |
|
169 } |
|
170 |
|
171 if (t1) |
|
172 { |
|
173 p = t1; |
|
174 free (t1); |
3174
|
175 } |
|
176 else |
3504
|
177 p = std::string (); |
3174
|
178 |
1786
|
179 int count = 0; |
3505
|
180 char *path_elt = ::octave_kpse_path_element (p.c_str ()); |
1786
|
181 while (path_elt) |
|
182 { |
3505
|
183 path_elt = ::octave_kpse_path_element (0); |
1786
|
184 count++; |
|
185 } |
|
186 |
|
187 pv.resize (count); |
|
188 |
3505
|
189 path_elt = ::octave_kpse_path_element (p.c_str ()); |
1786
|
190 |
|
191 for (int i = 0; i < count; i++) |
|
192 { |
|
193 pv[i] = path_elt; |
3505
|
194 path_elt = ::octave_kpse_path_element (0); |
1786
|
195 } |
|
196 |
|
197 initialized = true; |
|
198 } |
|
199 |
|
200 /* |
|
201 ;;; Local Variables: *** |
|
202 ;;; mode: C++ *** |
|
203 ;;; End: *** |
|
204 */ |