Add function for writing INI files
This commit is contained in:
parent
4518d13345
commit
8f82318cce
27
ini.go
27
ini.go
|
@ -32,9 +32,9 @@ func NewParser(reader io.Reader) *Parser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parses the next key-value pair in the `parser` stream, returning the parsed [Entry], or an empty
|
// Attempts to parse the next key-value pair in the `parser` stream, returning the parsed [Entry],
|
||||||
// one if nothing was parsed, and a `bool` representing whether or not there is any further data
|
// or an empty one if nothing was parsed, and a `bool` representing whether or not there is any
|
||||||
// available to parse.
|
// further data available to parse.
|
||||||
//
|
//
|
||||||
// Note that the `parser` does not guarantee any parse order for key-value pairs extracted from the
|
// Note that the `parser` does not guarantee any parse order for key-value pairs extracted from the
|
||||||
// `parser` stream.
|
// `parser` stream.
|
||||||
|
@ -95,6 +95,27 @@ type Parser struct {
|
||||||
isEnd bool
|
isEnd bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Attempts to write `entries` to `writer`, returning any error that occured during writing.
|
||||||
|
func Write(writer io.Writer, entries []Entry) error {
|
||||||
|
// TODO: Not happy with this solution to writing files, needs more revision.
|
||||||
|
var section = ""
|
||||||
|
|
||||||
|
for _, entry := range entries {
|
||||||
|
if entry.Section != section {
|
||||||
|
section = entry.Section
|
||||||
|
|
||||||
|
writer.Write([]byte{'['})
|
||||||
|
writer.Write([]byte(entry.Section))
|
||||||
|
writer.Write([]byte{']', '\n'})
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.Write([]byte(entry.Key))
|
||||||
|
writer.Write([]byte{' ', '=', ' '})
|
||||||
|
writer.Write([]byte(entry.Value))
|
||||||
|
writer.Write([]byte{'\n'})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Returns a string with the the outer-most quotes surrounding `s` trimmed, should they exist.
|
// Returns a string with the the outer-most quotes surrounding `s` trimmed, should they exist.
|
||||||
func unquote(s string) string {
|
func unquote(s string) string {
|
||||||
var sLen = len(s)
|
var sLen = len(s)
|
||||||
|
|
Loading…
Reference in New Issue