Skip to content

Commit

Permalink
[wip] lib/binary: implement BigEndianFile
Browse files Browse the repository at this point in the history
  • Loading branch information
shuLhan committed Dec 28, 2024
1 parent c1aab5c commit 9617673
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
78 changes: 78 additions & 0 deletions lib/binary/big_endian_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SPDX-FileCopyrightText: 2024 M. Shulhan <ms@kilabit.info>
//
// SPDX-License-Identifier: BSD-3-Clause

package binary

import (
"fmt"
"os"
"sync"
)

// BigEndianFile support reading and writing Go types into file with
// big-endian byte order.
// Unlike [binary.Write] in the standard library, the Write method in here
// support writing struct field with type slice and string.
type BigEndianFile struct {
name string
file *os.File

// val is the backing storage for the file.
val []byte

mtx sync.Mutex
}

// OpenBigEndianFile open the file for read and write.
// It will return an error if file does not exists.
func OpenBigEndianFile(name string) (bef *BigEndianFile, err error) {
var logp = `OpenBigEndianFile`

bef = &BigEndianFile{
name: name,
}

bef.file, err = os.OpenFile(name, os.O_RDWR, 0600)
if err != nil {
return nil, fmt.Errorf(`%s: %w`, logp, err)
}

var fi os.FileInfo
fi, err = bef.file.Stat()
if err != nil {
goto fail
}

bef.val = make([]byte, fi.Size())

// Read all contents.
_, err = bef.file.Read(bef.val)
if err != nil {
goto fail
}

return bef, nil
fail:
_ = bef.file.Close()
return nil, fmt.Errorf(`%s: %w`, logp, err)
}

func (bef *BigEndianFile) Close() (err error) {
bef.mtx.Lock()
err = bef.file.Close()
bef.mtx.Unlock()
return err
}

func (bef *BigEndianFile) Read(data any) (err error) {
return nil
}

func (bef *BigEndianFile) Seek(off int64, whence int) (n int64, err error) {
return n, nil
}

func (bef *BigEndianFile) Write(data any) (n int64, err error) {
return n, nil
}
47 changes: 47 additions & 0 deletions lib/binary/big_endian_file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-FileCopyrightText: 2024 M. Shulhan <ms@kilabit.info>
//
// SPDX-License-Identifier: BSD-3-Clause

package binary

import (
"testing"

"git.sr.ht/~shulhan/pakakeh.go/lib/test"
)

type testBigEndian struct {
data any
tag string
}

func TestOpenBigEndianFile(t *testing.T) {
listCase := []struct {
name string
expError string
expVal string
}{{
name: `notexist`,
expError: `OpenBigEndianFile: open notexist: no such file or directory`,
}, {
name: `testdata/BigEndianFile/open`,
expVal: "Test OpenBigEndianFile\n",
}}

for _, tcase := range listCase {
bef, err := OpenBigEndianFile(tcase.name)
if err != nil {
test.Assert(t, tcase.name+` error`,
tcase.expError, err.Error())
continue
}

test.Assert(t, `name`, tcase.name, bef.name)
test.Assert(t, `val`, tcase.expVal, string(bef.val))

err = bef.Close()
if err != nil {
t.Fatal(err)
}
}
}
Empty file.
1 change: 1 addition & 0 deletions lib/binary/testdata/BigEndianFile/open
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test OpenBigEndianFile

0 comments on commit 9617673

Please sign in to comment.