From b3ca06d08f4bba7af522a085e942d523c697a684 Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Wed, 10 Jan 2018 02:49:20 +0000 Subject: first commit --- deb822/parse.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 deb822/parse.go (limited to 'deb822/parse.go') diff --git a/deb822/parse.go b/deb822/parse.go new file mode 100644 index 0000000..a8a02e6 --- /dev/null +++ b/deb822/parse.go @@ -0,0 +1,80 @@ +package deb822 + +import ( + "bufio" + "fmt" + "strings" +) + +/*ParseStream parses a stream and returns an array of maps, where +/* each map contains all the fields of an RFC822 stanza */ +func ParseStream(stream *bufio.Scanner) (Stanzas, int) { + + s := make(Stanzas, 10) + curSize := 10 + for i := 0; i < 10; i++ { + s[i] = make(map[string]string) + } + curStanza := 0 + curField := "" + for stream.Scan() { + if strings.TrimSpace(stream.Text()) == "" { + if curField == "" { + continue + } + curStanza++ + curField = "" + fmt.Printf("==== New stanza ==== (Nr. %d/%d)\n", curStanza, curSize) + if curStanza >= curSize { + newS := make(Stanzas, curSize*2) + for i := curSize; i < curSize*2; i++ { + newS[i] = make(map[string]string) + } + curSize *= 2 + copy(newS, s) + s = newS + } + continue + } + + //fmt.Printf("line: %s\n", stream.Text()) + field := strings.SplitN(stream.Text(), ":", 2) + //fmt.Printf("field: %s\n", field) + if len(field) > 1 { + /* This is a new field*/ + curField = field[0] + s[curStanza][curField] = strings.TrimSpace(field[1]) + } else { + /* this is the continuation of the last field */ + s[curStanza][curField] += "\n" + field[0] + } + } + return s, curStanza +} + +/*ScanStanza reads a single RFC822 stanza from a stream and returns +/* it as a map */ +func ScanStanza(stream *bufio.Scanner) (Stanza, error) { + + s := make(Stanza) + + curField := "" + for stream.Scan() { + if strings.TrimSpace(stream.Text()) == "" { + return s, nil + } + + field := strings.SplitN(stream.Text(), ":", 2) + if len(field) > 1 { + /* This is a new field*/ + curField = field[0] + s[curField] = strings.TrimSpace(field[1]) + } else { + /* this is the continuation of the last field */ + s[curField] += "\n" + field[0] + } + } + + err := stream.Err() + return s, err +} -- cgit v1.2.3