2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2015 - Scilab Enterprises - Antoine ELIAS
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
7 * This file is hereby licensed under the terms of the GNU GPL v2.0,
8 * pursuant to article 5.3.4 of the CeCILL v.2.1.
9 * This file was originally licensed under the terms of the CeCILL v2.1,
10 * and continues to be available under such terms.
11 * For more information, see the COPYING file which you should have received
12 * along with this program.
17 #include "hdf5_gw.hxx"
18 #include "function.hxx"
21 #include "overload.hxx"
22 #include "loadlib.hxx"
23 #include "context.hxx"
27 #include "sci_malloc.h"
29 #include "FileExist.h"
30 #include "expandPathVariable.h"
31 #include "h5_fileManagement.h"
32 #include "h5_attributeConstants.h"
33 #include "h5_readDataFromFile.h"
36 static const std::string fname("load");
38 types::Function::ReturnValue sci_hdf5_load(types::typed_list &in, int _iRetCount, types::typed_list& out)
40 int rhs = static_cast<int>(in.size());
43 Scierror(999, _("%s: Wrong number of input argument(s): at least %d expected.\n"), fname.data(), 1);
44 return types::Function::Error;
47 for (int i = 0; i < in.size(); ++i)
49 if (in[i]->isString() == false)
51 Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname.data(), i+1);
52 return types::Function::Error;
55 if (in[i]->getAs<types::String>()->isScalar() == false)
57 Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), fname.data(), i + 1);
58 return types::Function::Error;
62 wchar_t* wcfilename = expandPathVariableW(in[0]->getAs<types::String>()->get()[0]);
63 char* cfilename = wide_string_to_UTF8(wcfilename);
64 std::string filename(cfilename);
65 std::wstring wfilename(wcfilename);
69 if (FileExistW(wfilename.data()) == FALSE)
71 Scierror(999, _("%s: Unable to open file: '%s'.\n"), fname.data(), filename.data());
72 return types::Function::Error;
76 if (isHDF5File(filename.data()) == false)
80 types::Library* lib = loadlib(in[0]->getAs<types::String>()->get()[0], &err);
87 Scierror(999, "%s: %s", fname.data(), _("Redefining permanent variable.\n"));
88 return types::Function::Error;
90 Scierror(999, _("%s: %s is not a valid lib file.\n"), fname.data(), filename.data());
91 return types::Function::Error;
95 return types::Function::OK;
98 int iFile = openHDF5File(filename.data(), 0);
101 Scierror(999, _("%s: Unable to open file: %s\n"), fname.data(), filename.data());
102 return types::Function::Error;
105 std::wstring wstFuncName;
106 //manage version information
107 int version = getSODFormatAttribute(iFile);
108 closeHDF5File(iFile);
110 bool needReprocess = false;
116 wstFuncName = L"hdf5_load_v1";
117 needReprocess = true;
122 wstFuncName = L"hdf5_load_v2";
123 needReprocess = true;
128 wstFuncName = L"hdf5_load_v3";
133 Scierror(999, _("%s: Wrong SOD file format version. Max Expected: %d Found: %d\n"), fname.data(), SOD_FILE_VERSION, version);
134 return types::Function::Error;
138 types::typed_list out1;
139 types::Function::ReturnValue ret = Overload::call(wstFuncName, in, _iRetCount, out1);
141 if (ret != types::Function::OK)
143 return types::Function::Error;
149 types::String* vars = out1[0]->getAs<types::String>();
151 int size = vars->getSize();
152 types::typed_list in2(1, vars);
153 types::typed_list out2;
154 std::wstring wstFuncName = L"%_sodload";
155 ret = Overload::call(wstFuncName, in2, size, out2);
158 symbol::Context* ctx = symbol::Context::getInstance();
159 wchar_t** names = vars->get();
161 //update context with values return by %_sodload
162 for (int i = 0; i < size; ++i)
164 ctx->put(symbol::Symbol(names[i]), out2[i]);
171 out.push_back(out1.front());