2 recfile -- GNU recutils'es recfiles parser on pure Go
3 Copyright (C) 2020-2021 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 var KeyValRe = regexp.MustCompile(`([a-zA-Z%][a-zA-Z0-9_]*):\s*(.*)$`)
31 scanner *bufio.Scanner
34 // Create Reader for iterating through the records. It uses
35 // bufio.Scanner, so can read more than currently parsed records take.
36 func NewReader(r io.Reader) *Reader {
37 return &Reader{bufio.NewScanner(r)}
40 // Get next record. Each record is just a collection of fields. io.EOF
41 // is returned if there is nothing to read more.
42 func (r *Reader) Next() ([]Field, error) {
43 fields := make([]Field, 0, 1)
47 lines := make([]string, 0)
51 if !r.scanner.Scan() {
52 if err := r.scanner.Err(); err != nil {
58 text = r.scanner.Text()
61 if text[len(text)-1] == '\\' {
62 lines = append(lines, text[:len(text)-1])
64 lines = append(lines, text)
65 fields = append(fields, Field{name, strings.Join(lines, "")})
66 lines = make([]string, 0)
72 if len(text) > 0 && text[0] == '#' {
76 if len(text) > 0 && text[0] == '+' {
77 lines = append(lines, "\n")
80 lines = append(lines, text[2:])
82 lines = append(lines, text[1:])
89 fields = append(fields, Field{name, strings.Join(lines, "")})
90 lines = make([]string, 0)
97 matches := KeyValRe.FindStringSubmatch(text)
98 if len(matches) == 0 {
99 return fields, errors.New("invalid field format")
104 if len(line) > 0 && line[len(line)-1] == '\\' {
106 lines = append(lines, line[:len(line)-1])
108 lines = append(lines, line)
112 return fields, errors.New("left continuation")
115 fields = append(fields, Field{name, strings.Join(lines, "")})
117 if len(fields) == 0 {
126 // Same as Next(), but with unique keys and last value.
127 func (r *Reader) NextMap() (map[string]string, error) {
128 fields, err := r.Next()
132 m := make(map[string]string, len(fields))
133 for _, f := range fields {
139 // Same as Next(), but with unique keys and slices of values.
140 func (r *Reader) NextMapWithSlice() (map[string][]string, error) {
141 fields, err := r.Next()
145 m := make(map[string][]string)
146 for _, f := range fields {
147 m[f.Name] = append(m[f.Name], f.Value)