diff --git a/decoder.go b/decoder.go new file mode 100644 index 0000000..4cdff7a --- /dev/null +++ b/decoder.go @@ -0,0 +1,49 @@ +package ini + +import ( + "bytes" + "io" + "strings" + + "asciigoat.org/core" + "asciigoat.org/ini/parser" +) + +// Decoder ... +type Decoder struct { + io.Closer + + p *parser.Parser +} + +// Decode ... +func (dec *Decoder) Decode() error { + defer dec.Close() + + return dec.p.Run() +} + +// NewDecoder creates a Decoder over the provided [io.Reader] +func NewDecoder(r io.Reader) *Decoder { + rc := core.NewReadCloser(r) + switch { + case rc == nil: + return nil + default: + dec := &Decoder{ + p: parser.NewParser(rc), + Closer: rc, + } + return dec + } +} + +// NewDecoderBytes creates a Decoder over a provided bytes array +func NewDecoderBytes(b []byte) *Decoder { + return NewDecoder(bytes.NewReader(b)) +} + +// NewDecoderString creates a Decoder over a provided string of data +func NewDecoderString(s string) *Decoder { + return NewDecoder(strings.NewReader(s)) +}