1
0
mirror of https://github.com/garraflavatra/go-fmp.git synced 2025-06-28 04:25:11 +00:00

Add sector reader

This commit is contained in:
2025-06-11 16:19:31 +02:00
parent ee1ac01fc4
commit 59220cc4bf
2 changed files with 42 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package fmp
import (
"bytes"
"encoding/binary"
"io"
"os"
"time"
@ -18,11 +19,27 @@ const (
type FmpFile struct {
Stream io.ReadSeeker
FileSize uint
NumSectors uint
VersionDate time.Time
ApplicationName string
}
type FmpSector struct {
Deleted bool
Level uint8
PrevSectorID uint32
NextSectorID uint32
Payload []byte
}
func OpenFile(path string) (*FmpFile, error) {
info, err := os.Stat(path)
if err != nil {
return nil, err
}
stream, err := os.Open(path)
if err != nil {
if stream != nil {
@ -35,6 +52,8 @@ func OpenFile(path string) (*FmpFile, error) {
stream.Close()
return nil, err
}
ctx.FileSize = uint(info.Size())
ctx.NumSectors = ctx.FileSize / sectorSize
return ctx, nil
}
@ -61,3 +80,19 @@ func (ctx *FmpFile) readHeader() error {
return nil
}
func (ctx *FmpFile) readSector() (*FmpSector, error) {
buf := make([]byte, sectorSize)
_, err := ctx.Stream.Read(buf)
if err != nil {
return nil, ErrRead
}
sector := &FmpSector{
Deleted: buf[0] != 0,
Level: uint8(buf[1]),
PrevSectorID: binary.BigEndian.Uint32(buf[2:6]),
NextSectorID: binary.BigEndian.Uint32(buf[6:10]),
Payload: buf[6:4076],
}
return sector, nil
}

View File

@ -7,6 +7,12 @@ func TestOpenFile(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if f.FileSize != 393216 {
t.Errorf("expected file size to be 393216, got %d", f.FileSize)
}
if f.NumSectors != 96 {
t.Errorf("expected number of sectors to be 96, got %d", f.NumSectors)
}
if f.ApplicationName != "Pro 12.0" {
t.Errorf("expected application name to be 'Pro 12.0', got '%s'", f.ApplicationName)
}