Mercurial > forge
changeset 12245:c5e19e295357 octave-forge
Use one variable for processed file type rather than 5
author | prnienhuis |
---|---|
date | Sat, 28 Dec 2013 16:12:05 +0000 |
parents | 6c4cb4f51848 |
children | b83ec8ae2335 |
files | main/io/inst/private/__OCT_spsh_open__.m main/io/inst/private/__POI_spsh_open__.m |
diffstat | 2 files changed, 45 insertions(+), 41 deletions(-) [+] |
line wrap: on
line diff
--- a/main/io/inst/private/__OCT_spsh_open__.m Sat Dec 28 16:10:17 2013 +0000 +++ b/main/io/inst/private/__OCT_spsh_open__.m Sat Dec 28 16:12:05 2013 +0000 @@ -1,4 +1,5 @@ -## Copyright (C) 2013 Philip Nienhuis +## Copyright (C) 2013 Philip Nienhuis +## Copyright (C) 2013 Markus Bergholz (.xlsx & archive unzip stuff) ## ## 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 @@ -34,15 +35,17 @@ ## 2013-11-16 Replace fgetl calls by fread to cope with EOLs ## 2013-12-13 Fix sheet names parsing regexpr ## 2013-12-14 Fix sheet names parsing regexpr # 2 (attrib order can change => -## that's why an XML parser is superior over regular expressions) +## that's why an XML parser is superior over regular expressions) +## 2013-12-27 Use one variable for processed file type +## '' Shuffled code around to file type order -function [ xls, xlssupport, lastintf] = __OCT_spsh_open__ (xls, xwrite, filename, xlssupport, chk2, chk3, chk5) +function [ xls, xlssupport, lastintf] = __OCT_spsh_open__ (xls, xwrite, filename, xlssupport, ftype) ## Open and unzip file to temp location (code by Markus Bergholz) ## create current work folder tmpdir = tmpnam; - if (chk5) + if (ftype == 5) ## Gnumeric xml files are gzipped system (sprintf ("gzip -d -c -S=gnumeric %s > %s", filename, tmpdir)); fid = fopen (tmpdir, 'r'); @@ -59,9 +62,38 @@ return end_try_catch endif - - ## First check if we're reading ODS - if (chk3) + + ## Set up file pointer struct + if (ftype == 2) + ## ======================= XLSX =========================================== + ## From xlsxread by Markus Bergholz <markuman+xlsread@gmail.com> + ## https://github.com/markuman/xlsxread + + ## Get sheet names. Speeds up other functions a lot if we can do it here + fid = fopen (sprintf ('%s/xl/workbook.xml', tmpdir)); + if (fid < 0) + ## File open error + error ("xls2oct: file %s couldn't be unzipped", filename); + 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 + + ## Get content.xml + xml = fread (fid, "char=>char").'; + ## Close file + fclose (fid); + + ## Get sheet names and indices + xls.sheets.sh_names = cell2mat (regexp (xml, '<sheet name="(.*?)"(?: r:id="\w+")? sheetId="\d+"', "tokens")); + xls.sheets.rid = str2double (cell2mat (regexp (xml, '<sheet name=".*?" sheetId="(\d+)"', "tokens"))); + + endif + + elseif (ftype == 3) ## ============== ODS. Read the actual data part in content.xml ============ fid = fopen (sprintf ("%s/content.xml", tmpdir), "r"); if (fid < 0) @@ -94,36 +126,7 @@ endif - elseif (chk2) - ## ======================= XLSX =========================================== - ## From xlsxread by Markus Bergholz <markuman+xlsread@gmail.com> - ## https://github.com/markuman/xlsxread - - ## Get sheet names. Speeds up other functions a lot if we can do it here - fid = fopen (sprintf ('%s/xl/workbook.xml', tmpdir)); - if (fid < 0) - ## File open error - error ("xls2oct: file %s couldn't be unzipped", filename); - 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 - - ## Get content.xml - xml = fread (fid, "char=>char").'; - ## Close file - fclose (fid); - - ## Get sheet names and indices - xls.sheets.sh_names = cell2mat (regexp (xml, '<sheet name="(.*?)"(?: r:id="\w+")? sheetId="\d+"', "tokens")); - xls.sheets.rid = str2double (cell2mat (regexp (xml, '<sheet name=".*?" sheetId="(\d+)"', "tokens"))); - - endif - - elseif (chk5) + elseif (ftype == 5) ## ====================== Gnumeric ========================================= xls.workbook = tmpdir; # location of unzipped files xls.xtype = "OCT"; # interface
--- a/main/io/inst/private/__POI_spsh_open__.m Sat Dec 28 16:10:17 2013 +0000 +++ b/main/io/inst/private/__POI_spsh_open__.m Sat Dec 28 16:12:05 2013 +0000 @@ -28,15 +28,16 @@ ## 2012-10-24 Style fixes; added UNO to fall-back for BIFF5 formats ## 2013-01-20 Adapted to ML-compatible Java calls ## 2013-12-06 Updated copyright strings +## 2013-12-27 Use one variable for processed file type -function [ xls, xlssupport, lastintf ] = __POI_spsh_open__ (xls, xwrite, filename, xlssupport, chk1, chk2, xlsinterfaces) +function [ xls, xlssupport, lastintf ] = __POI_spsh_open__ (xls, xwrite, filename, xlssupport, ftype, xlsinterfaces) ## Get handle to workbook try if (xwrite > 2) - if (chk1) + if (ftype == 1) wb = javaObject ("org.apache.poi.hssf.usermodel.HSSFWorkbook"); - elseif (chk2) + elseif (ftype == 2) wb = javaObject ("org.apache.poi.xssf.usermodel.XSSFWorkbook"); endif xls.app = "new_POI"; @@ -54,7 +55,7 @@ lastintf = "POI"; catch clear xlsin; - if (chk1 && (xlsinterfaces.JXL || xlsinterfaces.UNO)) + if (ftype == 1 && (xlsinterfaces.JXL || xlsinterfaces.UNO)) printf ... (["Couldn't open file %s using POI;\n" ... "trying Excel'95 format with JXL or UNO...\n"], filename);