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.
136 lines
2.4 KiB
Go
136 lines
2.4 KiB
Go
5 years ago
|
package starainrt
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"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 + "(.*)")
|
||
|
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
|
||
|
}
|