# HG changeset patch
# User Markus Muetzel <markus.muetzel@gmx.de>
# Date 1500236393 -7200
#      Sun Jul 16 22:19:53 2017 +0200
# Node ID 1268b6d2c1a8edd93b361f2dd21af894f05ecaf6
# Parent  32f5c1e46432f63d678ad085e9b8724667376e5b
__OCT_spsh_open__.m: Return to original directory also on error. Set output
arguments for early return. Minor style changes.

__OCT_spsh_close__.m: Minor changes.

diff -r 32f5c1e46432 -r 1268b6d2c1a8 inst/private/__OCT_spsh_close__.m
--- a/inst/private/__OCT_spsh_close__.m	Sun Jul 16 19:02:08 2017 +0200
+++ b/inst/private/__OCT_spsh_close__.m	Sun Jul 16 22:19:53 2017 +0200
@@ -42,7 +42,7 @@
   ## .ods and.xlsx are both zipped
   if (strcmpi (ext, ".ods") || strcmpi (ext, ".xlsx"))
     if (xls.changed && xls.changed < 3)
-      ## Go to temp dir where ods file has been unzipped
+      ## Go to temp dir where .ods or .xlsx file has been unzipped
       cd (xls.workbook);
             pause (0.05);
       ## Zip tmp directory into .ods or .xlsx and copy it over original file
@@ -53,7 +53,7 @@
           xls.changed = 0;
         endif
       catch
-        printf ("odsclose: could not zip ods contents in %s to %s\n", ...
+        printf ("odsclose: could not zip files in %s to %s\n", ...
                 xls.workbook, filename);
       end_try_catch;
     endif
diff -r 32f5c1e46432 -r 1268b6d2c1a8 inst/private/__OCT_spsh_open__.m
--- a/inst/private/__OCT_spsh_open__.m	Sun Jul 16 19:02:08 2017 +0200
+++ b/inst/private/__OCT_spsh_open__.m	Sun Jul 16 22:19:53 2017 +0200
@@ -27,6 +27,9 @@
 
 function [ xls, xlssupport, lastintf] = __OCT_spsh_open__ (xls, xwrite, filename, xlssupport, ftype)
 
+  ## default output (on error)
+  lastintf = "";
+
   ## Open and unzip file to temp location (code by Markus Bergholz)
   ## create current work folder
   tmpdir = tmpnam;
@@ -61,9 +64,12 @@
       unpack (templ, tmpdir, "unzip");
       ## Allow unpack dust to settle down (lazy write file I/O)
       pause (0.2);
-    catch
+    catch err
       printf ("file %s couldn't be unpacked. Is it the proper file format?\n", filename);
+      warning (err.message)
+      warning (err.stack)
       xls = [];
+      cd (opwd);
       return
     end_try_catch
   endif
@@ -83,39 +89,38 @@
       warning ("xls2open: file %s couldn't be unzipped\n", filename);
       xls = [];
       return
-    else
-      ## Fill xlsx pointer 
-      xls.workbook          = tmpdir;       # subdir containing content.xml
-      xls.xtype             = "OCT";        # OCT is fall-back interface
-      xls.app               = 'xlsx';       # must NOT be an empty string!
-      xls.filename = filename;              # spreadsheet filename
-      xls.changed = 0;                      # Dummy
+    endif
 
-      ## Get content.xml
-      xml = fread (fid, "char=>char").';
-      ## Close file
-      fclose (fid);
+    ## Fill xlsx pointer 
+    xls.workbook          = tmpdir;       # subdir containing content.xml
+    xls.xtype             = "OCT";        # OCT is fall-back interface
+    xls.app               = 'xlsx';       # must NOT be an empty string!
+    xls.filename = filename;              # spreadsheet filename
+    xls.changed = 0;                      # Dummy
+
+    ## Get content.xml
+    xml = fread (fid, "char=>char").';
+    ## Close file
+    fclose (fid);
 
-      ## Get sheet names and indices
-      sheets = getxmlnode (xml, "sheets", [], 1);
-      xls.sheets.sh_names = cell2mat (regexp (sheets, '<sheet name="(.*?)"', "tokens"));
-      xls.sheets.rid = str2double (cell2mat (regexp (sheets, ' r:id="rId(\d+)"', "tokens")));
-      xls.sheets.sheetid = str2double (cell2mat (regexp (sheets, ' sheetId="(\d+)"', "tokens")));
-      
-      ## read worksheet.xml.rels
-      fid = fopen (sprintf ("%s/xl/_rels/workbook.xml.rels", tmpdir));
-      if (fid > 0)
-        str = fread (fid, "char=>char").';
-        fclose (fid);
-        xls.sheets.rels = ...
-         regexp (str, ['<Relationship Id="rId(\d+)" ' ...
-                       'Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" ' ...
-                       'Target="worksheets/sheet(\d+).xml"[^/>]*/>'], "tokens");
-        xls.sheets.rels = reshape (str2double (cell2mat (xls.sheets.rels)), [], columns(xls.sheets.rels))';
-      else
-        xls.sheets.rels = reshape([xls.sheets.rid xls.sheets.rid], [], columns (xls.sheets.rid));
-      endif
-
+    ## Get sheet names and indices
+    sheets = getxmlnode (xml, "sheets", [], 1);
+    xls.sheets.sh_names = cell2mat (regexp (sheets, '<sheet name="(.*?)"', "tokens"));
+    xls.sheets.rid = str2double (cell2mat (regexp (sheets, ' r:id="rId(\d+)"', "tokens")));
+    xls.sheets.sheetid = str2double (cell2mat (regexp (sheets, ' sheetId="(\d+)"', "tokens")));
+    
+    ## read worksheet.xml.rels
+    fid = fopen (sprintf ("%s/xl/_rels/workbook.xml.rels", tmpdir));
+    if (fid > 0)
+      str = fread (fid, "char=>char").';
+      fclose (fid);
+      xls.sheets.rels = ...
+       regexp (str, ['<Relationship Id="rId(\d+)" ' ...
+                     'Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" ' ...
+                     'Target="worksheets/sheet(\d+).xml"[^/>]*/>'], "tokens");
+      xls.sheets.rels = reshape (str2double (cell2mat (xls.sheets.rels)), [], columns(xls.sheets.rels))';
+    else
+      xls.sheets.rels = reshape([xls.sheets.rid xls.sheets.rid], [], columns (xls.sheets.rid));
     endif
 
   elseif (ftype == 3)
