diff --git a/ini.go b/ini.go index e7b0529..0875579 100644 --- a/ini.go +++ b/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 -// one if nothing was parsed, and a `bool` representing whether or not there is any further data -// available to parse. +// 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. // // Note that the `parser` does not guarantee any parse order for key-value pairs extracted from the // `parser` stream. @@ -95,6 +95,27 @@ type Parser struct { 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. func unquote(s string) string { var sLen = len(s)