|
|
@@ -0,0 +1,80 @@
|
|
|
+// package tuf -- Text Universal Format
|
|
|
+package tuf
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "strings"
|
|
|
+
|
|
|
+ "gitp78su.ipnodns.ru/svi/kern/v4/lev0/defs/def_err"
|
|
|
+ "gitp78su.ipnodns.ru/svi/kern/v4/lev0/defs/def_spec"
|
|
|
+ "gitp78su.ipnodns.ru/svi/kern/v4/lev0/helpers"
|
|
|
+ "gitp78su.ipnodns.ru/svi/kern/v4/lev0/result"
|
|
|
+)
|
|
|
+
|
|
|
+// StrToTuf -- конвертация строки в ITuf.
|
|
|
+func StrToTuf(key, val def_spec.ATxt) def_spec.ATxt {
|
|
|
+ keyCheck(key)
|
|
|
+
|
|
|
+ txtRes := "ATxt;" + string(key) + ";" + replace(val) + "\n"
|
|
|
+ return def_spec.ATxt(txtRes)
|
|
|
+}
|
|
|
+
|
|
|
+// BoolToTuf -- конвертация bool в ITuf.
|
|
|
+func BoolToTuf(key def_spec.ATxt, val def_spec.Bool) def_spec.ATxt {
|
|
|
+ keyCheck(key)
|
|
|
+ txtRes := "Bool;" + string(key) + ";" + fmt.Sprint(val) + "\n"
|
|
|
+ return def_spec.ATxt(txtRes)
|
|
|
+}
|
|
|
+
|
|
|
+// RecToTuf -- конвертация записи в TUF.
|
|
|
+func RecToTuf(type_, val def_spec.ATxt) def_spec.ATxt {
|
|
|
+ keyCheck(type_)
|
|
|
+ strRes := "Rec;" + string(type_) + ";" + replace(val) + "\n"
|
|
|
+ return def_spec.ATxt(strRes)
|
|
|
+}
|
|
|
+
|
|
|
+// RecFromTuf -- конвертация записи из TUF.
|
|
|
+func RecFromTuf(type_, val def_spec.ATxt) def_spec.IResult[def_spec.ATxt] {
|
|
|
+ keyCheck(type_)
|
|
|
+ lstField := strings.Split(string(val), ";")
|
|
|
+ if len(lstField) != 3 {
|
|
|
+ return result.NewErr[def_spec.ATxt](
|
|
|
+ def_err.Err("tuf.RecFromTuf(): valTuf len(%v)!=3", len(lstField)))
|
|
|
+ }
|
|
|
+ if lstField[0] != "Rec" {
|
|
|
+ return result.NewErr[def_spec.ATxt](
|
|
|
+ def_err.Err("tuf.RecFromTuf(): valTuf(%v) not record", lstField[0]))
|
|
|
+ }
|
|
|
+ if lstField[1] != string(type_) {
|
|
|
+ return result.NewErr[def_spec.ATxt](
|
|
|
+ def_err.Err("tuf.RecFromTuf(): valTuf(%v) not valid type(%v)", lstField[1], type_))
|
|
|
+ }
|
|
|
+ txtRes := restore(lstField[2])
|
|
|
+ return result.NewOk(txtRes)
|
|
|
+}
|
|
|
+
|
|
|
+// Проверяет ключ на допустимое значение.
|
|
|
+func keyCheck(key def_spec.ATxt) {
|
|
|
+ helpers.If(def_spec.Bool(strings.Contains(string(key), ";"))).Hassert("tuf.keyCheck(): key contains ';'")
|
|
|
+ helpers.If(def_spec.Bool(strings.Contains(string(key), "\n"))).Hassert("tuf.keyCheck(): key contains '\\n'")
|
|
|
+ helpers.If(def_spec.Bool(strings.Contains(string(key), "="))).Hassert("tuf.keyCheck(): key contains '='")
|
|
|
+}
|
|
|
+
|
|
|
+// Заменяет все запрещённые комбинации.
|
|
|
+func replace(val def_spec.ATxt) string {
|
|
|
+ strVal := strings.ReplaceAll(string(val), "\r", "")
|
|
|
+ strVal = strings.ReplaceAll(strVal, "\n", "`NL")
|
|
|
+ strVal = strings.ReplaceAll(strVal, "`", "`TL")
|
|
|
+ strVal = strings.ReplaceAll(strVal, "=", "`EQ")
|
|
|
+ strVal = strings.ReplaceAll(strVal, ";", "`CS")
|
|
|
+ return strVal
|
|
|
+}
|
|
|
+
|
|
|
+// Восстанавливает строку.
|
|
|
+func restore(val string) def_spec.ATxt {
|
|
|
+ strVal := strings.ReplaceAll(val, "`TL", "`")
|
|
|
+ strVal = strings.ReplaceAll(strVal, "`CS", ";")
|
|
|
+ strVal = strings.ReplaceAll(strVal, "`EQ", "=")
|
|
|
+ strVal = strings.ReplaceAll(strVal, "`NL", "\n")
|
|
|
+ return def_spec.ATxt(strVal)
|
|
|
+}
|