2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2005-2008 - INRIA - Serge STEER <serge.steer@inria.fr>
4 * Copyright (C) 2005-2008 - INRIA - Pierrick MODE
5 * Copyright (C) 2007-2008 - INRIA - Allan CORNET <allan.cornet@inria.fr>
6 * Copyright (C) 2011 - DIGITEO - Cedric DELAMARRE
8 * This file must be used under the terms of the CeCILL.
9 * This source file is licensed as described in the file COPYING, which
10 * you should have received as part of this distribution. The terms
11 * are also available at
12 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
15 /*--------------------------------------------------------------------------*/
17 #include "spreadsheet_gw.hxx"
18 #include "function.hxx"
24 #include "sci_malloc.h"
25 #include "localization.h"
31 #include "expandPathVariable.h"
33 #include "sci_tmpdir.h"
34 #include "freeArrayOfString.h"
35 #include "FileExist.h"
37 /*--------------------------------------------------------------------------*/
38 static const char* xls_basename(const char* name);
39 /*--------------------------------------------------------------------------*/
40 types::Function::ReturnValue sci_xls_open(types::typed_list &in, int _iRetCount, types::typed_list &out)
44 #define max_char_xls_open 256
47 types::String* pStrPath = NULL;
48 char* filename_IN = NULL;
50 char TMP[max_char_xls_open];
54 char **Sheetnames = NULL;
71 // *** check the minimal number of input args. ***
74 Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), "xls_open", 1);
75 return types::Function::Error;
78 // *** check number of output args according the methode. ***
81 Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "xls_open", 4);
82 return types::Function::Error;
85 // *** check type of input args and get it. ***
87 if (in[0]->isString() == false)
89 Scierror(999, _("%s: Wrong type for input argument #%d : string expected.\n"), "xls_open", 1);
90 return types::Function::Error;
93 pStrPath = in[0]->getAs<types::String>();
94 filename_IN = expandPathVariable(pStrPath->get(0));
96 // *** Perform operation. ***
100 /* remove blank characters @ the end */
101 int len = (int)strlen(filename_IN);
106 for (i = len - 1; i >= 0 ; i--)
108 if (filename_IN[i] != ' ')
113 filename_IN[i] = '\0';
117 if (FileExist(filename_IN) == false)
119 Scierror(999, _("The file %s does not exist.\n"), filename_IN);
120 return types::Function::Error;
124 TMPDIR = getTMPDIR();
134 strcat(TMP, xls_basename(filename_IN));
136 int result = ripole(filename_IN, TMP, 0, 0);
138 if (result != OLE_OK)
140 if (result == OLEER_NO_INPUT_FILE)
142 Scierror(999, _("%s: The file %s does not exist.\n"), "xls_open", filename_IN);
144 else if (result == OLEER_NOT_OLE_FILE ||
145 result == OLEER_INSANE_OLE_FILE ||
146 result == OLEER_LOADFAT_BAD_BOUNDARY ||
147 result == OLEER_MINIFAT_READ_FAIL ||
148 result == OLEER_PROPERTIES_READ_FAIL)
150 Scierror(999, _("%s: File %s is not an ole2 file.\n"), "xls_open", filename_IN);
152 else if (result == -1)
154 Scierror(999, _("%s: Cannot open file %s.\n"), "xls_open", filename_IN);
163 return types::Function::Error;
167 strcat(TMP, "Workbook");
169 iErr = mopen(TMP, "rb", iSwap, &iId);
170 if (iErr != MOPEN_NO_ERROR)
172 sciprint(_("There is no xls stream in the ole2 file %s.\n"), filename_IN);
173 if (iErr == MOPEN_CAN_NOT_OPEN_FILE)
175 Scierror(999, _("%s: Cannot open file %s.\n"), "xls_open", TMP);
177 else if (iErr == MOPEN_INVALID_FILENAME)
179 Scierror(999, _("%s: invalid filename.\n"), "xls_open");
181 else //if(iErr == MOPEN_INVALID_STATUS)
183 Scierror(999, _("%s: invalid status.\n"), "xls_open");
187 return types::Function::Error;
196 xls_open(&iErr, &iId, &sst , &ns, &Sheetnames, &Abspos, &nsheets);
201 2 = no Workbook included
202 3 = memory allocation problem
204 5 = not a BIFF8 xls file
213 Scierror(999, _("%s: Not an ole2 file.\n"), "xls_open");
214 return types::Function::Error;
218 Scierror(999, _("%s: The file has no Workbook directory.\n"), "xls_open");
219 return types::Function::Error;
223 Scierror(999, _("%s: No more memory.\n"), "xls_open");
224 return types::Function::Error;
228 Scierror(990, _("%s: Incorrect or corrupted file.\n"), "xls_open");
229 return types::Function::Error;
233 Scierror(999, _("%s: Only BIFF8 file format is handled.\n"), "xls_open");
234 return types::Function::Error;
241 // *** Return result in Scilab. ***
242 types::Double* pDblId = new types::Double(static_cast<double>(iId));
243 out.push_back(pDblId);
247 /* Create a typed list to return the properties */
248 types::String* pStrSst = new types::String(1, ns);
249 for (int i = 0; i < ns; i++)
251 pStrSst->set(i, sst[i]);
253 freeArrayOfString(sst, ns);
254 out.push_back(pStrSst);
258 out.push_back(types::Double::Empty());
263 /* Create a typed list to return the properties */
264 types::String* pStrSheets = new types::String(1, nsheets);
265 for (int i = 0; i < nsheets; i++)
267 pStrSheets->set(i, Sheetnames[i]);
269 freeArrayOfString(Sheetnames, nsheets);
270 out.push_back(pStrSheets);
274 types::Double* pDblAbsPos = new types::Double(1, nsheets);
275 for (int i = 0; i < nsheets; i++)
277 pDblAbsPos->set(i, static_cast<double>(Abspos[i]));
279 out.push_back(pDblAbsPos);
285 out.push_back(types::Double::Empty());
290 out.push_back(types::Double::Empty());
291 out.push_back(types::Double::Empty());
294 return types::Function::OK;
296 /*--------------------------------------------------------------------------*/
297 static const char* xls_basename(const char* name)
299 const char* base = NULL;
301 base = strrchr(name, '\\');
303 base = strrchr(name, '/');
305 return base ? base + 1 : name;
307 /*--------------------------------------------------------------------------*/