diff options
author | KatolaZ <katolaz@freaknet.org> | 2018-01-10 02:49:20 +0000 |
---|---|---|
committer | KatolaZ <katolaz@freaknet.org> | 2018-01-10 02:49:20 +0000 |
commit | b3ca06d08f4bba7af522a085e942d523c697a684 (patch) | |
tree | ac1fe3ed403300d02a30222a987d7a1af250d56c /deb822/parse.go |
first commit
Diffstat (limited to 'deb822/parse.go')
-rw-r--r-- | deb822/parse.go | 80 |
1 files changed, 80 insertions, 0 deletions
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 +} |