diff --git a/c/inih/_demo/inihdemo/config.ini b/c/inih/_demo/inihdemo/config.ini deleted file mode 100644 index af786095..00000000 --- a/c/inih/_demo/inihdemo/config.ini +++ /dev/null @@ -1,3 +0,0 @@ -[settings] -username=admin -timeout=30 diff --git a/cpp/inih/READEME.md b/cpp/inih/READEME.md new file mode 100644 index 00000000..f679f1da --- /dev/null +++ b/cpp/inih/READEME.md @@ -0,0 +1 @@ +brew install inih \ No newline at end of file diff --git a/cpp/inih/_demo/inihdemo/ReaderDemo.go b/cpp/inih/_demo/inihdemo/ReaderDemo.go new file mode 100644 index 00000000..4fbf40f6 --- /dev/null +++ b/cpp/inih/_demo/inihdemo/ReaderDemo.go @@ -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) +} diff --git a/cpp/inih/_demo/inihdemo/config.ini b/cpp/inih/_demo/inihdemo/config.ini new file mode 100644 index 00000000..62e16fc7 --- /dev/null +++ b/cpp/inih/_demo/inihdemo/config.ini @@ -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 diff --git a/c/inih/_demo/inihdemo/inihdemo.go b/cpp/inih/_demo/inihdemo/inihdemo.go similarity index 98% rename from c/inih/_demo/inihdemo/inihdemo.go rename to cpp/inih/_demo/inihdemo/inihdemo.go index 66e36d2b..97c697d8 100644 --- a/c/inih/_demo/inihdemo/inihdemo.go +++ b/cpp/inih/_demo/inihdemo/inihdemo.go @@ -5,7 +5,7 @@ import ( "github.com/goplus/llgo/c/inih" ) -func main() { +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 { diff --git a/c/inih/inih.go b/cpp/inih/inih.go similarity index 87% rename from c/inih/inih.go rename to cpp/inih/inih.go index aae3d9f9..05f5c384 100644 --- a/c/inih/inih.go +++ b/cpp/inih/inih.go @@ -7,7 +7,7 @@ import ( ) const ( - LLGoPackage = "link: $(pkg-config --libs inih); -linih" + LLGoPackage = "link: $(pkg-config --libs inih INIReader) -lc++; -linih -lINIReader" ) //go:linkname Parse C.ini_parse diff --git a/cpp/inih/inihReader.go b/cpp/inih/inihReader.go new file mode 100644 index 00000000..43d566cb --- /dev/null +++ b/cpp/inih/inihReader.go @@ -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 +}