]> Cypherpunks.ru repositories - gorecfile.git/blob - w.go
Dummy commit for version raising
[gorecfile.git] / w.go
1 /*
2 recfile -- GNU recutils'es recfiles parser on pure Go
3 Copyright (C) 2020-2021 Sergey Matveev <stargrave@stargrave.org>
4
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.
8
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.
13
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/>.
16 */
17
18 // GNU recutils'es recfiles parser on pure Go
19 package recfile
20
21 import (
22         "io"
23         "strings"
24 )
25
26 type Writer struct {
27         w io.StringWriter
28 }
29
30 func NewWriter(w io.StringWriter) *Writer {
31         return &Writer{w}
32 }
33
34 func (w *Writer) RecordStart(fs ...Field) (written int, err error) {
35         return w.w.WriteString("\n")
36 }
37
38 func (w *Writer) WriteFields(fs ...Field) (written int, err error) {
39         var n int
40         for _, f := range fs {
41                 n, err = w.w.WriteString(f.Name + ": " + strings.TrimLeft(f.Value, " ") + "\n")
42                 written += n
43                 if err != nil {
44                         return
45                 }
46         }
47         return
48 }
49
50 func (w *Writer) WriteFieldMultiline(name string, lines []string) (written int, err error) {
51         var n int
52         n, err = w.w.WriteString(name + ": " + strings.TrimLeft(lines[0], " ") + "\n")
53         written += n
54         if err != nil {
55                 return
56         }
57         for _, l := range lines[1:] {
58                 n, err = w.w.WriteString("+ " + l + "\n")
59                 written += n
60                 if err != nil {
61                         return
62                 }
63         }
64         return
65 }