config.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2013, Cong Ding. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // author: Cong Ding <dinggnu@gmail.com>
  16. //
  17. package config
  18. import (
  19. "bufio"
  20. "os"
  21. "strings"
  22. )
  23. func Read(filename string) (map[string]string, error) {
  24. var res = map[string]string{}
  25. in, err := os.Open(filename)
  26. if err != nil {
  27. return res, err
  28. }
  29. scanner := bufio.NewScanner(in)
  30. line := ""
  31. for scanner.Scan() {
  32. if strings.HasPrefix(scanner.Text(), "//") {
  33. continue
  34. }
  35. if strings.HasPrefix(scanner.Text(), "#") {
  36. continue
  37. }
  38. line += scanner.Text()
  39. if strings.HasSuffix(line, "\\") {
  40. line = line[:len(line)-1]
  41. continue
  42. }
  43. sp := strings.SplitN(line, "=", 2)
  44. if len(sp) != 2 {
  45. continue
  46. }
  47. res[strings.TrimSpace(sp[0])] = strings.TrimSpace(sp[1])
  48. line = ""
  49. }
  50. in.Close()
  51. return res, nil
  52. }