1786
|
1 /* |
|
2 |
|
3 Copyright (C) 1996 John W. Eaton |
|
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 |
|
23 #include <cstdlib> |
|
24 |
|
25 #include <string> |
|
26 |
|
27 #include "pathsearch.h" |
|
28 #include "str-vec.h" |
|
29 |
|
30 extern "C" |
|
31 { |
|
32 // Have to redefine these because they conflict with new C++ keywords |
|
33 // or otherwise cause trouble... |
|
34 #define boolean kpse_boolean |
|
35 #define true kpse_true |
|
36 #define false kpse_false |
|
37 #define string kpse_string |
|
38 |
|
39 #include <kpathsea/pathsearch.h> |
|
40 |
|
41 extern unsigned int kpathsea_debug; |
|
42 |
|
43 #undef true |
|
44 #undef false |
|
45 #undef boolean |
|
46 #undef string |
|
47 } |
|
48 |
|
49 bool dir_path::kpathsea_debug_initialized = false; |
|
50 |
|
51 string_vector |
|
52 dir_path::elements (void) |
|
53 { |
|
54 return initialized ? pv : string_vector (); |
|
55 } |
|
56 |
|
57 string_vector |
|
58 dir_path::all_directories (void) |
|
59 { |
|
60 int count = 0; |
|
61 string_vector retval; |
|
62 |
|
63 if (initialized) |
|
64 { |
|
65 int len = pv.length (); |
|
66 |
|
67 int nmax = len > 32 ? len : 32; |
|
68 |
|
69 retval.resize (len); |
|
70 |
|
71 for (int i = 0; i < len; i++) |
|
72 { |
|
73 str_llist_type *elt_dirs = kpse_element_dirs (pv[i].c_str ()); |
|
74 |
|
75 str_llist_elt_type *dir; |
|
76 for (dir = *elt_dirs; dir; dir = STR_LLIST_NEXT (*dir)) |
|
77 { |
|
78 char *elt_dir = STR_LLIST (*dir); |
|
79 |
|
80 if (elt_dir) |
|
81 { |
|
82 if (count == nmax) |
|
83 nmax *= 2; |
|
84 |
|
85 retval.resize (nmax); |
|
86 |
|
87 retval[count++] = elt_dir; |
|
88 } |
|
89 } |
|
90 } |
|
91 |
|
92 retval.resize (count); |
|
93 } |
|
94 |
|
95 return retval; |
|
96 } |
|
97 |
|
98 string |
|
99 dir_path::find_first (const string& nm) |
|
100 { |
|
101 string retval; |
|
102 |
|
103 if (initialized) |
|
104 { |
|
105 char *tmp = kpse_path_search (p.c_str (), nm.c_str (), |
|
106 kpse_true); |
|
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 |
|
122 kpse_string *tmp = kpse_all_path_search (p.c_str (), nm.c_str ()); |
|
123 |
|
124 if (tmp) |
|
125 { |
|
126 int count = 0; |
|
127 kpse_string *ptr = tmp; |
|
128 while (*ptr++) |
|
129 count++; |
|
130 |
|
131 retval.resize (count); |
|
132 |
|
133 for (int i = 0; i < count; i++) |
2006
|
134 retval[i] = tmp[i]; |
1786
|
135 } |
|
136 |
|
137 return retval; |
|
138 } |
|
139 |
|
140 void |
|
141 dir_path::init (void) |
|
142 { |
|
143 initialized = false; |
|
144 |
|
145 if (! kpathsea_debug_initialized) |
|
146 { |
|
147 char *s = getenv ("KPATHSEA_DEBUG"); |
|
148 |
|
149 if (s) |
|
150 kpathsea_debug |= atoi (s); |
|
151 } |
|
152 |
|
153 int count = 0; |
|
154 char *path_elt = kpse_path_element (p.c_str ()); |
|
155 while (path_elt) |
|
156 { |
|
157 path_elt = kpse_path_element (0); |
|
158 count++; |
|
159 } |
|
160 |
|
161 pv.resize (count); |
|
162 |
|
163 path_elt = kpse_path_element (p.c_str ()); |
|
164 |
|
165 for (int i = 0; i < count; i++) |
|
166 { |
|
167 pv[i] = path_elt; |
|
168 path_elt = kpse_path_element (0); |
|
169 } |
|
170 |
|
171 initialized = true; |
|
172 } |
|
173 |
|
174 /* |
|
175 ;;; Local Variables: *** |
|
176 ;;; mode: C++ *** |
|
177 ;;; End: *** |
|
178 */ |