forked from codescalersinternships/Alaa-inigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
137 lines (118 loc) · 2.81 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
"strings"
)
type Parser struct { //nested maps
nested_map map[string]map[string]string
}
func checkLine(line string) (string, error) {
if line[0] == '[' && line[len(line)-1] == ']' {
return "section", nil
} else if line[0] == ';' {
return "comment", nil
} else if line[0] == '\n' {
return "empty line", nil
} else if strings.Contains(line, "=") && strings.Count(line, "=") == 1 {
return "key line", nil
} else {
return "", errors.New("Syntax Error !")
}
}
func (parser *Parser) LoadFromFile(path string) (err error) {
content, err := ioutil.ReadFile(path)
msg := string(content)
if err != nil {
return err
}
parser.nested_map, err = Parse(msg)
return err
}
func (parser *Parser) LoadFromText(txt string) (err error) {
if len(txt) != 0 {
parser.nested_map, err = Parse(txt)
} else {
return err
}
return err
}
func Parse(txt string) (map[string]map[string]string, error) {
ini := make(map[string]map[string]string)
var head string
sectionHead := regexp.MustCompile(`^\[([^]]*)\]\s*$`)
keyValue := regexp.MustCompile(`^(\w+)\s*=\s*(.*?)\s*$`)
reader := bufio.NewReader(strings.NewReader(txt))
for {
line, _ := reader.ReadString('\n')
result := sectionHead.FindStringSubmatch(line)
if len(result) > 0 {
head = result[1]
ini[head] = make(map[string]string)
continue
}
result = keyValue.FindStringSubmatch(line)
if len(result) > 0 {
key, value := result[1], result[2]
ini[head][key] = value
continue
}
if line == "" {
break
}
}
return ini, nil
}
func (parser *Parser) GetSections() map[string]map[string]string {
return parser.nested_map
}
func (parser *Parser) GetSectionsName() []string {
names := []string{}
for name, _ := range parser.nested_map {
names = append(names, name)
}
return names
}
func (parser *Parser) Set(name string, key string, value string) map[string]string { //ToDo
section := parser.nested_map[name]
section[key] = value
fmt.Println(section)
return section
}
func (parser *Parser) GetKeys(sectionName string) ([]string, error) {
keys := []string{}
section := parser.nested_map[sectionName]
if section != nil {
for key, value := range section {
keys = append(keys, key+":"+value)
}
} else {
return nil, errors.New("No Section with that name !!")
}
return keys, nil
}
func (parser *Parser) Get(sectionName string, key string) string {
section := parser.nested_map[sectionName][key]
return section
}
func (parser *Parser) SaveToFile() (err error) {
file, err := os.Create("output.ini")
file.Close()
if err != nil {
log.Fatal(err)
}
data := ""
for section, keys := range parser.nested_map {
data += "[" + section + "]\n"
for key, value := range keys {
data += key + " = " + value + "\n"
}
}
file.WriteString(data)
return err
}