2022-12-20 18:04:39 +01:00
|
|
|
package ini
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2022-12-23 14:26:54 +01:00
|
|
|
"fmt"
|
2022-12-20 18:04:39 +01:00
|
|
|
"io"
|
2022-12-23 14:26:54 +01:00
|
|
|
"reflect"
|
2022-12-20 18:04:39 +01:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Singular key-value pair under the given section within an INI file.
|
|
|
|
type Entry struct {
|
|
|
|
Section string
|
|
|
|
Key string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
2022-12-23 14:26:54 +01:00
|
|
|
// Attempts to write `value` to `writer` in an INI-encoded format, returning any error that
|
|
|
|
// occured during writing.
|
|
|
|
func Encode(writer io.Writer, section string, value any) error {
|
|
|
|
if _, printError := fmt.Fprintf(writer, "[%s]\n", section); printError != nil {
|
|
|
|
return printError
|
|
|
|
}
|
|
|
|
|
|
|
|
var valueType = reflect.TypeOf(value)
|
|
|
|
|
|
|
|
if valueType.Kind() != reflect.Struct {
|
|
|
|
return fmt.Errorf("only structs may be encoded")
|
|
|
|
}
|
|
|
|
|
|
|
|
var valueInfo = reflect.ValueOf(value)
|
|
|
|
|
|
|
|
for i := 0; i < valueInfo.NumField(); i += 1 {
|
|
|
|
if _, printError := fmt.Fprintf(writer, "%s = %s\n",
|
|
|
|
valueType.Field(i).Name, valueInfo.Field(i).Interface()); printError != nil {
|
|
|
|
|
|
|
|
return printError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-20 18:04:39 +01:00
|
|
|
// Returns the last error that occured during parsing.
|
|
|
|
func (parser *Parser) Err() error {
|
|
|
|
return parser.err
|
|
|
|
}
|
|
|
|
|
2022-12-20 22:48:24 +01:00
|
|
|
// Returns `true` if `parser` has reached the end of parsable tokens, either because the stream has
|
|
|
|
// ended or it has encountered an error.
|
|
|
|
func (parser *Parser) IsEnd() bool {
|
|
|
|
return parser.isEnd
|
|
|
|
}
|
|
|
|
|
2022-12-20 18:04:39 +01:00
|
|
|
// Creates and returns a new [Parser] by reference from `reader`
|
|
|
|
func NewParser(reader io.Reader) *Parser {
|
|
|
|
return &Parser{
|
|
|
|
scanner: bufio.NewScanner(reader),
|
2022-12-20 22:48:24 +01:00
|
|
|
isEnd: false,
|
2022-12-20 18:04:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-21 00:09:07 +01:00
|
|
|
// Attempts to parse the next key-value pair in the `parser` stream, returning the parsed [Entry],
|
|
|
|
// or an empty one if nothing was parsed, and a `bool` representing whether or not there is any
|
|
|
|
// further data available to parse.
|
2022-12-20 18:04:39 +01:00
|
|
|
//
|
|
|
|
// Note that the `parser` does not guarantee any parse order for key-value pairs extracted from the
|
|
|
|
// `parser` stream.
|
2022-12-20 22:48:24 +01:00
|
|
|
func (parser *Parser) Parse() Entry {
|
2022-12-20 18:04:39 +01:00
|
|
|
for parser.scanner.Scan() {
|
|
|
|
var line = strings.TrimSpace(parser.scanner.Text())
|
|
|
|
var lineLen = len(line)
|
|
|
|
|
|
|
|
if lineLen == 0 {
|
|
|
|
// Skip empty lines.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line[0] == '#') || (line[0] == ';') {
|
|
|
|
// Skip comment lines.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var lineTail = lineLen - 1
|
|
|
|
|
|
|
|
if (line[0] == '[') && (line[lineTail] == ']') {
|
|
|
|
// Section name.
|
|
|
|
parser.section = line[1:lineTail]
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if assignmentIndex := strings.Index(line, "="); assignmentIndex > -1 {
|
|
|
|
// Key with value.
|
|
|
|
return Entry{
|
|
|
|
Section: parser.section,
|
2022-12-20 22:53:34 +01:00
|
|
|
Key: unquote(strings.TrimSpace(line[0:assignmentIndex])),
|
|
|
|
Value: unquote(strings.TrimSpace(line[assignmentIndex+1:])),
|
2022-12-20 22:48:24 +01:00
|
|
|
}
|
2022-12-20 18:04:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Key which is its own value.
|
2022-12-20 22:53:34 +01:00
|
|
|
var keyValue = unquote(line[1:lineTail])
|
2022-12-20 18:04:39 +01:00
|
|
|
|
|
|
|
return Entry{
|
|
|
|
Section: parser.section,
|
|
|
|
Key: keyValue,
|
|
|
|
Value: keyValue,
|
2022-12-20 22:48:24 +01:00
|
|
|
}
|
2022-12-20 18:04:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
parser.err = parser.scanner.Err()
|
2022-12-20 22:48:24 +01:00
|
|
|
parser.isEnd = true
|
2022-12-20 18:04:39 +01:00
|
|
|
|
2022-12-20 22:48:24 +01:00
|
|
|
return Entry{}
|
2022-12-20 18:04:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// State machine for parsing streamable INI file sources.
|
|
|
|
type Parser struct {
|
|
|
|
scanner *bufio.Scanner
|
|
|
|
err error
|
|
|
|
section string
|
2022-12-20 22:48:24 +01:00
|
|
|
isEnd bool
|
2022-12-20 18:04:39 +01:00
|
|
|
}
|
2022-12-20 22:53:34 +01:00
|
|
|
|
|
|
|
// Returns a string with the the outer-most quotes surrounding `s` trimmed, should they exist.
|
|
|
|
func unquote(s string) string {
|
|
|
|
var sLen = len(s)
|
|
|
|
|
|
|
|
if sLen != 0 {
|
|
|
|
var sTail = sLen - 1
|
|
|
|
|
|
|
|
if (s[0] == '"') && (s[sTail] == '"') {
|
|
|
|
return s[1:sTail]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|