Skip to content

Commit

Permalink
hdf5: add CommonFG.LinkExists wrapper for C.H5Lexists
Browse files Browse the repository at this point in the history
Using boolean return since the error state is not well defined in the C code.
  • Loading branch information
zyc-sudo authored and kortschak committed Sep 20, 2019
1 parent 0554fa7 commit b0d662f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 7 additions & 0 deletions h5g_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,10 @@ func (g *Group) CreateTableFrom(name string, dtype interface{}, chunkSize, compr
func (g *Group) OpenTable(name string) (*Table, error) {
return openTable(g.id, name)
}

// LinkExists returns whether a link with the specified name exists in the group.
func (g *CommonFG) LinkExists(name string) bool {
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
return C.H5Lexists(g.id, c_name, 0) > 0
}
17 changes: 16 additions & 1 deletion h5g_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func TestGroup(t *testing.T) {
defer os.Remove(fname)
defer f.Close()

if f.LinkExists("/foo") {
t.Error("unexpected /foo link present")
}

g1, err := f.CreateGroup("foo")
if err != nil {
t.Fatalf("couldn't create group: %s", err)
Expand All @@ -25,7 +29,15 @@ func TestGroup(t *testing.T) {
t.Fatal("wrong file for group")
}
if g1.Name() != "/foo" {
t.Errorf("wrong Name for group: want %q, got %q", "/foo", g1.Name())
t.Errorf(`wrong name for group: want:"/foo", got:%q`, g1.Name())
}

if !f.LinkExists("/foo") {
t.Error(`unexpected "/foo" group link not present`)
}

if g1.LinkExists("bar") {
t.Error(`unexpected "bar" child link for "/foo" group`)
}

g2, err := g1.CreateGroup("bar")
Expand All @@ -38,6 +50,9 @@ func TestGroup(t *testing.T) {
if g2.Name() != "/foo/bar" {
t.Errorf("wrong Name for group: want %q, got %q", "/foo/bar", g1.Name())
}
if !g1.LinkExists("bar") {
t.Error(`expected "bar" child link for "/foo" group not present`)
}

g3, err := g2.CreateGroup("baz")
if err != nil {
Expand Down

0 comments on commit b0d662f

Please sign in to comment.