1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
/*
* Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: jouyouyun <jouyouwen717@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Package kv reads key value files.
package kv
import (
"bufio"
"bytes"
"errors"
"io"
"unicode"
)
type Reader struct {
// Delim is key value delimiter
Delim byte
// Comment, if not 0, is the comment character. Lines begin with the
// Comment character are ignored.
Comment byte
// TrimSpace determines the behavior of trim space
TrimSpace TrimSpaceFlag
r *bufio.Reader
}
type TrimSpaceFlag uint
const (
TrimLeadingSpace TrimSpaceFlag = 1 << iota
TrimTailingSpace
TrimDelimLeftSpace
TrimDelimRightSpace
)
const (
TrimAllSpace = TrimLeadingSpace | TrimTailingSpace |
TrimDelimLeftSpace | TrimDelimRightSpace
TrimLeadingTailingSpace = TrimLeadingSpace | TrimTailingSpace
)
// NewReader returns a new Reader that reads from r.
// The Delim field default to '=', the TrimSpace field default to TrimAllSpace.
func NewReader(r io.Reader) *Reader {
return &Reader{
Delim: '=',
r: bufio.NewReader(r),
TrimSpace: TrimAllSpace,
}
}
type Pair struct {
Key string
Value string
}
// Read reads one pair from r.
func (r *Reader) Read() (pair *Pair, err error) {
for {
pair, err = r.parseLine()
if pair != nil {
break
}
if err != nil {
return nil, err
}
}
return pair, nil
}
// ReadAll reads all the remaining pairs from r.
// A successful call returns err == nil, not err == io.EOF.
// Because ReadAll is defined to read until EOF,
// it does not treat end of file as an error to be reported.
func (r *Reader) ReadAll() (pairs []*Pair, err error) {
for {
pair, err := r.Read()
if err == io.EOF {
return pairs, nil
}
if err != nil {
return nil, err
}
pairs = append(pairs, pair)
}
}
var ErrBadLine = errors.New("bad line")
func (r *Reader) parseLine() (*Pair, error) {
line, err := r.r.ReadBytes('\n')
if err != nil {
return nil, err
}
if r.TrimSpace&TrimLeadingSpace != 0 {
line = bytes.TrimLeftFunc(line, unicode.IsSpace)
}
if r.TrimSpace&TrimTailingSpace != 0 {
line = bytes.TrimRightFunc(line, unicode.IsSpace)
}
// skip empty line
if len(line) == 0 {
return nil, nil
}
b1 := line[0]
if r.Comment != 0 && b1 == r.Comment {
// skip comment line
return nil, nil
}
parts := bytes.SplitN(line, []byte{r.Delim}, 2)
if len(parts) != 2 {
return nil, ErrBadLine
}
key := parts[0]
if r.TrimSpace&TrimDelimLeftSpace != 0 {
key = bytes.TrimRightFunc(key, unicode.IsSpace)
}
value := parts[1]
if r.TrimSpace&TrimDelimRightSpace != 0 {
value = bytes.TrimLeftFunc(value, unicode.IsSpace)
}
return &Pair{
Key: string(key),
Value: string(value),
}, nil
}
|