write.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2013 Unknwon
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // 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, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package goconfig
  15. import (
  16. "bytes"
  17. "io"
  18. "os"
  19. "strings"
  20. )
  21. // Write spaces around "=" to look better.
  22. var PrettyFormat = true
  23. // SaveConfigData writes configuration to a writer
  24. func SaveConfigData(c *ConfigFile, out io.Writer) (err error) {
  25. equalSign := "="
  26. if PrettyFormat {
  27. equalSign = " = "
  28. }
  29. buf := bytes.NewBuffer(nil)
  30. for _, section := range c.sectionList {
  31. // Write section comments.
  32. if len(c.GetSectionComments(section)) > 0 {
  33. if _, err = buf.WriteString(c.GetSectionComments(section) + LineBreak); err != nil {
  34. return err
  35. }
  36. }
  37. if section != DEFAULT_SECTION {
  38. // Write section name.
  39. if _, err = buf.WriteString("[" + section + "]" + LineBreak); err != nil {
  40. return err
  41. }
  42. }
  43. for _, key := range c.keyList[section] {
  44. if key != " " {
  45. // Write key comments.
  46. if len(c.GetKeyComments(section, key)) > 0 {
  47. if _, err = buf.WriteString(c.GetKeyComments(section, key) + LineBreak); err != nil {
  48. return err
  49. }
  50. }
  51. keyName := key
  52. // Check if it's auto increment.
  53. if keyName[0] == '#' {
  54. keyName = "-"
  55. }
  56. //[SWH|+]:支持键名包含等号和冒号
  57. if strings.Contains(keyName, `=`) || strings.Contains(keyName, `:`) {
  58. if strings.Contains(keyName, "`") {
  59. if strings.Contains(keyName, `"`) {
  60. keyName = `"""` + keyName + `"""`
  61. } else {
  62. keyName = `"` + keyName + `"`
  63. }
  64. } else {
  65. keyName = "`" + keyName + "`"
  66. }
  67. }
  68. value := c.data[section][key]
  69. // In case key value contains "`" or "\"".
  70. if strings.Contains(value, "`") {
  71. if strings.Contains(value, `"`) {
  72. value = `"""` + value + `"""`
  73. } else {
  74. value = `"` + value + `"`
  75. }
  76. }
  77. // Write key and value.
  78. if _, err = buf.WriteString(keyName + equalSign + value + LineBreak); err != nil {
  79. return err
  80. }
  81. }
  82. }
  83. // Put a line between sections.
  84. if _, err = buf.WriteString(LineBreak); err != nil {
  85. return err
  86. }
  87. }
  88. if _, err := buf.WriteTo(out); err != nil {
  89. return err
  90. }
  91. return nil
  92. }
  93. // SaveConfigFile writes configuration file to local file system
  94. func SaveConfigFile(c *ConfigFile, filename string) (err error) {
  95. // Write configuration file by filename.
  96. var f *os.File
  97. if f, err = os.Create(filename); err != nil {
  98. return err
  99. }
  100. if err := SaveConfigData(c, f); err != nil {
  101. return err
  102. }
  103. return f.Close()
  104. }