Work on porting config file parsing to Go

This commit is contained in:
Kovid Goyal
2023-02-17 19:50:57 +05:30
parent 6f63d9c5d4
commit 5822bb23f0
10 changed files with 162 additions and 162 deletions

32
tools/utils/config.go Normal file
View File

@@ -0,0 +1,32 @@
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
package utils
import (
"bufio"
"fmt"
"io"
"strings"
)
var _ = fmt.Print
func StringToBool(x string) bool {
x = strings.ToLower(x)
return x == "y" || x == "yes" || x == "true"
}
func ParseConfData(src io.Reader, callback func(key, val string, line int)) error {
scanner := bufio.NewScanner(src)
lnum := 0
for scanner.Scan() {
line := strings.TrimLeft(scanner.Text(), " ")
lnum++
if line == "" || strings.HasPrefix(line, "#") {
continue
}
key, val, _ := strings.Cut(line, " ")
callback(key, val, lnum)
}
return scanner.Err()
}