Mercurial > octave
annotate scripts/miscellaneous/movefile.m @ 27923:bd51beb6205e
update formatting of copyright notices
* Use <https://octave.org/copyright/> instead of
<https://octave.org/COPYRIGHT.html/>.
* For consistency with other comments in the Octave sources, use
C++-style comments for copyright blocks in C and C++ files.
* Use delimiters above and below copyright blocks that are appropriate
for the language used in the file.
* Eliminate extra spacing inside copyright blocks.
* lex.ll (looks_like_copyright): Also allow newlines and carriage
returns before the word "Copyright".
* scripts/mk-doc.pl (gethelp): Also skip empty comment lines.
* bp-table.cc, type.m: Adjust tests.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 08 Jan 2020 11:59:41 -0500 |
parents | 1891570abac8 |
children | b2e0a2ddfd7d 0a5b15007766 |
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 ## |
27919
1891570abac8
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27918
diff
changeset
|
3 ## Copyright (C) 2005-2020 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 ## |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
47 ## If successful, @var{status} is 1, and @var{msg}, @var{msgid} are empty |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
48 ## character strings (""). Otherwise, @var{status} is 0, @var{msg} contains a |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
49 ## system-dependent error message, and @var{msgid} contains a unique message |
17514
5b916efea542
doc: spellcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents:
17397
diff
changeset
|
50 ## identifier. Note that the status code is exactly opposite that of the |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
51 ## @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 | |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
57 if (nargin < 1 || nargin > 3) |
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; |
6047 | 62 status = true; |
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) |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
99 error ("movefile: when moving multiple files, F2 must be a directory"); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
100 endif |
6069 | 101 |
20713
2469d78a1d8b
Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
102 ## Protect the filename(s). |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
103 f1 = glob (f1); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
104 if (isempty (f1)) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
105 error ("movefile: no files to move"); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
106 endif |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
107 p1 = sprintf ('"%s" ', f1{:}); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
108 p2 = tilde_expand (f2); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
109 |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
110 if (isdir && length (p1) > max_cmd_line) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
111 l2 = length (p2) + length (cmd) + 6; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
112 while (! isempty (f1)) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
113 p1 = sprintf ('"%s" ', f1{1}); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
114 f1(1) = []; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
115 while (! isempty (f1) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
116 && (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
|
117 p1 = sprintf ('%s"%s" ', p1, f1{1}); |
10549 | 118 f1(1) = []; |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
119 endwhile |
6679 | 120 |
11300
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
121 if (ispc () && ! isunix () |
12497
1536ed546219
Fix bug #32443 preventing 'pkg install' on Windows platforms.
Rik <octave@nomad.inbox5.com>
parents:
12211
diff
changeset
|
122 && ! 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
|
123 p1 = strrep (p1, '\', '/'); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
124 p2 = strrep (p2, '\', '/'); |
6679 | 125 endif |
126 | |
25790
872111558bc0
reload editor files when their path changes by using movefile (bug #43922)
Torsten <mttl@mailbox.org>
parents:
25781
diff
changeset
|
127 ## 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
|
128 __event_manager_file_remove__ (p1, p2); |
6679 | 129 ## Move the file(s). |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
130 [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
|
131 if (err != 0) |
10549 | 132 status = false; |
133 msgid = "movefile"; | |
6679 | 134 endif |
25790
872111558bc0
reload editor files when their path changes by using movefile (bug #43922)
Torsten <mttl@mailbox.org>
parents:
25781
diff
changeset
|
135 ## Load new 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
|
136 __event_manager_file_renamed__ (status); |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
137 endwhile |
6047 | 138 else |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
139 if (ispc () && ! isunix () |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
140 && ! isempty (file_in_path (getenv ("PATH"), "cp.exe"))) |
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, '\', '/'); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
143 endif |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
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); |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
147 ## Move the file(s). |
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) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
150 status = false; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
151 msgid = "movefile"; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
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 |
27263
99aa1bcb8848
rename octave_link and octave_link_events classes, move inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
154 __event_manager_file_renamed__ (status); |
6047 | 155 endif |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
156 |
6047 | 157 endfunction |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
158 |
19261 | 159 |
160 %!test | |
161 %! unwind_protect | |
162 %! f1 = tempname; | |
163 %! tmp_var = pi; | |
164 %! save (f1, "tmp_var"); | |
165 %! fid = fopen (f1, "rb"); | |
166 %! assert (fid >= 0); | |
167 %! orig_data = fread (fid); | |
168 %! fclose (fid); | |
169 %! f2 = tempname; | |
170 %! assert (movefile (f1, f2)); | |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19261
diff
changeset
|
171 %! assert (! exist (f1, "file")); |
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19261
diff
changeset
|
172 %! assert (exist (f2, "file")); |
19261 | 173 %! fid = fopen (f2, "rb"); |
174 %! assert (fid >= 0); | |
175 %! new_data = fread (fid); | |
176 %! fclose (fid); | |
177 %! if (orig_data != new_data) | |
178 %! error ("moved file not equal to original file!"); | |
179 %! endif | |
180 %! unwind_protect_cleanup | |
181 %! delete (f2); | |
182 %! end_unwind_protect | |
183 | |
184 ## Test input validation | |
185 %!error movefile () | |
186 %!error movefile (1,2,3,4) | |
187 %!error <F1 must be a string> movefile (1, "foobar") | |
188 %!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
|
189 %!error <F2 must be a directory> movefile ({"a", "b"}, "%_NOT_A_DIR_%") |