octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Changeset] new bzip2 function


From: Thorsten Meyer
Subject: [Changeset] new bzip2 function
Date: Sun, 26 Oct 2008 11:25:10 +0100
User-agent: Mozilla-Thunderbird 2.0.0.16 (X11/20080724)

Hi,

the applied changeset contains a bzip2 function which is analog to the
gzip function.
While fixing the @seealso references to prepare for
@seealso->@ref expansion I found a reference to (the up to now missing)
bzip2 in the docstring of bunzip2.m, so I added this function (instead
of removing the reference to it in the helpfile).

regards

Thorsten Meyer
# HG changeset patch
# User Thorsten Meyer <address@hidden>
# Date 1225016293 -3600
# Node ID 668408d2f66210793cbf6dbd31e7831ac4f9b90c
# Parent  480e07987aa8612f33be5d07798c7e568cfcee56
new bzip2 function

diff -r 480e07987aa8 -r 668408d2f662 scripts/ChangeLog
--- a/scripts/ChangeLog Sun Oct 26 11:10:56 2008 +0100
+++ b/scripts/ChangeLog Sun Oct 26 11:18:13 2008 +0100
@@ -1,3 +1,7 @@
+2008-10-25  Thorsten Meyer  <address@hidden>
+        * miscellaneous/bzip2: added command (which is already referenced
+          in the bunzip2 command)
+                  
 2008-10-22  Ben Abbott <address@hidden>
 
        * plot/cla.m: Fix error when no children to clear.
diff -r 480e07987aa8 -r 668408d2f662 scripts/miscellaneous/bzip2.m
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/miscellaneous/bzip2.m     Sun Oct 26 11:18:13 2008 +0100
@@ -0,0 +1,111 @@
+## Copyright (C) 2008 Thorsten Meyer
+## (based on gzip.m by David Bateman)
+##
+## This file is part of Octave.
+##
+## Octave 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 3 of the License, or (at
+## your option) any later version.
+##
+## Octave 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 Octave; see the file COPYING.  If not, see
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} address@hidden =} bzip2 (@var{files})
+## @deftypefnx {Function File} address@hidden =} bzip2 (@var{files}, 
@var{outdir})
+## Compress the list of files specified in @var{files}.
+## Each file is compressed separately and a new file with a '.bz2' extension
+## is created. The original file is not touched. If @var{outdir} is defined 
+## the compressed versions of the files are placed in this directory.
+## @seealso{bunzip2, gzip, zip, tar}
+## @end deftypefn
+
+function entries = bzip2 (files, outdir)
+
+  if (nargin == 1 || nargin == 2)
+
+    if (nargin == 2 && ! exist (outdir, "dir"))
+      error ("bzip2: output directory does not exist");
+    endif
+
+    if (ischar (files))
+      files = cellstr (files);
+    endif
+
+    if (nargin == 1)
+      outdir = tmpnam ();
+      mkdir (outdir);
+    endif
+
+    cwd = pwd();
+    unwind_protect
+      if (iscellstr (files))
+       files = glob (files);
+
+       ## Ignore any file with a .bz2 extension
+       files (cellfun (@(x) strcmp (x(end-2:end), ".bz2"), files)) = [];
+        # FIXME handle filenames with length <= 2 !!
+        
+       copyfile (files, outdir);
+       [d, f] = myfileparts(files);
+       cd (outdir);
+
+       cmd = sprintf ("bzip2 %s", sprintf (" %s", f{:}));
+
+       [status, output] = system (cmd);
+
+       if (status == 0)
+
+         if (nargin == 2)
+           bz2files = cellfun(@(x) fullfile (outdir, sprintf ("%s.bz2", x)), 
...
+                             f, "UniformOutput", false);
+         else
+           movefile (cellfun(@(x) sprintf ("%s.bz2", x), f, ...
+                             "UniformOutput", false), cwd);
+           bz2files = cellfun(@(x) sprintf ("%s.bz2", x), ...
+                             files, "UniformOutput", false);
+         endif
+
+         if (nargout > 0)
+            entries = bz2files;
+         endif
+       else
+         error ("bzip2: failed with exit status = %d", status);
+       endif
+    
+      else
+       error ("bzip2: expecting all arguments to be character strings");
+      endif
+    unwind_protect_cleanup
+      cd(cwd);
+      if (nargin == 1)
+       crr = confirm_recursive_rmdir ();
+       unwind_protect
+         confirm_recursive_rmdir (false);
+         rmdir (outdir, "s");
+       unwind_protect_cleanup
+         confirm_recursive_rmdir (crr);
+       end_unwind_protect
+      endif
+    end_unwind_protect
+  else
+    print_usage ();
+  endif
+
+endfunction
+
+function [d, f] = myfileparts (x)
+  [d, f, ext] = cellfun (@(x) fileparts (x), x, "UniformOutput", false);
+  f = cellfun (@(x, y) sprintf ("%s%s", x, y), f, ext, ...
+              "UniformOutput", false); 
+  idx = cellfun (@(x) isdir (x), x);
+  d(idx) = "";
+  f(idx) = x(idx);
+endfunction

reply via email to

[Prev in Thread] Current Thread [Next in Thread]