]> Creatis software - CreaPhase.git/blob - octave_packages/m/miscellaneous/private/__xzip__.m
update packages
[CreaPhase.git] / octave_packages / m / miscellaneous / private / __xzip__.m
1 ## Copyright (C) 2008-2012 Thorsten Meyer
2 ## based on the original gzip function by David Bateman
3 ##
4 ## This file is part of Octave.
5 ##
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.
10 ##
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.
15 ##
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/>.
19
20 ## -*- texinfo -*-
21 ## @deftypefn {Function File} {@var{entries} =} __xzip__ (@var{commandname}, @var{extension}, @var{commandtemplate}, @var{files}, @var{outdir})
22 ## Undocumented internal function.
23 ## @end deftypefn
24
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.
32
33 function entries = __xzip__ (commandname, extension,
34                              commandtemplate, files, outdir)
35
36   if (nargin != 4 && nargin != 5)
37     print_usage ();
38   endif
39
40   if (! ischar (extension) || length (extension) == 0)
41     error ("__xzip__: EXTENSION must be a string with finite length");
42   endif
43
44   if (nargin == 5 && ! exist (outdir, "dir"))
45     error ("__xzip__: OUTDIR output directory does not exist");
46   endif
47
48   if (ischar (files))
49     files = cellstr (files);
50   endif
51   if (! iscellstr (files))
52     error ("__xzip__: FILES must be a character array or cellstr");
53   endif
54
55   if (nargin == 4)
56     outdir = tmpnam ();
57     mkdir (outdir);
58   endif
59
60   cwd = pwd ();
61   unwind_protect
62     files = glob (files);
63
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),
67       files)) = [];
68
69     copyfile (files, outdir);
70
71     [d, f] = myfileparts(files);
72
73     cd (outdir);
74
75     cmd = sprintf (commandtemplate, sprintf (" %s", f{:}));
76
77     [status, output] = system (cmd);
78     if (status)
79       error ("__xzip__: %s command failed with exit status = %d",
80              commandname, status);
81     endif
82
83     if (nargin == 5)
84       if (nargout > 0)
85         entries = cellfun(
86             @(x) fullfile (outdir, sprintf ("%s.%s", x, extension)),
87             f, "uniformoutput", false);
88       endif
89     else
90       movefile (cellfun(@(x) sprintf ("%s.%s", x, extension), f,
91                         "uniformoutput", false), cwd);
92       if (nargout > 0)
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);
96       endif
97     endif
98
99   unwind_protect_cleanup
100     cd (cwd);
101     if (nargin == 4)
102       confirm_recursive_rmdir (false, "local");
103       rmdir (outdir, "s");
104     endif
105   end_unwind_protect
106
107 endfunction
108
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);
114   d(idx) = "";
115   f(idx) = files(idx);
116 endfunction
117
118 ## FIXME -- reinstate these tests if we invent a way to test private
119 ## functions directly.
120 ##
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
127 ## %!  unwind_protect
128 ## %!    filename = tmpnam;
129 ## %!    dummy    = 1;
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