feat(cpp/inih): add inih parser_1
This commit is contained in:
1
cpp/inih/READEME.md
Normal file
1
cpp/inih/READEME.md
Normal file
@@ -0,0 +1 @@
|
||||
brew install inih
|
||||
59
cpp/inih/_demo/inihdemo/ReaderDemo.go
Normal file
59
cpp/inih/_demo/inihdemo/ReaderDemo.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgoexamples/cpp/inih"
|
||||
)
|
||||
|
||||
/*
|
||||
expected output:
|
||||
section: owner name: name value: John Doe
|
||||
section: owner name: organization value: Acme Widgets Inc.
|
||||
section: database name: server value: 192.0.2.62
|
||||
section: database name: port value: 143
|
||||
section: database name: file value: "payroll.dat"
|
||||
section: database name: enabled value: true
|
||||
section: owner name: name value: John Doe
|
||||
section: owner name: organization value: Acme Widgets Inc.
|
||||
section: database name: server value: 192.0.2.62
|
||||
section: database name: port value: 143
|
||||
section: database name: file value: "payroll.dat"
|
||||
section: database name: enabled value: true
|
||||
section: settings name: username value: lee
|
||||
section: settings name: timeout value: 20
|
||||
Config file parsed successfully
|
||||
isDatabaseEnabled: true port: 143
|
||||
0x10e2c8e20
|
||||
0
|
||||
value: 100
|
||||
|
||||
*/
|
||||
|
||||
func main() {
|
||||
demo1()
|
||||
reader := inih.NewReaderFile(inih.Str("config.ini"))
|
||||
if reader.ParseError() != 0 {
|
||||
println("Error parsing config file")
|
||||
return
|
||||
}
|
||||
isDatabaseEnabled := reader.GetBoolean(inih.Str("database"), inih.Str("enabled"), false)
|
||||
port := reader.GetInteger(inih.Str("database"), inih.Str("port"), 0)
|
||||
println("isDatabaseEnabled:", isDatabaseEnabled, "port:", port)
|
||||
demo2()
|
||||
|
||||
}
|
||||
|
||||
func demo2() {
|
||||
buf := `[settings]
|
||||
username=admin
|
||||
timeout=100
|
||||
`
|
||||
reader := inih.NewReader(c.Str(buf), c.Ulong(len(buf)))
|
||||
|
||||
println(&reader)
|
||||
println(reader.ParseError())
|
||||
sec := inih.Str("settings")
|
||||
name := inih.Str("timeout")
|
||||
value := reader.GetInteger(sec, name, 0)
|
||||
println("value:", value)
|
||||
}
|
||||
10
cpp/inih/_demo/inihdemo/config.ini
Normal file
10
cpp/inih/_demo/inihdemo/config.ini
Normal file
@@ -0,0 +1,10 @@
|
||||
; example.ini
|
||||
[owner]
|
||||
name = John Doe
|
||||
organization = Acme Widgets Inc.
|
||||
|
||||
[database]
|
||||
server = 192.0.2.62
|
||||
port = 143
|
||||
file = "payroll.dat"
|
||||
enabled = true
|
||||
44
cpp/inih/_demo/inihdemo/inihdemo.go
Normal file
44
cpp/inih/_demo/inihdemo/inihdemo.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/inih"
|
||||
)
|
||||
|
||||
func demo1() {
|
||||
filename := c.Str("config.ini")
|
||||
|
||||
if inih.Parse(filename, func(user c.Pointer, section *c.Char, name *c.Char, value *c.Char) c.Int {
|
||||
println("section:", c.GoString(section), "name:", c.GoString(name), "value:", c.GoString(value))
|
||||
return 1
|
||||
}, nil) < 0 {
|
||||
println("Error parsing config file")
|
||||
return
|
||||
}
|
||||
|
||||
file := c.Fopen(c.Str("config.ini"), c.Str("r"))
|
||||
if file == nil {
|
||||
println("Error opening config file")
|
||||
return
|
||||
} else {
|
||||
if inih.ParseFile(file, func(user c.Pointer, section *c.Char, name *c.Char, value *c.Char) c.Int {
|
||||
println("section:", c.GoString(section), "name:", c.GoString(name), "value:", c.GoString(value))
|
||||
return 1
|
||||
}, nil) < 0 {
|
||||
println("Error parsing config file")
|
||||
return
|
||||
}
|
||||
c.Fclose(file)
|
||||
}
|
||||
|
||||
configData := "[settings]\nusername=lee\ntimeout=20"
|
||||
if inih.ParseString(c.Str(configData), func(user c.Pointer, section *c.Char, name *c.Char, value *c.Char) c.Int {
|
||||
println("section:", c.GoString(section), "name:", c.GoString(name), "value:", c.GoString(value))
|
||||
return 1
|
||||
}, nil) < 0 {
|
||||
println("Error parsing config file")
|
||||
return
|
||||
} else {
|
||||
println("Config file parsed successfully")
|
||||
}
|
||||
}
|
||||
20
cpp/inih/inih.go
Normal file
20
cpp/inih/inih.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package inih
|
||||
|
||||
import (
|
||||
_ "unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
)
|
||||
|
||||
const (
|
||||
LLGoPackage = "link: $(pkg-config --libs inih INIReader) -lc++; -linih -lINIReader"
|
||||
)
|
||||
|
||||
//go:linkname Parse C.ini_parse
|
||||
func Parse(filename *c.Char, handler func(user c.Pointer, section *c.Char, name *c.Char, value *c.Char) c.Int, user c.Pointer) c.Int
|
||||
|
||||
//go:linkname ParseFile C.ini_parse_file
|
||||
func ParseFile(file c.FilePtr, handler func(user c.Pointer, section *c.Char, name *c.Char, value *c.Char) c.Int, user c.Pointer) c.Int
|
||||
|
||||
//go:linkname ParseString C.ini_parse_string
|
||||
func ParseString(str *c.Char, handler func(user c.Pointer, section *c.Char, name *c.Char, value *c.Char) c.Int, user c.Pointer) c.Int
|
||||
44
cpp/inih/inihReader.go
Normal file
44
cpp/inih/inihReader.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package inih
|
||||
|
||||
import (
|
||||
_ "unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
)
|
||||
|
||||
// llgo:type C
|
||||
type Reader struct {
|
||||
Unused [24]byte
|
||||
}
|
||||
|
||||
func Str(s string) *stdstring {
|
||||
var r stdstring
|
||||
r.init(c.GoStringData(s), c.Int(len(s)))
|
||||
return &r
|
||||
}
|
||||
|
||||
type stdstring struct {
|
||||
buf [24]byte
|
||||
}
|
||||
|
||||
// llgo:link (*stdstring).init C._ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE6__initEPKcm
|
||||
func (*stdstring) init(s *c.Char, size c.Int) {}
|
||||
|
||||
//go:linkname NewReader C._ZN9INIReaderC1EPKcm
|
||||
func NewReader(fileName *c.Char, size c.Ulong) Reader
|
||||
|
||||
//go:linkname NewReaderFile C._ZN9INIReaderC1ERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE
|
||||
func NewReaderFile(fileName *stdstring) Reader
|
||||
|
||||
// llgo:link (*Reader).ParseError C._ZNK9INIReader10ParseErrorEv
|
||||
func (*Reader) ParseError() c.Int { return 0 }
|
||||
|
||||
// llgo:link (*Reader).GetInteger C._ZNK9INIReader10GetIntegerERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_l
|
||||
func (*Reader) GetInteger(section *stdstring, name *stdstring, defaultValue c.Long) c.Long {
|
||||
return 0
|
||||
}
|
||||
|
||||
// llgo:link (*Reader).GetBoolean C._ZNK9INIReader10GetBooleanERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_b
|
||||
func (*Reader) GetBoolean(section *stdstring, name *stdstring, defaultValue bool) bool {
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user