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" |
3833
|
32 #include "oct-env.h" |
1786
|
33 #include "pathsearch.h" |
|
34 #include "str-vec.h" |
3024
|
35 #include "str-vec.h" |
1786
|
36 |
4378
|
37 #include "kpse.h" |
|
38 |
3174
|
39 static bool octave_kpathsea_initialized = false; |
1786
|
40 |
|
41 string_vector |
|
42 dir_path::elements (void) |
|
43 { |
|
44 return initialized ? pv : string_vector (); |
|
45 } |
|
46 |
|
47 string_vector |
|
48 dir_path::all_directories (void) |
|
49 { |
|
50 int count = 0; |
|
51 string_vector retval; |
|
52 |
|
53 if (initialized) |
|
54 { |
|
55 int len = pv.length (); |
|
56 |
|
57 int nmax = len > 32 ? len : 32; |
|
58 |
|
59 retval.resize (len); |
|
60 |
|
61 for (int i = 0; i < len; i++) |
|
62 { |
3505
|
63 str_llist_type *elt_dirs |
4378
|
64 = kpse_element_dirs (pv[i].c_str ()); |
1786
|
65 |
3415
|
66 if (elt_dirs) |
1786
|
67 { |
3415
|
68 str_llist_elt_type *dir; |
1786
|
69 |
3415
|
70 for (dir = *elt_dirs; dir; dir = STR_LLIST_NEXT (*dir)) |
1786
|
71 { |
4390
|
72 const std::string elt_dir = STR_LLIST (*dir); |
1786
|
73 |
4390
|
74 if (! elt_dir.empty ()) |
3415
|
75 { |
|
76 if (count == nmax) |
|
77 nmax *= 2; |
1786
|
78 |
3415
|
79 retval.resize (nmax); |
|
80 |
|
81 retval[count++] = elt_dir; |
|
82 } |
1786
|
83 } |
|
84 } |
|
85 } |
|
86 |
|
87 retval.resize (count); |
|
88 } |
|
89 |
|
90 return retval; |
|
91 } |
|
92 |
3504
|
93 std::string |
|
94 dir_path::find_first (const std::string& nm) |
1786
|
95 { |
4390
|
96 return initialized ? kpse_path_search (p, nm, true) : std::string (); |
4242
|
97 } |
|
98 |
|
99 string_vector |
|
100 dir_path::find_all (const std::string& nm) |
|
101 { |
4390
|
102 return initialized ? kpse_all_path_search (p, nm) : string_vector (); |
4242
|
103 } |
|
104 |
|
105 std::string |
|
106 dir_path::find_first_of (const string_vector& names) |
|
107 { |
4390
|
108 return initialized |
|
109 ? kpse_path_find_first_of (p, names, true) : std::string (); |
1786
|
110 } |
|
111 |
|
112 string_vector |
4242
|
113 dir_path::find_all_first_of (const string_vector& names) |
1786
|
114 { |
4390
|
115 return initialized |
|
116 ? kpse_all_path_find_first_of (p, names) : string_vector (); |
1786
|
117 } |
|
118 |
|
119 void |
|
120 dir_path::init (void) |
|
121 { |
3174
|
122 if (! octave_kpathsea_initialized) |
1786
|
123 { |
|
124 char *s = getenv ("KPATHSEA_DEBUG"); |
|
125 |
|
126 if (s) |
|
127 kpathsea_debug |= atoi (s); |
3174
|
128 |
|
129 octave_kpathsea_initialized = true; |
1786
|
130 } |
|
131 |
3196
|
132 if (p_default.empty ()) |
4392
|
133 p = kpse_path_expand (p_orig); |
3196
|
134 else |
3174
|
135 { |
3505
|
136 char *t2 |
4378
|
137 = kpse_expand_default (p_orig.c_str (), p_default.c_str ()); |
3196
|
138 |
4392
|
139 p = kpse_path_expand (t2); |
3196
|
140 |
|
141 if (t2) |
|
142 free (t2); |
|
143 } |
|
144 |
1786
|
145 int count = 0; |
4378
|
146 char *path_elt = kpse_path_element (p.c_str ()); |
1786
|
147 while (path_elt) |
|
148 { |
4378
|
149 path_elt = kpse_path_element (0); |
1786
|
150 count++; |
|
151 } |
|
152 |
|
153 pv.resize (count); |
|
154 |
4378
|
155 path_elt = kpse_path_element (p.c_str ()); |
1786
|
156 |
|
157 for (int i = 0; i < count; i++) |
|
158 { |
|
159 pv[i] = path_elt; |
4378
|
160 path_elt = kpse_path_element (0); |
1786
|
161 } |
|
162 |
|
163 initialized = true; |
|
164 } |
|
165 |
|
166 /* |
|
167 ;;; Local Variables: *** |
|
168 ;;; mode: C++ *** |
|
169 ;;; End: *** |
|
170 */ |