diff --git a/fmp/fmp.go b/fmp/fmp.go index ee0ac13..670598d 100644 --- a/fmp/fmp.go +++ b/fmp/fmp.go @@ -2,6 +2,7 @@ package fmp import ( "bytes" + "encoding/binary" "io" "os" "time" @@ -17,12 +18,28 @@ const ( ) type FmpFile struct { - Stream io.ReadSeeker + 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 +} diff --git a/fmp/fmp_test.go b/fmp/fmp_test.go index 9a9bcdd..954d4bd 100644 --- a/fmp/fmp_test.go +++ b/fmp/fmp_test.go @@ -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) }