Mercurial > octave
annotate liboctave/system/lo-sysdep.cc @ 31605:e88a07dec498 stable
maint: Use macros to begin/end C++ namespaces.
* oct-conf-post-public.in.h: Define two macros (OCTAVE_BEGIN_NAMESPACE,
OCTAVE_END_NAMESPACE) that can be used to start/end a namespace.
* mk-opts.pl, build-env.h, build-env.in.cc, __betainc__.cc, __contourc__.cc,
__dsearchn__.cc, __eigs__.cc, __expint__.cc, __ftp__.cc, __gammainc__.cc,
__ichol__.cc, __ilu__.cc, __isprimelarge__.cc, __lin_interpn__.cc,
__magick_read__.cc, __pchip_deriv__.cc, __qp__.cc, amd.cc, auto-shlib.cc,
auto-shlib.h, balance.cc, base-text-renderer.cc, base-text-renderer.h,
besselj.cc, bitfcns.cc, bsxfun.cc, c-file-ptr-stream.cc, c-file-ptr-stream.h,
call-stack.cc, call-stack.h, ccolamd.cc, cellfun.cc, chol.cc, colamd.cc,
colloc.cc, conv2.cc, daspk.cc, dasrt.cc, dassl.cc, data.cc, data.h, debug.cc,
defaults.cc, defaults.h, defun-int.h, defun.cc, det.cc, dirfns.cc, display.cc,
display.h, dlmread.cc, dmperm.cc, dot.cc, dynamic-ld.cc, dynamic-ld.h, eig.cc,
ellipj.cc, environment.cc, environment.h, error.cc, error.h, errwarn.h,
event-manager.cc, event-manager.h, event-queue.cc, event-queue.h, fcn-info.cc,
fcn-info.h, fft.cc, fft2.cc, fftn.cc, file-io.cc, filter.cc, find.cc,
ft-text-renderer.cc, ft-text-renderer.h, gcd.cc, getgrent.cc, getpwent.cc,
getrusage.cc, givens.cc, gl-render.cc, gl-render.h, gl2ps-print.cc,
gl2ps-print.h, graphics-toolkit.cc, graphics-toolkit.h, graphics.cc,
graphics.in.h, gsvd.cc, gtk-manager.cc, gtk-manager.h, hash.cc, help.cc,
help.h, hess.cc, hex2num.cc, hook-fcn.cc, hook-fcn.h, input.cc, input.h,
interpreter-private.cc, interpreter-private.h, interpreter.cc, interpreter.h,
inv.cc, jsondecode.cc, jsonencode.cc, kron.cc, latex-text-renderer.cc,
latex-text-renderer.h, load-path.cc, load-path.h, load-save.cc, load-save.h,
lookup.cc, ls-ascii-helper.cc, ls-ascii-helper.h, ls-oct-text.cc, ls-utils.cc,
ls-utils.h, lsode.cc, lu.cc, mappers.cc, matrix_type.cc, max.cc, mex-private.h,
mex.cc, mgorth.cc, nproc.cc, oct-fstrm.cc, oct-fstrm.h, oct-hdf5-types.cc,
oct-hdf5-types.h, oct-hist.cc, oct-hist.h, oct-iostrm.cc, oct-iostrm.h,
oct-opengl.h, oct-prcstrm.cc, oct-prcstrm.h, oct-procbuf.cc, oct-procbuf.h,
oct-process.cc, oct-process.h, oct-stdstrm.h, oct-stream.cc, oct-stream.h,
oct-strstrm.cc, oct-strstrm.h, oct-tex-lexer.in.ll, oct-tex-parser.yy,
ordqz.cc, ordschur.cc, pager.cc, pager.h, pinv.cc, pow2.cc, pr-flt-fmt.cc,
pr-output.cc, procstream.cc, procstream.h, psi.cc, qr.cc, quad.cc, quadcc.cc,
qz.cc, rand.cc, rcond.cc, regexp.cc, schur.cc, settings.cc, settings.h,
sighandlers.cc, sighandlers.h, sparse-xdiv.cc, sparse-xdiv.h, sparse-xpow.cc,
sparse-xpow.h, sparse.cc, spparms.cc, sqrtm.cc, stack-frame.cc, stack-frame.h,
stream-euler.cc, strfind.cc, strfns.cc, sub2ind.cc, svd.cc, sylvester.cc,
symbfact.cc, syminfo.cc, syminfo.h, symrcm.cc, symrec.cc, symrec.h,
symscope.cc, symscope.h, symtab.cc, symtab.h, syscalls.cc, sysdep.cc, sysdep.h,
text-engine.cc, text-engine.h, text-renderer.cc, text-renderer.h, time.cc,
toplev.cc, tril.cc, tsearch.cc, typecast.cc, url-handle-manager.cc,
url-handle-manager.h, urlwrite.cc, utils.cc, utils.h, variables.cc,
variables.h, xdiv.cc, xdiv.h, xnorm.cc, xnorm.h, xpow.cc, xpow.h,
__delaunayn__.cc, __fltk_uigetfile__.cc, __glpk__.cc, __init_fltk__.cc,
__init_gnuplot__.cc, __ode15__.cc, __voronoi__.cc, audiodevinfo.cc,
audioread.cc, convhulln.cc, fftw.cc, gzip.cc, mk-build-env-features.sh,
mk-builtins.pl, cdef-class.cc, cdef-class.h, cdef-fwd.h, cdef-manager.cc,
cdef-manager.h, cdef-method.cc, cdef-method.h, cdef-object.cc, cdef-object.h,
cdef-package.cc, cdef-package.h, cdef-property.cc, cdef-property.h,
cdef-utils.cc, cdef-utils.h, ov-base.cc, ov-base.h, ov-bool-mat.cc,
ov-builtin.h, ov-cell.cc, ov-class.cc, ov-class.h, ov-classdef.cc,
ov-classdef.h, ov-complex.cc, ov-fcn-handle.cc, ov-fcn-handle.h, ov-fcn.h,
ov-java.cc, ov-java.h, ov-mex-fcn.h, ov-null-mat.cc, ov-oncleanup.cc,
ov-struct.cc, ov-typeinfo.cc, ov-typeinfo.h, ov-usr-fcn.cc, ov-usr-fcn.h,
ov.cc, ov.h, octave.cc, octave.h, mk-ops.sh, op-b-b.cc, op-b-bm.cc,
op-b-sbm.cc, op-bm-b.cc, op-bm-bm.cc, op-bm-sbm.cc, op-cdm-cdm.cc, op-cell.cc,
op-chm.cc, op-class.cc, op-cm-cm.cc, op-cm-cs.cc, op-cm-m.cc, op-cm-s.cc,
op-cm-scm.cc, op-cm-sm.cc, op-cs-cm.cc, op-cs-cs.cc, op-cs-m.cc, op-cs-s.cc,
op-cs-scm.cc, op-cs-sm.cc, op-dm-dm.cc, op-dm-scm.cc, op-dm-sm.cc,
op-dm-template.cc, op-dms-template.cc, op-fcdm-fcdm.cc, op-fcm-fcm.cc,
op-fcm-fcs.cc, op-fcm-fm.cc, op-fcm-fs.cc, op-fcn.cc, op-fcs-fcm.cc,
op-fcs-fcs.cc, op-fcs-fm.cc, op-fcs-fs.cc, op-fdm-fdm.cc, op-fm-fcm.cc,
op-fm-fcs.cc, op-fm-fm.cc, op-fm-fs.cc, op-fs-fcm.cc, op-fs-fcs.cc,
op-fs-fm.cc, op-fs-fs.cc, op-i16-i16.cc, op-i32-i32.cc, op-i64-i64.cc,
op-i8-i8.cc, op-int-concat.cc, op-m-cm.cc, op-m-cs.cc, op-m-m.cc, op-m-s.cc,
op-m-scm.cc, op-m-sm.cc, op-mi.cc, op-pm-pm.cc, op-pm-scm.cc, op-pm-sm.cc,
op-pm-template.cc, op-range.cc, op-s-cm.cc, op-s-cs.cc, op-s-m.cc, op-s-s.cc,
op-s-scm.cc, op-s-sm.cc, op-sbm-b.cc, op-sbm-bm.cc, op-sbm-sbm.cc,
op-scm-cm.cc, op-scm-cs.cc, op-scm-m.cc, op-scm-s.cc, op-scm-scm.cc,
op-scm-sm.cc, op-sm-cm.cc, op-sm-cs.cc, op-sm-m.cc, op-sm-s.cc, op-sm-scm.cc,
op-sm-sm.cc, op-str-m.cc, op-str-s.cc, op-str-str.cc, op-struct.cc,
op-ui16-ui16.cc, op-ui32-ui32.cc, op-ui64-ui64.cc, op-ui8-ui8.cc, ops.h,
anon-fcn-validator.cc, anon-fcn-validator.h, bp-table.cc, bp-table.h,
comment-list.cc, comment-list.h, filepos.h, lex.h, lex.ll, oct-lvalue.cc,
oct-lvalue.h, oct-parse.yy, parse.h, profiler.cc, profiler.h,
pt-anon-scopes.cc, pt-anon-scopes.h, pt-arg-list.cc, pt-arg-list.h,
pt-args-block.cc, pt-args-block.h, pt-array-list.cc, pt-array-list.h,
pt-assign.cc, pt-assign.h, pt-binop.cc, pt-binop.h, pt-bp.cc, pt-bp.h,
pt-cbinop.cc, pt-cbinop.h, pt-cell.cc, pt-cell.h, pt-check.cc, pt-check.h,
pt-classdef.cc, pt-classdef.h, pt-cmd.h, pt-colon.cc, pt-colon.h, pt-const.cc,
pt-const.h, pt-decl.cc, pt-decl.h, pt-eval.cc, pt-eval.h, pt-except.cc,
pt-except.h, pt-exp.cc, pt-exp.h, pt-fcn-handle.cc, pt-fcn-handle.h, pt-id.cc,
pt-id.h, pt-idx.cc, pt-idx.h, pt-jump.h, pt-loop.cc, pt-loop.h, pt-mat.cc,
pt-mat.h, pt-misc.cc, pt-misc.h, pt-pr-code.cc, pt-pr-code.h, pt-select.cc,
pt-select.h, pt-spmd.cc, pt-spmd.h, pt-stmt.cc, pt-stmt.h, pt-tm-const.cc,
pt-tm-const.h, pt-unop.cc, pt-unop.h, pt-vm-eval.cc, pt-walk.cc, pt-walk.h,
pt.cc, pt.h, token.cc, token.h, Range.cc, Range.h, idx-vector.cc, idx-vector.h,
range-fwd.h, CollocWt.cc, CollocWt.h, aepbalance.cc, aepbalance.h, chol.cc,
chol.h, gepbalance.cc, gepbalance.h, gsvd.cc, gsvd.h, hess.cc, hess.h,
lo-mappers.cc, lo-mappers.h, lo-specfun.cc, lo-specfun.h, lu.cc, lu.h,
oct-convn.cc, oct-convn.h, oct-fftw.cc, oct-fftw.h, oct-norm.cc, oct-norm.h,
oct-rand.cc, oct-rand.h, oct-spparms.cc, oct-spparms.h, qr.cc, qr.h, qrp.cc,
qrp.h, randgamma.cc, randgamma.h, randmtzig.cc, randmtzig.h, randpoisson.cc,
randpoisson.h, schur.cc, schur.h, sparse-chol.cc, sparse-chol.h, sparse-lu.cc,
sparse-lu.h, sparse-qr.cc, sparse-qr.h, svd.cc, svd.h, child-list.cc,
child-list.h, dir-ops.cc, dir-ops.h, file-ops.cc, file-ops.h, file-stat.cc,
file-stat.h, lo-sysdep.cc, lo-sysdep.h, lo-sysinfo.cc, lo-sysinfo.h,
mach-info.cc, mach-info.h, oct-env.cc, oct-env.h, oct-group.cc, oct-group.h,
oct-password.cc, oct-password.h, oct-syscalls.cc, oct-syscalls.h, oct-time.cc,
oct-time.h, oct-uname.cc, oct-uname.h, action-container.cc, action-container.h,
base-list.h, cmd-edit.cc, cmd-edit.h, cmd-hist.cc, cmd-hist.h, f77-fcn.h,
file-info.cc, file-info.h, lo-array-errwarn.cc, lo-array-errwarn.h, lo-hash.cc,
lo-hash.h, lo-ieee.h, lo-regexp.cc, lo-regexp.h, lo-utils.cc, lo-utils.h,
oct-base64.cc, oct-base64.h, oct-glob.cc, oct-glob.h, oct-inttypes.h,
oct-mutex.cc, oct-mutex.h, oct-refcount.h, oct-shlib.cc, oct-shlib.h,
oct-sparse.cc, oct-sparse.h, oct-string.h, octave-preserve-stream-state.h,
pathsearch.cc, pathsearch.h, quit.cc, quit.h, unwind-prot.cc, unwind-prot.h,
url-transfer.cc, url-transfer.h : Use new macros to begin/end C++ namespaces.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 01 Dec 2022 14:23:45 -0800 |
parents | 1921d9d0e62b |
children | aac27ad79be6 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 //////////////////////////////////////////////////////////////////////// |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 // |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
29657
diff
changeset
|
3 // Copyright (C) 1996-2022 The Octave Project Developers |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
4 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 // See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 // distribution or <https://octave.org/copyright/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
7 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
8 // This file is part of Octave. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
9 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
10 // Octave is free software: you can redistribute it and/or modify it |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
11 // under the terms of the GNU General Public License as published by |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
12 // the Free Software Foundation, either version 3 of the License, or |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
13 // (at your option) any later version. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
14 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
15 // Octave is distributed in the hope that it will be useful, but |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
16 // WITHOUT ANY WARRANTY; without even the implied warranty of |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
18 // GNU General Public License for more details. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
19 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
20 // You should have received a copy of the GNU General Public License |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
21 // along with Octave; see the file COPYING. If not, see |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
22 // <https://www.gnu.org/licenses/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 //////////////////////////////////////////////////////////////////////// |
2926 | 25 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21642
diff
changeset
|
26 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21213
diff
changeset
|
27 # include "config.h" |
2926 | 28 #endif |
29 | |
29141
f873857f5f86
system: Support executing commands with non-ASCII characters on Windows (bug #59572).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29016
diff
changeset
|
30 #include <cstdlib> |
29492
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
31 #include <locale> |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
32 #include <codecvt> |
29141
f873857f5f86
system: Support executing commands with non-ASCII characters on Windows (bug #59572).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29016
diff
changeset
|
33 |
25516
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
34 #include "dir-ops.h" |
5872 | 35 #include "file-ops.h" |
3069 | 36 #include "lo-error.h" |
21910
4d723ba06b4a
provide wrappers for some unistd.h functions
John W. Eaton <jwe@octave.org>
parents:
21828
diff
changeset
|
37 #include "lo-sysdep.h" |
28534
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
38 #include "localcharset-wrapper.h" |
25916
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
39 #include "putenv-wrapper.h" |
21910
4d723ba06b4a
provide wrappers for some unistd.h functions
John W. Eaton <jwe@octave.org>
parents:
21828
diff
changeset
|
40 #include "unistd-wrappers.h" |
25916
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
41 #include "unsetenv-wrapper.h" |
2926 | 42 |
25516
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
43 #if defined (OCTAVE_USE_WINDOWS_API) |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
44 # include <windows.h> |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
45 # include <wchar.h> |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
46 |
28534
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
47 # include "filepos-wrappers.h" |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
48 # include "lo-hash.h" |
28534
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
49 # include "oct-locbuf.h" |
29492
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
50 # include "uniconv-wrappers.h" |
26531
481ac37b0e0e
lo-sysdep.cc: Fix static analyzer detected issue (bug #55464).
Markus Mützel <markus.muetzel@gmx.de>
parents:
26506
diff
changeset
|
51 # include "unwind-prot.h" |
25516
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
52 #endif |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
53 |
31605
e88a07dec498
maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents:
30881
diff
changeset
|
54 OCTAVE_BEGIN_NAMESPACE(octave) |
e88a07dec498
maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents:
30881
diff
changeset
|
55 |
e88a07dec498
maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents:
30881
diff
changeset
|
56 OCTAVE_BEGIN_NAMESPACE(sys) |
e88a07dec498
maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents:
30881
diff
changeset
|
57 |
29141
f873857f5f86
system: Support executing commands with non-ASCII characters on Windows (bug #59572).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29016
diff
changeset
|
58 int |
f873857f5f86
system: Support executing commands with non-ASCII characters on Windows (bug #59572).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29016
diff
changeset
|
59 system (const std::string& cmd_str) |
f873857f5f86
system: Support executing commands with non-ASCII characters on Windows (bug #59572).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29016
diff
changeset
|
60 { |
f873857f5f86
system: Support executing commands with non-ASCII characters on Windows (bug #59572).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29016
diff
changeset
|
61 #if defined (OCTAVE_USE_WINDOWS_API) |
f873857f5f86
system: Support executing commands with non-ASCII characters on Windows (bug #59572).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29016
diff
changeset
|
62 const std::wstring wcmd_str = u8_to_wstring (cmd_str); |
f873857f5f86
system: Support executing commands with non-ASCII characters on Windows (bug #59572).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29016
diff
changeset
|
63 |
f873857f5f86
system: Support executing commands with non-ASCII characters on Windows (bug #59572).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29016
diff
changeset
|
64 return _wsystem (wcmd_str.c_str ()); |
f873857f5f86
system: Support executing commands with non-ASCII characters on Windows (bug #59572).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29016
diff
changeset
|
65 #else |
f873857f5f86
system: Support executing commands with non-ASCII characters on Windows (bug #59572).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29016
diff
changeset
|
66 return ::system (cmd_str.c_str ()); |
f873857f5f86
system: Support executing commands with non-ASCII characters on Windows (bug #59572).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29016
diff
changeset
|
67 #endif |
f873857f5f86
system: Support executing commands with non-ASCII characters on Windows (bug #59572).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29016
diff
changeset
|
68 } |
f873857f5f86
system: Support executing commands with non-ASCII characters on Windows (bug #59572).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29016
diff
changeset
|
69 |
21738
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
70 std::string |
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
71 getcwd (void) |
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
72 { |
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
73 std::string retval; |
3069 | 74 |
28470
39c078e14824
Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28223
diff
changeset
|
75 #if defined (OCTAVE_USE_WINDOWS_API) |
39c078e14824
Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28223
diff
changeset
|
76 wchar_t *tmp = _wgetcwd (nullptr, 0); |
39c078e14824
Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28223
diff
changeset
|
77 |
39c078e14824
Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28223
diff
changeset
|
78 if (! tmp) |
39c078e14824
Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28223
diff
changeset
|
79 (*current_liboctave_error_handler) ("unable to find current directory"); |
39c078e14824
Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28223
diff
changeset
|
80 |
39c078e14824
Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28223
diff
changeset
|
81 std::wstring tmp_wstr (tmp); |
39c078e14824
Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28223
diff
changeset
|
82 free (tmp); |
39c078e14824
Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28223
diff
changeset
|
83 |
39c078e14824
Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28223
diff
changeset
|
84 std::string tmp_str = u8_from_wstring (tmp_wstr); |
39c078e14824
Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28223
diff
changeset
|
85 |
39c078e14824
Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28223
diff
changeset
|
86 retval = tmp_str; |
39c078e14824
Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28223
diff
changeset
|
87 |
39c078e14824
Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28223
diff
changeset
|
88 #else |
21910
4d723ba06b4a
provide wrappers for some unistd.h functions
John W. Eaton <jwe@octave.org>
parents:
21828
diff
changeset
|
89 // Using octave_getcwd_wrapper ensures that we have a getcwd that |
21738
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
90 // will allocate a buffer as large as necessary if buf and size are |
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
91 // both 0. |
3069 | 92 |
23795
980f39c3ab90
Use C++11 nullptr rather than 0 in code (bug #51565).
Rik <rik@octave.org>
parents:
23696
diff
changeset
|
93 char *tmp = octave_getcwd_wrapper (nullptr, 0); |
2926 | 94 |
21738
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
95 if (! tmp) |
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
96 (*current_liboctave_error_handler) ("unable to find current directory"); |
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
97 |
10250 | 98 retval = tmp; |
99 free (tmp); | |
28470
39c078e14824
Use wide character API to get current directory on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28223
diff
changeset
|
100 #endif |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
19864
diff
changeset
|
101 |
21738
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
102 return retval; |
10250 | 103 } |
2926 | 104 |
21738
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
105 int |
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
106 chdir (const std::string& path_arg) |
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
107 { |
23696
08036a7f3660
remove octave:: namespace tag from symbols used inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
108 std::string path = sys::file_ops::tilde_expand (path_arg); |
5872 | 109 |
21979
d04da18a407a
use OCTAVE_USE_WINDOWS_API more consistently
John W. Eaton <jwe@octave.org>
parents:
21949
diff
changeset
|
110 #if defined (OCTAVE_USE_WINDOWS_API) |
21738
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
111 if (path.length () == 2 && path[1] == ':') |
23804
8057d3f0673d
Use C++11 raw string literals to avoid escaping backslashes in strings.
Rik <rik@octave.org>
parents:
23795
diff
changeset
|
112 path += '\\'; |
6244 | 113 #endif |
114 | |
21910
4d723ba06b4a
provide wrappers for some unistd.h functions
John W. Eaton <jwe@octave.org>
parents:
21828
diff
changeset
|
115 return octave_chdir_wrapper (path.c_str ()); |
21738
a99c2407f930
use namespace for lo-sysdep.h functions
John W. Eaton <jwe@octave.org>
parents:
21733
diff
changeset
|
116 } |
25514
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
117 |
25516
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
118 bool |
27933
863ae57eee69
maint: Use Octave coding conventions in liboctave/
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
119 get_dirlist (const std::string& dirname, string_vector& dirlist, |
863ae57eee69
maint: Use Octave coding conventions in liboctave/
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
120 std::string& msg) |
25516
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
121 { |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
122 dirlist = ""; |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
123 msg = ""; |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
124 #if defined (OCTAVE_USE_WINDOWS_API) |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
125 _WIN32_FIND_DATAW ffd; |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
126 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
127 std::string path_name (dirname); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
128 if (path_name.empty ()) |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
129 return true; |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
130 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
131 if (path_name.back () == '\\' || path_name.back () == '/') |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
132 path_name.push_back ('*'); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
133 else |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
134 path_name.append (R"(\*)"); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
135 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
136 // Find first file in directory. |
28175
b895daca20e2
Avoid potential lifetime issues with temporary std::wstring objects.
Markus Mützel <markus.muetzel@gmx.de>
parents:
27933
diff
changeset
|
137 std::wstring wpath_name = u8_to_wstring (path_name); |
b895daca20e2
Avoid potential lifetime issues with temporary std::wstring objects.
Markus Mützel <markus.muetzel@gmx.de>
parents:
27933
diff
changeset
|
138 HANDLE hFind = FindFirstFileW (wpath_name.c_str (), &ffd); |
25609
506419b5f817
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
25532
diff
changeset
|
139 if (INVALID_HANDLE_VALUE == hFind) |
25516
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
140 { |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
141 DWORD errCode = GetLastError (); |
26163
d478d5d1c8e9
Fix potential segfault on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25916
diff
changeset
|
142 char *errorText = nullptr; |
25516
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
143 FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM | |
27933
863ae57eee69
maint: Use Octave coding conventions in liboctave/
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
144 FORMAT_MESSAGE_ALLOCATE_BUFFER | |
863ae57eee69
maint: Use Octave coding conventions in liboctave/
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
145 FORMAT_MESSAGE_IGNORE_INSERTS, |
863ae57eee69
maint: Use Octave coding conventions in liboctave/
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
146 nullptr, errCode, |
863ae57eee69
maint: Use Octave coding conventions in liboctave/
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
147 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), |
863ae57eee69
maint: Use Octave coding conventions in liboctave/
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
148 reinterpret_cast <char *> (&errorText), 0, nullptr); |
26163
d478d5d1c8e9
Fix potential segfault on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25916
diff
changeset
|
149 if (errorText != nullptr) |
25516
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
150 { |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
151 msg = std::string (errorText); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
152 LocalFree (errorText); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
153 } |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
154 return false; |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
155 } |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
156 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
157 std::list<std::string> dirlist_str; |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
158 do |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
159 dirlist_str.push_back (u8_from_wstring (ffd.cFileName)); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
160 while (FindNextFileW (hFind, &ffd) != 0); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
161 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
162 FindClose(hFind); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
163 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
164 dirlist = string_vector (dirlist_str); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
165 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
166 #else |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
167 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
168 dir_entry dir (dirname); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
169 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
170 if (! dir) |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
171 { |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
172 msg = dir.error (); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
173 return false; |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
174 } |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
175 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
176 dirlist = dir.read (); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
177 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
178 dir.close (); |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
179 #endif |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
180 |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
181 return true; |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
182 } |
8945a6a6c0eb
Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25514
diff
changeset
|
183 |
28220
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
184 #if defined (OCTAVE_USE_WINDOWS_API) |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
185 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
186 static bool check_fseek_ftell_workaround_needed (bool set_nonbuffered_mode) |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
187 { |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
188 // To check whether the workaround is needed: |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
189 // |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
190 // * Create a tmp file with LF line endings only. |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
191 // |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
192 // * Open that file for reading in text mode. |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
193 // |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
194 // * Read a line. |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
195 // |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
196 // * Use ftello to record the position of the beginning of the |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
197 // second line. |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
198 // |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
199 // * Read and save the contents of the second line. |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
200 // |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
201 // * Use fseeko to return to the saved position. |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
202 // |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
203 // * Read the second line again and compare to the previously |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
204 // saved text. |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
205 // |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
206 // * If the lines are different, we need to set non-buffered |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
207 // input mode for files opened in text mode. |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
208 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
209 std::string tmpname = sys::tempnam ("", "oct-"); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
210 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
211 if (tmpname.empty ()) |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
212 { |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
213 (*current_liboctave_warning_handler) |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
214 ("fseek/ftell bug check failed (tmp name creation)!"); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
215 return false; |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
216 } |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
217 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
218 std::FILE *fptr = std::fopen (tmpname.c_str (), "wb"); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
219 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
220 if (! fptr) |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
221 { |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
222 (*current_liboctave_warning_handler) |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
223 ("fseek/ftell bug check failed (opening tmp file for writing)!"); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
224 return false; |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
225 } |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
226 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
227 fprintf (fptr, "%s", "foo\nbar\nbaz\n"); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
228 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
229 std::fclose (fptr); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
230 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
231 fptr = std::fopen (tmpname.c_str (), "rt"); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
232 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
233 if (! fptr) |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
234 { |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
235 (*current_liboctave_warning_handler) |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
236 ("fseek/ftell bug check failed (opening tmp file for reading)!"); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
237 return false; |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
238 } |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
239 |
28851
1ac5a76ae91d
use [=] capture default specification where possible
John W. Eaton <jwe@octave.org>
parents:
28850
diff
changeset
|
240 unwind_action act ([=] () |
1ac5a76ae91d
use [=] capture default specification where possible
John W. Eaton <jwe@octave.org>
parents:
28850
diff
changeset
|
241 { |
28220
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
242 std::fclose (fptr); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
243 sys::unlink (tmpname); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
244 }); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
245 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
246 if (set_nonbuffered_mode) |
28223
45763d59cb4f
use nullptr instead of NULL or 0 in a few more places
John W. Eaton <jwe@octave.org>
parents:
28220
diff
changeset
|
247 ::setvbuf (fptr, nullptr, _IONBF, 0); |
28220
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
248 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
249 while (true) |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
250 { |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
251 int c = fgetc (fptr); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
252 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
253 if (c == EOF) |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
254 { |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
255 (*current_liboctave_warning_handler) |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
256 ("fseek/ftell bug check failed (skipping first line)!"); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
257 return false; |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
258 } |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
259 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
260 if (c == '\n') |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
261 break; |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
262 } |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
263 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
264 off_t pos = octave_ftello_wrapper (fptr); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
265 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
266 char buf1[8]; |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
267 int i = 0; |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
268 while (true) |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
269 { |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
270 int c = fgetc (fptr); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
271 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
272 if (c == EOF) |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
273 { |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
274 (*current_liboctave_warning_handler) |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
275 ("fseek/ftell bug check failed (reading second line)!"); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
276 return false; |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
277 } |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
278 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
279 if (c == '\n') |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
280 break; |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
281 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
282 buf1[i++] = static_cast<char> (c); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
283 } |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
284 buf1[i] = '\0'; |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
285 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
286 octave_fseeko_wrapper (fptr, pos, SEEK_SET); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
287 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
288 char buf2[8]; |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
289 i = 0; |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
290 while (true) |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
291 { |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
292 int c = fgetc (fptr); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
293 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
294 if (c == EOF) |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
295 { |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
296 (*current_liboctave_warning_handler) |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
297 ("fseek/ftell bug check failed (reading after repositioning)!"); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
298 return false; |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
299 } |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
300 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
301 if (c == '\n') |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
302 break; |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
303 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
304 buf2[i++] = static_cast<char> (c); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
305 } |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
306 buf2[i] = '\0'; |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
307 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
308 return strcmp (buf1, buf2); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
309 } |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
310 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
311 #endif |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
312 |
25517
7fbc39a46be8
Add wrapper to fopen for files with non-ASCII chars in path on Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25516
diff
changeset
|
313 std::FILE * |
7fbc39a46be8
Add wrapper to fopen for files with non-ASCII chars in path on Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25516
diff
changeset
|
314 fopen (const std::string& filename, const std::string& mode) |
7fbc39a46be8
Add wrapper to fopen for files with non-ASCII chars in path on Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25516
diff
changeset
|
315 { |
7fbc39a46be8
Add wrapper to fopen for files with non-ASCII chars in path on Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25516
diff
changeset
|
316 #if defined (OCTAVE_USE_WINDOWS_API) |
28220
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
317 |
28175
b895daca20e2
Avoid potential lifetime issues with temporary std::wstring objects.
Markus Mützel <markus.muetzel@gmx.de>
parents:
27933
diff
changeset
|
318 std::wstring wfilename = u8_to_wstring (filename); |
b895daca20e2
Avoid potential lifetime issues with temporary std::wstring objects.
Markus Mützel <markus.muetzel@gmx.de>
parents:
27933
diff
changeset
|
319 std::wstring wmode = u8_to_wstring (mode); |
28220
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
320 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
321 std::FILE *fptr = _wfopen (wfilename.c_str (), wmode.c_str ()); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
322 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
323 static bool fseek_ftell_bug_workaround_needed = false; |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
324 static bool fseek_ftell_bug_checked = false; |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
325 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
326 if (! fseek_ftell_bug_checked && mode.find ('t') != std::string::npos) |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
327 { |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
328 // FIXME: Is the following workaround needed for all files |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
329 // opened in text mode, or only for files opened for reading? |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
330 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
331 // Try to avoid fseek/ftell bug on Windows systems by setting |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
332 // non-buffered input mode for files opened in text mode, but |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
333 // only if it appears that the workaround is needed. See |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
334 // Octave bug #58055. |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
335 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
336 // To check whether the workaround is needed: |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
337 // |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
338 // * Create a tmp file with LF line endings only. |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
339 // |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
340 // * Open that file for reading in text mode. |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
341 // |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
342 // * Read a line. |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
343 // |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
344 // * Use ftello to record the position of the beginning of |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
345 // the second line. |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
346 // |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
347 // * Read and save the contents of the second line. |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
348 // |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
349 // * Use fseeko to return to the saved position. |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
350 // |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
351 // * Read the second line again and compare to the |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
352 // previously saved text. |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
353 // |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
354 // * If the lines are different, we need to set non-buffered |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
355 // input mode for files opened in text mode. |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
356 // |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
357 // * To verify that the workaround solves the problem, |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
358 // repeat the above test with non-buffered input mode. If |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
359 // that fails, warn that there may be trouble with |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
360 // ftell/fseek when reading files opened in text mode. |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
361 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
362 if (check_fseek_ftell_workaround_needed (false)) |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
363 { |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
364 if (check_fseek_ftell_workaround_needed (true)) |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
365 (*current_liboctave_warning_handler) |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
366 ("fseek/ftell may fail for files opened in text mode"); |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
367 else |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
368 fseek_ftell_bug_workaround_needed = true; |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
369 } |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
370 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
371 fseek_ftell_bug_checked = true; |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
372 } |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
373 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
374 if (fseek_ftell_bug_workaround_needed |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
375 && mode.find ('t') != std::string::npos) |
28223
45763d59cb4f
use nullptr instead of NULL or 0 in a few more places
John W. Eaton <jwe@octave.org>
parents:
28220
diff
changeset
|
376 ::setvbuf (fptr, nullptr, _IONBF, 0); |
28220
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
377 |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
378 return fptr; |
6cccc3c82175
attempt to work around Windows ftello/fseeko bug (bug #58055)
John W. Eaton <jwe@octave.org>
parents:
28175
diff
changeset
|
379 |
25517
7fbc39a46be8
Add wrapper to fopen for files with non-ASCII chars in path on Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25516
diff
changeset
|
380 #else |
7fbc39a46be8
Add wrapper to fopen for files with non-ASCII chars in path on Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25516
diff
changeset
|
381 return std::fopen (filename.c_str (), mode.c_str ()); |
7fbc39a46be8
Add wrapper to fopen for files with non-ASCII chars in path on Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25516
diff
changeset
|
382 #endif |
7fbc39a46be8
Add wrapper to fopen for files with non-ASCII chars in path on Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25516
diff
changeset
|
383 } |
7fbc39a46be8
Add wrapper to fopen for files with non-ASCII chars in path on Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25516
diff
changeset
|
384 |
30881
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
385 std::FILE * |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
386 fopen_tmp (const std::string& name, const std::string& mode) |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
387 { |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
388 #if defined (OCTAVE_USE_WINDOWS_API) |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
389 |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
390 // Append "D" to the mode string to indicate that this is a temporary |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
391 // file that should be deleted when the last open handle is closed. |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
392 std::string tmp_mode = mode + "D"; |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
393 |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
394 return std::fopen (name.c_str (), tmp_mode.c_str ()); |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
395 |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
396 #else |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
397 |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
398 std::FILE *fptr = std::fopen (name.c_str (), mode.c_str ()); |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
399 |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
400 // From gnulib: This relies on the Unix semantics that a file is not |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
401 // really removed until it is closed. |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
402 octave_unlink_wrapper (name.c_str ()); |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
403 |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
404 return fptr; |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
405 |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
406 #endif |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
407 } |
1921d9d0e62b
Unify detection of temporary directory (bug #62215).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30565
diff
changeset
|
408 |
29006
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
409 std::fstream |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
410 fstream (const std::string& filename, const std::ios::openmode mode) |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
411 { |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
412 #if defined (OCTAVE_USE_WINDOWS_API) |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
413 |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
414 std::wstring wfilename = u8_to_wstring (filename); |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
415 |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
416 return std::fstream (wfilename.c_str (), mode); |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
417 |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
418 #else |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
419 return std::fstream (filename.c_str (), mode); |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
420 #endif |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
421 } |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
422 |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
423 std::ifstream |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
424 ifstream (const std::string& filename, const std::ios::openmode mode) |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
425 { |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
426 #if defined (OCTAVE_USE_WINDOWS_API) |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
427 |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
428 std::wstring wfilename = u8_to_wstring (filename); |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
429 |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
430 return std::ifstream (wfilename.c_str (), mode); |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
431 |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
432 #else |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
433 return std::ifstream (filename.c_str (), mode); |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
434 #endif |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
435 } |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
436 |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
437 std::ofstream |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
438 ofstream (const std::string& filename, const std::ios::openmode mode) |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
439 { |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
440 #if defined (OCTAVE_USE_WINDOWS_API) |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
441 |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
442 std::wstring wfilename = u8_to_wstring (filename); |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
443 |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
444 return std::ofstream (wfilename.c_str (), mode); |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
445 |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
446 #else |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
447 return std::ofstream (filename.c_str (), mode); |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
448 #endif |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
449 } |
139df373c107
Use wide character overload to open file streams on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28470
diff
changeset
|
450 |
25916
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
451 void |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
452 putenv_wrapper (const std::string& name, const std::string& value) |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
453 { |
29492
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
454 std::string new_env = name + "=" + value; |
25916
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
455 |
29492
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
456 // FIXME: The malloc leaks memory, but so would a call to setenv. |
25916
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
457 // Short of extreme measures to track memory, altering the environment |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
458 // always leaks memory, but the saving grace is that the leaks are small. |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
459 |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
460 // As far as I can see there's no way to distinguish between the |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
461 // various errors; putenv doesn't have errno values. |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
462 |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
463 #if defined (OCTAVE_USE_WINDOWS_API) |
29492
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
464 std::wstring new_wenv = u8_to_wstring (new_env); |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
465 |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
466 int len = (new_wenv.length () + 1) * sizeof (wchar_t); |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
467 |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
468 wchar_t *new_item = static_cast<wchar_t *> (std::malloc (len)); |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
469 |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
470 wcscpy (new_item, new_wenv.c_str()); |
28852
fe0298edc04d
Fix compilation error with lambda expression.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28851
diff
changeset
|
471 |
29492
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
472 if (_wputenv (new_item) < 0) |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
473 (*current_liboctave_error_handler) |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
474 ("putenv (%s) failed", new_env.c_str()); |
25916
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
475 #else |
29492
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
476 int len = new_env.length () + 1; |
28824
a98fd0657e35
Replace unwind_protect with more efficient constructs in liboctave (bug #59192).
Rik <rik@octave.org>
parents:
28534
diff
changeset
|
477 |
29492
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
478 char *new_item = static_cast<char *> (std::malloc (len)); |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
479 |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
480 std::strcpy (new_item, new_env.c_str()); |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
481 |
25916
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
482 if (octave_putenv_wrapper (new_item) < 0) |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
483 (*current_liboctave_error_handler) ("putenv (%s) failed", new_item); |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
484 #endif |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
485 } |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
486 |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
487 std::string |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
488 getenv_wrapper (const std::string& name) |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
489 { |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
490 #if defined (OCTAVE_USE_WINDOWS_API) |
28175
b895daca20e2
Avoid potential lifetime issues with temporary std::wstring objects.
Markus Mützel <markus.muetzel@gmx.de>
parents:
27933
diff
changeset
|
491 std::wstring wname = u8_to_wstring (name); |
b895daca20e2
Avoid potential lifetime issues with temporary std::wstring objects.
Markus Mützel <markus.muetzel@gmx.de>
parents:
27933
diff
changeset
|
492 wchar_t *env = _wgetenv (wname.c_str ()); |
25916
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
493 return env ? u8_from_wstring (env) : ""; |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
494 #else |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
495 char *env = ::getenv (name.c_str ()); |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
496 return env ? env : ""; |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
497 #endif |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
498 } |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
499 |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
500 int |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
501 unsetenv_wrapper (const std::string& name) |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
502 { |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
503 #if defined (OCTAVE_USE_WINDOWS_API) |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
504 putenv_wrapper (name, ""); |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
505 |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
506 std::wstring wname = u8_to_wstring (name); |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
507 return (SetEnvironmentVariableW (wname.c_str (), nullptr) ? 0 : -1); |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
508 #else |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
509 return octave_unsetenv_wrapper (name.c_str ()); |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
510 #endif |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
511 } |
3f2282954600
Support non-ASCII characters in environment variables on Windows (bug #53922).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25660
diff
changeset
|
512 |
25514
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
513 std::wstring |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
514 u8_to_wstring (const std::string& utf8_string) |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
515 { |
29492
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
516 // convert multibyte UTF-8 string to wide character string |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
517 static std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
518 wchar_conv; |
25514
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
519 |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
520 std::wstring retval = L""; |
29492
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
521 |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
522 try |
25514
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
523 { |
29492
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
524 retval = wchar_conv.from_bytes (utf8_string); |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
525 } |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
526 catch (const std::range_error& e) |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
527 { |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
528 // What to do in case of error? |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
529 // error ("u8_to_wstring: converting from UTF-8 to wchar_t: %s", |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
530 // e.what ()); |
25514
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
531 } |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
532 |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
533 return retval; |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
534 } |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
535 |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
536 std::string |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
537 u8_from_wstring (const std::wstring& wchar_string) |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
538 { |
29492
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
539 // convert wide character string to multibyte UTF-8 string |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
540 static std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
541 wchar_conv; |
25514
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
542 |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
543 std::string retval = ""; |
29492
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
544 |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
545 try |
25514
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
546 { |
29492
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
547 retval = wchar_conv.to_bytes (wchar_string); |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
548 } |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
549 catch (const std::range_error& e) |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
550 { |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
551 // What to do in case of error? |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
552 // error ("u8_from_wstring: converting from wchar_t to UTF-8: %s", |
be4b78fb4411
Replace Unicode conversion functions from gnulib with STL functions.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
553 // e.what ()); |
25514
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
554 } |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
555 |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
556 return retval; |
c63f67d87b4a
Add C++ functions to convert between UTF-8 string and wstring (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25054
diff
changeset
|
557 } |
25517
7fbc39a46be8
Add wrapper to fopen for files with non-ASCII chars in path on Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25516
diff
changeset
|
558 |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
559 // At quite a few places in the code we are passing file names as |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
560 // char arrays to external library functions. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
561 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
562 // When these functions try to locate the corresponding file on the |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
563 // disc, they need to use the wide character API on Windows to |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
564 // correctly open files with non-ASCII characters. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
565 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
566 // But they have no way of knowing which encoding we are using for |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
567 // the passed string. So they have no way of reliably converting to |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
568 // a wchar_t array. (I.e. there is no possible fix for these |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
569 // functions with current C or C++.) |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
570 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
571 // To solve the dilemma, the function "get_ASCII_filename" first |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
572 // checks whether there are any non-ASCII characters in the passed |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
573 // file name. If there are not, it returns the original name. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
574 |
28534
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
575 // Otherwise, it optionally tries to convert the file name to the locale |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
576 // charset. |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
577 |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
578 // If the file name contains characters that cannot be converted to the |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
579 // locale charset (or that step is skipped), it tries to obtain the short |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
580 // file name (8.3 naming scheme) which only consists of ASCII characters |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
581 // and are safe to pass. However, short file names can be disabled for |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
582 // performance reasons on the file system level with NTFS and they are not |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
583 // stored on other file systems (e.g. ExFAT). So there is no guarantee |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
584 // that these exist. |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
585 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
586 // If short file names are not stored, a hard link to the file is |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
587 // created. For this the path to the file is split at the deepest |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
588 // possible level that doesn't contain non-ASCII characters. At |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
589 // that level a hidden folder is created that holds the hard links. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
590 // That means we need to have write access on that location. A path |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
591 // to that hard link is returned. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
592 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
593 // If the file system is FAT32, there are no hard links. But FAT32 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
594 // always stores short file names. So we are safe. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
595 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
596 // ExFAT that is occasionally used on USB sticks and SD cards stores |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
597 // neither short file names nor does it support hard links. So for |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
598 // exFAT with this function, there is (currently) no way to generate |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
599 // a file name that is stripped from non-ASCII characters but still |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
600 // is valid. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
601 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
602 // For Unixy systems, this function does nothing. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
603 |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
604 std::string |
28534
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
605 get_ASCII_filename (const std::string& orig_file_name, |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
606 const bool allow_locale) |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
607 { |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
608 #if defined (OCTAVE_USE_WINDOWS_API) |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
609 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
610 // Return file name that only contains ASCII characters that can |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
611 // be used to access the file orig_file_name. The original file |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
612 // must exist in the file system before calling this function. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
613 // This is useful for passing file names to functions that are not |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
614 // aware of the character encoding we are using. |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
615 |
28534
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
616 // 0. Check whether filename contains non-ASCII (UTF-8) characters. |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
617 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
618 std::string::const_iterator first_non_ASCII |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
619 = std::find_if (orig_file_name.begin (), orig_file_name.end (), |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
620 [](char c) { return (c < 0 || c >= 128); }); |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
621 |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
622 if (first_non_ASCII == orig_file_name.end ()) |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
623 return orig_file_name; |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
624 |
28534
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
625 // 1. Optionally, check if all characters in the path can be successfully |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
626 // converted to the locale charset |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
627 if (allow_locale) |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
628 { |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
629 const char *locale = octave_locale_charset_wrapper (); |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
630 if (locale) |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
631 { |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
632 const uint8_t *name_u8 = reinterpret_cast<const uint8_t *> |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
633 (orig_file_name.c_str ()); |
29572
aef11bb4e6d1
use std::size_t and std::ptrdiff_t in C++ code (bug #60471)
John W. Eaton <jwe@octave.org>
parents:
29492
diff
changeset
|
634 std::size_t length = 0; |
28534
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
635 char *name_locale = octave_u8_conv_to_encoding_strict |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
636 (locale, name_u8, |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
637 orig_file_name.length () + 1, &length); |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
638 if (name_locale) |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
639 { |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
640 std::string file_name_locale (name_locale, length); |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
641 free (name_locale); |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
642 return file_name_locale; |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
643 } |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
644 } |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
645 } |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
646 |
29016
3b90b3b00451
get_ASCII_filename: Short file names might contain non-ASCII characters (bug #59269).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29007
diff
changeset
|
647 // 2. Check if file system stores short filenames (might be ASCII-only). |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
648 |
25660
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
649 std::wstring w_orig_file_name_str = u8_to_wstring (orig_file_name); |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
650 const wchar_t *w_orig_file_name = w_orig_file_name_str.c_str (); |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
651 |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
652 // Get full path to file |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
653 wchar_t w_full_file_name[_MAX_PATH]; |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
654 if (_wfullpath (w_full_file_name, w_orig_file_name, _MAX_PATH) == nullptr) |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
655 return orig_file_name; |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
656 |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
657 std::wstring w_full_file_name_str = w_full_file_name; |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
658 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
659 // Get short filename (8.3) from UTF-16 filename. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
660 |
25660
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
661 long length = GetShortPathNameW (w_full_file_name, nullptr, 0); |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
662 |
25660
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
663 if (length > 0) |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
664 { |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
665 // Dynamically allocate the correct size (terminating null char |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
666 // was included in length). |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
667 |
28534
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
668 OCTAVE_LOCAL_BUFFER (wchar_t, w_short_file_name, length); |
25660
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
669 GetShortPathNameW (w_full_file_name, w_short_file_name, length); |
25609
506419b5f817
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
25532
diff
changeset
|
670 |
25660
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
671 std::wstring w_short_file_name_str |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
672 = std::wstring (w_short_file_name, length); |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
673 |
25660
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
674 if (w_short_file_name_str.compare (0, length-1, w_full_file_name_str) != 0) |
29016
3b90b3b00451
get_ASCII_filename: Short file names might contain non-ASCII characters (bug #59269).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29007
diff
changeset
|
675 { |
3b90b3b00451
get_ASCII_filename: Short file names might contain non-ASCII characters (bug #59269).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29007
diff
changeset
|
676 // Check whether short file name contains non-ASCII characters |
3b90b3b00451
get_ASCII_filename: Short file names might contain non-ASCII characters (bug #59269).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29007
diff
changeset
|
677 std::string short_file_name |
3b90b3b00451
get_ASCII_filename: Short file names might contain non-ASCII characters (bug #59269).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29007
diff
changeset
|
678 = u8_from_wstring (w_short_file_name_str); |
3b90b3b00451
get_ASCII_filename: Short file names might contain non-ASCII characters (bug #59269).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29007
diff
changeset
|
679 first_non_ASCII |
3b90b3b00451
get_ASCII_filename: Short file names might contain non-ASCII characters (bug #59269).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29007
diff
changeset
|
680 = std::find_if (short_file_name.begin (), |
3b90b3b00451
get_ASCII_filename: Short file names might contain non-ASCII characters (bug #59269).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29007
diff
changeset
|
681 short_file_name.end (), |
3b90b3b00451
get_ASCII_filename: Short file names might contain non-ASCII characters (bug #59269).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29007
diff
changeset
|
682 [](char c) { return (c < 0 || c >= 128); }); |
3b90b3b00451
get_ASCII_filename: Short file names might contain non-ASCII characters (bug #59269).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29007
diff
changeset
|
683 if (first_non_ASCII == short_file_name.end ()) |
3b90b3b00451
get_ASCII_filename: Short file names might contain non-ASCII characters (bug #59269).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29007
diff
changeset
|
684 return short_file_name; |
3b90b3b00451
get_ASCII_filename: Short file names might contain non-ASCII characters (bug #59269).
Markus Mützel <markus.muetzel@gmx.de>
parents:
29007
diff
changeset
|
685 } |
25660
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
686 } |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
687 |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
688 // 3. Create hard link with only-ASCII characters. |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
689 // Get longest possible part of path that only contains ASCII chars. |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
690 |
25660
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
691 std::wstring::iterator w_first_non_ASCII |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
692 = std::find_if (w_full_file_name_str.begin (), w_full_file_name_str.end (), |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
693 [](wchar_t c) { return (c < 0 || c >= 128); }); |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
694 std::wstring tmp_substr |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
695 = std::wstring (w_full_file_name_str.begin (), w_first_non_ASCII); |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
696 |
29654
d13d090cb03a
use std::size_t and std::ptrdiff_t in C++ code (bug #60471)
John W. Eaton <jwe@octave.org>
parents:
29358
diff
changeset
|
697 std::size_t pos |
25660
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
698 = tmp_substr.find_last_of (u8_to_wstring (file_ops::dir_sep_chars ())); |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
699 |
25660
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
700 std::string par_dir |
4de7535ece11
Relative path specifications for non-ASCII files on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents:
25615
diff
changeset
|
701 = u8_from_wstring (w_full_file_name_str.substr (0, pos+1)); |
25609
506419b5f817
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
25532
diff
changeset
|
702 |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
703 // Create .oct_ascii directory. |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
704 // FIXME: We need to have write permission in this location. |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
705 |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
706 std::string oct_ascii_dir = par_dir + ".oct_ascii"; |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
707 std::string test_dir = canonicalize_file_name (oct_ascii_dir); |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
708 |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
709 if (test_dir.empty ()) |
27933
863ae57eee69
maint: Use Octave coding conventions in liboctave/
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
710 { |
863ae57eee69
maint: Use Octave coding conventions in liboctave/
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
711 std::string msg; |
863ae57eee69
maint: Use Octave coding conventions in liboctave/
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
712 int status = sys::mkdir (oct_ascii_dir, 0777, msg); |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
713 |
27933
863ae57eee69
maint: Use Octave coding conventions in liboctave/
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
714 if (status < 0) |
863ae57eee69
maint: Use Octave coding conventions in liboctave/
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
715 return orig_file_name; |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
716 |
27933
863ae57eee69
maint: Use Octave coding conventions in liboctave/
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
717 // Set hidden property. |
863ae57eee69
maint: Use Octave coding conventions in liboctave/
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
718 SetFileAttributesA (oct_ascii_dir.c_str (), FILE_ATTRIBUTE_HIDDEN); |
863ae57eee69
maint: Use Octave coding conventions in liboctave/
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
719 } |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
720 |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
721 // Create file from hash of full filename. |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
722 std::string filename_hash |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
723 = (oct_ascii_dir + file_ops::dir_sep_str () |
27102
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26531
diff
changeset
|
724 + crypto::hash ("SHA1", orig_file_name)); |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
725 |
30534
f69ddab4b0ec
get_ASCII_filename: Replace VLA with std::wstring.
Gene Harvey <gharveymn@gmail.com>
parents:
29657
diff
changeset
|
726 // FIXME: This is just to check if the file exists. Use a more efficient |
f69ddab4b0ec
get_ASCII_filename: Replace VLA with std::wstring.
Gene Harvey <gharveymn@gmail.com>
parents:
29657
diff
changeset
|
727 // method. |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
728 std::string abs_filename_hash = canonicalize_file_name (filename_hash); |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
729 |
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
730 if (! abs_filename_hash.empty ()) |
27102
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26531
diff
changeset
|
731 sys::unlink (filename_hash); |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
732 |
30534
f69ddab4b0ec
get_ASCII_filename: Replace VLA with std::wstring.
Gene Harvey <gharveymn@gmail.com>
parents:
29657
diff
changeset
|
733 // At this point, we know that we have only ASCII characters. |
f69ddab4b0ec
get_ASCII_filename: Replace VLA with std::wstring.
Gene Harvey <gharveymn@gmail.com>
parents:
29657
diff
changeset
|
734 // So instead of converting, just copy the characters to std::wstring. |
f69ddab4b0ec
get_ASCII_filename: Replace VLA with std::wstring.
Gene Harvey <gharveymn@gmail.com>
parents:
29657
diff
changeset
|
735 std::wstring w_filename_hash (filename_hash.begin (), |
f69ddab4b0ec
get_ASCII_filename: Replace VLA with std::wstring.
Gene Harvey <gharveymn@gmail.com>
parents:
29657
diff
changeset
|
736 filename_hash.end ()); |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
737 |
30534
f69ddab4b0ec
get_ASCII_filename: Replace VLA with std::wstring.
Gene Harvey <gharveymn@gmail.com>
parents:
29657
diff
changeset
|
738 if (CreateHardLinkW (w_filename_hash.c_str (), w_orig_file_name, nullptr)) |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
739 return filename_hash; |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
740 |
28534
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
741 #else |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
742 |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
743 octave_unused_parameter (allow_locale); |
548598760b66
get_ASCII_filename: On Windows, optionally try to convert to the locale charset.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28472
diff
changeset
|
744 |
25615
ffc858064239
style fixes and commentary for get_ASCII_filename function (bug #54299)
John W. Eaton <jwe@octave.org>
parents:
25609
diff
changeset
|
745 #endif |
25532
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
746 |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
747 return orig_file_name; |
7dad5fa7e88e
Add function for ASCII only file names (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25531
diff
changeset
|
748 } |
31605
e88a07dec498
maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents:
30881
diff
changeset
|
749 |
e88a07dec498
maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents:
30881
diff
changeset
|
750 OCTAVE_END_NAMESPACE(sys) |
e88a07dec498
maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents:
30881
diff
changeset
|
751 OCTAVE_END_NAMESPACE(octave) |