octave-maintainers
[Top][All Lists]
Advanced

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

Re: untar.m on Solaris with non-GNU tar


From: John W. Eaton
Subject: Re: untar.m on Solaris with non-GNU tar
Date: Fri, 20 Oct 2006 19:48:23 -0400

On 20-Oct-2006, Bill Denney wrote:

|   ## Instructions on what to do for any extension.
|   ##
|   ## The first field names are the file extension without periods.
|   ## The .cmd part is what is executed to unpack an archive.
|   ## The .parser is the function to execute on output to get the files list.
|   ## The .move part indicates if the files may need to be manually moved
|   ##   (i.e. tar and unzip decompress into the current directory while
|   ##   bzip2 and gzip decompress the file at its location).
|   commandlist.gz.cmd = "gunzip -v -r \"%s\"";
|   commandlist.gz.parser = @__parse_gzip__;
|   commandlist.gz.move = true;
|   commandlist.z = commandlist.gz;
|   commandlist.zip.move = false;
|   ...
| 
|   nodotext = ext(~ ismember (ext, "."));
|   
|   origdir = pwd ();
| 
|   if isfield (commandlist, nodotext)
|     command = commandlist.(nodotext).cmd;
|     parser = commandlist.(nodotext).parser;
|     cstartdir = canonicalize_file_name (origdir);
|     cenddir = canonicalize_file_name (directory);
|     needmove = commandlist.(nodotext).move && ...
|       (~ strcmp(cstartdir, cenddir));
|   else
|     warning ("unpack:filetype", "unrecognised file type, %s", ext);
|     files = file;
|     return;
|   endif

OK, this seems like a nice idea.  Since this is an internal data
structure, I'm not sure we really need to have the second level of the
structure.  It would be more concise to write it like this:

  commandlist.TYPE = {CMD, PARSER, MOVE};

Then you could unpack it like this:

  if (isfield (commandlist, nodotext)
    [command, parser, move] = deal (commandlist.(nodotext){:});
    cstartdir = canonicalize_file_name (origdir);
    cenddir = canonicalize_file_name (directory);
    needmove = move && ! strcmp (cstartdir, cenddir);
  else
    ...
  endif

Also, to avoid having to create the structure each time the function
is called, you could write

  persistent commandlist;
  if (isempty (commandlist))
    commandlist.gz = {"gunzip -v -r \"%s\"", @__parse_gzip__, true};
    commandlist.z = commandlist.gz;
    commandlist.bz2 = {"bunzip2 -v \"%s\"", @__parse_bzip2__, true};
    commandlist.bz = commandlist.bz2;
    commandlist.tar = {"tar -x -v -f \"%s\"", @__parse_tar__, false};
    commandlist.targz = {"gunzip -c \"%s\" | tar -x -v", @__parse_tar__, false};
    commandlist.tgz = commandlist.targz;
    commandlist.tarbz2 = {"bunzip2 -c \"%s\" | tar -x -v", @__parse_tar__, 
false};
    commandlist.tbz2 = commandlist.tarbz2;
    commandlist.tbz = commandlist.tarbz2;
    commandlist.zip = {"unzip \"%s\"", @__parse_zip__, false};
  endif

|   unwind_protect
|     cd (directory);
|     [status, output] = system (sprintf ([command " 2>&1"], file));
|     cd (origdir);
|   unwind_protect_cleanup
|     cd (origdir);
|   end_unwind_protect

The cleanup code in an unwind_protect block is always executed, so the
above code does "cd (origdir") twice.

I applied the changes for copyfile and movefile.

Thanks,

jwe


reply via email to

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