修复ini文件读取注释的错误

master v1.0.0
兔子 5 years ago
parent 1a8d46f61e
commit 81efabb6ca

@ -2,6 +2,7 @@ package starainrt
import (
"bufio"
"errors"
"io"
"io/ioutil"
"os"
@ -115,7 +116,7 @@ func GetINIValue(filepath, rem, conn string, kv []string) (bool, map[string]stri
v2 += string(b)
}
}
keypar := regexp.MustCompile("^" + v2 + ".*?" + conn + "(.*)")
keypar := regexp.MustCompile("^" + v2 + ".*?" + conn + "(.*?)" + "(" + rem + "|$)")
if keypar.Match([]byte(txt)) {
i++
kekka := keypar.FindSubmatch([]byte(txt))
@ -133,3 +134,64 @@ func GetINIValue(filepath, rem, conn string, kv []string) (bool, map[string]stri
}
return true, outputstr
}
func ReadINIConfig(filepath string) (map[string]map[string]string, error) {
var result map[string]map[string]string
result = make(map[string]map[string]string)
if !Exists(filepath) {
return result, errors.New("file not exists")
}
data, err := ioutil.ReadFile(filepath)
if err != nil {
return result, nil
}
strdata := string(data)
datas := strings.Split(strdata, "\n")
var inseg, nolabel bool = false, false
var segname string
jump:
if nolabel {
segname = "unnamed"
inseg = true
}
for _, v := range datas {
v = strings.TrimSpace(v)
if ok, _ := regexp.MatchString("^#", v); ok {
continue
}
segfind := regexp.MustCompile(`\[(.*)\]`)
if !inseg {
if ok, _ := regexp.MatchString(`(.*?)=(.*)`, v); ok {
nolabel = true
goto jump
}
if !segfind.MatchString(v) {
continue
} else {
tmp := segfind.FindStringSubmatch(v)
segname = tmp[1]
inseg = true
}
} else {
if segfind.MatchString(v) {
tmp := segfind.FindStringSubmatch(v)
segname = tmp[1]
inseg = true
}
}
if fn := strings.Index(v, "#"); fn >= 0 {
v = v[0:fn]
}
if ok, _ := regexp.MatchString(`(.*?)=(.*)`, v); ok {
regtmp := regexp.MustCompile(`(.*?)=(.*)`)
tmp := regtmp.FindStringSubmatch(v)
if result[segname] == nil {
result[segname] = make(map[string]string)
}
result[segname][strings.TrimSpace(tmp[1])] = strings.TrimSpace(tmp[2])
} else {
continue
}
}
return result, nil
}

@ -17,6 +17,7 @@ var DBRows *sql.Rows
var ShellRes, ShellErr string
var ShellExit bool
//Exits返回指定文件夹/文件是否存在
func Exists(filepath string) bool {
_, err := os.Stat(filepath)
if err != nil && os.IsNotExist(err) {
@ -24,6 +25,9 @@ func Exists(filepath string) bool {
}
return true
}
//IsFile返回给定文件地址是否是一个文件
//True为是一个文件,False为不是文件或路径无效
func IsFile(fpath string) bool {
s, err := os.Stat(fpath)
if err != nil {
@ -31,6 +35,9 @@ func IsFile(fpath string) bool {
}
return !s.IsDir()
}
//IsFolder返回给定文件地址是否是一个文件夹
//True为是一个文件夹,False为不是文件夹或路径无效
func IsFolder(fpath string) bool {
s, err := os.Stat(fpath)
if err != nil {
@ -39,15 +46,19 @@ func IsFolder(fpath string) bool {
return s.IsDir()
}
//CurlGet发起一个HTTP GET请求
func CurlGet(url string) (error, []byte) {
err, _, res, _, _ := Curl(url, "", HttpNul, HttpNul2, "GET")
return err, res
}
//CurlPost发起一个基于表单的HTTP Post请求
func CurlPost(url, postdata string) (error, []byte) {
err, _, res, _, _ := Curl(url, postdata, HttpNul, HttpNul2, "POST")
return err, res
}
//HttpNulReset将重置Header和Cookie为空
func HttpNulReset() {
var tmp map[string]string
HttpNul, HttpNul2 = tmp, tmp

Loading…
Cancel
Save