Skip to content

Commit

Permalink
added ReadChunks, ReadFileChunks and ReadStringChunks
Browse files Browse the repository at this point in the history
  • Loading branch information
aiq committed Feb 3, 2023
1 parent ef2ac67 commit 6420ac1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
3 changes: 1 addition & 2 deletions Reader.Read_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mxt

import (
"io"
"strings"
"testing"
)
Expand Down Expand Up @@ -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",
Expand Down
53 changes: 45 additions & 8 deletions Reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
8 changes: 8 additions & 0 deletions mxt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == ""
}

0 comments on commit 6420ac1

Please sign in to comment.