c/clang/symg:move get conf func
This commit is contained in:
60
chore/_xtool/llcppsymg/config/config.go
Normal file
60
chore/_xtool/llcppsymg/config/config.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/cjson"
|
||||
"github.com/goplus/llgo/chore/llcppg/types"
|
||||
)
|
||||
|
||||
type Conf struct {
|
||||
*cjson.JSON
|
||||
*types.Config
|
||||
}
|
||||
|
||||
func GetConf(data []byte) (Conf, error) {
|
||||
parsedConf := cjson.ParseBytes(data)
|
||||
if parsedConf == nil {
|
||||
return Conf{}, errors.New("failed to parse config")
|
||||
}
|
||||
|
||||
config := &types.Config{
|
||||
Name: GetStringItem(parsedConf, "name", ""),
|
||||
CFlags: GetStringItem(parsedConf, "cflags", ""),
|
||||
Libs: GetStringItem(parsedConf, "libs", ""),
|
||||
Include: GetStringArrayItem(parsedConf, "include"),
|
||||
TrimPrefixes: GetStringArrayItem(parsedConf, "trimPrefixes"),
|
||||
}
|
||||
|
||||
return Conf{
|
||||
JSON: parsedConf,
|
||||
Config: config,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func GetString(obj *cjson.JSON) (value string) {
|
||||
str := obj.GetStringValue()
|
||||
return unsafe.String((*byte)(unsafe.Pointer(str)), c.Strlen(str))
|
||||
}
|
||||
|
||||
func GetStringItem(obj *cjson.JSON, key string, defval string) (value string) {
|
||||
item := obj.GetObjectItemCaseSensitive(c.AllocaCStr(key))
|
||||
if item == nil {
|
||||
return defval
|
||||
}
|
||||
return GetString(item)
|
||||
}
|
||||
|
||||
func GetStringArrayItem(obj *cjson.JSON, key string) (value []string) {
|
||||
item := obj.GetObjectItemCaseSensitive(c.AllocaCStr(key))
|
||||
if item == nil {
|
||||
return
|
||||
}
|
||||
value = make([]string, item.GetArraySize())
|
||||
for i := range value {
|
||||
value[i] = GetString(item.GetArrayItem(c.Int(i)))
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user