1
0
mirror of https://github.com/garraflavatra/go-fmp.git synced 2025-06-28 20:45:11 +00:00
Files
go-fmp/fmp/fmp_debug.go

105 lines
1.9 KiB
Go
Raw Normal View History

2025-06-12 09:41:46 +02:00
package fmp
import (
"fmt"
"os"
2025-06-22 18:43:06 +02:00
"slices"
2025-06-12 09:41:46 +02:00
)
2025-06-15 21:09:39 +02:00
const debugging = false
2025-06-14 14:24:35 +02:00
func debug(str string, args ...interface{}) {
2025-06-15 21:09:39 +02:00
if debugging {
fmt.Printf(str+"\n", args...)
}
2025-06-14 14:24:35 +02:00
}
2025-06-14 16:28:41 +02:00
func dump(data []byte) {
2025-06-15 21:09:39 +02:00
if debugging {
for _, b := range data {
fmt.Printf("%02x ", b)
}
fmt.Println()
2025-06-14 16:28:41 +02:00
}
}
func dumpPath(path []uint64) {
2025-06-15 21:09:39 +02:00
if debugging {
for _, p := range path {
fmt.Printf("%v. ", p)
}
fmt.Println()
2025-06-14 16:28:41 +02:00
}
}
2025-06-12 09:41:46 +02:00
func (f *FmpFile) ToDebugFile(fname string) {
2025-06-13 12:09:39 +02:00
f_sectors, err := os.Create(fname + ".sectors")
if err != nil {
panic(err)
}
defer func() {
if err := f_sectors.Close(); err != nil {
panic(err)
}
}()
for _, sect := range f.Sectors {
fmt.Fprintf(f_sectors, "%s\n", sect.String())
}
2025-06-12 10:53:31 +02:00
f_chunks, err := os.Create(fname + ".chunks")
2025-06-12 09:41:46 +02:00
if err != nil {
panic(err)
}
defer func() {
2025-06-12 10:53:31 +02:00
if err := f_chunks.Close(); err != nil {
2025-06-12 09:41:46 +02:00
panic(err)
}
}()
for _, chunk := range f.Chunks {
2025-06-12 10:53:31 +02:00
fmt.Fprintf(f_chunks, "%s, %s\n", chunk.String(), string(chunk.Value))
}
f_dicts, err := os.Create(fname + ".dicts")
if err != nil {
panic(err)
2025-06-12 09:41:46 +02:00
}
2025-06-12 10:53:31 +02:00
defer func() {
if err := f_dicts.Close(); err != nil {
panic(err)
}
}()
fmt.Fprint(f_dicts, f.Dictionary.String())
2025-06-12 09:41:46 +02:00
}
2025-06-13 12:09:39 +02:00
func (sect *FmpSector) String() string {
2025-06-13 13:01:25 +02:00
return fmt.Sprintf("<Sector, id=%20v, del=%5v, lev=%1v, prevID=%20v, nextID=%20v>", sect.ID, sect.Deleted, sect.Level, sect.PrevID, sect.NextID)
2025-06-13 12:09:39 +02:00
}
2025-06-12 09:41:46 +02:00
func (c *FmpChunk) String() string {
2025-06-13 13:01:25 +02:00
return fmt.Sprintf("<Chunk, type=%v, len=%v>", c.Type, c.Length)
2025-06-12 09:41:46 +02:00
}
2025-06-12 10:53:31 +02:00
2025-06-12 21:21:55 +02:00
func (dict *FmpDict) string(parentPath string) string {
2025-06-12 10:53:31 +02:00
s := ""
2025-06-22 18:43:06 +02:00
keys := make([]uint64, 0, len(*dict))
for k := range *dict {
keys = append(keys, k)
}
slices.Sort(keys)
for _, k := range keys {
v := (*dict)[k]
2025-06-12 21:21:55 +02:00
s += fmt.Sprintf("%v%v: %v\n", parentPath, k, string(v.Value))
if v.Children != nil {
s += v.Children.string(fmt.Sprintf("%v%v.", parentPath, k))
}
2025-06-12 10:53:31 +02:00
}
return s
}
2025-06-12 21:21:55 +02:00
func (dict *FmpDict) String() string {
return dict.string("")
}