Mercurial > octave-antonio
annotate scripts/miscellaneous/unpack.m @ 19234:6ebc2ca01c76
unpack.m: Overhaul function.
* unpack.m: Redo docstring. Match function variable name to documentation
name. Correctly report error if file is not found and input is cellstr.
Reword error() messages to be clearer. Allow case insensitive matching of
extensions such as ".gz" or ".GZ". Fix huge problems with files not ending
up in destination directory specified. Eliminate for loop and call strrep
directly with cell array input. Add BIST tests.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 07 Oct 2014 13:13:16 -0700 |
parents | 0ded8964c13a |
children | cac9d4c49522 |
rev | line source |
---|---|
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
17000
diff
changeset
|
1 ## Copyright (C) 2006-2013 Bill Denney |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
2 ## |
6082 | 3 ## This file is part of Octave. |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
6082 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
6082 | 18 |
19 ## -*- texinfo -*- | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
20 ## @deftypefn {Function File} {@var{files} =} unpack (@var{file}) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
21 ## @deftypefnx {Function File} {@var{files} =} unpack (@var{file}, @var{dir}) |
6082 | 22 ## @deftypefnx {Function File} {@var{files} =} unpack (@var{file}, @var{dir}, @var{filetype}) |
23 ## Unpack the archive @var{file} based on its extension to the directory | |
19234 | 24 ## @var{dir}. |
25 ## | |
26 ## If @var{file} is a list of strings, then each file is unpacked | |
27 ## individually. Shell wildcards in the filename such as @samp{*} or @samp{?} | |
28 ## are accepted and expanded. | |
29 ## | |
30 ## If @var{dir} is not specified or is empty (@code{[]}), it defaults to the | |
31 ## current directory. If a directory is in the file list, then @var{filetype} | |
32 ## must also be specified. | |
33 ## | |
34 ## The specific archive filetype is inferred from the extension of the file. | |
35 ## The @var{filetype} may also be specified directly using a string which | |
36 ## corresponds to a known extension. | |
37 ## | |
38 ## Valid filetype extensions: | |
39 ## | |
40 ## @table @code | |
41 ## @item bz | |
42 ## @itemx bz2 | |
43 ## bzip archive | |
44 ## | |
45 ## @item gz | |
46 ## gzip archive | |
47 ## | |
48 ## @item tar | |
49 ## tar archive | |
50 ## | |
51 ## @item tarbz | |
52 ## @itemxtarbz2 | |
53 ## @itemx tbz | |
54 ## @itemx tbz2 | |
55 ## tar + bzip archive | |
56 ## | |
57 ## @item targz | |
58 ## @itemx tgz | |
59 ## tar + gzip archive | |
60 ## | |
61 ## @item z | |
62 ## compress archive | |
63 ## | |
64 ## @item zip | |
65 ## zip archive | |
66 ## @end table | |
6082 | 67 ## |
12642
f96b9b9f141b
doc: Periodic grammarcheck and spellcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents:
12500
diff
changeset
|
68 ## The optional return value is a list of @var{files} unpacked. |
12500
8f2056646dba
Improve docstrings for archive functions (gzip, bzip2, etc.)
Rik <octave@nomad.inbox5.com>
parents:
12499
diff
changeset
|
69 ## @seealso{bzip2, gzip, zip, tar} |
6082 | 70 ## @end deftypefn |
71 | |
72 ## Author: Bill Denney <denney@seas.upenn.edu> | |
73 | |
19234 | 74 function filelist = unpack (file, dir = ".", filetype = "") |
6082 | 75 |
76 if (nargin < 1 || nargin > 3) | |
77 print_usage (); | |
78 endif | |
79 | |
19234 | 80 if (! ischar (file) && ! iscellstr (file)) |
81 error ("unpack: FILE must be a string or cell array of strings"); | |
12499
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
82 endif |
6082 | 83 |
19234 | 84 ## Convert char arrays to cell strings to simplify further processing |
85 if (ischar (file)) | |
86 file = cellstr (file); | |
87 endif | |
88 if (numel (file) == 1) | |
89 gfile = glob (file); | |
90 if (isempty (gfile)) | |
91 error ('unpack: file "%s" not found', file{1}); | |
92 else | |
93 file = gfile; | |
94 endif | |
12499
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
95 endif |
6082 | 96 |
12499
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
97 ## Recursively unpack cellstr arrays one file at a time |
19234 | 98 if (numel (file) > 1) |
6082 | 99 files = {}; |
100 for i = 1:numel (file) | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
101 tmpfiles = unpack (file{i}, dir); |
6082 | 102 files = {files{:} tmpfiles{:}}; |
103 endfor | |
104 | |
8352
33337f1aca75
fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8297
diff
changeset
|
105 ## Return output if requested. |
33337f1aca75
fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8297
diff
changeset
|
106 if (nargout > 0) |
33337f1aca75
fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8297
diff
changeset
|
107 filelist = files; |
33337f1aca75
fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8297
diff
changeset
|
108 endif |
19234 | 109 return; |
8352
33337f1aca75
fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8297
diff
changeset
|
110 |
19233
0ded8964c13a
Added wildcard * support to archive-related routines (bug #43211).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
18857
diff
changeset
|
111 else |
19234 | 112 file = file{1}; |
12499
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
113 endif |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
114 |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
115 if (isdir (file)) |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
116 if (isempty (filetype)) |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
117 error ("unpack: FILETYPE must be given for a directory"); |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
118 elseif (! any (strcmpi (filetype, "gunzip"))) |
19234 | 119 error ('unpack: FILETYPE must be "gunzip" for a directory'); |
12499
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
120 endif |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
121 ext = ".gz"; |
6082 | 122 else |
12499
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
123 [pathstr, name, ext] = fileparts (file); |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
124 |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
125 ## Check to see if it's .tar.gz, .tar.Z, etc. |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
126 if (any (strcmpi ({".gz" ".Z" ".bz2" ".bz"}, ext))) |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
127 [~, tmpname, tmpext] = fileparts (name); |
12499
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
128 if (strcmpi (tmpext, ".tar")) |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
129 name = tmpname; |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16724
diff
changeset
|
130 ext = [tmpext ext]; |
12499
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
131 endif |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
132 endif |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
133 |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
134 ## If the file is a URL, download it and then work with that file. |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
135 if (! isempty (strfind (file, "://"))) |
19234 | 136 ## FIXME: The above code is not a perfect test for a URL |
12499
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
137 urlfile = file; |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16724
diff
changeset
|
138 tmpfile = [tmpnam() ext]; |
12499
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
139 [file, success, msg] = urlwrite (urlfile, tmpfile); |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
140 if (! success) |
19234 | 141 error ('unpack: could not fetch "%s": %s', urlfile, msg); |
12499
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
142 endif |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
143 endif |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
144 |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
145 endif |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
146 |
19234 | 147 file = canonicalize_file_name (file); |
6082 | 148 |
149 ## Instructions on what to do for any extension. | |
150 ## | |
151 ## The field names are the file extension without periods. | |
152 ## The first cell is what is executed to unpack an archive verbosely. | |
153 ## The second cell is what is executed to unpack an archive quietly. | |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
154 ## The third cell is the function to execute on output to get the files list. |
6082 | 155 ## The fourth cell indicates if the files may need to be manually moved |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
156 ## (i.e., tar and unzip decompress into the current directory while |
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
157 ## bzip2 and gzip decompress the file at its location). |
6082 | 158 persistent commandlist; |
159 if (isempty (commandlist)) | |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
160 commandlist.gz = {'gzip -d -v -r "%s"', ... |
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
161 'gzip -d -r "%s"', ... |
10549 | 162 @__parse_gzip__, true}; |
6082 | 163 commandlist.z = commandlist.gz; |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
164 commandlist.bz2 = {'bzip2 -d -v "%s"', ... |
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
165 'bzip2 -d "%s"', ... |
10549 | 166 @__parse_bzip2__, true}; |
6082 | 167 commandlist.bz = commandlist.bz2; |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
168 commandlist.tar = {'tar xvf "%s"', ... |
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
169 'tar xf "%s"', ... |
10549 | 170 @__parse_tar__, false}; |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
171 commandlist.targz = {'gzip -d -c "%s" | tar xvf -', ... |
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
172 'gzip -d -c "%s" | tar xf -', ... |
10549 | 173 @__parse_tar__, false}; |
6082 | 174 commandlist.tgz = commandlist.targz; |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
175 commandlist.tarbz2 = {'bzip2 -d -c "%s" | tar xvf -', ... |
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
176 'bzip2 -d -c "%s" | tar xf -', ... |
10549 | 177 @__parse_tar__, false}; |
6082 | 178 commandlist.tarbz = commandlist.tarbz2; |
179 commandlist.tbz2 = commandlist.tarbz2; | |
180 commandlist.tbz = commandlist.tarbz2; | |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
181 commandlist.zip = {'unzip -n "%s"', ... |
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
182 'unzip -nq "%s"', ... |
10549 | 183 @__parse_zip__, false}; |
6082 | 184 endif |
185 | |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
186 ## Unzip doesn't actually care about the extension |
19234 | 187 if (strcmpi (filetype, "unzip")) |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
188 nodotext = "zip"; |
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
189 else |
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
190 nodotext = ext(ext != '.'); |
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
191 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
192 |
19234 | 193 if (isfield (commandlist, tolower (nodotext))) |
194 [commandv, commandq, parsefcn, move] = deal (commandlist.(nodotext){:}); | |
195 origdir = pwd (); | |
196 if (move) | |
197 startdir = fileparts (file); | |
198 else | |
199 startdir = origdir; | |
200 endif | |
201 cstartdir = canonicalize_file_name (startdir); | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
202 cenddir = canonicalize_file_name (dir); |
6082 | 203 needmove = move && ! strcmp (cstartdir, cenddir); |
204 if (nargout > 0 || needmove) | |
205 command = commandv; | |
206 else | |
207 command = commandq; | |
208 endif | |
209 else | |
19234 | 210 warning ("unpack: unrecognized FILETYPE <%s>", ext); |
6082 | 211 files = file; |
212 return; | |
213 endif | |
214 | |
215 ## Create the directory if necessary. | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
216 s = stat (dir); |
6082 | 217 if (isempty (s)) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
218 [status, msg] = mkdir (dir); |
6082 | 219 if (! status) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
220 error ("unpack: mkdir failed to create %s: %s", dir, msg); |
6082 | 221 endif |
222 elseif (! S_ISDIR (s.mode)) | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
223 error ("unpack: %s: not a directory", dir); |
6082 | 224 endif |
225 | |
226 unwind_protect | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
227 cd (dir); |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16724
diff
changeset
|
228 [status, output] = system (sprintf ([command " 2>&1"], file)); |
6082 | 229 unwind_protect_cleanup |
230 cd (origdir); | |
231 end_unwind_protect | |
232 | |
233 if (status) | |
234 error ("unpack: unarchiving program exited with status: %d\n%s", | |
10549 | 235 status, output); |
6082 | 236 endif |
237 | |
6084 | 238 if (nargout > 0 || needmove) |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
239 ## Trim the last CR if needed. |
18857
7bbe3658c5ef
maint: Use "FIXME:" coding convention in m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
240 ## FIXME: Will this need to change to a check for "\r\n" for windows? |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
241 if (output(end) == "\n") |
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
242 output(end) = []; |
6082 | 243 endif |
19234 | 244 files = parsefcn (ostrsplit (output, "\n"))'; |
6082 | 245 |
19234 | 246 ## Move files if necessary. |
6082 | 247 if (needmove) |
19234 | 248 [st, msg] = movefile (files, cenddir); |
6082 | 249 if (! st) |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
250 error ('unpack: unable to move files to "%s": %s', dir, msg); |
6082 | 251 endif |
252 | |
19234 | 253 ## Fix the names of the files since they were moved. |
254 files = strrep (files, cstartdir, cenddir); | |
6082 | 255 endif |
256 | |
257 ## Return output if requested. | |
258 if (nargout > 0) | |
259 filelist = files; | |
260 endif | |
261 endif | |
262 | |
263 endfunction | |
264 | |
265 function files = __parse_zip__ (output) | |
266 ## Parse the output from zip and unzip. | |
267 | |
19234 | 268 ## Skip first line which is Archive header. |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
269 files = char (output(2:end)); |
19234 | 270 ## Trim constant width prefix and return cell array. |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
271 files = cellstr (files(:,14:end)) |
6082 | 272 endfunction |
273 | |
274 function output = __parse_tar__ (output) | |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
275 ## This is a no-op, but it makes things simpler for other cases. |
6082 | 276 endfunction |
277 | |
278 function files = __parse_gzip__ (output) | |
279 ## Parse the output from gzip and gunzip returning the files | |
280 ## commpressed (or decompressed). | |
281 | |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
282 files = regexprep (output, '^.+ with (.*)$', '$1'); |
6082 | 283 endfunction |
284 | |
285 function files = __parse_bzip2__ (output) | |
286 ## Parse the output from bzip2 and bunzip2 returning the files | |
287 ## commpressed (or decompressed). | |
288 | |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
289 ## Strip leading blanks and .bz2 extension from file name |
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
290 files = regexprep (output, '^\s+(.*)\.bz2: .*', '$1'); |
19234 | 291 endfunction |
292 | |
293 | |
294 ## FIXME: Maybe there should be a combined test script to test all of the | |
295 ## various pack/unpack routines in the tests/ directory. | |
296 %!test | |
297 %! ## Create temporary file for packing and unpacking | |
298 %! fname = tempname (); | |
299 %! fid = fopen (fname, "wt"); | |
300 %! if (fid < 0) | |
301 %! error ("unpack: Unable to open temporary file for testing"); | |
302 %! endif | |
303 %! fprintf (fid, "Hello World\n"); | |
304 %! fprintf (fid, "123 456 789\n"); | |
305 %! fclose (fid); | |
306 %! | |
307 %! unwind_protect | |
308 %! copyfile (fname, [fname ".orig"]); | |
309 %! gzip (fname, P_tmpdir); | |
310 %! filelist = unpack ([fname ".gz"], P_tmpdir); | |
311 %! assert (filelist{1}, fname); | |
312 %! r = system (sprintf ("diff %s %s.orig", fname, fname)); | |
313 %! if (r) | |
314 %! error ("unpack: Unpacked file does not equal original"); | |
315 %! endif | |
316 %! unwind_protect_cleanup | |
317 %! exist (fname) && unlink (fname); | |
318 %! unlink ([fname ".orig"]); | |
319 %! end_unwind_protect | |
320 | |
321 ## Test input validation | |
322 %!error unpack () | |
323 %!error unpack (1,2,3,4) | |
324 %!error <FILE must be a string or cell array of strings> unpack (1) | |
325 %!error <file "_%NOT_A_FILENAME%_" not found> unpack ("_%NOT_A_FILENAME%_") | |
326 %!error <file "_%NOT_A_FILENAME%_" not found> unpack ({"_%NOT_A_FILENAME%_"}) | |
327 %!error <file "_%NOT_A_FILENAME%_" not found> unpack ({"_%NOT_A_FILENAME%_", "2nd_filename"}) | |
328 %!error <FILETYPE must be given for a directory> | |
329 %! if (isunix || ismac) | |
330 %! unpack ("/"); | |
331 %! else | |
332 %! unpack ('C:\'); | |
333 %! endif | |
334 %!error <FILETYPE must be "gunzip" for a directory> | |
335 %! if (isunix || ismac) | |
336 %! unpack ("/", [], "foobar"); | |
337 %! else | |
338 %! unpack ('C:\', [], "foobar"); | |
339 %! endif | |
340 |