1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
/*
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
* Copyright (C) 2010 - DIGITEO - Allan CORNET
* Copyright (C) 2012 - Scilab Enterprises - Antoine ELIAS
* Copyright (C) 2013 - Scilab Enterprises - Calixte DENIZET
*
* This file must be used under the terms of the CeCILL.
* This source file is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at
* http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
*
*/
/*--------------------------------------------------------------------------*/
#include <hdf5.h>
#include <string.h>
#include "h5_fileManagement.h"
#include "FileExist.h"
#include "deleteafile.h"
#include "isdir.h"
#include "splitpath.h"
#include "scicurdir.h"
#include "MALLOC.h"
#ifdef _MSC_VER
#include "strdup_windows.h"
#endif
/*--------------------------------------------------------------------------*/
static char *getPathFilename(char *fullfilename);
static char *getFilenameWithExtension(char *fullfilename);
/*--------------------------------------------------------------------------*/
extern void H5_term_library(void);
void HDF5cleanup(void)
{
/* H5_term_library is not public but it is useful to cleanup hdf5 by ourselves
rather than let atexit do it.
HDF5cleanup is called in TerminateCore::TerminateCorePart2 and that avoids
to have the message:
HDF5: infinite loop closing library
D,T,F,FD,P,FD,P,FD,P,E,E,E,...
when exiting a javasci code.
*/
H5_term_library();
}
/*--------------------------------------------------------------------------*/
int createHDF5File(char *name)
{
hid_t file;
hid_t fapl = H5Pcreate(H5P_FILE_ACCESS);
char *pathdest = getPathFilename(name);
char *currentpath = NULL;
char *filename = getFilenameWithExtension(name);
int ierr = 0;
//H5Pset_fclose_degree(fapl, H5F_CLOSE_STRONG);
/* TO DO : remove when HDF5 will be fixed ... */
/* HDF5 does not manage no ANSI characters */
/* UGLY workaround :( */
/* We split path, move in this path, open file */
/* and return in previous place */
/* see BUG 6440 */
currentpath = scigetcwd(&ierr);
//prevent error msg to change directory to ""
if (strcmp(pathdest, "") != 0)
{
scichdir(pathdest);
}
FREE(pathdest);
/*bug 5629 : to prevent replace directory by file*/
if (isdir(filename))
{
FREE(filename);
FREE(currentpath);
return -2;
}
if (FileExist(filename))
{
deleteafile(filename);
}
/*
* Create a new file using the default properties.
*/
file = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl);
scichdir(currentpath);
FREE(currentpath);
FREE(filename);
return file;
}
/*--------------------------------------------------------------------------*/
int openHDF5File(char *name, int _iAppendMode)
{
hid_t file;
char *pathdest = getPathFilename(name);
char *currentpath = NULL;
char *filename = getFilenameWithExtension(name);
int ierr = 0;
void *oldclientdata = NULL;
/* Used to avoid stack trace to be displayed */
H5E_auto2_t oldfunc;
/* TO DO : remove when HDF5 will be fixed ... */
/* HDF5 does not manage no ANSI characters */
/* UGLY workaround :( */
/* We split path, move in this path, open file */
/* and return in previous place */
/* see BUG 6440 */
currentpath = scigetcwd(&ierr);
//prevent error msg to change directory to ""
if (strcmp(pathdest, "") != 0)
{
scichdir(pathdest);
}
/* Save old error handler */
H5Eget_auto2(H5E_DEFAULT, &oldfunc, &oldclientdata);
/* Turn off error handling */
H5Eset_auto2(H5E_DEFAULT, NULL, NULL);
if (_iAppendMode == 0)
{
//read only
file = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);
}
else
{
//read write to append
file = H5Fopen(filename, H5F_ACC_RDWR, H5P_DEFAULT);
}
/* The following test will display the backtrace in case of error */
/* Deactivated because displayed each time we call 'load' to open a non-HDF5 file */
/*if (file < 0)
{
H5Eprint(stderr);
}*/
/* Restore previous error handler */
H5Eset_auto2(H5E_DEFAULT, oldfunc, oldclientdata);
scichdir(currentpath);
FREE(currentpath);
FREE(filename);
FREE(pathdest);
return file;
}
/*--------------------------------------------------------------------------*/
int isHDF5File(char* _pstFilename)
{
int iRet = 0;
char *pathdest = getPathFilename(_pstFilename);
char *currentpath = NULL;
char *filename = getFilenameWithExtension(_pstFilename);
int ierr = 0;
/* TO DO : remove when HDF5 will be fixed ... */
/* HDF5 does not manage no ANSI characters */
/* UGLY workaround :( */
/* We split path, move in this path, open file */
/* and return in previous place */
/* see BUG 6440 */
currentpath = scigetcwd(&ierr);
{
//prevent error msg to change directory to ""
if (strcmp(pathdest, "") != 0)
{
scichdir(pathdest);
}
FREE(pathdest);
iRet = H5Fis_hdf5(filename);
FREE(filename);
}
scichdir(currentpath);
FREE(currentpath);
return iRet > 0 ? 1 : 0;
}
void closeHDF5File(int file)
{
herr_t status = 0;
/* printf("Open groups: %d\n", H5Fget_obj_count(file, H5F_OBJ_GROUP));
printf("Open datasets: %d\n", H5Fget_obj_count(file, H5F_OBJ_DATASET));
printf("Open datatypes: %d\n", H5Fget_obj_count(file, H5F_OBJ_DATATYPE));
printf("Open attributes: %d\n", H5Fget_obj_count(file, H5F_OBJ_ATTR));
printf("Open all (except the file itself): %d\n", H5Fget_obj_count(file, H5F_OBJ_ALL) - 1);*/
// H5Fflush(file, H5F_SCOPE_GLOBAL);
status = H5Fclose(file);
if (status < 0)
{
fprintf(stderr, "%s", "failed to close file");
}
}
/*--------------------------------------------------------------------------*/
static char *getPathFilename(char *fullfilename)
{
char *path = NULL;
if (fullfilename)
{
char* drv = strdup(fullfilename);
char* dir = strdup(fullfilename);
char* name = strdup(fullfilename);
char* ext = strdup(fullfilename);
path = strdup(fullfilename);
if (drv && dir && name && ext && path)
{
splitpath(fullfilename, FALSE, drv, dir, name, ext);
if (strcmp(drv, "") == 0)
{
strcpy(path, dir);
}
else
{
strcpy(path, drv);
strcat(path, dir);
}
}
if (drv)
{
FREE(drv);
drv = NULL;
}
if (dir)
{
FREE(dir);
dir = NULL;
}
if (name)
{
FREE(name);
name = NULL;
}
if (ext)
{
FREE(ext);
ext = NULL;
}
}
return path;
}
/*--------------------------------------------------------------------------*/
static char *getFilenameWithExtension(char *fullfilename)
{
char *filename = NULL;
if (fullfilename)
{
char* drv = strdup(fullfilename);
char* dir = strdup(fullfilename);
char* name = strdup(fullfilename);
char* ext = strdup(fullfilename);
filename = strdup(fullfilename);
if (drv && dir && name && ext && filename)
{
splitpath(fullfilename, FALSE, drv, dir, name, ext);
if (strcmp(ext, "") == 0)
{
strcpy(filename, name);
}
else
{
strcpy(filename, name);
strcat(filename, ext);
}
}
if (drv)
{
FREE(drv);
drv = NULL;
}
if (dir)
{
FREE(dir);
dir = NULL;
}
if (name)
{
FREE(name);
name = NULL;
}
if (ext)
{
FREE(ext);
ext = NULL;
}
}
return filename;
}
/*--------------------------------------------------------------------------*/
|