You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
3.9 KiB
Go

package starainrt
import (
"bufio"
"errors"
"io"
"io/ioutil"
"os"
"regexp"
"strings"
)
func ReWriteFileByKV(filepath, rem, conn, ret string, kv map[string]string) bool {
var outputstr string
var ataru bool = false
if (!Exists(filepath)) || (!IsFile(filepath)) {
return false
}
fso, err := os.Open(filepath)
if err != nil {
return false
}
rempar := regexp.MustCompile("^" + rem)
buf := bufio.NewReader(fso)
for {
bytxt, _, eof := buf.ReadLine()
if eof == io.EOF {
break
}
txt := strings.TrimSpace(string(bytxt))
if rempar.Match([]byte(txt)) {
outputstr += txt + ret
continue
}
for k, v := range kv {
keypar := regexp.MustCompile("^" + k)
if keypar.Match([]byte(txt)) {
ataru = true
outputstr += k + conn + v + ret
delete(kv, k)
}
}
if ataru {
ataru = false
continue
} else {
outputstr += txt + ret
}
}
fso.Close()
if len(kv) != 0 {
for k, v := range kv {
outputstr += k + conn + v + ret
delete(kv, k)
}
}
err = ioutil.WriteFile(filepath, []byte(outputstr), 0755)
if err != nil {
return false
}
return true
}
func GetINIValue(filepath, rem, conn string, kv []string) (bool, map[string]string) {
outputstr := make(map[string]string)
if (!Exists(filepath)) || (!IsFile(filepath)) {
return false, outputstr
}
fso, err := os.Open(filepath)
if err != nil {
return false, outputstr
}
rempar := regexp.MustCompile("^" + rem)
buf := bufio.NewReader(fso)
for {
bytxt, _, eof := buf.ReadLine()
if eof == io.EOF {
break
}
txt := strings.TrimSpace(string(bytxt))
if rempar.Match([]byte(txt)) {
continue
}
var i int = 0
var v2 string
for k, v := range kv {
if v == "" {
continue
}
v2 = ""
for _, b := range []byte(v) {
switch string(b) {
case ".":
v2 += "\\."
case "\\":
v2 += "\\"
case "-":
v2 += "\\-"
case "(":
v2 += "\\("
case ")":
v2 += "\\)"
case "{":
v2 += "\\{"
case "}":
v2 += "\\}"
case "[":
v2 += "\\["
case "]":
v2 += "\\]"
case "$":
v2 += "\\]$"
case "^":
v2 += "\\^"
default:
v2 += string(b)
}
}
keypar := regexp.MustCompile("^" + v2 + ".*?" + conn + "(.*?)" + "(" + rem + "|$)")
if keypar.Match([]byte(txt)) {
i++
kekka := keypar.FindSubmatch([]byte(txt))
outputstr[v] = string(kekka[1])
kv[k] = ""
}
}
if i != len(kv) {
for _, v := range kv {
if v != "" {
outputstr[v] = ""
}
}
}
}
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
}