```
package ini
import (
"bufio"
"bytes"
"io"
"log"
"os"
"strings"
"sync"
)
var (
DEFAULT_SECTION string
COMMENT []byte
SEPARATOR []byte
)
func init() {
DEFAULT_SECTION = "default"
COMMENT = []byte{'#'}
SEPARATOR = []byte{'='}
}
type IniEntry struct {
value interface{}
}
type IniConfig struct {
filename string
section map[string]*IniSection
sync.RWMutex
}
func NewIniConfig(path string) *IniConfig {
config := &IniConfig{path, make(map[string]*IniSection), sync.RWMutex{}}
config.section[DEFAULT_SECTION] = NewIniSection()
return config
}
func (c *IniConfig) Parse() error {
file, err := os.Open("D:\\GoPoject\\initype\\src\\ini\\gconfig.ini")
if err != nil {
return err
}
c.Lock()
defer c.Unlock()
defer file.Close()
buf := bufio.NewReader(file)
section := DEFAULT_SECTION
var bufRead int
for {
line, _, err := buf.ReadLine()
bufRead = bufRead + len(line)
if err == io.EOF {
break
}
if bytes.Equal(line, []byte("")) {
continue
}
line = bytes.TrimSpace(line)
if bytes.HasPrefix(line, COMMENT) {
continue
}
if bytes.HasPrefix(line, []byte("[")) && bytes.HasSuffix(line, []byte("]")) {
section = string(line[1 : len(line)-1])
section = strings.ToLower(section)
if _, ok := c.section[section]; !ok {
c.section[section] = NewIniSection()
}
} else {
pair := bytes.SplitN(line, SEPARATOR, 2)
key := pair[0]
val := pair[1]
if _, ok := c.section[section]; !ok {
c.section[section] = NewIniSection()
}
log.Println(key, val)
c.section[section].addEntry(string(key), string(val))
}
}
return nil
}
func (c *IniConfig) RemoveSection(key string) error {
return nil
}
func (c *IniConfig) GetString(k string) string {
s := strings.Split(k, ":")
var sec string
var key string
if len(s) == 1 {
sec = DEFAULT_SECTION
key = s[0]
log.Println(sec, key)
} else {
sec = s[0]
key = s[1]
log.Println(sec, key)
}
if v, ok := c.section[sec].getEntry(key).(string); ok {
return v
}
return ""
}
```
找的代码 一直报错不知道怎么解决
更多评论