ran gofmt
This commit is contained in:
parent
5817e58bf8
commit
4cea74d15d
5 changed files with 189 additions and 181 deletions
64
read.go
64
read.go
|
@ -11,13 +11,13 @@ import (
|
|||
// ReadConfigFile reads a file and returns a new configuration representation.
|
||||
// This representation can be queried with GetString, etc.
|
||||
func ReadConfigFile(fname string) (c *ConfigFile, err os.Error) {
|
||||
var file *os.File;
|
||||
var file *os.File
|
||||
|
||||
if file, err = os.Open(fname, os.O_RDONLY, 0); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c = NewConfigFile();
|
||||
c = NewConfigFile()
|
||||
if err = c.Read(file); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -31,12 +31,12 @@ func ReadConfigFile(fname string) (c *ConfigFile, err os.Error) {
|
|||
|
||||
func ReadConfigBytes(conf []byte) (c *ConfigFile, err os.Error) {
|
||||
buf := bytes.NewBuffer(conf)
|
||||
|
||||
c = NewConfigFile();
|
||||
|
||||
c = NewConfigFile()
|
||||
if err = c.Read(buf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
return c, err
|
||||
}
|
||||
|
||||
|
@ -44,61 +44,61 @@ func ReadConfigBytes(conf []byte) (c *ConfigFile, err os.Error) {
|
|||
// representation can be queried with GetString, etc.
|
||||
func (c *ConfigFile) Read(reader io.Reader) (err os.Error) {
|
||||
buf := bufio.NewReader(reader)
|
||||
|
||||
var section, option string;
|
||||
|
||||
var section, option string
|
||||
section = "default"
|
||||
for {
|
||||
l, buferr := buf.ReadString('\n'); // parse line-by-line
|
||||
l, buferr := buf.ReadString('\n') // parse line-by-line
|
||||
if buferr != nil && buferr != os.EOF {
|
||||
return err
|
||||
}
|
||||
|
||||
l = strings.TrimSpace(l);
|
||||
|
||||
l = strings.TrimSpace(l)
|
||||
// switch written for readability (not performance)
|
||||
switch {
|
||||
case len(l) == 0: // empty line
|
||||
case len(l) == 0: // empty line
|
||||
continue
|
||||
|
||||
case l[0] == '#': // comment
|
||||
case l[0] == '#': // comment
|
||||
continue
|
||||
|
||||
case l[0] == ';': // comment
|
||||
case l[0] == ';': // comment
|
||||
continue
|
||||
|
||||
case len(l) >= 3 && strings.ToLower(l[0:3]) == "rem": // comment (for windows users)
|
||||
case len(l) >= 3 && strings.ToLower(l[0:3]) == "rem": // comment (for windows users)
|
||||
continue
|
||||
|
||||
case l[0] == '[' && l[len(l)-1] == ']': // new section
|
||||
option = ""; // reset multi-line value
|
||||
section = strings.TrimSpace(l[1 : len(l)-1]);
|
||||
c.AddSection(section);
|
||||
case l[0] == '[' && l[len(l)-1] == ']': // new section
|
||||
option = "" // reset multi-line value
|
||||
section = strings.TrimSpace(l[1 : len(l)-1])
|
||||
c.AddSection(section)
|
||||
|
||||
case section == "": // not new section and no section defined so far
|
||||
case section == "": // not new section and no section defined so far
|
||||
return ReadError{BlankSection, l}
|
||||
|
||||
default: // other alternatives
|
||||
i := firstIndex(l, []byte{'=', ':'});
|
||||
default: // other alternatives
|
||||
i := firstIndex(l, []byte{'=', ':'})
|
||||
switch {
|
||||
case i > 0: // option and value
|
||||
i := firstIndex(l, []byte{'=', ':'});
|
||||
option = strings.TrimSpace(l[0:i]);
|
||||
value := strings.TrimSpace(stripComments(l[i+1:]));
|
||||
c.AddOption(section, option, value);
|
||||
case i > 0: // option and value
|
||||
i := firstIndex(l, []byte{'=', ':'})
|
||||
option = strings.TrimSpace(l[0:i])
|
||||
value := strings.TrimSpace(stripComments(l[i+1:]))
|
||||
c.AddOption(section, option, value)
|
||||
|
||||
case section != "" && option != "": // continuation of multi-line value
|
||||
prev, _ := c.GetRawString(section, option);
|
||||
value := strings.TrimSpace(stripComments(l));
|
||||
c.AddOption(section, option, prev+"\n"+value);
|
||||
case section != "" && option != "": // continuation of multi-line value
|
||||
prev, _ := c.GetRawString(section, option)
|
||||
value := strings.TrimSpace(stripComments(l))
|
||||
c.AddOption(section, option, prev+"\n"+value)
|
||||
|
||||
default:
|
||||
return ReadError{CouldNotParse, l}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Reached end of file
|
||||
if buferr == os.EOF {
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil;
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue