-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
68 lines (54 loc) · 1.23 KB
/
main.go
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
// Package hdf5 provides a reader and writer of HDF5 files.
//
// https://en.wikipedia.org/wiki/Hierarchical_Data_Format
package hdf5
/*
#cgo CFLAGS: -Itarget/include
#cgo LDFLAGS: -ldl -lm -lz
#include <stdlib.h>
#include <hdf5.h>
uint _H5F_ACC_TRUNC() { return H5F_ACC_TRUNC; }
uint _H5F_ACC_RDWR() { return H5F_ACC_RDWR; }
*/
import "C"
import (
"errors"
"unsafe"
)
// File represents a file.
type File struct {
fid C.hid_t
}
// Create creates a new file.
func Create(path string) (*File, error) {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
fid := C.H5Fcreate(cpath, C._H5F_ACC_TRUNC(), C.H5P_DEFAULT, C.H5P_DEFAULT)
if fid < 0 {
return nil, errors.New("failed to create a file")
}
file := &File{
fid: fid,
}
return file, nil
}
// Open opens an existing file.
func Open(path string) (*File, error) {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
fid := C.H5Fopen(cpath, C._H5F_ACC_RDWR(), C.H5P_DEFAULT)
if fid < 0 {
return nil, errors.New("failed to open the file")
}
file := &File{
fid: fid,
}
return file, nil
}
// Close closes the file.
func (f *File) Close() error {
if err := C.H5Fclose(f.fid); err != 0 {
return errors.New("failed to close the file")
}
return nil
}