-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathini.go
132 lines (108 loc) · 3.27 KB
/
ini.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
package ini
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/shellucas/go-ini/config"
"github.com/shellucas/go-ini/enums/subsection"
"github.com/shellucas/go-ini/feature"
"github.com/shellucas/go-ini/utils"
)
var Files map[string]*file = make(map[string]*file)
type file struct {
feature.Section
Config config.Config
}
func CreateFile() file {
f := file{
Config: config.InitDefault(),
}
return f
}
func Load(path string, file file) *file {
currentDir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
filePath := utils.RegSplit(path, `[\\\/]`)
fileName := strings.TrimSuffix(filePath[len(filePath)-1], ".ini")
content, err := ioutil.ReadFile(fmt.Sprintf("%s/%s/%s.ini", currentDir, strings.Join(filePath[:len(filePath)-1], `/`), fileName))
if err != nil {
log.Fatal(err)
}
text := utils.RegSplit(string(content), "[\\n\\r]+")
iniFile := readFile(text, file)
iniFile.Name = fileName
Files[fileName] = iniFile
return iniFile
}
func readFile(content []string, iniFile file) *file {
switch iniFile.Config.GetSubSectionType() {
case subsection.Indented:
readIndentedSection(&iniFile.Section, content, 0, iniFile.Config)
break
case subsection.Seperated:
readSeperatedSection(&iniFile.Section, content, iniFile.Config)
break
}
return &iniFile
}
func readProperty(line string, config config.Config) feature.Property {
property, err := getProperty(line, config)
if err != nil {
log.Fatal(err)
}
return property
}
func readIndentedSection(currentSection *feature.Section, file []string, index int, config config.Config) int {
var parent *feature.Section
if currentSection.Parent == nil {
parent = currentSection
} else {
parent = currentSection.Parent
}
for len(file) > index {
line := file[index]
if isSection(line) {
var section *feature.Section
if checkSectionLineDepth(&line, currentSection, config.GetSubSectionType()) > 0 {
section = createSection(line, currentSection, config.GetSubSectionType())
index = readIndentedSection(section, file, index+1, config) - 1
} else if checkSectionLineDepth(&line, currentSection, config.GetSubSectionType()) < 0 {
return index
} else {
section = createSection(line, parent, config.GetSubSectionType())
currentSection = section
}
} else if isProperty(line, config) {
prefix := getFeaturePrefix(line, config.GetSubSectionType())
if currentSection.Prefix != prefix {
return index
}
property := readProperty(line, config)
currentSection.AddProperty(&property)
}
index++
}
return index
}
func readSeperatedSection(parent *feature.Section, file []string, config config.Config) {
var currentSection *feature.Section
currentSection = parent
for i := 0; i < len(file); i++ {
line := file[i]
if isSection(line) {
parents := utils.RegSplit(line, `\.`)
currentSection = parent
for _, x := range parents[:len(parents)-1] {
currentSection = currentSection.GetSection(strings.TrimPrefix(x, "["))
}
currentSection = createSection(line, currentSection, config.GetSubSectionType())
} else if isProperty(line, config) {
property := readProperty(line, config)
currentSection.AddProperty(&property)
}
}
}