Skip to content

Instantly share code, notes, and snippets.

@Fohlen
Created March 18, 2020 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fohlen/315bf85fd397f54448182f56a016c2ac to your computer and use it in GitHub Desktop.
Save Fohlen/315bf85fd397f54448182f56a016c2ac to your computer and use it in GitHub Desktop.
Read relevant information from a cfg file
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