-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generate: add findstruct tool (#209)
* generate: add findstruct tool * docs: fix findstruct example * generate: add build ignore to tools
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//go:build ignore | ||
|
||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) < 2 { | ||
fmt.Println("Usage: findstruct <struct_name>") | ||
return | ||
} | ||
|
||
cmd := exec.Command("xcrun", "--show-sdk-path") | ||
b, err := cmd.CombinedOutput() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
dir := fmt.Sprintf("%s/System/Library/Frameworks", strings.TrimSpace(string(b))) | ||
structName := os.Args[1] | ||
|
||
// Naive regex to find C struct definitions | ||
// It won't capture complex scenarios or structs split across lines. | ||
re := regexp.MustCompile(`(?s)struct\s+` + structName + `\s+\{.*?\};`) | ||
|
||
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !info.IsDir() && (filepath.Ext(path) == ".h" || filepath.Ext(path) == ".c") { | ||
file, err := os.Open(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
var content string | ||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
content += scanner.Text() + "\n" | ||
} | ||
|
||
matches := re.FindAllString(content, -1) | ||
for _, match := range matches { | ||
fmt.Println(match) | ||
} | ||
} | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build ignore | ||
|
||
package main | ||
|
||
import ( | ||
|