annotate liboctave/system/file-ops.cc @ 29654:d13d090cb03a stable

use std::size_t and std::ptrdiff_t in C++ code (bug #60471) Files affected: make_int.cc, file-editor-tab.cc, octave-qscintilla.cc, Cell.cc, Cell.h, call-stack.cc, call-stack.h, cellfun.cc, data.cc, debug.cc, dlmread.cc, error.cc, event-queue.h, fcn-info.cc, fcn-info.h, file-io.cc, ft-text-renderer.cc, gl2ps-print.cc, graphics.cc, graphics.in.h, help.cc, hex2num.cc, input.cc, latex-text-renderer.cc, load-path.cc, load-save.cc, load-save.h, ls-hdf5.cc, ls-mat-ascii.cc, ls-mat5.cc, ls-oct-text.cc, mex.cc, mexproto.h, mxarray.h, oct-map.cc, oct-stream.cc, oct-stream.h, pager.cc, pager.h, pr-output.cc, regexp.cc, settings.h, stack-frame.cc, stack-frame.h, strfns.cc, syminfo.cc, symrec.h, symscope.cc, symscope.h, symtab.cc, sysdep.cc, toplev.cc, utils.cc, utils.h, variables.cc, __fltk_uigetfile__.cc, __init_fltk__.cc, audioread.cc, gzip.cc, cdef-class.cc, cdef-manager.cc, cdef-method.cc, cdef-object.cc, cdef-object.h, ov-base-diag.cc, ov-base-diag.h, ov-base-mat.cc, ov-base-mat.h, ov-base-scalar.cc, ov-base-scalar.h, ov-base-sparse.h, ov-base.cc, ov-base.h, ov-cell.cc, ov-cell.h, ov-ch-mat.cc, ov-class.cc, ov-class.h, ov-classdef.cc, ov-fcn-handle.cc, ov-java.cc, ov-lazy-idx.h, ov-perm.cc, ov-perm.h, ov-range.h, ov-str-mat.cc, ov-struct.cc, ov-struct.h, ov-usr-fcn.cc, ov-usr-fcn.h, ov.cc, ov.h, ovl.cc, octave.cc, bp-table.cc, jit-ir.cc, jit-ir.h, jit-typeinfo.cc, jit-typeinfo.h, jit-util.h, lex.h, lex.ll, oct-lvalue.cc, oct-parse.yy, parse.h, profiler.h, pt-eval.cc, pt-eval.h, pt-jit.cc, pt-jit.h, pt-pr-code.cc, pt-tm-const.cc, pt-tm-const.h, Array.h, CMatrix.cc, DiagArray2.h, PermMatrix.h, Sparse.h, dMatrix.cc, fCMatrix.cc, fMatrix.cc, bsxfun-defs.cc, oct-fftw.cc, oct-fftw.h, randpoisson.cc, sparse-chol.cc, mx-inlines.cc, file-ops.cc, lo-sysdep.cc, oct-env.cc, oct-time.cc, action-container.cc, action-container.h, base-list.h, caseless-str.h, cmd-edit.cc, cmd-hist.cc, data-conv.cc, data-conv.h, f77-fcn.h, file-info.cc, file-info.h, kpse.cc, kpse.h, lo-cutils.h, lo-hash.h, lo-regexp.cc, oct-base64.cc, oct-base64.h, oct-binmap.h, oct-glob.cc, oct-shlib.cc, oct-shlib.h, oct-sort.cc, oct-sparse.h, oct-string.cc, quit.cc, unwind-prot.h, url-transfer.cc, main.in.cc, mkoctfile.in.cc, and shared-fcns.h. (grafted from aef11bb4e6d1f303ad9de5688fcb7244ef48867e)
author John W. Eaton <jwe@octave.org>
date Wed, 28 Apr 2021 22:57:42 -0400
parents 0a5b15007766
children 32f4357ac8d9 7f5bd197fea6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 //
29358
0a5b15007766 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 28471
diff changeset
3 // Copyright (C) 1996-2021 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 ////////////////////////////////////////////////////////////////////////
1765
a51354c34bea [project @ 1996-01-23 03:33:34 by jwe]
jwe
parents:
diff changeset
25
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21308
diff changeset
26 #if defined (HAVE_CONFIG_H)
21301
40de9f8f23a6 Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents: 21202
diff changeset
27 # include "config.h"
1765
a51354c34bea [project @ 1996-01-23 03:33:34 by jwe]
jwe
parents:
diff changeset
28 #endif
a51354c34bea [project @ 1996-01-23 03:33:34 by jwe]
jwe
parents:
diff changeset
29
a51354c34bea [project @ 1996-01-23 03:33:34 by jwe]
jwe
parents:
diff changeset
30 #include <cerrno>
10463
bbe99b2a5ba7 undo recent gnulib-related changes
John W. Eaton <jwe@octave.org>
parents: 10447
diff changeset
31 #include <cstdio>
bbe99b2a5ba7 undo recent gnulib-related changes
John W. Eaton <jwe@octave.org>
parents: 10447
diff changeset
32 #include <cstdlib>
bbe99b2a5ba7 undo recent gnulib-related changes
John W. Eaton <jwe@octave.org>
parents: 10447
diff changeset
33 #include <cstring>
1765
a51354c34bea [project @ 1996-01-23 03:33:34 by jwe]
jwe
parents:
diff changeset
34
28471
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
35 #include <vector>
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
36
27496
def608acdfa9 Canonicalize case of file path on Windows file systems (bug #56267).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27102
diff changeset
37 #if defined (OCTAVE_USE_WINDOWS_API)
def608acdfa9 Canonicalize case of file path on Windows file systems (bug #56267).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27102
diff changeset
38 # include <windows.h>
28053
b70b9eaaf751 Actually canonicalize UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28051
diff changeset
39 # include <shlwapi.h>
27496
def608acdfa9 Canonicalize case of file path on Windows file systems (bug #56267).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27102
diff changeset
40 #endif
21896
83c824c727c9 hide gnulib pathlen header
John W. Eaton <jwe@octave.org>
parents: 21890
diff changeset
41
28471
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
42 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
43 # include <algorithm>
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
44 # include "localcharset-wrapper.h"
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
45 # include "uniconv-wrappers.h"
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
46 #endif
3040
443851377f3f [project @ 1997-06-06 09:29:28 by jwe]
jwe
parents: 2947
diff changeset
47
21896
83c824c727c9 hide gnulib pathlen header
John W. Eaton <jwe@octave.org>
parents: 21890
diff changeset
48 #include "areadlink-wrapper.h"
21888
00f1249f2483 create wrappers for some gnulib functions
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
49 #include "canonicalize-file-name-wrapper.h"
5476
941f0fc6b596 [project @ 2005-09-29 22:46:07 by jwe]
jwe
parents: 5307
diff changeset
50 #include "dir-ops.h"
1765
a51354c34bea [project @ 1996-01-23 03:33:34 by jwe]
jwe
parents:
diff changeset
51 #include "file-ops.h"
5476
941f0fc6b596 [project @ 2005-09-29 22:46:07 by jwe]
jwe
parents: 5307
diff changeset
52 #include "file-stat.h"
21896
83c824c727c9 hide gnulib pathlen header
John W. Eaton <jwe@octave.org>
parents: 21890
diff changeset
53 #include "gen-tempname-wrapper.h"
25516
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25438
diff changeset
54 #include "lo-sysdep.h"
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2847
diff changeset
55 #include "oct-env.h"
13983
7dd7cccf0757 clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
56 #include "oct-locbuf.h"
2934
dddc1b5c324e [project @ 1997-05-05 22:56:37 by jwe]
jwe
parents: 2926
diff changeset
57 #include "oct-passwd.h"
5476
941f0fc6b596 [project @ 2005-09-29 22:46:07 by jwe]
jwe
parents: 5307
diff changeset
58 #include "quit.h"
21909
d7cac41df82a provide wrappers for some sys/stat functions
John W. Eaton <jwe@octave.org>
parents: 21896
diff changeset
59 #include "stat-wrappers.h"
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2847
diff changeset
60 #include "str-vec.h"
21910
4d723ba06b4a provide wrappers for some unistd.h functions
John W. Eaton <jwe@octave.org>
parents: 21909
diff changeset
61 #include "unistd-wrappers.h"
20503
0829b6ff3ac9 Use backslash as windows file separator for canonicalize_file_name (Bug #45816).
Rik <rik@octave.org>
parents: 19697
diff changeset
62
21734
11af9c03400c write namespace keyword and name on same line in C++ files
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
63 namespace octave
8007
a2ab20ba78f7 make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents: 7520
diff changeset
64 {
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
65 // The following tilde-expansion code was stolen and adapted from
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
66 // readline.
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
67
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
68 // The default value of tilde_additional_prefixes. This is set to
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
69 // whitespace preceding a tilde so that simple programs which do not
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
70 // perform any word separation get desired behavior.
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
71 static const char *default_prefixes[] = { " ~", "\t~", ":~", nullptr };
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
72
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
73 // The default value of tilde_additional_suffixes. This is set to
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
74 // whitespace or newline so that simple programs which do not perform
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
75 // any word separation get desired behavior.
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
76 static const char *default_suffixes[] = { " ", "\n", ":", nullptr };
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
77
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
78 static std::size_t
d13d090cb03a use std::size_t and std::ptrdiff_t in C++ code (bug #60471)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
79 tilde_find_prefix (const std::string& s, std::size_t& len)
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
80 {
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
81 len = 0;
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
82
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
83 std::size_t s_len = s.length ();
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
84
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
85 if (s_len == 0 || s[0] == '~')
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
86 return 0;
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
87
27102
84ff9953faa1 where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 27101
diff changeset
88 string_vector prefixes = sys::file_ops::tilde_additional_prefixes;
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
89
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
90 if (! prefixes.empty ())
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
91 {
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
92 for (std::size_t i = 0; i < s_len; i++)
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
93 {
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
94 for (int j = 0; j < prefixes.numel (); j++)
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
95 {
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
96 std::size_t pfx_len = prefixes[j].length ();
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
97
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
98 if (prefixes[j] == s.substr (i, pfx_len))
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
99 {
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
100 len = pfx_len - 1;
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
101 return i + len;
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
102 }
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
103 }
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
104 }
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
105 }
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
106
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
107 return s_len;
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
108 }
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
109
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
110 // Find the end of a tilde expansion in S, and return the index
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
111 // of the character which ends the tilde definition.
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
112
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
113 static std::size_t
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
114 tilde_find_suffix (const std::string& s)
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
115 {
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
116 std::size_t s_len = s.length ();
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
117
27102
84ff9953faa1 where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 27101
diff changeset
118 string_vector suffixes = sys::file_ops::tilde_additional_suffixes;
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
119
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
120 std::size_t i = 0;
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
121
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
122 for ( ; i < s_len; i++)
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
123 {
27102
84ff9953faa1 where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 27101
diff changeset
124 if (sys::file_ops::is_dir_sep (s[i]))
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
125 break;
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
126
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
127 if (! suffixes.empty ())
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
128 {
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
129 for (int j = 0; j < suffixes.numel (); j++)
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
130 {
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
131 std::size_t sfx_len = suffixes[j].length ();
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
132
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
133 if (suffixes[j] == s.substr (i, sfx_len))
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
134 return i;
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
135 }
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
136 }
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
137 }
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
138
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
139 return i;
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
140 }
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
141
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
142 // Take FNAME and return the tilde prefix we want expanded.
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
143
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
144 static std::string
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
145 isolate_tilde_prefix (const std::string& fname)
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
146 {
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
147 std::size_t f_len = fname.length ();
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
148
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
149 std::size_t len = 1;
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
150
27102
84ff9953faa1 where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 27101
diff changeset
151 while (len < f_len && ! sys::file_ops::is_dir_sep (fname[len]))
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
152 len++;
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
153
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
154 return fname.substr (1, len);
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
155 }
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
156
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
157 // Do the work of tilde expansion on FILENAME. FILENAME starts with a
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
158 // tilde.
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
159
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
160 static std::string
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
161 tilde_expand_word (const std::string& filename)
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
162 {
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
163 std::size_t f_len = filename.length ();
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
164
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
165 if (f_len == 0 || filename[0] != '~')
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
166 return std::string (filename);
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
167
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
168 // A leading '~/' or a bare '~' is *always* translated to the value
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
169 // of $HOME or the home directory of the current user, regardless of
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
170 // any preexpansion hook.
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
171
27102
84ff9953faa1 where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 27101
diff changeset
172 if (f_len == 1 || sys::file_ops::is_dir_sep (filename[1]))
84ff9953faa1 where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 27101
diff changeset
173 return sys::env::get_home_directory () + filename.substr (1);
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
174
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
175 std::string username = isolate_tilde_prefix (filename);
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
176
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
177 std::size_t user_len = username.length ();
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
178
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
179 std::string dirname;
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
180
27102
84ff9953faa1 where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 27101
diff changeset
181 if (sys::file_ops::tilde_expansion_preexpansion_hook)
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
182 {
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
183 std::string expansion
27102
84ff9953faa1 where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 27101
diff changeset
184 = sys::file_ops::tilde_expansion_preexpansion_hook (username);
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
185
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
186 if (! expansion.empty ())
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
187 return expansion + filename.substr (user_len+1);
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
188 }
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
189
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
190 // No preexpansion hook, or the preexpansion hook failed. Look in the
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
191 // password database.
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
192
27102
84ff9953faa1 where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 27101
diff changeset
193 sys::password pw = sys::password::getpwnam (username);
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
194
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
195 if (! pw)
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
196 {
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
197 // If the calling program has a special syntax for expanding tildes,
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
198 // and we couldn't find a standard expansion, then let them try.
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
199
27102
84ff9953faa1 where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 27101
diff changeset
200 if (sys::file_ops::tilde_expansion_failure_hook)
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
201 {
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
202 std::string expansion
27102
84ff9953faa1 where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 27101
diff changeset
203 = sys::file_ops::tilde_expansion_failure_hook (username);
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
204
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
205 if (! expansion.empty ())
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
206 dirname = expansion + filename.substr (user_len+1);
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
207 }
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
208
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
209 // If we don't have a failure hook, or if the failure hook did not
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
210 // expand the tilde, return a copy of what we were passed.
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
211
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
212 if (dirname.empty ())
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
213 dirname = filename;
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
214 }
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
215 else
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
216 dirname = pw.dir () + filename.substr (user_len+1);
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
217
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
218 return dirname;
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
219 }
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
220
21734
11af9c03400c write namespace keyword and name on same line in C++ files
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
221 namespace sys
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
222 {
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
223 namespace file_ops
8007
a2ab20ba78f7 make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents: 7520
diff changeset
224 {
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
225 char dev_sep_char (void)
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
226 {
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
227 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
228 return ':';
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
229 #else
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
230 return 0;
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
231 #endif
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
232 }
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
233
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
234 char dir_sep_char (void)
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
235 {
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
236 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
237 return '\\';
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
238 #else
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
239 return '/';
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
240 #endif
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
241 }
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
242
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
243 std::string dir_sep_str (void)
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
244 {
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
245 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
23804
8057d3f0673d Use C++11 raw string literals to avoid escaping backslashes in strings.
Rik <rik@octave.org>
parents: 23795
diff changeset
246 return R"(\)";
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
247 #else
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
248 return "/";
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
249 #endif
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
250 }
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
251
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
252 std::string dir_sep_chars (void)
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
253 {
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
254 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM)
23804
8057d3f0673d Use C++11 raw string literals to avoid escaping backslashes in strings.
Rik <rik@octave.org>
parents: 23795
diff changeset
255 return R"(/\)";
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
256 #else
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
257 return dir_sep_str ();
5138
5fa9670b5956 [project @ 2005-02-09 23:13:03 by jwe]
jwe
parents: 4726
diff changeset
258 #endif
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
259 }
6271
6fab59e81b4e [project @ 2007-02-06 01:36:46 by jwe]
jwe
parents: 6208
diff changeset
260
23795
980f39c3ab90 Use C++11 nullptr rather than 0 in code (bug #51565).
Rik <rik@octave.org>
parents: 23744
diff changeset
261 tilde_expansion_hook tilde_expansion_preexpansion_hook = nullptr;
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
262
23795
980f39c3ab90 Use C++11 nullptr rather than 0 in code (bug #51565).
Rik <rik@octave.org>
parents: 23744
diff changeset
263 tilde_expansion_hook tilde_expansion_failure_hook = nullptr;
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
264
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
265 string_vector tilde_additional_prefixes = default_prefixes;
5138
5fa9670b5956 [project @ 2005-02-09 23:13:03 by jwe]
jwe
parents: 4726
diff changeset
266
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
267 string_vector tilde_additional_suffixes = default_suffixes;
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
268
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
269 bool is_dev_sep (char c)
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
270 {
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
271 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
272 return c == dev_sep_char ();
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
273 #else
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
274 octave_unused_parameter (c);
1802
8173b1fa052d [project @ 1996-01-29 06:10:18 by jwe]
jwe
parents: 1779
diff changeset
275
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
276 return false;
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
277 #endif
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
278 }
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2847
diff changeset
279
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
280 bool is_dir_sep (char c)
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
281 {
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
282 std::string tmp = dir_sep_chars ();
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
283 return tmp.find (c) != std::string::npos;
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
284 }
3040
443851377f3f [project @ 1997-06-06 09:29:28 by jwe]
jwe
parents: 2947
diff changeset
285
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
286 std::string tilde_expand (const std::string& name)
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
287 {
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
288 if (name.find ('~') == std::string::npos)
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
289 return std::string (name);
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
290 else
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
291 {
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
292 std::string result;
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
293
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
294 std::size_t name_len = name.length ();
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
295
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
296 // Scan through S expanding tildes as we come to them.
3040
443851377f3f [project @ 1997-06-06 09:29:28 by jwe]
jwe
parents: 2947
diff changeset
297
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
298 std::size_t pos = 0;
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
299
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
300 while (1)
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
301 {
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
302 if (pos > name_len)
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
303 break;
3040
443851377f3f [project @ 1997-06-06 09:29:28 by jwe]
jwe
parents: 2947
diff changeset
304
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
305 std::size_t len;
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
306
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
307 // Make START point to the tilde which starts the expansion.
3040
443851377f3f [project @ 1997-06-06 09:29:28 by jwe]
jwe
parents: 2947
diff changeset
308
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
309 std::size_t start = tilde_find_prefix (name.substr (pos), len);
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
310
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
311 result.append (name.substr (pos, start));
3040
443851377f3f [project @ 1997-06-06 09:29:28 by jwe]
jwe
parents: 2947
diff changeset
312
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
313 // Advance STRING to the starting tilde.
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
314
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
315 pos += start;
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
316
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
317 // Make FINI be the index of one after the last character of the
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
318 // username.
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
319
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
320 std::size_t fini = tilde_find_suffix (name.substr (pos));
3040
443851377f3f [project @ 1997-06-06 09:29:28 by jwe]
jwe
parents: 2947
diff changeset
321
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
322 // If both START and FINI are zero, we are all done.
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
323
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
324 if (! (start || fini))
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
325 break;
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
326
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
327 // Expand the entire tilde word, and copy it into RESULT.
3040
443851377f3f [project @ 1997-06-06 09:29:28 by jwe]
jwe
parents: 2947
diff changeset
328
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
329 std::string tilde_word = name.substr (pos, fini);
3040
443851377f3f [project @ 1997-06-06 09:29:28 by jwe]
jwe
parents: 2947
diff changeset
330
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
331 pos += fini;
3040
443851377f3f [project @ 1997-06-06 09:29:28 by jwe]
jwe
parents: 2947
diff changeset
332
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
333 std::string expansion = tilde_expand_word (tilde_word);
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
334
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
335 result.append (expansion);
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
336 }
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
337
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
338 return result;
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
339 }
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
340 }
3040
443851377f3f [project @ 1997-06-06 09:29:28 by jwe]
jwe
parents: 2947
diff changeset
341
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
342 string_vector tilde_expand (const string_vector& names)
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
343 {
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
344 int n = names.numel ();
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
345
23812
057a894914df Use C++11 string fcns back() and pop_back() to simplify code.
Rik <rik@octave.org>
parents: 23804
diff changeset
346 string_vector retval (n);
3040
443851377f3f [project @ 1997-06-06 09:29:28 by jwe]
jwe
parents: 2947
diff changeset
347
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
348 for (int i = 0; i < n; i++)
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
349 retval[i] = tilde_expand (names[i]);
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
350
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
351 return retval;
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
352 }
3040
443851377f3f [project @ 1997-06-06 09:29:28 by jwe]
jwe
parents: 2947
diff changeset
353
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
354 std::string concat (const std::string& dir, const std::string& file)
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
355 {
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
356 return dir.empty ()
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
357 ? file
23812
057a894914df Use C++11 string fcns back() and pop_back() to simplify code.
Rik <rik@octave.org>
parents: 23804
diff changeset
358 : (is_dir_sep (dir.back ())
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
359 ? dir + file
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
360 : dir + dir_sep_char () + file);
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
361 }
3040
443851377f3f [project @ 1997-06-06 09:29:28 by jwe]
jwe
parents: 2947
diff changeset
362
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
363 std::string dirname (const std::string& path)
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
364 {
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
365 std::size_t ipos = path.find_last_of (dir_sep_chars ());
3040
443851377f3f [project @ 1997-06-06 09:29:28 by jwe]
jwe
parents: 2947
diff changeset
366
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
367 return (ipos != std::string::npos) ? path.substr (0, ipos) : "";
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
368 }
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
369
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
370 std::string tail (const std::string& path)
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
371 {
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
372 std::size_t ipos = path.find_last_of (dir_sep_chars ());
3040
443851377f3f [project @ 1997-06-06 09:29:28 by jwe]
jwe
parents: 2947
diff changeset
373
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
374 if (ipos != std::string::npos)
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
375 ipos++;
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
376 else
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
377 ipos = 0;
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
378
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
379 return path.substr (ipos);
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
380 }
3040
443851377f3f [project @ 1997-06-06 09:29:28 by jwe]
jwe
parents: 2947
diff changeset
381
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
382 std::string native_separator_path (const std::string& path)
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
383 {
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
384 std::string retval;
3040
443851377f3f [project @ 1997-06-06 09:29:28 by jwe]
jwe
parents: 2947
diff changeset
385
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
386 if (dir_sep_char () == '/')
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
387 retval = path;
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
388 else
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
389 {
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
390 std::size_t n = path.length ();
d13d090cb03a use std::size_t and std::ptrdiff_t in C++ code (bug #60471)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
391 for (std::size_t i = 0; i < n; i++)
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
392 {
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
393 if (path[i] == '/')
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
394 retval += dir_sep_char();
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
395 else
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
396 retval += path[i];
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
397 }
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
398 }
3040
443851377f3f [project @ 1997-06-06 09:29:28 by jwe]
jwe
parents: 2947
diff changeset
399
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
400 return retval;
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
401 }
3040
443851377f3f [project @ 1997-06-06 09:29:28 by jwe]
jwe
parents: 2947
diff changeset
402 }
443851377f3f [project @ 1997-06-06 09:29:28 by jwe]
jwe
parents: 2947
diff changeset
403
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
404 int mkdir (const std::string& nm, mode_t md)
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
405 {
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
406 std::string msg;
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
407 return mkdir (nm, md, msg);
21881
aea1bf9493c0 * file-ops.h, file-ops.cc (file_ops::file_ops): Now protected.
John W. Eaton <jwe@octave.org>
parents: 21852
diff changeset
408 }
aea1bf9493c0 * file-ops.h, file-ops.cc (file_ops::file_ops): Now protected.
John W. Eaton <jwe@octave.org>
parents: 21852
diff changeset
409
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
410 int mkdir (const std::string& name, mode_t mode, std::string& msg)
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
411 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
412 msg = "";
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
413
21909
d7cac41df82a provide wrappers for some sys/stat functions
John W. Eaton <jwe@octave.org>
parents: 21896
diff changeset
414 int status = octave_mkdir_wrapper (name.c_str (), mode);
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
415
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
416 if (status < 0)
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21910
diff changeset
417 msg = std::strerror (errno);
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
418
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
419 return status;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
420 }
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
421
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
422 int mkfifo (const std::string& nm, mode_t md)
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
423 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
424 std::string msg;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
425 return mkfifo (nm, md, msg);
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
426 }
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
427
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
428 int mkfifo (const std::string& name, mode_t mode, std::string& msg)
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
429 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
430 msg = "";
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
431
21909
d7cac41df82a provide wrappers for some sys/stat functions
John W. Eaton <jwe@octave.org>
parents: 21896
diff changeset
432 int status = octave_mkfifo_wrapper (name.c_str (), mode);
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
433
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
434 if (status < 0)
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21910
diff changeset
435 msg = std::strerror (errno);
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
436
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
437 return status;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
438 }
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
439
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
440 int link (const std::string& old_name, const std::string& new_name)
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
441 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
442 std::string msg;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
443 return link (old_name, new_name, msg);
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
444 }
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
445
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
446 int link (const std::string& old_name, const std::string& new_name,
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
447 std::string& msg)
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
448 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
449 msg = "";
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
450
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
451 int status = -1;
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
452
21910
4d723ba06b4a provide wrappers for some unistd.h functions
John W. Eaton <jwe@octave.org>
parents: 21909
diff changeset
453 status = octave_link_wrapper (old_name.c_str (), new_name.c_str ());
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
454
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
455 if (status < 0)
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21910
diff changeset
456 msg = std::strerror (errno);
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
457
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
458 return status;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
459 }
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
460
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
461 int symlink (const std::string& old_name, const std::string& new_name)
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
462 {
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
463 std::string msg;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
464 return symlink (old_name, new_name, msg);
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
465 }
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
466
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
467 int symlink (const std::string& old_name, const std::string& new_name,
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
468 std::string& msg)
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
469 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
470 msg = "";
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
471
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
472 int status = -1;
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
473
21910
4d723ba06b4a provide wrappers for some unistd.h functions
John W. Eaton <jwe@octave.org>
parents: 21909
diff changeset
474 status = octave_symlink_wrapper (old_name.c_str (), new_name.c_str ());
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
475
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
476 if (status < 0)
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21910
diff changeset
477 msg = std::strerror (errno);
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
478
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
479 return status;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
480 }
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
481
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
482 int readlink (const std::string& path, std::string& result)
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
483 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
484 std::string msg;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
485 return readlink (path, result, msg);
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
486 }
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
487
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
488 int readlink (const std::string& path, std::string& result, std::string& msg)
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
489 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
490 int status = -1;
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
491
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
492 msg = "";
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
493
21896
83c824c727c9 hide gnulib pathlen header
John W. Eaton <jwe@octave.org>
parents: 21890
diff changeset
494 char *buf = octave_areadlink_wrapper (path.c_str ());
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
495
21896
83c824c727c9 hide gnulib pathlen header
John W. Eaton <jwe@octave.org>
parents: 21890
diff changeset
496 if (! buf)
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21910
diff changeset
497 msg = std::strerror (errno);
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
498 else
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
499 {
21896
83c824c727c9 hide gnulib pathlen header
John W. Eaton <jwe@octave.org>
parents: 21890
diff changeset
500 result = buf;
83c824c727c9 hide gnulib pathlen header
John W. Eaton <jwe@octave.org>
parents: 21890
diff changeset
501 ::free (buf);
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
502 status = 0;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
503 }
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
504
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
505 return status;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
506 }
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
507
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
508 int rename (const std::string& from, const std::string& to)
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
509 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
510 std::string msg;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
511 return rename (from, to, msg);
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
512 }
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
513
25624
815cfd72ddf5 Fix "rename" for files with non-ASCII chars on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25516
diff changeset
514 int rename (const std::string& from, const std::string& to,
815cfd72ddf5 Fix "rename" for files with non-ASCII chars on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25516
diff changeset
515 std::string& msg)
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
516 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
517 int status = -1;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
518
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
519 msg = "";
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
520
25624
815cfd72ddf5 Fix "rename" for files with non-ASCII chars on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25516
diff changeset
521 #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: 28053
diff changeset
522 std::wstring wfrom = u8_to_wstring (from);
b895daca20e2 Avoid potential lifetime issues with temporary std::wstring objects.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28053
diff changeset
523 std::wstring wto = u8_to_wstring (to);
b895daca20e2 Avoid potential lifetime issues with temporary std::wstring objects.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28053
diff changeset
524 status = _wrename (wfrom.c_str (), wto.c_str ());
25624
815cfd72ddf5 Fix "rename" for files with non-ASCII chars on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25516
diff changeset
525 #else
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21910
diff changeset
526 status = std::rename (from.c_str (), to.c_str ());
25624
815cfd72ddf5 Fix "rename" for files with non-ASCII chars on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 25516
diff changeset
527 #endif
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
528
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
529 if (status < 0)
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21910
diff changeset
530 msg = std::strerror (errno);
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
531
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
532 return status;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
533 }
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
534
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
535 int rmdir (const std::string& name)
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
536 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
537 std::string msg;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
538 return rmdir (name, msg);
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
539 }
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
540
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
541 int rmdir (const std::string& name, std::string& msg)
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
542 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
543 msg = "";
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
544
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
545 int status = -1;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
546
21910
4d723ba06b4a provide wrappers for some unistd.h functions
John W. Eaton <jwe@octave.org>
parents: 21909
diff changeset
547 status = octave_rmdir_wrapper (name.c_str ());
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
548
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
549 if (status < 0)
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21910
diff changeset
550 msg = std::strerror (errno);
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
551
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
552 return status;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
553 }
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
554
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
555 // And a version that works recursively.
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
556
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
557 int recursive_rmdir (const std::string& name)
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
558 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
559 std::string msg;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
560 return recursive_rmdir (name, msg);
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
561 }
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
562
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
563 int recursive_rmdir (const std::string& name, std::string& msg)
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
564 {
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
565 msg = "";
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
566
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
567 int status = 0;
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
568
25516
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25438
diff changeset
569 string_vector dirlist;
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
570
25516
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25438
diff changeset
571 if (get_dirlist (name, dirlist, msg))
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
572 {
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
573 for (octave_idx_type i = 0; i < dirlist.numel (); i++)
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
574 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
575 octave_quit ();
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
576
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
577 std::string nm = dirlist[i];
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
578
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
579 // Skip current directory and parent.
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
580 if (nm == "." || nm == "..")
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
581 continue;
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
582
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
583 std::string fullnm = name + file_ops::dir_sep_str () + nm;
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
584
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
585 // Get info about the file. Don't follow links.
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
586 file_stat fs (fullnm, false);
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
587
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
588 if (fs)
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
589 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
590 if (fs.is_dir ())
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
591 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
592 status = recursive_rmdir (fullnm, msg);
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
593
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
594 if (status < 0)
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
595 break;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
596 }
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
597 else
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
598 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
599 status = unlink (fullnm, msg);
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
600
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
601 if (status < 0)
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
602 break;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
603 }
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
604 }
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
605 else
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
606 {
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
607 msg = fs.error ();
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
608 break;
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
609 }
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
610 }
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
611
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
612 if (status >= 0)
25516
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25438
diff changeset
613 status = rmdir (name, msg);
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
614 }
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
615 else
25516
8945a6a6c0eb Add Unicode support for getting directory listing in Windows (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 25438
diff changeset
616 status = -1;
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
617
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
618 return status;
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
619 }
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
620
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
621 int umask (mode_t mode)
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
622 {
21909
d7cac41df82a provide wrappers for some sys/stat functions
John W. Eaton <jwe@octave.org>
parents: 21896
diff changeset
623 return octave_umask_wrapper (mode);
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
624 }
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
625
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
626 int unlink (const std::string& name)
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
627 {
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
628 std::string msg;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
629 return unlink (name, msg);
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
630 }
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
631
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
632 int unlink (const std::string& name, std::string& msg)
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
633 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
634 msg = "";
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
635
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
636 int status = -1;
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
637
21910
4d723ba06b4a provide wrappers for some unistd.h functions
John W. Eaton <jwe@octave.org>
parents: 21909
diff changeset
638 status = octave_unlink_wrapper (name.c_str ());
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
639
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
640 if (status < 0)
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21910
diff changeset
641 msg = std::strerror (errno);
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
642
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
643 return status;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
644 }
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
645
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
646 std::string tempnam (const std::string& dir, const std::string& pfx)
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
647 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
648 std::string msg;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
649 return tempnam (dir, pfx, msg);
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
650 }
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
651
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
652 std::string tempnam (const std::string& dir, const std::string& pfx,
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
653 std::string& msg)
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
654 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
655 msg = "";
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
656
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
657 std::string retval;
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10189
diff changeset
658
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
659 // get dir path to use for template
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
660 std::string templatename;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
661 if (dir.empty ())
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
662 templatename = env::get_temp_directory ();
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
663 else if (! file_stat (dir, false).is_dir ())
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
664 templatename = env::get_temp_directory ();
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
665 else
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
666 templatename = dir;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
667
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
668 // add dir sep char if it is not there
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
669 if (*templatename.rbegin () != file_ops::dir_sep_char ())
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
670 templatename += file_ops::dir_sep_char ();
19420
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 18412
diff changeset
671
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
672 if (pfx.empty ())
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
673 templatename += "file";
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
674 else
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
675 templatename += pfx;
19458
d8fd3842a507 Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents: 19428
diff changeset
676
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
677 // add the required XXXXXX for the template
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
678 templatename += "XXXXXX";
19428
eee9f111c164 Check if tempfile name exists when changing path of temporary file name (Bug #43872)
John Donoghue
parents: 19420
diff changeset
679
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
680 // create and copy template to char array for call to gen_tempname
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
681 char tname [templatename.length () + 1];
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19458
diff changeset
682
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
683 strcpy (tname, templatename.c_str ());
10252
2fcc927a8757 liboctave/file-ops.h: avoid gnulib #define for canonicalize_file_name
John W. Eaton <jwe@octave.org>
parents: 10250
diff changeset
684
21888
00f1249f2483 create wrappers for some gnulib functions
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
685 if (octave_gen_tempname_wrapper (tname) == -1)
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21910
diff changeset
686 msg = std::strerror (errno);
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
687 else
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
688 retval = tname;
10252
2fcc927a8757 liboctave/file-ops.h: avoid gnulib #define for canonicalize_file_name
John W. Eaton <jwe@octave.org>
parents: 10250
diff changeset
689
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
690 return retval;
10252
2fcc927a8757 liboctave/file-ops.h: avoid gnulib #define for canonicalize_file_name
John W. Eaton <jwe@octave.org>
parents: 10250
diff changeset
691 }
2fcc927a8757 liboctave/file-ops.h: avoid gnulib #define for canonicalize_file_name
John W. Eaton <jwe@octave.org>
parents: 10250
diff changeset
692
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
693 std::string canonicalize_file_name (const std::string& name)
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
694 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
695 std::string msg;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
696 return canonicalize_file_name (name, msg);
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
697 }
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
698
23744
ae74608b6a5d don't use singleton pattern for file_ops
John W. Eaton <jwe@octave.org>
parents: 23696
diff changeset
699 std::string canonicalize_file_name (const std::string& name, std::string& msg)
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
700 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
701 msg = "";
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
702
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
703 std::string retval;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
704
28471
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
705 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
706 // On Windows, convert to locale charset before passing to
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
707 // canonicalize_file_name, and convert back to UTF-8 after that.
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
708
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
709 // FIXME: This only allows non-ASCII characters in the file or path that
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
710 // can be encoded in the locale charset.
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
711 // Consider replacing this with std::filesystem::canonical once we allow
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
712 // using C++17.
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
713
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
714 const char *locale = octave_locale_charset_wrapper ();
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
715 const uint8_t *name_u8 = reinterpret_cast<const uint8_t *>
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
716 (name.c_str ());
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
717 size_t length = 0;
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
718 char *name_locale = octave_u8_conv_to_encoding (locale, name_u8,
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
719 name.length () + 1,
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
720 &length);
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
721
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
722 if (name_locale)
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
723 {
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
724 char *tmp_locale =
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
725 octave_canonicalize_file_name_wrapper (name_locale);
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
726 free (name_locale);
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
727
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
728 if (tmp_locale)
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
729 {
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
730 char *tmp = reinterpret_cast<char *>
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
731 (octave_u8_conv_from_encoding (locale, tmp_locale,
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
732 strlen (tmp_locale),
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
733 &length));
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
734 free (tmp_locale);
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
735
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
736 if (tmp)
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
737 {
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
738 retval = std::string (tmp, length);
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
739 free (tmp);
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
740 }
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
741 }
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
742 }
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
743 #else
21888
00f1249f2483 create wrappers for some gnulib functions
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
744 char *tmp = octave_canonicalize_file_name_wrapper (name.c_str ());
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
745
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
746 if (tmp)
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
747 {
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
748 retval = tmp;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
749 free (tmp);
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
750 }
28471
a6c89130cfaa Improve canonicalize_file_name with non-ASCII characters on Windows.
Markus Mützel <markus.muetzel@gmx.de>
parents: 28204
diff changeset
751 #endif
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
752
20503
0829b6ff3ac9 Use backslash as windows file separator for canonicalize_file_name (Bug #45816).
Rik <rik@octave.org>
parents: 19697
diff changeset
753 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
754 // Canonical Windows file separator is backslash.
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
755 std::replace (retval.begin (), retval.end (), '/', '\\');
20503
0829b6ff3ac9 Use backslash as windows file separator for canonicalize_file_name (Bug #45816).
Rik <rik@octave.org>
parents: 19697
diff changeset
756 #endif
0829b6ff3ac9 Use backslash as windows file separator for canonicalize_file_name (Bug #45816).
Rik <rik@octave.org>
parents: 19697
diff changeset
757
27496
def608acdfa9 Canonicalize case of file path on Windows file systems (bug #56267).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27102
diff changeset
758 #if defined (OCTAVE_USE_WINDOWS_API)
28051
087a21522aa3 Support canonicalize_file_name on UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27923
diff changeset
759 std::wstring w_tmp;
087a21522aa3 Support canonicalize_file_name on UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27923
diff changeset
760 bool strip_marker = true;
087a21522aa3 Support canonicalize_file_name on UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27923
diff changeset
761 if (retval.empty ())
087a21522aa3 Support canonicalize_file_name on UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27923
diff changeset
762 {
087a21522aa3 Support canonicalize_file_name on UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27923
diff changeset
763 // For UNC paths, take the input as is.
28191
6ea1e96b69d4 canonicalize_file_name: Fix for non-existent file on Windows (bug #58118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28175
diff changeset
764 // Also translate forward slashes to backslashes.
6ea1e96b69d4 canonicalize_file_name: Fix for non-existent file on Windows (bug #58118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28175
diff changeset
765 std::string name_backsl = name;
6ea1e96b69d4 canonicalize_file_name: Fix for non-existent file on Windows (bug #58118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28175
diff changeset
766 std::replace (name_backsl.begin (), name_backsl.end (), '/', '\\');
6ea1e96b69d4 canonicalize_file_name: Fix for non-existent file on Windows (bug #58118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28175
diff changeset
767 if (name_backsl.compare (0, 2, "\\\\") == 0)
28051
087a21522aa3 Support canonicalize_file_name on UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27923
diff changeset
768 {
28191
6ea1e96b69d4 canonicalize_file_name: Fix for non-existent file on Windows (bug #58118).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28175
diff changeset
769 w_tmp = u8_to_wstring (name_backsl);
28051
087a21522aa3 Support canonicalize_file_name on UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27923
diff changeset
770 strip_marker = false;
28053
b70b9eaaf751 Actually canonicalize UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28051
diff changeset
771 wchar_t canon_path[MAX_PATH];
b70b9eaaf751 Actually canonicalize UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28051
diff changeset
772 if (PathCanonicalizeW (canon_path, w_tmp.c_str ()))
b70b9eaaf751 Actually canonicalize UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28051
diff changeset
773 w_tmp = std::wstring (canon_path);
28051
087a21522aa3 Support canonicalize_file_name on UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27923
diff changeset
774 }
087a21522aa3 Support canonicalize_file_name on UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27923
diff changeset
775 }
087a21522aa3 Support canonicalize_file_name on UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27923
diff changeset
776 else
087a21522aa3 Support canonicalize_file_name on UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27923
diff changeset
777 w_tmp = L"\\\\?\\" + u8_to_wstring (retval);
087a21522aa3 Support canonicalize_file_name on UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27923
diff changeset
778
087a21522aa3 Support canonicalize_file_name on UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27923
diff changeset
779 if (! w_tmp.empty ())
087a21522aa3 Support canonicalize_file_name on UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27923
diff changeset
780 {
087a21522aa3 Support canonicalize_file_name on UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27923
diff changeset
781 // Get a more canonical name wrt case and full names
28204
9a1b6400c706 Canonicalize case of long parts of path on Windows file systems (bug #58148).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28191
diff changeset
782 // FIXME: To make this work on partitions that don't store short file
9a1b6400c706 Canonicalize case of long parts of path on Windows file systems (bug #58148).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28191
diff changeset
783 // names, use FindFirstFileW on each component of the path.
9a1b6400c706 Canonicalize case of long parts of path on Windows file systems (bug #58148).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28191
diff changeset
784 // Insufficient access permissions on parent folders might make this
9a1b6400c706 Canonicalize case of long parts of path on Windows file systems (bug #58148).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28191
diff changeset
785 // tricky.
9a1b6400c706 Canonicalize case of long parts of path on Windows file systems (bug #58148).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28191
diff changeset
786
9a1b6400c706 Canonicalize case of long parts of path on Windows file systems (bug #58148).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28191
diff changeset
787 // Parts of the path that wouldn't fit into a short 8.3 file name are
9a1b6400c706 Canonicalize case of long parts of path on Windows file systems (bug #58148).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28191
diff changeset
788 // copied as is by GetLongPathNameW. To also get the correct case
9a1b6400c706 Canonicalize case of long parts of path on Windows file systems (bug #58148).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28191
diff changeset
789 // for these parts, first convert to short file names and than back
9a1b6400c706 Canonicalize case of long parts of path on Windows file systems (bug #58148).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28191
diff changeset
790 // to long.
9a1b6400c706 Canonicalize case of long parts of path on Windows file systems (bug #58148).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28191
diff changeset
791 wchar_t buffer[32767] = L"";
9a1b6400c706 Canonicalize case of long parts of path on Windows file systems (bug #58148).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28191
diff changeset
792 int w_len = GetShortPathNameW (w_tmp.c_str (), buffer, 32767);
9a1b6400c706 Canonicalize case of long parts of path on Windows file systems (bug #58148).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28191
diff changeset
793 w_len = GetLongPathNameW (buffer, buffer, 32767);
28051
087a21522aa3 Support canonicalize_file_name on UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27923
diff changeset
794
087a21522aa3 Support canonicalize_file_name on UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27923
diff changeset
795 if (! strip_marker)
28204
9a1b6400c706 Canonicalize case of long parts of path on Windows file systems (bug #58148).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28191
diff changeset
796 retval = u8_from_wstring (std::wstring (buffer, w_len));
28051
087a21522aa3 Support canonicalize_file_name on UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27923
diff changeset
797 else if (w_len > 4)
28204
9a1b6400c706 Canonicalize case of long parts of path on Windows file systems (bug #58148).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28191
diff changeset
798 retval = u8_from_wstring (std::wstring (buffer+4, w_len-4));
28051
087a21522aa3 Support canonicalize_file_name on UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27923
diff changeset
799
28204
9a1b6400c706 Canonicalize case of long parts of path on Windows file systems (bug #58148).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28191
diff changeset
800 // If root is a drive, use an upper case letter for the drive letter.
28051
087a21522aa3 Support canonicalize_file_name on UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27923
diff changeset
801 if (retval.length () > 1 && retval[1] == ':')
087a21522aa3 Support canonicalize_file_name on UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27923
diff changeset
802 retval[0] = toupper (retval[0]);
087a21522aa3 Support canonicalize_file_name on UNC paths (bug #57439).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27923
diff changeset
803 }
27496
def608acdfa9 Canonicalize case of file path on Windows file systems (bug #56267).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27102
diff changeset
804 #endif
def608acdfa9 Canonicalize case of file path on Windows file systems (bug #56267).
Markus Mützel <markus.muetzel@gmx.de>
parents: 27102
diff changeset
805
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
806 if (retval.empty ())
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21910
diff changeset
807 msg = std::strerror (errno);
10252
2fcc927a8757 liboctave/file-ops.h: avoid gnulib #define for canonicalize_file_name
John W. Eaton <jwe@octave.org>
parents: 10250
diff changeset
808
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
809 return retval;
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
810 }
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21732
diff changeset
811 }
10252
2fcc927a8757 liboctave/file-ops.h: avoid gnulib #define for canonicalize_file_name
John W. Eaton <jwe@octave.org>
parents: 10250
diff changeset
812 }