Use TokenCounter and correct unsafe pointer usage

Record write errors in TokenCounter.Printf and add Println method
to propagate write failures. Route file-read warnings to stderr
instead of mixing them into token output. Use unsafe.Add with a
base pointer when computing the drop target on Windows.
This commit is contained in:
2026-01-31 03:33:36 +01:00
parent 14dfacea29
commit a32b7e4693
3 changed files with 24 additions and 15 deletions
+4 -2
View File
@@ -62,11 +62,13 @@ func CopyFile(path string) error {
return fmt.Errorf("GlobalLock failed")
}
df := (*dropFiles)(unsafe.Pointer(ptrVal))
basePtr := unsafe.Pointer(ptrVal)
df := (*dropFiles)(basePtr)
df.pFiles = dropSize
df.fWide = 1
targetPtr := unsafe.Pointer(ptrVal + uintptr(dropSize))
targetPtr := unsafe.Add(basePtr, dropSize)
srcSlice := unsafe.Slice((*uint16)(unsafe.Pointer(&pathUTF16[0])), len(pathUTF16))
dstSlice := unsafe.Slice((*uint16)(targetPtr), len(pathUTF16))
+12 -12
View File
@@ -30,19 +30,19 @@ func writeOutput(root string, files []string, outputPath string) (count int64, e
tc := &TokenCounter{w: bw}
fmt.Fprintf(tc, "Project Path: %s\n\n", filepath.Base(root))
fmt.Fprintln(tc, "Source Tree:")
fmt.Fprintln(tc, "")
tc.Printf("Project Path: %s\n\n", filepath.Base(root))
tc.Println("Source Tree:")
tc.Println("")
fmt.Fprintln(tc, "```txt")
fmt.Fprintln(tc, filepath.Base(root))
tc.Println("```txt")
tc.Println(filepath.Base(root))
if err := writeTree(tc, files); err != nil {
return 0, err
}
fmt.Fprintln(tc, "```")
fmt.Fprintln(tc, "")
tc.Println("```")
tc.Println("")
for _, file := range files {
if file == outputPath || filepath.Base(file) == outputPath {
@@ -52,7 +52,7 @@ func writeOutput(root string, files []string, outputPath string) (count int64, e
fullPath := filepath.Join(root, file)
content, err := os.ReadFile(fullPath)
if err != nil {
fmt.Fprintf(tc, "Error reading %s: %v\n", file, err)
fmt.Fprintf(os.Stderr, "ctx: warning: skipping %s: %v\n", file, err)
continue
}
@@ -61,8 +61,8 @@ func writeOutput(root string, files []string, outputPath string) (count int64, e
ext = "txt"
}
fmt.Fprintf(tc, "`%s`:\n\n", file)
fmt.Fprintf(tc, "```%s\n", ext)
tc.Printf("`%s`:\n\n", file)
tc.Printf("```%s\n", ext)
if _, err := tc.Write(content); err != nil {
return 0, err
@@ -73,8 +73,8 @@ func writeOutput(root string, files []string, outputPath string) (count int64, e
return 0, err
}
}
fmt.Fprintln(tc, "```")
fmt.Fprintln(tc, "")
tc.Println("```")
tc.Println("")
}
return tc.Count, tc.Err
+8 -1
View File
@@ -89,5 +89,12 @@ func (tc *TokenCounter) Printf(format string, a ...any) {
if tc.Err != nil {
return
}
fmt.Fprintf(tc, format, a...)
_, tc.Err = fmt.Fprintf(tc, format, a...)
}
func (tc *TokenCounter) Println(a ...any) {
if tc.Err != nil {
return
}
_, tc.Err = fmt.Fprintln(tc, a...)
}