Fork me on GitHub
随笔 - 215  文章 - 13  trackbacks - 0
<2016年11月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910


专注即时通讯及网游服务端编程
------------------------------------
Openresty 官方模块
Openresty 标准模块(Opm)
Openresty 三方模块
------------------------------------
本博收藏大部分文章为转载,并在文章开头给出了原文出处,如有再转,敬请保留相关信息,这是大家对原创作者劳动成果的自觉尊重!!如为您带来不便,请于本博下留言,谢谢配合。

常用链接

留言簿(1)

随笔分类

随笔档案

相册

Awesome

Blog

Book

GitHub

Link

搜索

  •  

积分与排名

  • 积分 - 204430
  • 排名 - 126

最新评论

阅读排行榜

>> github: https://github.com/widuu/goini
conf.go
/**
 * Read the configuration file
 *
 * @copyright           (C) 2014  widuu
 * @lastmodify          2014-2-22
 * @website        
http://www.widuu.com
 *
 
*/


package goini

import (
    
"bufio"
    
"fmt"
    
"io"
    
"os"
    
"strings"
)

type Config 
struct {
    filepath 
string                         //your ini file path directory+file
    conflist []map[string]map[string]string //configuration information slice
}


//Create an empty configuration file
func SetConfig(filepath string*Config {
    c :
= new(Config)
    c.filepath 
= filepath

    
return c
}


//To obtain corresponding value of the key values
func (c *Config) GetValue(section, name stringstring {
    c.ReadList()
    conf :
= c.ReadList()
    
for _, v := range conf {
        
for key, value := range v {
            
if key == section {
                
return value[name]
            }

        }

    }

    
return "no value"
}


//Set the corresponding value of the key value, if not add, if there is a key change
func (c *Config) SetValue(section, key, value stringbool {
    c.ReadList()
    data :
= c.conflist
    var ok 
bool
    var index 
= make(map[int]bool)
    var conf 
= make(map[string]map[string]string)
    
for i, v := range data {
        _, ok 
= v[section]
        index[i] 
= ok
    }


    i, ok :
= func(m map[int]bool) (i int, v bool{
        
for i, v := range m {
            
if v == true {
                
return i, true
            }

        }

        
return 0false
    }
(index)

    
if ok {
        c.conflist[i][section][key] 
= value
        
return true
    }
 else {
        conf[section] 
= make(map[string]string)
        conf[section][key] 
= value
        c.conflist 
= append(c.conflist, conf)
        
return true
    }


    
return false
}


//Delete the corresponding key values
func (c *Config) DeleteValue(section, name stringbool {
    c.ReadList()
    data :
= c.conflist
    
for i, v := range data {
        
for key, _ := range v {
            
if key == section {
                delete(c.conflist[i][key], name)
                
return true
            }

        }

    }

    
return false
}


//List all the configuration file
func (c *Config) ReadList() []map[string]map[string]string {

    file, err :
= os.Open(c.filepath)
    
if err != nil {
        CheckErr(err)
    }

    defer file.Close()
    var data map[
string]map[string]string
    var section 
string
    buf :
= bufio.NewReader(file)
    
for {
        l, err :
= buf.ReadString('\n')
        line :
= strings.TrimSpace(l)
        
if err != nil {
            
if err != io.EOF {
                CheckErr(err)
            }

            
if len(line) == 0 {
                
break
            }

        }

        
switch {
        
case len(line) == 0:
        
case line[0== '[' && line[len(line)-1== ']':
            section 
= strings.TrimSpace(line[1 : len(line)-1])
            data 
= make(map[string]map[string]string)
            data[section] 
= make(map[string]string)
        
default:
            i :
= strings.IndexAny(line, "=")
            value :
= strings.TrimSpace(line[i+1 : len(line)])
            data[section][strings.TrimSpace(line[
0:i])] = value
            
if c.uniquappend(section) == true {
                c.conflist 
= append(c.conflist, data)
            }

        }


    }


    
return c.conflist
}


func CheckErr(err error) 
string {
    
if err != nil {
        
return fmt.Sprintf("Error is :'%s'", err.Error())
    }

    
return "Notfound this error"
}


//Ban repeated appended to the slice method
func (c *Config) uniquappend(conf stringbool {
    
for _, v := range c.conflist {
        
for k, _ := range v {
            
if k == conf {
                
return false
            }

        }

    }

    
return true
}


posted on 2016-11-24 16:13 思月行云 阅读(894) 评论(0)  编辑 收藏 引用 所属分类: Golang

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理