diff --git a/gen/structdef.go b/gen/structdef.go index b4aa2b990be2cb28ec06bdb27b7469154cf79264..621810c3f372b2ebe81ee2e7ac4f17bb744f2789 100644 --- a/gen/structdef.go +++ b/gen/structdef.go @@ -1,6 +1,7 @@ package main import ( + "regexp" "sort" "strings" "text/template" @@ -9,6 +10,11 @@ import ( "github.com/richardwilkes/toolbox/txt" ) +var ( + defRegex = regexp.MustCompile(` struct\s+_(cef_\S+)\s+definition$`) + fieldRegex = regexp.MustCompile(`-FieldDecl .*>\s+\S+\s+(\S+)\s+'([^']+)'`) +) + type structDef struct { Name string GoName string @@ -75,29 +81,17 @@ func translateStructTypeName(name string) string { } func processRecordDecl(block []lineInfo) { - line := block[0].Line - if strings.HasSuffix(line, " definition") { - if i := strings.Index(line, " struct _cef_"); i != -1 { - name := line[i+9 : len(line)-11] - if _, exclude := excludeMap[name]; !exclude { - if _, exists := sdefsMap[name]; !exists { - sdef := newStructDef(name, block[0].Position) - for i := 1; i < len(block); i++ { - line = block[i].Line - if len(line) > 3 && strings.HasPrefix(line[3:], "-FieldDecl ") { - if start := strings.Index(line, "> "); start != -1 { - line = line[start+2:] - if space := strings.Index(line, " "); space != -1 { - line = line[space+1:] - if start = strings.Index(line, " "); space != -1 { - sdef.Fields = append(sdef.Fields, newField(sdef, line[:start], strings.Trim(line[start+1:], "'"), block[i].Position)) - } - } - } - } + if result := defRegex.FindAllStringSubmatch(block[0].Line, -1); result != nil { + name := result[0][1] + if _, exclude := excludeMap[name]; !exclude { + if _, exists := sdefsMap[name]; !exists { + sdef := newStructDef(name, block[0].Position) + for i := 1; i < len(block); i++ { + if result = fieldRegex.FindAllStringSubmatch(block[i].Line, -1); result != nil { + sdef.Fields = append(sdef.Fields, newField(sdef, result[0][1], result[0][2], block[i].Position)) } - sdefsMap[name] = sdef } + sdefsMap[name] = sdef } } } diff --git a/gen/variable.go b/gen/variable.go index 92e37997cb0d93b6c972fabcf7a79986791faa4d..7cd6520d45362ba59323b0d1b58d0b143b741edd 100644 --- a/gen/variable.go +++ b/gen/variable.go @@ -29,10 +29,10 @@ type variable struct { func newCVar(name, typeInfo string, pos position) *variable { name = strings.TrimSpace(name) - typeInfo = strings.Replace(strings.TrimPrefix(strings.Trim(strings.TrimSpace(strings.Replace(typeInfo, "const ", "", -1)), "'"), "struct _"), "long long", "int64_t", -1) - if i := strings.Index(typeInfo, "':'"); i != -1 { - typeInfo = typeInfo[:i] - } + typeInfo = strings.Replace(typeInfo, "const ", "", -1) + typeInfo = strings.TrimSpace(typeInfo) + typeInfo = strings.TrimPrefix(typeInfo, "struct _") + typeInfo = strings.Replace(typeInfo, "long long", "int64_t", -1) v := &variable{ Name: name, GoName: txt.ToCamelCase(name),