123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- 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))
- fmt.Printf("func ANSI%s() []string {\n", strings.Title(basename))
- // insert CP437_to_Unicode in here, if needed.
- // fmt.Printf(" data := [%d]string {\n", len(lines))
- fmt.Print(" data := []string {\n")
- for _, line := range lines {
- fmt.Printf(" \"%s\",\n", repr_line(line))
- }
- fmt.Println(" }")
- fmt.Println(" if !door.Unicode && !door.CP437 {")
- fmt.Println(" log.Panicf(\"Call after door.Init() so Unicode/CP437 has been set.\")")
- fmt.Println(" }")
- fmt.Println(" if door.Unicode {")
- fmt.Println(" for idx := range data {")
- 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 \"log\"\n )\n\n")
- for _, filename := range flag.Args() {
- readfile(filename)
- }
- }
|