Mercurial > hg > octave-nkf
annotate liboctave/oct-env.cc @ 12541:dd2c70b30f28
Add tests for ifftshift.m
author | Robert T. Short <octave@phaselockedsystems.com.com> |
---|---|
date | Sat, 26 Mar 2011 06:50:12 -0700 |
parents | 12df7854fa7c |
children | 7dd7cccf0757 |
rev | line source |
---|---|
2926 | 1 /* |
2 | |
11523 | 3 Copyright (C) 1996-2011 John W. Eaton |
2926 | 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. | |
2926 | 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/>. | |
2926 | 20 |
21 */ | |
22 | |
23 /* | |
24 | |
25 The functions listed below were adapted from a similar functions | |
26 from GNU Bash, the Bourne Again SHell, copyright (C) 1987, 1989, 1991 | |
27 Free Software Foundation, Inc. | |
28 | |
29 octave_env::do_absolute_pathname | |
30 octave_env::do_base_pathname | |
31 octave_env::do_chdir | |
32 octave_env::do_getcwd | |
33 octave_env::do_make_absolute | |
34 octave_env::do_polite_directory_format | |
35 octave_env::pathname_backup | |
36 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
37 */ |
2926 | 38 |
39 #ifdef HAVE_CONFIG_H | |
40 #include <config.h> | |
41 #endif | |
42 | |
4093 | 43 #include <cctype> |
2926 | 44 #include <cstdlib> |
10463
bbe99b2a5ba7
undo recent gnulib-related changes
John W. Eaton <jwe@octave.org>
parents:
10447
diff
changeset
|
45 #include <cstring> |
4093 | 46 |
3504 | 47 #include <string> |
2926 | 48 |
49 #include <sys/types.h> | |
50 #include <unistd.h> | |
51 | |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
52 #include "progname.h" |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
53 |
4097 | 54 #include "file-ops.h" |
2926 | 55 #include "lo-error.h" |
56 #include "lo-sysdep.h" | |
57 #include "lo-utils.h" | |
58 #include "oct-env.h" | |
2934 | 59 #include "oct-passwd.h" |
2947 | 60 #include "oct-syscalls.h" |
2926 | 61 |
62 octave_env::octave_env (void) | |
63 : follow_symbolic_links (true), verbatim_pwd (true), | |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
64 current_directory (), prog_name (), prog_invocation_name (), |
2926 | 65 user_name (), host_name () |
66 { | |
67 // Get a real value for the current directory. | |
68 do_getcwd (); | |
69 | |
70 // Etc. | |
71 do_get_user_name (); | |
72 | |
73 do_get_host_name (); | |
74 } | |
75 | |
76 octave_env *octave_env::instance = 0; | |
77 | |
78 bool | |
79 octave_env::instance_ok (void) | |
80 { | |
81 bool retval = true; | |
82 | |
83 if (! instance) | |
84 instance = new octave_env (); | |
85 | |
86 if (! instance) | |
87 { | |
88 (*current_liboctave_error_handler) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
89 ("unable to create current working directory object!"); |
2926 | 90 |
91 retval = false; | |
92 } | |
93 | |
94 return retval; | |
95 } | |
96 | |
3504 | 97 std::string |
98 octave_env::polite_directory_format (const std::string& name) | |
2926 | 99 { |
100 return (instance_ok ()) | |
3504 | 101 ? instance->do_polite_directory_format (name) : std::string (); |
2926 | 102 } |
103 | |
104 bool | |
3504 | 105 octave_env::absolute_pathname (const std::string& s) |
2926 | 106 { |
107 return (instance_ok ()) | |
108 ? instance->do_absolute_pathname (s) : false; | |
109 } | |
110 | |
6838 | 111 bool |
112 octave_env::rooted_relative_pathname (const std::string& s) | |
113 { | |
114 return (instance_ok ()) | |
115 ? instance->do_rooted_relative_pathname (s) : false; | |
116 } | |
117 | |
3504 | 118 std::string |
119 octave_env::base_pathname (const std::string& s) | |
2926 | 120 { |
121 return (instance_ok ()) | |
3504 | 122 ? instance->do_base_pathname (s) : std::string (); |
2926 | 123 } |
124 | |
3504 | 125 std::string |
126 octave_env::make_absolute (const std::string& s, const std::string& dot_path) | |
2926 | 127 { |
128 return (instance_ok ()) | |
3504 | 129 ? instance->do_make_absolute (s, dot_path) : std::string (); |
2926 | 130 } |
131 | |
3504 | 132 std::string |
10250 | 133 octave_env::get_current_directory () |
2926 | 134 { |
135 return (instance_ok ()) | |
3504 | 136 ? instance->do_getcwd () : std::string (); |
2926 | 137 } |
138 | |
3504 | 139 std::string |
2926 | 140 octave_env::get_home_directory () |
141 { | |
142 return (instance_ok ()) | |
3504 | 143 ? instance->do_get_home_directory () : std::string (); |
2926 | 144 } |
145 | |
3504 | 146 std::string |
2926 | 147 octave_env::get_program_name (void) |
148 { | |
149 return (instance_ok ()) | |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
150 ? instance->prog_name : std::string (); |
2926 | 151 } |
152 | |
3504 | 153 std::string |
2926 | 154 octave_env::get_program_invocation_name (void) |
155 { | |
156 return (instance_ok ()) | |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
157 ? instance->prog_invocation_name : std::string (); |
2926 | 158 } |
159 | |
160 void | |
3504 | 161 octave_env::set_program_name (const std::string& s) |
2926 | 162 { |
163 if (instance_ok ()) | |
164 instance->do_set_program_name (s); | |
165 } | |
166 | |
3504 | 167 std::string |
2926 | 168 octave_env::get_user_name (void) |
169 { | |
170 return (instance_ok ()) | |
3504 | 171 ? instance->do_get_user_name () : std::string (); |
2926 | 172 } |
173 | |
3504 | 174 std::string |
2926 | 175 octave_env::get_host_name (void) |
176 { | |
177 return (instance_ok ()) | |
3504 | 178 ? instance->do_get_host_name () : std::string (); |
2926 | 179 } |
180 | |
5775 | 181 // FIXME -- this leaves no way to distinguish between a |
2926 | 182 // variable that is not set and one that is set to the empty string. |
183 // Is this a problem? | |
184 | |
3504 | 185 std::string |
186 octave_env::getenv (const std::string& name) | |
2926 | 187 { |
188 return (instance_ok ()) | |
3504 | 189 ? instance->do_getenv (name) : std::string (); |
2926 | 190 } |
191 | |
192 void | |
3504 | 193 octave_env::putenv (const std::string& name, const std::string& value) |
2926 | 194 { |
195 octave_putenv (name, value); | |
196 } | |
197 | |
198 bool | |
5489 | 199 octave_env::have_x11_display (void) |
200 { | |
201 std::string display = getenv ("DISPLAY"); | |
202 | |
203 return ! display.empty (); | |
204 } | |
205 | |
206 bool | |
3504 | 207 octave_env::chdir (const std::string& newdir) |
2926 | 208 { |
209 return (instance_ok ()) | |
210 ? instance->do_chdir (newdir) : false; | |
211 } | |
212 | |
213 void | |
3504 | 214 octave_env::do_set_program_name (const std::string& s) const |
2926 | 215 { |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
216 // For gnulib. |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
217 ::set_program_name (s.c_str ()); |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
218 |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
219 // Let gnulib strip off things like the "lt-" prefix from libtool. |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
220 prog_invocation_name = program_name; |
2926 | 221 |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
222 size_t pos |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
223 = prog_invocation_name.find_last_of (file_ops::dir_sep_chars ()); |
2926 | 224 |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
225 // Also keep a shortened version of the program name. |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
226 prog_name = (pos == std::string::npos) |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
227 ? prog_invocation_name : prog_invocation_name.substr (pos+1); |
2926 | 228 } |
229 | |
230 // Return a pretty pathname. If the first part of the pathname is the | |
231 // same as $HOME, then replace that with `~'. | |
232 | |
3504 | 233 std::string |
234 octave_env::do_polite_directory_format (const std::string& name) const | |
2926 | 235 { |
3504 | 236 std::string retval; |
2926 | 237 |
3504 | 238 std::string home_dir = do_get_home_directory (); |
2926 | 239 |
240 size_t len = home_dir.length (); | |
241 | |
3516 | 242 if (len > 1 && home_dir == name.substr (0, len) |
4097 | 243 && (name.length () == len || file_ops::is_dir_sep (name[len]))) |
2926 | 244 { |
245 retval = "~"; | |
246 retval.append (name.substr (len)); | |
247 } | |
248 else | |
249 retval = name; | |
250 | |
251 return retval; | |
252 } | |
253 | |
254 bool | |
3504 | 255 octave_env::do_absolute_pathname (const std::string& s) const |
2926 | 256 { |
4087 | 257 size_t len = s.length (); |
258 | |
259 if (len == 0) | |
260 return false; | |
2926 | 261 |
4097 | 262 if (file_ops::is_dir_sep (s[0])) |
2926 | 263 return true; |
264 | |
4101 | 265 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) |
4088 | 266 if ((len == 2 && isalpha (s[0]) && s[1] == ':') |
4097 | 267 || (len > 2 && isalpha (s[0]) && s[1] == ':' |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
268 && file_ops::is_dir_sep (s[2]))) |
4087 | 269 return true; |
270 #endif | |
271 | |
2926 | 272 return false; |
273 } | |
274 | |
6838 | 275 bool |
276 octave_env::do_rooted_relative_pathname (const std::string& s) const | |
277 { | |
278 size_t len = s.length (); | |
279 | |
280 if (len == 0) | |
281 return false; | |
282 | |
283 if (len == 1 && s[0] == '.') | |
284 return true; | |
285 | |
286 if (len > 1 && s[0] == '.' && file_ops::is_dir_sep (s[1])) | |
287 return true; | |
288 | |
289 if (len == 2 && s[0] == '.' && s[1] == '.') | |
290 return true; | |
291 | |
292 if (len > 2 && s[0] == '.' && s[1] == '.' && file_ops::is_dir_sep (s[2])) | |
293 return true; | |
294 | |
295 return false; | |
296 } | |
297 | |
2926 | 298 // Return the `basename' of the pathname in STRING (the stuff after |
4097 | 299 // the last directory separator). If STRING is not a full pathname, |
300 // simply return it. | |
2926 | 301 |
3504 | 302 std::string |
303 octave_env::do_base_pathname (const std::string& s) const | |
2926 | 304 { |
7609
7e6002d15d4d
octave_env::do_base_pathname: handle rooted relativel names
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
305 if (! (do_absolute_pathname (s) || do_rooted_relative_pathname (s))) |
2926 | 306 return s; |
307 | |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
308 size_t pos = s.find_last_of (file_ops::dir_sep_chars ()); |
2926 | 309 |
8021 | 310 if (pos == std::string::npos) |
2926 | 311 return s; |
312 else | |
313 return s.substr (pos+1); | |
314 } | |
315 | |
316 // Turn STRING (a pathname) into an absolute pathname, assuming that | |
4097 | 317 // DOT_PATH contains the symbolic location of the current directory. |
2926 | 318 |
3504 | 319 std::string |
320 octave_env::do_make_absolute (const std::string& s, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
321 const std::string& dot_path) const |
2926 | 322 { |
4087 | 323 if (dot_path.empty () || s.empty () || do_absolute_pathname (s)) |
2926 | 324 return s; |
325 | |
4097 | 326 std::string current_dir = dot_path; |
2926 | 327 |
4097 | 328 if (current_dir.empty ()) |
329 current_dir = do_getcwd (); | |
2926 | 330 |
4097 | 331 size_t pos = current_dir.length () - 1; |
2926 | 332 |
4097 | 333 if (! file_ops::is_dir_sep (current_dir[pos])) |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
334 current_dir.append (file_ops::dir_sep_str ()); |
4097 | 335 |
5775 | 336 // FIXME -- this is probably not correct for all systems. |
2926 | 337 |
338 size_t i = 0; | |
339 size_t slen = s.length (); | |
340 | |
341 while (i < slen) | |
342 { | |
343 if (s[i] == '.') | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
344 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
345 if (i + 1 == slen) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
346 return current_dir; |
2926 | 347 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
348 if (file_ops::is_dir_sep (s[i+1])) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
349 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
350 i += 2; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
351 continue; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
352 } |
2926 | 353 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
354 if (s[i+1] == '.' |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
355 && (i + 2 == slen || file_ops::is_dir_sep (s[i+2]))) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
356 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
357 i += 2; |
2926 | 358 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
359 if (i != slen) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
360 i++; |
2926 | 361 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
362 pathname_backup (current_dir, 1); |
2926 | 363 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
364 continue; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
365 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
366 } |
2926 | 367 |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
368 size_t tmp = s.find_first_of (file_ops::dir_sep_chars (), i); |
2926 | 369 |
8021 | 370 if (tmp == std::string::npos) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
371 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
372 current_dir.append (s, i, tmp-i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
373 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
374 } |
2926 | 375 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
376 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
377 current_dir.append (s, i, tmp-i+1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
378 i = tmp + 1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
379 } |
2926 | 380 } |
381 | |
4097 | 382 return current_dir; |
2926 | 383 } |
384 | |
4097 | 385 // Return a string which is the current working directory. |
2926 | 386 |
3504 | 387 std::string |
4097 | 388 octave_env::do_getcwd () const |
2926 | 389 { |
390 if (! follow_symbolic_links) | |
391 current_directory = ""; | |
392 | |
393 if (verbatim_pwd || current_directory.empty ()) | |
394 current_directory = ::octave_getcwd (); | |
395 | |
396 return current_directory; | |
397 } | |
398 | |
399 // This value is not cached because it can change while Octave is | |
400 // running. | |
401 | |
3504 | 402 std::string |
2926 | 403 octave_env::do_get_home_directory (void) const |
404 { | |
3504 | 405 std::string hd = do_getenv ("HOME"); |
2926 | 406 |
6096 | 407 #if defined (__MINGW32__) || defined (_MSC_VER) |
408 // Maybe we are started directly from cmd.exe. | |
5451 | 409 if (hd.empty ()) |
5454 | 410 { |
411 std::string drv = do_getenv ("HOMEDRIVE"); | |
412 if (drv.empty ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
413 hd = do_getenv ("HOMEPATH"); |
5454 | 414 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
415 hd = drv + do_getenv ("HOMEPATH"); |
5454 | 416 } |
5451 | 417 #endif |
418 | |
2947 | 419 if (hd.empty ()) |
420 { | |
421 octave_passwd pw = octave_passwd::getpwuid (octave_syscalls::getuid ()); | |
422 | |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
423 hd = pw ? pw.dir () : std::string (file_ops::dir_sep_str ()); |
2947 | 424 } |
425 | |
426 return hd; | |
2926 | 427 } |
428 | |
3504 | 429 std::string |
2926 | 430 octave_env::do_get_user_name (void) const |
431 { | |
432 if (user_name.empty ()) | |
433 { | |
2947 | 434 octave_passwd pw = octave_passwd::getpwuid (octave_syscalls::getuid ()); |
2926 | 435 |
3504 | 436 user_name = pw ? pw.name () : std::string ("unknown"); |
2926 | 437 } |
438 | |
439 return user_name; | |
440 } | |
441 | |
3504 | 442 std::string |
2926 | 443 octave_env::do_get_host_name (void) const |
444 { | |
445 if (host_name.empty ()) | |
446 { | |
11006
aca961a3f387
provide gethostname function
John W. Eaton <jwe@octave.org>
parents:
10463
diff
changeset
|
447 char hostname[1024]; |
2926 | 448 |
11006
aca961a3f387
provide gethostname function
John W. Eaton <jwe@octave.org>
parents:
10463
diff
changeset
|
449 int status = gnulib::gethostname (hostname, 1023); |
2926 | 450 |
3185 | 451 host_name = (status < 0) ? "unknown" : hostname; |
2926 | 452 } |
453 | |
454 return host_name; | |
455 } | |
456 | |
3504 | 457 std::string |
458 octave_env::do_getenv (const std::string& name) const | |
2926 | 459 { |
460 char *value = ::getenv (name.c_str ()); | |
461 | |
462 return value ? value : ""; | |
463 } | |
464 | |
465 // Do the work of changing to the directory NEWDIR. Handle symbolic | |
466 // link following, etc. | |
467 | |
468 bool | |
3504 | 469 octave_env::do_chdir (const std::string& newdir) |
2926 | 470 { |
471 bool retval = false; | |
472 | |
3504 | 473 std::string tmp; |
2926 | 474 |
475 if (follow_symbolic_links) | |
476 { | |
477 if (current_directory.empty ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
478 do_getcwd (); |
2926 | 479 |
480 if (current_directory.empty ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
481 tmp = newdir; |
2926 | 482 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
483 tmp = do_make_absolute (newdir, current_directory); |
2926 | 484 |
4097 | 485 // Get rid of trailing directory separator. |
2926 | 486 |
487 size_t len = tmp.length (); | |
488 | |
489 if (len > 1) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
490 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
491 if (file_ops::is_dir_sep (tmp[--len])) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
492 tmp.resize (len); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
493 } |
2926 | 494 |
495 if (! ::octave_chdir (tmp)) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
496 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
497 current_directory = tmp; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
498 retval = true; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
499 } |
2926 | 500 } |
501 else | |
502 retval = (! ::octave_chdir (newdir)); | |
503 | |
504 return retval; | |
505 } | |
506 | |
507 // Remove the last N directories from PATH. | |
508 | |
509 void | |
3504 | 510 octave_env::pathname_backup (std::string& path, int n) const |
2926 | 511 { |
512 if (path.empty ()) | |
513 return; | |
514 | |
515 size_t i = path.length () - 1; | |
516 | |
517 while (n--) | |
518 { | |
4097 | 519 while (file_ops::is_dir_sep (path[i]) && i > 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
520 i--; |
2926 | 521 |
4097 | 522 while (! file_ops::is_dir_sep (path[i]) && i > 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
523 i--; |
2926 | 524 |
525 i++; | |
526 } | |
527 | |
528 path.resize (i); | |
529 } | |
530 | |
531 void | |
532 octave_env::error (int err_num) const | |
533 { | |
10411 | 534 (*current_liboctave_error_handler) ("%s", gnulib::strerror (err_num)); |
2926 | 535 } |
536 | |
537 void | |
3504 | 538 octave_env::error (const std::string& s) const |
2926 | 539 { |
540 (*current_liboctave_error_handler) ("%s", s.c_str ()); | |
541 } |