Mercurial > octave-nkf
annotate liboctave/util/pathsearch.cc @ 20595:c1a6c31ac29a
eliminate more simple uses of error_state
* ov-classdef.cc: Eliminate simple uses of error_state.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 06 Oct 2015 00:20:02 -0400 |
parents | a9574e3c6e9e |
children |
rev | line source |
---|---|
1786 | 1 /* |
2 | |
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17769
diff
changeset
|
3 Copyright (C) 1996-2015 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 | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
1786 | 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 | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
1786 | 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" |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
34 #include "singleton-cleanup.h" |
3024 | 35 #include "str-vec.h" |
1786 | 36 |
4399 | 37 #include "kpse.cc" |
4378 | 38 |
8008
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
39 dir_path::static_members *dir_path::static_members::instance = 0; |
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
40 |
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
41 dir_path::static_members::static_members (void) |
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
42 : xpath_sep_char (SEPCHAR), xpath_sep_str (SEPCHAR_STR) { } |
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
43 |
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
44 bool |
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
45 dir_path::static_members::instance_ok (void) |
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
46 { |
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
47 bool retval = true; |
5777 | 48 |
8008
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
49 if (! instance) |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
50 { |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
51 instance = new static_members (); |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
52 |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
53 if (instance) |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
54 singleton_cleanup_list::add (cleanup_instance); |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
55 } |
5777 | 56 |
8008
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
57 if (! instance) |
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
58 { |
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
59 (*current_liboctave_error_handler) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
60 ("unable to create dir_path::static_members object!"); |
8008
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
61 |
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
62 retval = false; |
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
63 } |
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
64 |
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
65 return retval; |
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
66 } |
1786 | 67 |
68 string_vector | |
69 dir_path::elements (void) | |
70 { | |
71 return initialized ? pv : string_vector (); | |
72 } | |
73 | |
74 string_vector | |
75 dir_path::all_directories (void) | |
76 { | |
77 int count = 0; | |
78 string_vector retval; | |
79 | |
80 if (initialized) | |
81 { | |
20267
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19731
diff
changeset
|
82 int len = pv.numel (); |
1786 | 83 |
84 int nmax = len > 32 ? len : 32; | |
85 | |
86 retval.resize (len); | |
87 | |
88 for (int i = 0; i < len; i++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
89 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
90 str_llist_type *elt_dirs = kpse_element_dirs (pv[i]); |
1786 | 91 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
92 if (elt_dirs) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
93 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
94 str_llist_elt_type *dir; |
1786 | 95 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
96 for (dir = *elt_dirs; dir; dir = STR_LLIST_NEXT (*dir)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
97 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
98 const std::string elt_dir = STR_LLIST (*dir); |
1786 | 99 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
100 if (! elt_dir.empty ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
101 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
102 if (count == nmax) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
103 nmax *= 2; |
1786 | 104 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
105 retval.resize (nmax); |
3415 | 106 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
107 retval[count++] = elt_dir; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
108 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
109 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
110 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
111 } |
1786 | 112 |
113 retval.resize (count); | |
114 } | |
115 | |
116 return retval; | |
117 } | |
118 | |
3504 | 119 std::string |
120 dir_path::find_first (const std::string& nm) | |
1786 | 121 { |
4390 | 122 return initialized ? kpse_path_search (p, nm, true) : std::string (); |
4242 | 123 } |
124 | |
125 string_vector | |
126 dir_path::find_all (const std::string& nm) | |
127 { | |
4390 | 128 return initialized ? kpse_all_path_search (p, nm) : string_vector (); |
4242 | 129 } |
130 | |
131 std::string | |
132 dir_path::find_first_of (const string_vector& names) | |
133 { | |
4390 | 134 return initialized |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
135 ? kpse_path_find_first_of (p, names, true) : std::string (); |
1786 | 136 } |
137 | |
138 string_vector | |
4242 | 139 dir_path::find_all_first_of (const string_vector& names) |
1786 | 140 { |
4390 | 141 return initialized |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
142 ? kpse_all_path_find_first_of (p, names) : string_vector (); |
1786 | 143 } |
144 | |
145 void | |
146 dir_path::init (void) | |
147 { | |
8008
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
148 static bool octave_kpathsea_initialized = false; |
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
149 |
3174 | 150 if (! octave_kpathsea_initialized) |
1786 | 151 { |
4394 | 152 std::string val = octave_env::getenv ("KPATHSEA_DEBUG"); |
1786 | 153 |
4394 | 154 if (! val.empty ()) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
155 kpathsea_debug |= atoi (val.c_str ()); |
3174 | 156 |
157 octave_kpathsea_initialized = true; | |
1786 | 158 } |
159 | |
4395 | 160 p = kpse_path_expand (p_default.empty () |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
161 ? p_orig : kpse_expand_default (p_orig, p_default)); |
3196 | 162 |
1786 | 163 int count = 0; |
8021 | 164 for (kpse_path_iterator pi (p); pi != std::string::npos; pi++) |
4394 | 165 count++; |
1786 | 166 |
167 pv.resize (count); | |
168 | |
4394 | 169 kpse_path_iterator pi (p); |
1786 | 170 |
171 for (int i = 0; i < count; i++) | |
4398 | 172 pv[i] = *pi++; |
1786 | 173 |
174 initialized = true; | |
175 } |