func parse_arg(t string) { if t[0] == '-' { for i := 1; i < len(t); i++ { if t[i] == 'b' { params = append(params, "bus=checked") } else if t[i] == 's' { params = append(params, "sbahn=checked") } else if t[i] == 't' { params = append(params, "tram=checked") } else if t[i] == 'u' { params = append(params, "ubahn=checked") } } } else if !gotstation { buf := new(bytes.Buffer) w, err := charset.NewWriter("latin1", buf) if err != nil { log.Fatal(err) } fmt.Fprintf(w, t) w.Close() fmt.Printf("%q\n", buf.Bytes()) params = append(params, "haltestelle="+url.QueryEscape(string(buf.Bytes()))) gotstation = true } }
func testCodepage(t *testing.T, name string, inReader, outReader func(io.Reader) io.Reader) { data := make([]byte, 256) for i := range data { data[i] = byte(i) } inr := inReader(bytes.NewBuffer(data)) r, err := charset.NewReader(name, inr) if err != nil { t.Fatalf("cannot make reader for charset %q: %v", name, err) } outr := outReader(r) r = outr var outbuf bytes.Buffer w, err := charset.NewWriter(name, &outbuf) if err != nil { t.Fatalf("cannot make writer for charset %q: %v", name, err) } _, err = io.Copy(w, r) if err != nil { t.Fatalf("copy failed: %v", err) } err = w.Close() if err != nil { t.Fatalf("close failed: %v", err) } if len(outbuf.Bytes()) != len(data) { t.Fatalf("short result of roundtrip, charset %q, readers %T, %T; expected 256, got %d", name, inr, outr, len(outbuf.Bytes())) } for i, x := range outbuf.Bytes() { if data[i] != x { t.Fatalf("charset %q, round trip expected %d, got %d", name, i, data[i]) } } }
func ExampleNewWriter() { buf := new(bytes.Buffer) w, err := charset.NewWriter("latin1", buf) if err != nil { log.Fatal(err) } fmt.Fprintf(w, "£5 for Peppé") w.Close() fmt.Printf("%q\n", buf.Bytes()) // Output: "\xa35 for Pepp\xe9" }
func Utf8Conver(text string, toconv string) string { buf := new(bytes.Buffer) w, err := charset.NewWriter(toconv, buf) if err != nil { log.Fatal(err) } w.Close() strout := string(buf.Bytes()) // fmt.Printf("%s\n", strout) return strout }
func drawText(x, y, size int, text string, fg, bg int) error { //RAIO_print_text( (DISPLAY_WIDTH/4)-45, (DISPLAY_HEIGHT/4)*3-20, "Button 3 is", COLOR_GREEN, COLOR_BLACK ); buf := new(bytes.Buffer) w, err := charset.NewWriter("latin1", buf) if err != nil { return err } fmt.Fprintf(w, text) w.Close() b := []byte(buf.String()) b = append(b, 0) ctext := (*C.uchar)(unsafe.Pointer(&b[0])) C.RAIO_SetFontSizeFactor(C.uint8_t(size)) C.RAIO_print_text(C.uint16_t(x), C.uint16_t(y), ctext, C.uint8_t(bg), C.uint8_t(fg)) return nil }
func main() { flag.Usage = func() { fmt.Fprintf(os.Stderr, "usage: tcs [-l] [-v] [charset]\n") fmt.Fprintf(os.Stderr, "\ttcs [-f charset] [-t charset] [file]\n") } flag.Parse() if *listFlag { cs := "" switch flag.NArg() { case 1: cs = flag.Arg(0) case 0: default: flag.Usage() } listCharsets(*verboseFlag, cs) return } var f *os.File switch flag.NArg() { case 0: f = os.Stdin case 1: var err error f, err = os.Open(flag.Arg(0)) if err != nil { fatalf("cannot open %q: %v", err) } } r, err := charset.NewReader(*fromCharset, f) if err != nil { fatalf("cannot translate from %q: %v", *fromCharset, err) } w, err := charset.NewWriter(*toCharset, os.Stdout) if err != nil { fatalf("cannot translate to %q: ", err) } _, err = io.Copy(w, r) if err != nil { fatalf("%v", err) } }
// Encode YML catalog to xml and write into file. func SaveToFile(fileName string, catalog *Catalog, encoding string) error { if encoding == "" { encoding = "UTF-8" } file, err := os.Create(fileName) if err != nil { return err } defer file.Close() writer, err := charset.NewWriter(encoding, file) if err != nil { return err } defer writer.Close() writer.Write([]byte("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n")) writer.Write([]byte("<!DOCTYPE yml_catalog SYSTEM \"shops.dtd\">\n")) return Encode(writer, catalog) }
func main() { var fileEncoding, outputEncoding, parseSeperator, printSeperator string var debug bool flag.StringVar(&fileEncoding, "e", "", "input encoding, e.g. latin9, defaults to UTF-8") flag.StringVar(&outputEncoding, "o", "", "output encoding, e.g. latin9, defaults to LC_ALL/LANG or UTF-8") flag.StringVar(&parseSeperator, "c", ";", "seperator char used for parsing") flag.StringVar(&printSeperator, "s", "|", "seperator string used for printing") flag.BoolVar(&debug, "d", false, "debug output") // TODO //var alignRight bool //flag.BoolVar(&alignRight, "r", false, "align values to the right instead to the left") flag.Parse() if utf8.RuneCountInString(parseSeperator) > 1 { fmt.Fprintln(os.Stderr, "The parse seperator must be a single char.") flag.Usage() os.Exit(5) } if outputEncoding == "" { outputEncoding = getOutputEnc() } var f *os.File var err error if len(flag.Args()) != 0 { f, err = os.Open(flag.Arg(0)) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(10) } } else { f = os.Stdin } var inputReader io.Reader if fileEncoding != "" { inputReader, err = charset.NewReader(fileEncoding, f) if err != nil { fmt.Fprintf(os.Stderr, "input encoding: %s\n", err) os.Exit(20) } } else { inputReader = f } r := csv.NewReader(inputReader) r.Comma, _ = utf8.DecodeLastRuneInString(parseSeperator) r.TrailingComma = true r.TrimLeadingSpace = true r.LazyQuotes = true data, err := r.ReadAll() if len(os.Args) == 2 { f.Close() } if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(30) } if len(data) == 0 || len(data[0]) == 0 { os.Exit(0) } if debug { fmt.Fprintf(os.Stderr, "DEBUG columns: %d\n", len(data[0])) } colLens := make(map[int]int) for ri, row := range data { for ci, col := range row { col = strings.Trim(col, " \t") data[ri][ci] = col cl := utf8.RuneCountInString(col) l, ex := colLens[ci] if !ex || cl > l { colLens[ci] = cl } } } var out io.Writer = os.Stdout if outputEncoding != "UTF-8" { out, err = charset.NewWriter(outputEncoding, out) if err != nil { fmt.Fprintf(os.Stderr, "output encoding: %s\n", err) os.Exit(40) } } for _, row := range data { for i, col := range row { fmt.Fprintf(out, fmt.Sprint("%-", colLens[i]+1, "s"), col) if i != len(colLens)-1 { fmt.Fprintf(out, "%s ", printSeperator) } } fmt.Fprint(out, "\n") } }