1786
|
1 // pathsearch.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1996 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #include <cstdlib> |
|
25 |
|
26 #include <string> |
|
27 |
|
28 #include "pathsearch.h" |
|
29 #include "str-vec.h" |
|
30 |
|
31 extern "C" |
|
32 { |
|
33 // Have to redefine these because they conflict with new C++ keywords |
|
34 // or otherwise cause trouble... |
|
35 #define boolean kpse_boolean |
|
36 #define true kpse_true |
|
37 #define false kpse_false |
|
38 #define string kpse_string |
|
39 |
|
40 #include <kpathsea/pathsearch.h> |
|
41 |
|
42 extern unsigned int kpathsea_debug; |
|
43 |
|
44 #undef true |
|
45 #undef false |
|
46 #undef boolean |
|
47 #undef string |
|
48 } |
|
49 |
|
50 bool dir_path::kpathsea_debug_initialized = false; |
|
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 { |
|
106 char *tmp = kpse_path_search (p.c_str (), nm.c_str (), |
|
107 kpse_true); |
|
108 if (tmp) |
|
109 { |
|
110 retval = tmp; |
|
111 free (tmp); |
|
112 } |
|
113 } |
|
114 |
|
115 return retval; |
|
116 } |
|
117 |
|
118 string_vector |
|
119 dir_path::find_all (const string& nm) |
|
120 { |
|
121 string_vector retval; |
|
122 |
|
123 kpse_string *tmp = kpse_all_path_search (p.c_str (), nm.c_str ()); |
|
124 |
|
125 if (tmp) |
|
126 { |
|
127 int count = 0; |
|
128 kpse_string *ptr = tmp; |
|
129 while (*ptr++) |
|
130 count++; |
|
131 |
|
132 retval.resize (count); |
|
133 |
|
134 for (int i = 0; i < count; i++) |
|
135 retval[i] = *tmp[i]; |
|
136 } |
|
137 |
|
138 return retval; |
|
139 } |
|
140 |
|
141 void |
|
142 dir_path::init (void) |
|
143 { |
|
144 initialized = false; |
|
145 |
|
146 if (! kpathsea_debug_initialized) |
|
147 { |
|
148 char *s = getenv ("KPATHSEA_DEBUG"); |
|
149 |
|
150 if (s) |
|
151 kpathsea_debug |= atoi (s); |
|
152 } |
|
153 |
|
154 int count = 0; |
|
155 char *path_elt = kpse_path_element (p.c_str ()); |
|
156 while (path_elt) |
|
157 { |
|
158 path_elt = kpse_path_element (0); |
|
159 count++; |
|
160 } |
|
161 |
|
162 pv.resize (count); |
|
163 |
|
164 path_elt = kpse_path_element (p.c_str ()); |
|
165 |
|
166 for (int i = 0; i < count; i++) |
|
167 { |
|
168 pv[i] = path_elt; |
|
169 path_elt = kpse_path_element (0); |
|
170 } |
|
171 |
|
172 initialized = true; |
|
173 } |
|
174 |
|
175 /* |
|
176 ;;; Local Variables: *** |
|
177 ;;; mode: C++ *** |
|
178 ;;; page-delimiter: "^/\\*" *** |
|
179 ;;; End: *** |
|
180 */ |