Created
March 18, 2020 15:12
-
-
Save Fohlen/315bf85fd397f54448182f56a016c2ac to your computer and use it in GitHub Desktop.
Read relevant information from a cfg file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"strings" | |
) | |
type Resource struct { | |
property, path string | |
} | |
func ReadConfig(configPath string) ([]Resource, error) { | |
file, err := os.Open(configPath) | |
if err != nil { | |
return nil, err | |
} | |
defer file.Close() | |
// Maps the relation between ICOMMAND and the respective file path | |
properties := map[string]int{ | |
"texture": 2, | |
"mmodel": 1, | |
"mapsound": 1, | |
"skybox": 1, | |
"exec": 1, | |
"cloudlayer": 1, | |
} | |
resources := make([]Resource, 0) | |
scanner := bufio.NewScanner(file) | |
for scanner.Scan() { | |
line := scanner.Text() | |
fields := strings.Split(line, " ") | |
property := fields[0] | |
if index, ok := properties[property]; ok { | |
resources = append(resources, Resource{property, fields[index]}) | |
} | |
} | |
if err := scanner.Err(); err != nil { | |
return nil, err | |
} | |
return resources, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment