1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
/*
* Copyright (C) 2017 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: jouyouyun <jouyouwen717@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package keyfile
import (
"bytes"
"strconv"
)
func stringEscape(in string) string {
var buf bytes.Buffer
for _, r := range in {
switch r {
case '\n':
buf.WriteString(`\n`)
case '\t':
buf.WriteString(`\t`)
case '\r':
buf.WriteString(`\r`)
case '\\':
buf.WriteString(`\\`)
default:
buf.WriteRune(r)
}
}
return buf.String()
}
func (f *KeyFile) SetString(section, key, value string) {
f.SetValue(section, key, stringEscape(value))
}
// TODO SetLocaleString
func (f *KeyFile) SetBool(section, key string, value bool) {
f.SetValue(section, key, strconv.FormatBool(value))
}
func (f *KeyFile) SetInt(section, key string, value int) {
f.SetValue(section, key, strconv.Itoa(value))
}
func (f *KeyFile) SetInt64(section, key string, value int64) {
f.SetValue(section, key, strconv.FormatInt(value, 10))
}
func (f *KeyFile) SetUint64(section, key string, value uint64) {
f.SetValue(section, key, strconv.FormatUint(value, 10))
}
func (f *KeyFile) SetFloat64(section, key string, value float64) {
f.SetValue(section, key, strconv.FormatFloat(value, 'g', -1, 64))
}
func (f *KeyFile) SetStringList(section, key string, values []string) {
var buf bytes.Buffer
for _, val := range values {
for _, r := range val {
switch r {
case ' ':
buf.WriteString(`\s`)
case '\n':
buf.WriteString(`\n`)
case '\t':
buf.WriteString(`\t`)
case '\r':
buf.WriteString(`\r`)
case '\\':
buf.WriteString(`\\`)
case rune(f.ListSeparator):
buf.WriteByte('\\')
buf.WriteRune(r)
default:
buf.WriteRune(r)
}
}
buf.WriteByte(f.ListSeparator)
}
f.SetValue(section, key, buf.String())
}
func (f *KeyFile) SetBoolList(section, key string, values []bool) {
var buf bytes.Buffer
for _, val := range values {
buf.WriteString(strconv.FormatBool(val))
buf.WriteByte(f.ListSeparator)
}
f.SetValue(section, key, buf.String())
}
func (f *KeyFile) SetIntList(section, key string, values []int) {
var buf bytes.Buffer
for _, val := range values {
buf.WriteString(strconv.Itoa(val))
buf.WriteByte(f.ListSeparator)
}
f.SetValue(section, key, buf.String())
}
func (f *KeyFile) SetFloat64List(section, key string, values []float64) {
var buf bytes.Buffer
for _, val := range values {
buf.WriteString(strconv.FormatFloat(val, 'g', -1, 64))
buf.WriteByte(f.ListSeparator)
}
f.SetValue(section, key, buf.String())
}
|