# HG changeset patch # User jwe # Date 1147312136 0 # Node ID 29c4fb42b2101c81c53902160dab9b45be980085 # Parent b2a802aa0cda69b914b000252a7f3687267f57b1 [project @ 2006-05-11 01:48:56 by jwe] diff -r b2a802aa0cda -r 29c4fb42b210 scripts/ChangeLog --- a/scripts/ChangeLog Wed May 10 22:30:50 2006 +0000 +++ b/scripts/ChangeLog Thu May 11 01:48:56 2006 +0000 @@ -1,3 +1,7 @@ +2006-05-10 Soren Hauberg + + * tar.m, untar.m, unzip.m: New files. + 2006-05-10 John W. Eaton * path/addpath.m, path/rmpath.m: Improve compatibility. diff -r b2a802aa0cda -r 29c4fb42b210 scripts/miscellaneous/tar.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/miscellaneous/tar.m Thu May 11 01:48:56 2006 +0000 @@ -0,0 +1,94 @@ +## Copyright (C) 2005 Søren Hauberg +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, write to the Free Software +## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +## -*- texinfo -*- +## @deftypefn {Function File} tar (@var{filename}, @var{files}) +## @deftypefnx{Function File} tar (@var{filename}, @var{files}, @var{root}) +## @deftypefnx{Function File} @var{entries} = tar (...) +## Packs the files listed in @var{files} into @var{filename} using +## the @code{tar} program. @var{files} must either be a string or a cell +## array of strings containing the files to be packed. The extension of +## @var{filename} determines if the tar-file is also to be compressed +## (if no extension is present @code{.tar} will be appended to @var{filename}) +## @table @asis +## @item @code{.tar} +## No compression. +## @item @code{.tar.gz} or @code{.tgz} +## The tar-file will be compressed using @code{gzip}. +## @item @code{tar.bz}, @code{tar.bz2}, @code{tbz}, or @code{tbz2} +## The tar-file will be compressed using @code{bzip2}. +## @end table +## The optional argument @var{root} changes the relative path of @var{files} +## from the current directory. +## +## If an output argument is requested the filename entries in the archive +## is returned. +## +## @end deftypefn +## @seealso{untar, gzip, gunzip, zip, unzip} + +## Author: Søren Hauberg + +function entries = tar(filename, files, root) + if (nargin < 2 || nargin > 3) + print_usage("tar"); + elseif (nargin == 2) + root = "."; + endif + + supported_extensions = {"tar", "tar.gz", "tgz", "tar.bz", "tar.bz2", "tbz", "tbz2"}; + + ## Test type of input + if (ischar(files)) + files = {files}; + endif + if (!ischar(filename) || !iscellstr(files) || !ischar(root)) + error("All arguments must be strings.\n"); + endif + + ## Get extension of filename + dots = find(filename == "."); + for dot = dots + curext = filename(dot+1:end); + if (any(strcmp(curext, supported_extensions))) + ext = curext; + break; + endif + endfor + + ## If no extension was found default to "tar" + if (!exist("ext", "var")) + filename = sprintf("%s.tar", filename); + ext = "tar"; + endif + + ## Determine which flags to use with tar + switch (ext) + case {"tar"} flag = ""; + case {"tar.gz", "tgz"} flag = "z"; + case {"tar.bz", "tar.bz2", "tbz", "tbz2"} flag = "j"; + endswitch + + ## Call tar + [output, status] = system(["tar -" flag "cvf " filename " -C " root sprintf(" %s", files{:})]); + if (status != 0) + error("tar returned the following error: %s\n", output); + endif + + if (nargout) + entries = split(output(1:end-1), "\n"); + endif +endfunction diff -r b2a802aa0cda -r 29c4fb42b210 scripts/miscellaneous/untar.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/miscellaneous/untar.m Thu May 11 01:48:56 2006 +0000 @@ -0,0 +1,97 @@ +## Copyright (C) 2005 Søren Hauberg +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, write to the Free Software +## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +## -*- texinfo -*- +## @deftypefn {Function File} untar (@var{filename}) +## @deftypefnx {Function File} untar (@var{filename}, @var{outputdir}) +## Unpacks the archive @var{filename} using the tar program. +## The resulting files are placed in the directory @var{outputdir}, +## which defaults to the current directory. +## The used uncompressing algorithm depends on the extension of +## @var{filename}. If @var{filename} doesn't have an extension a list +## of supported extensions is tried. +## @end deftypefn + +## Author: Søren Hauberg + +# XXX: how do we support returning the extracted filenames? +# If strip was sane, we could simply return strip(output), +# which is what is currently done. However this is not +# Matlab compatible, since Matlab returns a cellstr and +# this implementation returns a matrix. +function out = untar(filename, outputdir) + if (nargin == 0) + print_usage("untar"); + elseif (nargin == 1) + outputdir = "."; + endif + + ## If filename is a sq_string, convert it to a string + filename = sprintf("%s", filename); + + ## XXX: what about "" (i.e. no extension) ? + supported_extensions = {"tar", "tar.gz", "tgz", "tar.bz", "tar.bz2", "tbz", "tbz2"}; + + ## Make sure filename and outputdir are strings + if (!ischar(filename) || !ischar(outputdir)) + error("All arguments must be strings.\n"); + endif + + ## Get extension of filename + dots = find(filename == "."); + for dot = dots + curext = filename(dot+1:end); + if (any(strcmp(curext, supported_extensions))) + ext = curext; + break; + endif + endfor + + ## If no extension was found, iterate over possible extensions + ## and try to append them to filename + if (!exist("ext", "var")) + for i = 1:length(supported_extensions) + curext = supported_extensions{i}; + if (exist([filename "." curext], "file")) + filename = [filename "." curext]; + ext = curext; + break; + endif + endfor + endif + + ## If no usable extension was found give an error + if (!exist("ext", "var")) + error("No supported extension was found for %s\n", filename); + endif + + ## Determine which flags to use with tar + switch (ext) + case {"tar"} flag = ""; + case {"tar.gz", "tgz"} flag = "z"; + case {"tar.bz", "tar.bz2", "tbz", "tbz2"} flag = "j"; + endswitch + + ## Call tar + [output, status] = system(["tar -" flag "xvf " filename " -C " outputdir]); + if (status != 0) + error("tar returned the following error: %s\n", output); + endif + + if (nargout > 0) + out = split(output, "\n"); + endif +endfunction diff -r b2a802aa0cda -r 29c4fb42b210 scripts/miscellaneous/unzip.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/miscellaneous/unzip.m Thu May 11 01:48:56 2006 +0000 @@ -0,0 +1,59 @@ +## Copyright (C) 2005 Søren Hauberg +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, write to the Free Software +## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +## -*- texinfo -*- +## @deftypefn {Function File} unzip (@var{filename}) +## @deftypefnx {Function File} unzip (@var{filename}, @var{outputdir}) +## Unpacks the archive @var{filename} using the unzip program. +## The resulting files are placed in the directory @var{outputdir}, +## which defaults to the current directory. +## @end deftypefn + +## Author: Søren Hauberg + +function files = unzip(filename, outputdir) + if (nargin == 0) + print_usage("unzip"); + elseif (nargin == 1) + outputdir = "."; + endif + + ## Make sure filename and outputdir are strings + if (!ischar(filename) || !ischar(outputdir)) + error("All arguments must be strings.\n"); + endif + + ## Should we append ".zip" to filename? + if (length(filename) <= 4 || !strcmp(filename(end-3:end), ".zip")) + filename = sprintf("%s.zip", filename); + endif + + ## Call unzip + [output, status] = system(["unzip -o " filename " -d " outputdir]); + if (status != 0) + error("unzip returned the following error: %s\n", output); + endif + + ## Create list of extracted files. This might depend on which version + ## unzip that it used, although I do not know this for sure. + if (nargout) + files = strrep(output, " inflating: ", ""); + files = split(files, "\n"); + # remove first and last line from the output + files = files(2:end-1, :); + endif +endfunction +