1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
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.
13  *
14  */
15 
16 extern "C"
17 {
18 #include "gw_hdf5.h"
19 #include "Scierror.h"
20 #include "api_scilab.h"
21 #include "localization.h"
22 #include "expandPathVariable.h"
23 }
24 
25 #include "HDF5Scilab.hxx"
26 #include "H5File.hxx"
27 
28 using namespace org_modules_hdf5;
29 
30 /*
31   Copy an object (this code has been copied for h5mv).
32   Scilab prototype:
33   - h5cp(srcobj, destobj)
34   - h5cp(srcobj, destobj, destloc)
35   - h5cp(srcobj, destfile, destloc)
36   - h5cp(srcobj, sloc, destobj)
37   - h5cp(srcobj, sloc, destobj, destloc)
38   - h5cp(srcobj, sloc, destfile, destloc)
39   - h5cp(srcfile, sloc, destobj)
40   - h5cp(srcfile, sloc, destobj, destloc)
41   - h5cp(srcfile, sloc, destfile, destloc)
42 */
43 
44 /*--------------------------------------------------------------------------*/
sci_h5cp(char * fname,int * pvApiCtx)45 int sci_h5cp(char *fname, int* pvApiCtx)
46 {
47     SciErr err;
48     H5Object * sobj = 0;
49     H5Object * dobj = 0;
50     int * addr = 0;
51     char * str = 0;
52     char * expandedPath = 0;
53     std::string sfile;
54     std::string dfile;
55     std::string sloc;
56     std::string dloc;
57     const int nbIn = nbInputArgument(pvApiCtx);
58 
59     CheckOutputArgument(pvApiCtx, 0, 1);
60     CheckInputArgument(pvApiCtx, 2, 4);
61 
62     err = getVarAddressFromPosition(pvApiCtx, 1, &addr);
63     if (err.iErr)
64     {
65         printError(&err, 0);
66         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
67         return 0;
68     }
69 
70     if (HDF5Scilab::isH5Object(addr, pvApiCtx))
71     {
72         sobj = HDF5Scilab::getH5Object(addr, pvApiCtx);
73         if (!sobj)
74         {
75             Scierror(999, _("%s: Invalid H5Object.\n"), fname);
76             return 0;
77         }
78     }
79     else
80     {
81         if (!isStringType(pvApiCtx, addr) || !checkVarDimension(pvApiCtx, addr, 1, 1))
82         {
83             Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 1);
84             return 0;
85         }
86 
87         if (getAllocatedSingleString(pvApiCtx, addr, &str) != 0)
88         {
89             Scierror(999, _("%s: No more memory.\n"), fname);
90             return 0;
91         }
92 
93         expandedPath = expandPathVariable(str);
94         freeAllocatedSingleString(str);
95         sfile = std::string(expandedPath);
96         FREE(expandedPath);
97     }
98 
99     err = getVarAddressFromPosition(pvApiCtx, 2, &addr);
100     if (err.iErr)
101     {
102         printError(&err, 0);
103         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
104         return 0;
105     }
106 
107     if (HDF5Scilab::isH5Object(addr, pvApiCtx))
108     {
109         dobj = HDF5Scilab::getH5Object(addr, pvApiCtx);
110         if (!dobj)
111         {
112             Scierror(999, _("%s: Invalid H5Object.\n"), fname);
113             return 0;
114         }
115     }
116     else
117     {
118         if (!isStringType(pvApiCtx, addr) || !checkVarDimension(pvApiCtx, addr, 1, 1))
119         {
120             Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 2);
121             return 0;
122         }
123 
124         if (getAllocatedSingleString(pvApiCtx, addr, &str) != 0)
125         {
126             Scierror(999, _("%s: No more memory.\n"), fname);
127             return 0;
128         }
129 
130         sloc = std::string(str);
131         freeAllocatedSingleString(str);
132     }
133 
134     if (nbIn >= 3)
135     {
136         err = getVarAddressFromPosition(pvApiCtx, 3, &addr);
137         if (err.iErr)
138         {
139             printError(&err, 0);
140             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
141             return 0;
142         }
143 
144         if (HDF5Scilab::isH5Object(addr, pvApiCtx))
145         {
146             dobj = HDF5Scilab::getH5Object(addr, pvApiCtx);
147             if (!dobj)
148             {
149                 Scierror(999, _("%s: Invalid H5Object.\n"), fname);
150                 return 0;
151             }
152         }
153         else
154         {
155             if (!isStringType(pvApiCtx, addr) || !checkVarDimension(pvApiCtx, addr, 1, 1))
156             {
157                 Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 3);
158                 return 0;
159             }
160 
161             if (getAllocatedSingleString(pvApiCtx, addr, &str) != 0)
162             {
163                 Scierror(999, _("%s: No more memory.\n"), fname);
164                 return 0;
165             }
166 
167             if (nbIn == 4)
168             {
169                 expandedPath = expandPathVariable(str);
170                 dfile = std::string(expandedPath);
171                 FREE(expandedPath);
172             }
173             else
174             {
175                 if (sobj)
176                 {
177                     expandedPath = expandPathVariable(const_cast<char *>(sloc.c_str()));
178                     dfile = std::string(expandedPath);
179                     FREE(expandedPath);
180                     dfile = sloc;
181                     sloc = std::string("");
182                 }
183                 dloc = std::string(str);
184             }
185             freeAllocatedSingleString(str);
186         }
187 
188         if (nbIn == 4)
189         {
190             err = getVarAddressFromPosition(pvApiCtx, 4, &addr);
191             if (err.iErr)
192             {
193                 printError(&err, 0);
194                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
195                 return 0;
196             }
197 
198             if (!isStringType(pvApiCtx, addr) || !checkVarDimension(pvApiCtx, addr, 1, 1))
199             {
200                 Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 3);
201                 return 0;
202             }
203 
204             if (getAllocatedSingleString(pvApiCtx, addr, &str) != 0)
205             {
206                 Scierror(999, _("%s: No more memory.\n"), fname);
207                 return 0;
208             }
209 
210             dloc = std::string(str);
211             freeAllocatedSingleString(str);
212         }
213     }
214 
215     try
216     {
217         if (sobj)
218         {
219             if (dobj)
220             {
221                 HDF5Scilab::copy(*sobj, sloc, *dobj, dloc);
222             }
223             else
224             {
225                 HDF5Scilab::copy(*sobj, sloc, dfile, dloc);
226             }
227         }
228         else
229         {
230             if (dobj)
231             {
232                 HDF5Scilab::copy(sfile, sloc, *dobj, dloc);
233             }
234             else
235             {
236                 HDF5Scilab::copy(sfile, sloc, dfile, dloc);
237             }
238         }
239     }
240     catch (const std::exception & e)
241     {
242         Scierror(999, _("%s: %s\n"), fname, e.what());
243         return 0;
244     }
245 
246     AssignOutputVariable(pvApiCtx, 1) = 0;
247     ReturnArguments(pvApiCtx);
248 
249     return 0;
250 }
251