Skip to content

Commit

Permalink
Merge pull request #9 from Galaco/fix-mixed-newlines
Browse files Browse the repository at this point in the history
Fix mixed newlines
  • Loading branch information
Galaco authored Sep 20, 2019
2 parents adb1f77 + 6ec3007 commit 1a53bea
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
10 changes: 7 additions & 3 deletions examples/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"log"
"os"
"strings"
)

func main() {
Expand All @@ -15,7 +16,7 @@ func main() {

// Vmt creates KeyValues for all files in vmt dir
func Vmt() {
samplesDir := "./examples/vmt/"
samplesDir := "./vmt/"
fileInfos := getFilenames(samplesDir)
read(samplesDir, fileInfos, func(filename string, value *keyvalues.KeyValue) {
log.Println(value.Children())
Expand All @@ -24,7 +25,7 @@ func Vmt() {

// Vmf creates KeyValues for all files in vmf dir
func Vmf() {
samplesDir := "./examples/vmf/"
samplesDir := "./vmf/"
fileInfos := getFilenames(samplesDir)
read(samplesDir, fileInfos, func(filename string, value *keyvalues.KeyValue) {
log.Println(value.Children())
Expand All @@ -33,7 +34,7 @@ func Vmf() {

// GameInfo creates KeyValues for all files in gameinfo dir
func GameInfo() {
samplesDir := "./examples/gameinfo/"
samplesDir := "./gameinfo/"
fileInfos := getFilenames(samplesDir)
read(samplesDir, fileInfos, func(filename string, value *keyvalues.KeyValue) {
log.Println(value.Children())
Expand All @@ -42,6 +43,9 @@ func GameInfo() {

func read(basePath string, fileInfos []os.FileInfo, callback func(filename string, value *keyvalues.KeyValue)) {
for _, info := range fileInfos {
if strings.HasPrefix(info.Name(), ".") {
continue
}
f, err := os.Open(basePath + info.Name())
if err != nil {
log.Fatal(err)
Expand Down
31 changes: 23 additions & 8 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,40 @@ func readScope(reader *bufio.Reader, scope *KeyValue) *KeyValue {
}

// Read keyvalue & append to current scope
result := parseKV(line)
result.parent = scope
scope.value = append(scope.value, result)
results := parseKV(line)
for idx := range results {
results[idx].parent = scope
scope.value = append(scope.value, results[idx])
}
}

return scope
}

// parseKV reads a single line that should contain a KeyValue pair
func parseKV(line string) *KeyValue {
func parseKV(line string) (res []*KeyValue) {
prop := strings.Split(line, tokenSeparator)
// value also defined on this line
val := trim(strings.Replace(line, prop[0], "", -1))
vals := strings.Split(trim(strings.Replace(line, prop[0], "", -1)), "\r")

return &KeyValue{
res = append(res, &KeyValue{
key: trim(prop[0]),
valueType: getType(val),
value: append(make([]interface{}, 0), val),
valueType: getType(trim(vals[0])),
value: append(make([]interface{}, 0), trim(vals[0])),
})

// Hack to catch \r carriage returns
if len(vals) == 2 {
prop := strings.Split(trim(vals[1]), tokenSeparator)
val2 := trim(strings.Replace(vals[1], prop[0], "", -1))
res = append(res, &KeyValue{
key: strings.Replace(trim(prop[0]), "\"", "", -1),
valueType: getType(val2),
value: append(make([]interface{}, 0), val2),
})
}

return res
}

func isCharacterEscaped(value string, char string) bool {
Expand Down

0 comments on commit 1a53bea

Please sign in to comment.