From 6420ac168659455f830f66facfe31e340b0c7fa8 Mon Sep 17 00:00:00 2001 From: aiq Date: Fri, 3 Feb 2023 21:07:40 +0100 Subject: [PATCH] added ReadChunks, ReadFileChunks and ReadStringChunks --- Reader.Read_test.go | 3 +-- Reader.go | 53 ++++++++++++++++++++++++++++++++++++++------- mxt.go | 8 +++++++ 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/Reader.Read_test.go b/Reader.Read_test.go index 4dcb106..7fbcecb 100644 --- a/Reader.Read_test.go +++ b/Reader.Read_test.go @@ -1,7 +1,6 @@ package mxt import ( - "io" "strings" "testing" ) @@ -84,7 +83,7 @@ int main(void) { tt := &TestToken{res, err, t} tt.expectLen(5) - tt.expectErr(io.EOF) + tt.expectErr(nil) tt.expectContent("user.json", `{ "user": "alucard", diff --git a/Reader.go b/Reader.go index 0b64b12..2ef91c8 100644 --- a/Reader.go +++ b/Reader.go @@ -152,23 +152,60 @@ func (my *Reader) ReadChunk() (ch Chunk, err error) { //************************************************************************ Read -// Read creates a Reader and reads all chunks from r and stores them in the map. +// ReadChunks creates a Reader and reads all chunks from r and stores them in +// the map. // The name will be used as key, the content will be stored as value. // Possible comments in the mxt will be ignored. // // A successful call returns err == nil, not err == io.EOF. -// Because Read is defined to read until EOF, it does not treat end of file as -// an error to be reported. -func Read(r io.Reader) (map[string]string, error) { - res := make(map[string]string) +// Because ReadChunks is defined to read until EOF, it does not treat end of +// file as an error to be reported. +func ReadChunks(r io.Reader) ([]Chunk, error) { + res := []Chunk{} var err error my := NewReader(r) for err == nil { - var chunk Chunk - chunk, err = my.ReadChunk() + var c Chunk + c, err = my.ReadChunk() if err == nil || err != io.EOF { - res[chunk.Header.Name] = chunk.Content + res = append(res, c) + } + } + + if err == io.EOF { + err = nil + } + return res, err +} + +// ReadStringChunks +func ReadStringChunks(str string) ([]Chunk, error) { + return ReadChunks(strings.NewReader(str)) +} + +// ReadFileChunks reads the mxt file behind path with Read. +func ReadFileChunks(path string) ([]Chunk, error) { + file, err := os.Open(path) + if err != nil { + return nil, err + } + defer file.Close() + + return ReadChunks(file) +} + +// Read creates a Reader and reads all with ReadChunks and stores them in the +// map. +// The name will be used as key, the content will be stored as value. +// Possible comments in the mxt will be ignored. +func Read(r io.Reader) (map[string]string, error) { + res := make(map[string]string) + + chunks, err := ReadChunks(r) + if err == nil { + for _, c := range chunks { + res[c.Header.Name] = c.Content } } diff --git a/mxt.go b/mxt.go index 5c99f2d..3e72e14 100644 --- a/mxt.go +++ b/mxt.go @@ -11,8 +11,16 @@ type Header struct { Comment string } +func (h Header) IsEmpty() bool { + return h.Name == "" && h.Comment == "" +} + // A Chunk represents a mxt chunk. type Chunk struct { Header Content string } + +func (c Chunk) IsEmpty() bool { + return c.Header.IsEmpty() && c.Comment == "" +}