From 81efabb6cadbf75992ba162e15e6663749c5d815 Mon Sep 17 00:00:00 2001 From: ren yuze Date: Thu, 11 Jul 2019 11:01:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dini=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E6=B3=A8=E9=87=8A=E7=9A=84=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ini.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++- starainrt.go | 11 +++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/ini.go b/ini.go index acbaee5..e2ca1f7 100644 --- a/ini.go +++ b/ini.go @@ -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 +} diff --git a/starainrt.go b/starainrt.go index 2bd160c..a7670c6 100644 --- a/starainrt.go +++ b/starainrt.go @@ -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