go語言解析INI檔案

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
package main  import ( "bufio" "bytes" "io" "os" "unicode" )  const ( stat_none = iota stat_group stat_key stat_value stat_comment )  type Attr struct { Name    string Value   string Comment string next    *Attr }  type Element struct { Element string Attr    *Attr next    *Element }  type Decoder struct { state int b     byte t     bytes.Buffer r     io.ByteReader err   error m     *Element n     string }  func (d *Decoder) getAttr(m *Element, e string) string { for n := m.Attr; nil != n; n = n.next { if e == n.Name { return n.Value } } return "" }  func (d *Decoder) GetElement(e string, a string) string { for n := d.m; nil != n; n = n.next { if e == n.Element { return d.getAttr(n, a) } } return "" }  func (d *Decoder) newAttrNextComment(value string) { d.m.Attr.Comment = value println(value) }  func (d *Decoder) newAttrNext(name string, value string) { attr := new(Attr) attr.Name = name attr.Value = value if nil == d.m.Attr { attr.next = nil } else { attr.next = d.m.Attr } d.m.Attr = attr }  func (d *Decoder) newElement(name string) { element := new(Element) element.Element = name element.Attr = nil if nil == d.m { element.next = nil } else { element.next = d.m } d.m = element }  func (d *Decoder) switchToMap() { for { d.b, d.err = d.r.ReadByte() if d.err != nil { break } switch d.state { case stat_none: if d.b == '[' { d.state = stat_group } else if d.b == ';' { d.state = stat_comment } else if !unicode.IsSpace(rune(d.b)) { d.state = stat_key d.t.WriteByte(byte(d.b)) } break case stat_group: if d.b == ']' { d.state = stat_none d.newElement(d.t.String()) d.t.Reset() } else if !unicode.IsSpace(rune(d.b)) { d.t.WriteByte(byte(d.b)) } break case stat_key: if d.b == '=' { d.state = stat_value d.n = d.t.String() d.t.Reset() } else if !unicode.IsSpace(rune(d.b)) { d.t.WriteByte(byte(d.b)) } break case stat_value: if !unicode.IsSpace(rune(d.b)) { d.t.WriteByte(byte(d.b)) } else { d.state = stat_none d.newAttrNext(d.n, d.t.String()) d.t.Reset() } break case stat_comment: if !unicode.IsSpace(rune(d.b)) { d.t.WriteByte(byte(d.b)) } else { d.state = stat_none d.newAttrNextComment(d.t.String()) d.t.Reset() } break default: d.state = stat_none break } } }  func (d *Decoder) switchToReader(r io.Reader) { if rb, ok := r.(io.ByteReader); ok { d.r = rb } else { d.r = bufio.NewReader(r) } d.switchToMap() }  func NewDecoder(r io.Reader) *Decoder { d := &Decoder{} d.switchToReader(r) return d }  func main() { xmlFile, err := os.Open("hh.ini") if nil != err { return } defer xmlFile.Close() d := NewDecoder(xmlFile) println(d.GetElement("aaaa", "ac")) } 

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.