1 ## Copyright (C) 2008-2012 Thorsten Meyer
2 ## based on the original gzip function by David Bateman
4 ## This file is part of Octave.
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ## General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>.
21 ## @deftypefn {Function File} {@var{entries} =} __xzip__ (@var{commandname}, @var{extension}, @var{commandtemplate}, @var{files}, @var{outdir})
22 ## Undocumented internal function.
25 ## Compress the list of files and/or directories specified in @var{files}
26 ## with the external compression command @var{commandname}. The template
27 ## @var{commandtemplate} is used to actually start the command. Each file
28 ## is compressed separately and a new file with the extension @var{extension}
29 ## is created and placed into the directory @var{outdir}. The original files
30 ## are not touched. Existing compressed files are silently overwritten.
31 ## This is an internal function. Do not use directly.
33 function entries = __xzip__ (commandname, extension,
34 commandtemplate, files, outdir)
36 if (nargin != 4 && nargin != 5)
40 if (! ischar (extension) || length (extension) == 0)
41 error ("__xzip__: EXTENSION must be a string with finite length");
44 if (nargin == 5 && ! exist (outdir, "dir"))
45 error ("__xzip__: OUTDIR output directory does not exist");
49 files = cellstr (files);
51 if (! iscellstr (files))
52 error ("__xzip__: FILES must be a character array or cellstr");
64 ## Ignore any file with the compress extension
65 files(cellfun (@(x) length(x) > length(extension)
66 && strcmp (x((end - length(extension) + 1):end), extension),
69 copyfile (files, outdir);
71 [d, f] = myfileparts(files);
75 cmd = sprintf (commandtemplate, sprintf (" %s", f{:}));
77 [status, output] = system (cmd);
79 error ("__xzip__: %s command failed with exit status = %d",
86 @(x) fullfile (outdir, sprintf ("%s.%s", x, extension)),
87 f, "uniformoutput", false);
90 movefile (cellfun(@(x) sprintf ("%s.%s", x, extension), f,
91 "uniformoutput", false), cwd);
93 ## FIXME this does not work when you try to compress directories
94 entries = cellfun(@(x) sprintf ("%s.%s", x, extension),
95 files, "uniformoutput", false);
99 unwind_protect_cleanup
102 confirm_recursive_rmdir (false, "local");
109 function [d, f] = myfileparts (files)
110 [d, f, ext] = cellfun ("fileparts", files, "uniformoutput", false);
111 f = cellfun (@(x, y) sprintf ("%s%s", x, y), f, ext,
112 "uniformoutput", false);
113 idx = cellfun ("isdir", files);
118 ## FIXME -- reinstate these tests if we invent a way to test private
119 ## functions directly.
121 ## %!error <extension has to be a string with finite length>
122 ## %! __xzip__("gzip", "", "gzip -r %s", "bla");
123 ## %!error <no files to move>
124 ## %! __xzip__("gzip", ".gz", "gzip -r %s", tmpnam);
125 ## %!error <command failed with exit status>
126 ## %! # test __xzip__ with invalid compression command
128 ## %! filename = tmpnam;
130 ## %! save(filename, "dummy");
131 ## %! dirname = tmpnam;
132 ## %! mkdir(dirname);
133 ## %! entry = __xzip__("gzip", ".gz", "xxxzipxxx -r %s 2>/dev/null",
134 ## %! filename, dirname);
135 ## %! unwind_protect_cleanup
136 ## %! delete(filename);
137 ## %! rmdir(dirname);
138 ## %! end_unwind_protect