Mercurial > octave
annotate scripts/miscellaneous/movefile.m @ 30241:ba4aebad10d1
Return logical status variable from file functions for Matlab compatibility.
* NEWS: Announce change.
* dirfns.cc (Frmdir): Update documentation. Change return status to true/false
rather than 1.0/0.0.
* copyfile.m: Update documentation. Change internal variable "sts" to logical
variable. Change any assignments to status to true/false.
* movefile.m: Update documentation. Change internal variable "sts" to logical
variable. Change any assignments to status to true/false.
* mkdir.m: Update documentation.
* mkdir.m (mkdir_recur): Change internal variable "status" to logical.
author | Rik <rik@octave.org> |
---|---|
date | Wed, 13 Oct 2021 10:36:20 -0700 |
parents | 7854d5752dd2 |
children | 796f54d4ddbf |
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:
27923
diff
changeset
|
3 ## Copyright (C) 2005-2021 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27263
diff
changeset
|
4 ## |
27923
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/>. |
6047 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
6047 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## (at your option) any later version. |
6047 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
6047 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
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 ######################################################################## |
6047 | 25 |
26 ## -*- texinfo -*- | |
25040
328518881291
Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
27 ## @deftypefn {} {} movefile @var{f1} |
328518881291
Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
28 ## @deftypefnx {} {} movefile @var{f1} @var{f2} |
328518881291
Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
29 ## @deftypefnx {} {} movefile @var{f1} @var{f2} f |
328518881291
Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
30 ## @deftypefnx {} {} movefile (@var{f1}) |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20713
diff
changeset
|
31 ## @deftypefnx {} {} movefile (@var{f1}, @var{f2}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20713
diff
changeset
|
32 ## @deftypefnx {} {} movefile (@var{f1}, @var{f2}, 'f') |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20713
diff
changeset
|
33 ## @deftypefnx {} {[@var{status}, @var{msg}, @var{msgid}] =} movefile (@dots{}) |
25040
328518881291
Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
34 ## Move the source file or directory @var{f1} to the destination @var{f2}. |
6047 | 35 ## |
25040
328518881291
Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
36 ## The name @var{f1} may contain globbing patterns, or may be a cell array of |
328518881291
Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
37 ## strings. If @var{f1} expands to multiple filenames, @var{f2} must be a |
328518881291
Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
38 ## directory. |
328518881291
Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
39 ## |
328518881291
Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
40 ## If no destination @var{f2} is specified then the destination is the present |
328518881291
Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
41 ## working directory. If @var{f2} is a filename then @var{f1} is renamed to |
328518881291
Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
42 ## @var{f2}. |
19261 | 43 ## |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
44 ## When the force flag @qcode{'f'} is given any existing files will be |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
45 ## overwritten without prompting. |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
46 ## |
30241
ba4aebad10d1
Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
47 ## If successful, @var{status} is logical 1, and @var{msg}, @var{msgid} are |
ba4aebad10d1
Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
48 ## empty character strings (""). Otherwise, @var{status} is logical 0, |
ba4aebad10d1
Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
49 ## @var{msg} contains a system-dependent error message, and @var{msgid} |
ba4aebad10d1
Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
50 ## contains a unique message identifier. Note that the status code is exactly |
ba4aebad10d1
Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
51 ## opposite that of the @code{system} command. |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17394
diff
changeset
|
52 ## @seealso{rename, copyfile, unlink, delete, glob} |
6047 | 53 ## @end deftypefn |
54 | |
55 function [status, msg, msgid] = movefile (f1, f2, force) | |
56 | |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
28104
diff
changeset
|
57 if (nargin < 1) |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
58 print_usage (); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
59 endif |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
60 |
6679 | 61 max_cmd_line = 1024; |
30241
ba4aebad10d1
Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
62 sts = true; |
6047 | 63 msg = ""; |
64 msgid = ""; | |
65 | |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
66 ## FIXME: maybe use the same method as in ls to allow users control |
19261 | 67 ## over the command that is executed. |
6210 | 68 |
11300
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
69 if (ispc () && ! isunix () |
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
70 && isempty (file_in_path (getenv ("PATH"), "mv.exe"))) |
6233 | 71 ## Windows. |
6210 | 72 cmd = "cmd /C move"; |
73 cmd_force_flag = "/Y"; | |
74 else | |
75 cmd = "mv"; | |
76 cmd_force_flag = "-f"; | |
77 endif | |
78 | |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
79 ## Input type check. |
19261 | 80 if (ischar (f1)) |
81 f1 = cellstr (f1); | |
82 elseif (! iscellstr (f1)) | |
83 error ("copyfile: F1 must be a string or a cell array of strings"); | |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
84 endif |
6233 | 85 |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
86 if (nargin == 1) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
87 f2 = pwd (); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
88 elseif (! ischar (f2)) |
19261 | 89 error ("movefile: F2 must be a string"); |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
90 endif |
6069 | 91 |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
92 if (nargin == 3 && strcmp (force, "f")) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
93 cmd = [cmd " " cmd_force_flag]; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
94 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
95 |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
96 ## If f1 has more than 1 element f2 must be a directory |
25781
e04c56bbbace
isdir.m: Make m-file a legacy function (bug #54489)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
97 isdir = isfolder (f2); |
19261 | 98 if (numel (f1) > 1 && ! isdir) |
28104
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
99 if (nargout == 0) |
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
100 error ("movefile: when copying multiple files, F2 must be a directory"); |
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
101 else |
30241
ba4aebad10d1
Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
102 status = false; |
28104
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
103 msg = "when copying multiple files, F2 must be a directory"; |
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
104 msgid = "movefile"; |
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
105 return; |
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
106 endif |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
107 endif |
6069 | 108 |
20713
2469d78a1d8b
Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
109 ## Protect the filename(s). |
29093
1c212b36a35e
Use "__wglob__" instead of "glob" on Windows (bug #59287).
Markus Mützel <markus.muetzel@gmx.de>
parents:
28896
diff
changeset
|
110 if (ispc ()) |
1c212b36a35e
Use "__wglob__" instead of "glob" on Windows (bug #59287).
Markus Mützel <markus.muetzel@gmx.de>
parents:
28896
diff
changeset
|
111 f1 = __wglob__ (f1); |
1c212b36a35e
Use "__wglob__" instead of "glob" on Windows (bug #59287).
Markus Mützel <markus.muetzel@gmx.de>
parents:
28896
diff
changeset
|
112 else |
1c212b36a35e
Use "__wglob__" instead of "glob" on Windows (bug #59287).
Markus Mützel <markus.muetzel@gmx.de>
parents:
28896
diff
changeset
|
113 f1 = glob (f1); |
1c212b36a35e
Use "__wglob__" instead of "glob" on Windows (bug #59287).
Markus Mützel <markus.muetzel@gmx.de>
parents:
28896
diff
changeset
|
114 endif |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
115 if (isempty (f1)) |
28104
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
116 if (nargout == 0) |
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
117 error ("movefile: no files to move"); |
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
118 else |
30241
ba4aebad10d1
Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
119 status = false; |
28104
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
120 msg = "no files to move"; |
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
121 msgid = "movefile"; |
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
122 return; |
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
123 endif |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
124 endif |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
125 p1 = sprintf ('"%s" ', f1{:}); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
126 p2 = tilde_expand (f2); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
127 |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
128 if (isdir && length (p1) > max_cmd_line) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
129 l2 = length (p2) + length (cmd) + 6; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
130 while (! isempty (f1)) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
131 p1 = sprintf ('"%s" ', f1{1}); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
132 f1(1) = []; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
133 while (! isempty (f1) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
134 && (length (p1) + length (f1{1}) + l2 < max_cmd_line)) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
135 p1 = sprintf ('%s"%s" ', p1, f1{1}); |
10549 | 136 f1(1) = []; |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
137 endwhile |
6679 | 138 |
11300
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
139 if (ispc () && ! isunix () |
12497
1536ed546219
Fix bug #32443 preventing 'pkg install' on Windows platforms.
Rik <octave@nomad.inbox5.com>
parents:
12211
diff
changeset
|
140 && ! isempty (file_in_path (getenv ("PATH"), "cp.exe"))) |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
141 p1 = strrep (p1, '\', '/'); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
142 p2 = strrep (p2, '\', '/'); |
6679 | 143 endif |
144 | |
25790
872111558bc0
reload editor files when their path changes by using movefile (bug #43922)
Torsten <mttl@mailbox.org>
parents:
25781
diff
changeset
|
145 ## Close old file(s) in editor |
27263
99aa1bcb8848
rename octave_link and octave_link_events classes, move inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
146 __event_manager_file_remove__ (p1, p2); |
6679 | 147 ## Move the file(s). |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
148 [err, msg] = system (sprintf ('%s %s "%s"', cmd, p1, p2)); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
149 if (err != 0) |
30241
ba4aebad10d1
Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
150 sts = false; |
10549 | 151 msgid = "movefile"; |
6679 | 152 endif |
25790
872111558bc0
reload editor files when their path changes by using movefile (bug #43922)
Torsten <mttl@mailbox.org>
parents:
25781
diff
changeset
|
153 ## Load new file(s) in editor |
28104
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
154 __event_manager_file_renamed__ (sts); |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
155 endwhile |
6047 | 156 else |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
157 if (ispc () && ! isunix () |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
158 && ! isempty (file_in_path (getenv ("PATH"), "cp.exe"))) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
159 p1 = strrep (p1, '\', '/'); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
160 p2 = strrep (p2, '\', '/'); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
161 endif |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
162 |
25790
872111558bc0
reload editor files when their path changes by using movefile (bug #43922)
Torsten <mttl@mailbox.org>
parents:
25781
diff
changeset
|
163 ## Close old file(s) in editor |
27263
99aa1bcb8848
rename octave_link and octave_link_events classes, move inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
164 __event_manager_file_remove__ (p1, p2); |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
165 ## Move the file(s). |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
166 [err, msg] = system (sprintf ('%s %s "%s"', cmd, p1, p2)); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
167 if (err != 0) |
30241
ba4aebad10d1
Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
168 sts = false; |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
169 msgid = "movefile"; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
170 endif |
25790
872111558bc0
reload editor files when their path changes by using movefile (bug #43922)
Torsten <mttl@mailbox.org>
parents:
25781
diff
changeset
|
171 ## Load new file(s) in editor |
28104
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
172 __event_manager_file_renamed__ (sts); |
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
173 endif |
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
174 |
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
175 if (nargout == 0) |
30241
ba4aebad10d1
Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
176 if (! sts) |
28104
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
177 error ("movefile: operation failed: %s", msg); |
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
178 endif |
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
179 else |
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
180 status = sts; |
6047 | 181 endif |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
182 |
6047 | 183 endfunction |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
184 |
19261 | 185 |
186 %!test | |
187 %! unwind_protect | |
28892
3cac3ceb9629
maint: Use coding style with parentheses after function name to distinguish from a variable.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
188 %! f1 = tempname (); |
19261 | 189 %! tmp_var = pi; |
190 %! save (f1, "tmp_var"); | |
191 %! fid = fopen (f1, "rb"); | |
192 %! assert (fid >= 0); | |
193 %! orig_data = fread (fid); | |
194 %! fclose (fid); | |
28892
3cac3ceb9629
maint: Use coding style with parentheses after function name to distinguish from a variable.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
195 %! f2 = tempname (); |
19261 | 196 %! assert (movefile (f1, f2)); |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19261
diff
changeset
|
197 %! assert (! exist (f1, "file")); |
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19261
diff
changeset
|
198 %! assert (exist (f2, "file")); |
19261 | 199 %! fid = fopen (f2, "rb"); |
200 %! assert (fid >= 0); | |
201 %! new_data = fread (fid); | |
202 %! fclose (fid); | |
203 %! if (orig_data != new_data) | |
204 %! error ("moved file not equal to original file!"); | |
205 %! endif | |
206 %! unwind_protect_cleanup | |
207 %! delete (f2); | |
208 %! end_unwind_protect | |
209 | |
210 ## Test input validation | |
28896
90fea9cc9caa
test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents:
28892
diff
changeset
|
211 %!error <Invalid call> movefile () |
19261 | 212 %!error <F1 must be a string> movefile (1, "foobar") |
213 %!error <F2 must be a string> movefile ("foobar", 1) | |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19261
diff
changeset
|
214 %!error <F2 must be a directory> movefile ({"a", "b"}, "%_NOT_A_DIR_%") |
28104
b2e0a2ddfd7d
movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
215 %!error <no files to move> movefile ("%_NOT_A_FILENAME1_%", "%_NOT_A_FILENAME2_%") |