2018-02-24 13:50:35 +00:00
|
|
|
package data
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2018-03-18 05:04:46 +00:00
|
|
|
|
2018-03-22 20:31:10 +00:00
|
|
|
"ur.gs/ordoor/internal/util/asciiscan"
|
2018-02-24 13:50:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Comments are `//`, start of line only, for accounting
|
|
|
|
var (
|
|
|
|
comment = []byte("//")
|
|
|
|
)
|
|
|
|
|
|
|
|
type Accounting map[string]string
|
|
|
|
|
|
|
|
func LoadAccounting(filename string) (Accounting, error) {
|
2018-03-18 05:04:46 +00:00
|
|
|
s, err := asciiscan.New(filename)
|
2018-02-24 13:50:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-03-18 05:04:46 +00:00
|
|
|
defer s.Close()
|
|
|
|
scanLines := s.Bufio()
|
|
|
|
|
2018-02-24 13:50:35 +00:00
|
|
|
out := make(Accounting)
|
|
|
|
|
|
|
|
for scanLines.Scan() {
|
|
|
|
if err := parse(out, scanLines.Bytes()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanLines.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parse(into Accounting, line []byte) error {
|
|
|
|
if len(line) == 0 || bytes.Equal(line[0:2], comment) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
idx := bytes.Index(line, []byte("="))
|
|
|
|
key := string(line[0:idx])
|
|
|
|
value := string(bytes.Trim(line[idx+1:len(line)], "\t\r\n"))
|
|
|
|
|
|
|
|
if _, ok := into[key]; ok {
|
|
|
|
return errors.New("Duplicate key: " + key)
|
|
|
|
}
|
|
|
|
|
|
|
|
into[key] = value
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|