|
@@ -0,0 +1,101 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "bufio"
|
|
|
+ "flag"
|
|
|
+ "fmt"
|
|
|
+ "log"
|
|
|
+ "os"
|
|
|
+ "strings"
|
|
|
+)
|
|
|
+
|
|
|
+func hexout(text string) {
|
|
|
+ for _, b := range []byte(text) {
|
|
|
+ fmt.Printf("%02x ", b)
|
|
|
+ }
|
|
|
+ fmt.Println("")
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func repr_line(line string) string {
|
|
|
+ output := ""
|
|
|
+
|
|
|
+ for _, ch := range []byte(line) {
|
|
|
+ switch {
|
|
|
+ case ch == '\'':
|
|
|
+ output += "\\'"
|
|
|
+ case ch == '"':
|
|
|
+ output += "\\\""
|
|
|
+ case ch == '\\':
|
|
|
+ output += "\\\\"
|
|
|
+ case ch < byte(0x20) || ch > byte(0x7e):
|
|
|
+ output += fmt.Sprintf("\\x%02x", ch)
|
|
|
+ default:
|
|
|
+ output += string(ch)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return output
|
|
|
+}
|
|
|
+
|
|
|
+func readfile(filename string) {
|
|
|
+ var basename string
|
|
|
+ var lines []string
|
|
|
+
|
|
|
+ idx := strings.LastIndex(filename, "/")
|
|
|
+ basename = filename[idx+1:]
|
|
|
+
|
|
|
+ idx = strings.Index(basename, ".")
|
|
|
+ basename = basename[:idx]
|
|
|
+
|
|
|
+ file, err := os.Open(filename)
|
|
|
+ if err != nil {
|
|
|
+ log.Panicf("Open(%s): %s\n", filename, err)
|
|
|
+ }
|
|
|
+
|
|
|
+ defer file.Close()
|
|
|
+
|
|
|
+ // Read the lines
|
|
|
+ scanner := bufio.NewScanner(file)
|
|
|
+ for scanner.Scan() {
|
|
|
+ line := scanner.Text()
|
|
|
+ lines = append(lines, line)
|
|
|
+ }
|
|
|
+
|
|
|
+ // Ok, is what we have usable, or did it mangle up our Latin1 text?
|
|
|
+ // hexout(lines[0])
|
|
|
+
|
|
|
+ fmt.Printf("func ANSI%s() [%d]string {\n", strings.Title(basename), len(lines))
|
|
|
+ // insert CP437_to_Unicode in here, if needed.
|
|
|
+ fmt.Printf(" data := [%d]string {\n", len(lines))
|
|
|
+ for _, line := range lines {
|
|
|
+ fmt.Printf(" \"%s\",\n", repr_line(line))
|
|
|
+ }
|
|
|
+ fmt.Println(" }")
|
|
|
+ fmt.Println(" if door.Unicode {")
|
|
|
+ fmt.Println(" for idx := range lines {")
|
|
|
+ fmt.Println(" data[idx] = door.CP437_to_Unicode(data[idx])")
|
|
|
+ fmt.Println(" }")
|
|
|
+ fmt.Println(" }")
|
|
|
+ fmt.Println(" return data")
|
|
|
+ fmt.Printf("}\n\n")
|
|
|
+}
|
|
|
+
|
|
|
+func main() {
|
|
|
+ var defaultPackage string = "main"
|
|
|
+
|
|
|
+ flag.StringVar(&defaultPackage, "p", "main", "Package name to use")
|
|
|
+ flag.Parse()
|
|
|
+
|
|
|
+ if flag.NArg() == 0 {
|
|
|
+ fmt.Println("ANSI (CP437) to go converter.")
|
|
|
+ fmt.Println("No ANSI filenames given.")
|
|
|
+ flag.PrintDefaults()
|
|
|
+ os.Exit(2)
|
|
|
+ }
|
|
|
+
|
|
|
+ fmt.Printf("package %s\n\n", defaultPackage)
|
|
|
+ fmt.Printf("import (\n \"red-green/door\"\n )\n\n")
|
|
|
+ for _, filename := range flag.Args() {
|
|
|
+ readfile(filename)
|
|
|
+ }
|
|
|
+}
|