syncfiles/gen/rez.go
Dietrich Epp 5ad207f785 Embed character map tables in executable
This simplifies the conversion test, since we don't need to be careful
about which data we run the conversion test in. It will also simplify
the command-line conversion tool and its distribution. The classic Mac
OS version of this program will continue to embed conversion tables in
the resource fork.
2022-03-24 23:44:37 -04:00

73 lines
1.4 KiB
Go

package main
import (
"bufio"
"fmt"
"os"
)
// writeStrings writes a 'STR#' resource in Rez format.
func writeStrings(w *bufio.Writer, rsrc string, strs []string) {
fmt.Fprintf(w, "resource 'STR#' (%s) {{\n", rsrc)
for i, s := range strs {
fmt.Fprintf(w, "\t%q", s)
if i < len(strs)-1 {
w.WriteByte(',')
}
w.WriteByte('\n')
}
w.WriteString("}};\n")
}
func charmapNames(d *scriptdata) []string {
r := make([]string, len(d.charmaps))
for i, c := range d.charmaps {
r[i] = c.name
}
return r
}
func constStrings(c *constmap) []string {
var n int
for i := range c.values {
if i >= n {
n = i + 1
}
}
r := make([]string, n)
for i, name := range c.values {
r[i] = name
}
return r
}
func writeRez(d *scriptdata, filename string) error {
if !flagQuiet {
fmt.Fprintln(os.Stderr, "Writing:", filename)
}
fp, err := os.Create(filename)
if err != nil {
return err
}
defer fp.Close()
w := bufio.NewWriter(fp)
w.WriteString(header)
w.WriteString(
"#include \"resources.h\"\n" +
"#include \"MacTypes.r\"\n")
writeStrings(w, `rSTRS_Charmaps, "Character Maps"`, charmapNames(d))
writeStrings(w, `rSTRS_Scripts, "Scripts"`, constStrings(&d.scripts))
writeStrings(w, `rSTRS_Regions, "Regions"`, constStrings(&d.regions))
for i, cm := range d.charmaps {
if cm.filename != "" {
fmt.Fprintf(w, "read 'cmap' (%d, %q) %q;\n", 128+i, cm.name, cm.filename)
}
}
if err := w.Flush(); err != nil {
return err
}
return fp.Close()
}