-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wip] lib/binary: implement BigEndianFile
- Loading branch information
Showing
4 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Test OpenBigEndianFile |