设为首页 收藏本站
查看: 668|回复: 0

[经验分享] iniutils for Golang

[复制链接]

尚未签到

发表于 2018-9-20 07:11:42 | 显示全部楼层 |阅读模式
// Copyright 2017 ecofast(无尽愿). All rights reserved.  
// Use of this source code is governed by a BSD-style license.
  

  
// Package iniutils was translated from TMemIniFile in Delphi(2007) RTL,
  
// which loads an entire INI file into memory
  
// and allows all operations to be performed on the memory image.
  
// The image can then be written out to the disk file.
  
package iniutils
  

  
import (
  "bufio"
  "bytes"
  "fmt"
  "os"
  "strconv"
  "strings"
  

  "github.com/ecofast/sysutils"
  
)
  

  
type IniFile struct {
  fileName      string
  caseSensitive bool
  sections      map[string][]string
  
}
  

  
func NewIniFile(filename string, casesensitive bool) *IniFile {
  ini := &IniFile{
  fileName:      filename,
  caseSensitive: casesensitive,
  sections:      make(map[string][]string),
  }
  ini.loadValues()
  return ini
  
}
  

  
func (ini *IniFile) FileName() string {
  return ini.fileName
  
}
  

  
func (ini *IniFile) CaseSensitive() bool {
  return ini.caseSensitive
  
}
  

  
func (ini *IniFile) String() string {
  var buf bytes.Buffer
  for sec, lst := range ini.sections {
  buf.WriteString(fmt.Sprintf("[%s]\n", sec))
  for _, s := range lst {
  buf.WriteString(fmt.Sprintf("%s\n", s))
  }
  buf.WriteString("\n")
  }
  return buf.String()
  
}
  

  
func (ini *IniFile) getRealValue(s string) string {
  if !ini.caseSensitive {
  return strings.ToLower(s)
  }
  return s
  
}
  

  
func (ini *IniFile) loadValues() {
  if !sysutils.FileExists(ini.fileName) {
  return
  }
  

  file, err := os.Open(ini.fileName)
  if err != nil {
  return
  }
  defer file.Close()
  

  section := ""
  scanner := bufio.NewScanner(file)
  for scanner.Scan() {
  s := scanner.Text()
  s = strings.TrimSpace(s)
  s = ini.getRealValue(s)
  if s != "" && s[0] != ';' {
  if s[0] == '[' && s[len(s)-1] == ']' {
  s = s[1 : len(s)-1]
  section = s
  } else {
  if section != "" {
  if pos := strings.Index(s, "="); pos > 0 {
  if sl, ok := ini.sections[section]; ok {
  ini.sections[section] = append(sl, s)
  } else {
  ini.sections[section] = []string{s}
  }
  } else {

  // ingore invalid>  //
  }
  }
  }
  }
  }
  
}
  

  
func (ini *IniFile) flush() {
  file, err := os.Create(ini.fileName)
  sysutils.CheckError(err)
  defer file.Close()
  

  fw := bufio.NewWriter(file)
  for sec, lst := range ini.sections {
  _, err = fw.WriteString(fmt.Sprintf("[%s]\n", sec))
  sysutils.CheckError(err)
  

  for _, s := range lst {
  _, err = fw.WriteString(fmt.Sprintf("%s\n", s))
  sysutils.CheckError(err)
  }
  

  _, err = fw.WriteString("\n")
  sysutils.CheckError(err)
  }
  fw.Flush()
  
}
  

  
func (ini *IniFile) SectionExists(section string) bool {
  sec := ini.getRealValue(section)
  if _, ok := ini.sections[sec]; ok {
  return true
  }
  return false
  
}
  

  
func (ini *IniFile) ReadSections() []string {
  var ss []string
  for sec, _ := range ini.sections {
  ss = append(ss, sec)
  }
  return ss
  
}
  

  
func (ini *IniFile) EraseSection(section string) {
  sec := ini.getRealValue(section)
  delete(ini.sections, sec)
  
}
  

  
func (ini *IniFile) ReadSectionIdents(section string) []string {
  var ss []string
  sec := ini.getRealValue(section)
  if sl, ok := ini.sections[sec]; ok {
  for _, s := range sl {
  if pos := strings.Index(s, "="); pos > 0 {
  ss = append(ss, s[0:pos])
  }
  }
  }
  return ss
  
}
  

  
func (ini *IniFile) ReadSectionValues(section string) []string {
  var ss []string
  sec := ini.getRealValue(section)
  if sl, ok := ini.sections[sec]; ok {
  for _, s := range sl {
  ss = append(ss, s)
  }
  }
  return ss
  
}
  


  
func (ini *IniFile) DeleteIdent(section,>  sec := ini.getRealValue(section)
  id := ini.getRealValue(ident)
  if sl, ok := ini.sections[sec]; ok {
  for i := 0; i < len(sl); i++ {
  s := sl
  if pos := strings.Index(s, "="); pos > 0 {

  if s[0:pos] ==>  var ss []string
  for j := 0; j < i; j++ {
  ss = append(ss, sl[j])
  }
  for j := i + 1; j < len(sl); j++ {
  ss = append(ss, sl[j])
  }
  ini.sections[sec] = ss
  return
  }
  }
  }
  }
  
}
  


  
func (ini *IniFile)>  sec := ini.getRealValue(section)
  id := ini.getRealValue(ident)
  if sl, ok := ini.sections[sec]; ok {
  for _, s := range sl {
  if pos := strings.Index(s, "="); pos > 0 {

  if s[0:pos] ==>  return true
  }
  }
  }
  }
  return false
  
}
  


  
func (ini *IniFile) ReadString(section,>  sec := ini.getRealValue(section)
  id := ini.getRealValue(ident)
  if sl, ok := ini.sections[sec]; ok {
  for _, s := range sl {
  if pos := strings.Index(s, "="); pos > 0 {

  if s[0:pos] ==>  return s[pos+1:]
  }
  }
  }
  }
  return defaultValue
  
}
  


  
func (ini *IniFile) WriteString(section,>  sec := ini.getRealValue(section)
  id := ini.getRealValue(ident)
  if sl, ok := ini.sections[sec]; ok {
  for i := 0; i < len(sl); i++ {
  s := sl
  if pos := strings.Index(s, "="); pos > 0 {

  if s[0:pos] ==>  var ss []string
  for j := 0; j < i; j++ {
  ss = append(ss, sl[j])
  }

  ss = append(ss,>  for j := i + 1; j < len(sl); j++ {
  ss = append(ss, sl[j])
  }
  ini.sections[sec] = ss
  return
  }
  }
  }

  ini.sections[sec] = append(sl,>  } else {
  ini.sections[sec] = []string{ident + "=" + value}
  }
  
}
  


  
func (ini *IniFile) ReadInt(section,>
  s := ini.ReadString(section,>  if ret, err := strconv.Atoi(s); err == nil {
  return ret
  } else {
  return defaultValue
  }
  
}
  


  
func (ini *IniFile) WriteInt(section,>
  ini.WriteString(section,>  
}
  


  
func (ini *IniFile) ReadBool(section,>
  s := ini.ReadString(section,>  return sysutils.StrToBool(s)
  
}
  


  
func (ini *IniFile) WriteBool(section,>
  ini.WriteString(section,>  
}
  


  
func (ini *IniFile) ReadFloat(section,>
  s := ini.ReadString(section,>  if s != "" {
  if ret, err := strconv.ParseFloat(s, 64); err == nil {
  return ret
  }
  }
  return defaultValue
  
}
  


  
func (ini *IniFile) WriteFloat(section,>
  ini.WriteString(section,>  
}
  

  
func (ini *IniFile) Close() {
  ini.flush()
  
}
  

  
func (ini *IniFile) Clear() {
  ini.sections = make(map[string][]string)
  
}
  


  
func IniReadString(fileName, section,>  inifile := NewIniFile(fileName, false)
  defer inifile.Close()

  return inifile.ReadString(section,>  
}
  


  
func IniWriteString(fileName, section,>  inifile := NewIniFile(fileName, false)
  defer inifile.Close()

  inifile.WriteString(section,>  
}
  


  
func IniReadInt(fileName, section,>  inifile := NewIniFile(fileName, false)
  defer inifile.Close()

  return inifile.ReadInt(section,>  
}
  


  
func IniWriteInt(fileName, section,>  inifile := NewIniFile(fileName, false)
  defer inifile.Close()

  inifile.WriteInt(section,>  
}
  

  
func IniSectionExists(fileName, section string) bool {
  inifile := NewIniFile(fileName, false)
  defer inifile.Close()
  return inifile.SectionExists(section)
  
}
  

  
func IniReadSectionIdents(fileName, section string) []string {
  inifile := NewIniFile(fileName, false)
  defer inifile.Close()
  return inifile.ReadSectionIdents(section)
  
}
  

  
func IniReadSections(fileName string) []string {
  inifile := NewIniFile(fileName, false)
  defer inifile.Close()
  return inifile.ReadSections()
  
}
  

  
func IniReadSectionValues(fileName, section string) []string {
  inifile := NewIniFile(fileName, false)
  defer inifile.Close()
  return inifile.ReadSectionValues(section)
  
}
  

  
func IniEraseSection(fileName, section string) {
  inifile := NewIniFile(fileName, false)
  defer inifile.Close()
  inifile.EraseSection(section)
  
}
  


  
func IniIdentExists(fileName, section,>  inifile := NewIniFile(fileName, false)
  defer inifile.Close()

  return inifile.IdentExists(section,>  
}
  


  
func IniDeleteIdent(fileName, section,>  inifile := NewIniFile(fileName, false)
  defer inifile.Close()

  inifile.DeleteIdent(section,>  
}
  




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-597396-1-1.html 上篇帖子: Golang学习 - unicode/utf8 包 下篇帖子: golang中发送http请求的几种常见情况
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表