library: cpp/std; cpp/inih demo: inihreader
This commit is contained in:
@@ -1 +1,14 @@
|
||||
brew install inih
|
||||
LLGo wrapper of benhoyt/inih
|
||||
=====
|
||||
|
||||
## How to install
|
||||
|
||||
### on macOS (Homebrew)
|
||||
|
||||
```sh
|
||||
brew install inih
|
||||
```
|
||||
|
||||
### on Linux (Debian/Ubuntu)
|
||||
|
||||
TODO
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/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
|
||||
s : 192.0.2.62
|
||||
isDatabaseEnabled: true port: 143
|
||||
0x109f54da0
|
||||
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)
|
||||
s := reader.GetString(inih.Str("database"), inih.Str("server"), inih.Str("unknown"))
|
||||
println("s :", s.String())
|
||||
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)
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"github.com/goplus/llgo/cpp/inih"
|
||||
)
|
||||
|
||||
func demo1() {
|
||||
func main() {
|
||||
filename := c.Str("config.ini")
|
||||
|
||||
if inih.Parse(filename, func(user c.Pointer, section *c.Char, name *c.Char, value *c.Char) c.Int {
|
||||
|
||||
10
cpp/inih/_demo/inihreader/config.ini
Normal file
10
cpp/inih/_demo/inihreader/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
|
||||
31
cpp/inih/_demo/inihreader/reader_demo.go
Normal file
31
cpp/inih/_demo/inihreader/reader_demo.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/cpp/inih"
|
||||
"github.com/goplus/llgo/cpp/std"
|
||||
)
|
||||
|
||||
func main() {
|
||||
buf := `[settings]
|
||||
username=admin
|
||||
timeout=100
|
||||
`
|
||||
reader := inih.NewReader(c.Str(buf), uintptr(len(buf)))
|
||||
println(reader.ParseError())
|
||||
sec := std.Str("settings")
|
||||
name := std.Str("timeout")
|
||||
value := reader.GetInteger(sec, name, 0)
|
||||
println("value:", value)
|
||||
|
||||
reader = inih.NewReaderFile(std.Str("config.ini"))
|
||||
if ret := reader.ParseError(); ret != 0 {
|
||||
println("Error parsing config file:", ret)
|
||||
return
|
||||
}
|
||||
isDatabaseEnabled := reader.GetBoolean(std.Str("database"), std.Str("enabled"), false)
|
||||
port := reader.GetInteger(std.NewString("database"), std.Str("port"), 0)
|
||||
s := reader.GetString(std.Str("database"), std.Str("server"), std.Str("unknown"))
|
||||
println("s:", s.Str())
|
||||
println("isDatabaseEnabled:", isDatabaseEnabled, "port:", port)
|
||||
}
|
||||
@@ -1,3 +1,19 @@
|
||||
/*
|
||||
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package inih
|
||||
|
||||
import (
|
||||
@@ -7,7 +23,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
LLGoPackage = "link: $(pkg-config --libs inih INIReader) -lc++; -linih -lINIReader"
|
||||
LLGoPackage = "link: $(pkg-config --libs inih INIReader); -linih -lINIReader"
|
||||
)
|
||||
|
||||
//go:linkname Parse C.ini_parse
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
package inih
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
)
|
||||
|
||||
// llgo:type C
|
||||
type Reader struct {
|
||||
Unused [24]byte
|
||||
}
|
||||
|
||||
type StdString struct {
|
||||
buf [24]byte
|
||||
}
|
||||
|
||||
type __long struct {
|
||||
__cap_ int
|
||||
__size_ int
|
||||
__data_ unsafe.Pointer
|
||||
}
|
||||
|
||||
func Str(s string) *StdString {
|
||||
var r StdString
|
||||
r.init(c.GoStringData(s), c.Int(len(s)))
|
||||
return &r
|
||||
}
|
||||
|
||||
func (r *StdString) String() string {
|
||||
if r.buf[0]&1 == 0 {
|
||||
return c.GoString((*c.Char)(unsafe.Pointer(&r.buf[1])))
|
||||
} else {
|
||||
v := *(*__long)(unsafe.Pointer(&r.buf[0]))
|
||||
return unsafe.String((*byte)(v.__data_), v.__size_)
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// llgo:link (*Reader).GetString C._ZNK9INIReader9GetStringERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_S8_
|
||||
func (*Reader) GetString(section *StdString, name *StdString, defaultValue *StdString) StdString {
|
||||
return StdString{}
|
||||
}
|
||||
73
cpp/inih/reader.go
Normal file
73
cpp/inih/reader.go
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package inih
|
||||
|
||||
import (
|
||||
_ "unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/cpp/std"
|
||||
)
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// llgo:type C
|
||||
type Reader struct {
|
||||
Unused [24]byte
|
||||
}
|
||||
|
||||
// llgo:link (*Reader).InitFromBuffer C._ZN9INIReaderC1EPKcm
|
||||
func (r *Reader) InitFromBuffer(buffer *c.Char, bufferSize uintptr) {}
|
||||
|
||||
// llgo:link (*Reader).InitFromFile C._ZN9INIReaderC1ERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE
|
||||
func (r *Reader) InitFromFile(buffer *c.Char, bufferSize uintptr) {}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
func NewReader(buffer *c.Char, bufferSize uintptr) (ret Reader) {
|
||||
ret.InitFromBuffer(buffer, bufferSize)
|
||||
return
|
||||
}
|
||||
|
||||
func NewReaderFile(fileName *std.String) (ret Reader) {
|
||||
ret.InitFromFile(fileName.CStr(), fileName.Size())
|
||||
return
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// 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 *std.String, name *std.String, defaultValue c.Long) c.Long {
|
||||
return 0
|
||||
}
|
||||
|
||||
// llgo:link (*Reader).GetBoolean C._ZNK9INIReader10GetBooleanERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_b
|
||||
func (*Reader) GetBoolean(section *std.String, name *std.String, defaultValue bool) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// llgo:link (*Reader).GetString C._ZNK9INIReader9GetStringERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_S8_
|
||||
func (*Reader) GetString(section *std.String, name *std.String, defaultValue *std.String) (ret std.String) {
|
||||
return
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
Reference in New Issue
Block a user