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