123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- package ini
- import (
- "bytes"
- "fmt"
- "io"
- "io/ioutil"
- "os"
- "regexp"
- "runtime"
- )
- const (
-
-
- DEFAULT_SECTION = "DEFAULT"
-
- _DEPTH_VALUES = 99
- _VERSION = "1.37.0"
- )
- func Version() string {
- return _VERSION
- }
- var (
-
-
-
- LineBreak = "\n"
-
- varPattern = regexp.MustCompile(`%\(([^\)]+)\)s`)
-
-
- PrettyFormat = true
-
- PrettyEqual = false
-
- DefaultHeader = false
-
- PrettySection = true
- )
- func init() {
- if runtime.GOOS == "windows" {
- LineBreak = "\r\n"
- }
- }
- func inSlice(str string, s []string) bool {
- for _, v := range s {
- if str == v {
- return true
- }
- }
- return false
- }
- type dataSource interface {
- ReadCloser() (io.ReadCloser, error)
- }
- type sourceFile struct {
- name string
- }
- func (s sourceFile) ReadCloser() (_ io.ReadCloser, err error) {
- return os.Open(s.name)
- }
- type sourceData struct {
- data []byte
- }
- func (s *sourceData) ReadCloser() (io.ReadCloser, error) {
- return ioutil.NopCloser(bytes.NewReader(s.data)), nil
- }
- type sourceReadCloser struct {
- reader io.ReadCloser
- }
- func (s *sourceReadCloser) ReadCloser() (io.ReadCloser, error) {
- return s.reader, nil
- }
- func parseDataSource(source interface{}) (dataSource, error) {
- switch s := source.(type) {
- case string:
- return sourceFile{s}, nil
- case []byte:
- return &sourceData{s}, nil
- case io.ReadCloser:
- return &sourceReadCloser{s}, nil
- default:
- return nil, fmt.Errorf("error parsing data source: unknown type '%s'", s)
- }
- }
- type LoadOptions struct {
-
- Loose bool
-
- Insensitive bool
-
- IgnoreContinuation bool
-
- IgnoreInlineComment bool
-
-
- AllowBooleanKeys bool
-
- AllowShadows bool
-
-
- AllowNestedValues bool
-
-
-
-
- AllowPythonMultilineValues bool
-
-
-
-
- SpaceBeforeInlineComment bool
-
-
- UnescapeValueDoubleQuotes bool
-
-
-
- UnescapeValueCommentSymbols bool
-
-
- UnparseableSections []string
- }
- func LoadSources(opts LoadOptions, source interface{}, others ...interface{}) (_ *File, err error) {
- sources := make([]dataSource, len(others)+1)
- sources[0], err = parseDataSource(source)
- if err != nil {
- return nil, err
- }
- for i := range others {
- sources[i+1], err = parseDataSource(others[i])
- if err != nil {
- return nil, err
- }
- }
- f := newFile(sources, opts)
- if err = f.Reload(); err != nil {
- return nil, err
- }
- return f, nil
- }
- func Load(source interface{}, others ...interface{}) (*File, error) {
- return LoadSources(LoadOptions{}, source, others...)
- }
- func LooseLoad(source interface{}, others ...interface{}) (*File, error) {
- return LoadSources(LoadOptions{Loose: true}, source, others...)
- }
- func InsensitiveLoad(source interface{}, others ...interface{}) (*File, error) {
- return LoadSources(LoadOptions{Insensitive: true}, source, others...)
- }
- func ShadowLoad(source interface{}, others ...interface{}) (*File, error) {
- return LoadSources(LoadOptions{AllowShadows: true}, source, others...)
- }
|