Debian Codegraph @

src/gauche-cesconv.in

raw
1 ;;;
2 ;;; gauche-cesconv - a simple Gauche script to convert CESes
3 ;;;
4
5 ;; iconv-like script. useful to have during make process.
6
7 (use gauche.charconv)
8 (use gauche.parseopt)
9 (use file.filter)
10 (use file.util)
11
12 (define (usage)
13 (print "Usage: gauche-cesconv [options] inputfile")
14 (print " -f, --from-code=encoding : input ces. can be '*jp' to guess.")
15 (print " -t, --to-code=encoding : output ces.")
16 (print " -o, --output=file : output file (instead of stdout)")
17 (print " --in-place : replace inputfile with converted file")
18 (print "When inputfile is omitted, input is taken from stdin.")
19 (print "(The inputfile argument is required if --in-place is given.)")
20 (print "when input/output ces is omitted, Gauche's native CES is used.")
21 (exit 0))
22
23 (define (process iport oport ices oces)
24 (copy-port (wrap-with-input-conversion iport ices)
25 (wrap-with-output-conversion oport oces)
26 :unit 'char))
27
28 (define (main args)
29 (let-args (cdr args) ((ices "f|from-code=s" (gauche-character-encoding))
30 (oces "t|to-code=s" (gauche-character-encoding))
31 (outfile "o|output=s" #f)
32 (in-place "in-place" #f)
33 (#f "h|help" => (cut usage))
34 . files)
35 (when (and in-place (null? files))
36 (exit 1 "In-place flag requires input file name"))
37 (let* ([ifile (if (pair? files) (car files) (current-input-port))]
38 [ofile (or outfile
39 (and in-place ifile)
40 (current-output-port))])
41 (if in-place
42 (unwind-protect
43 (begin
44 (sys-unlink #"~|ofile|.orig")
45 (sys-link ofile #"~|ofile|.orig")
46 (do-it ices oces ifile ofile))
47 (when (file-exists? #"~|ofile|.orig")
48 (if (not (file-eqv? ofile #"~|ofile|.orig"))
49 (sys-unlink #"~|ofile|.orig")
50 (sys-rename #"~|ofile|.orig" ofile))))
51 (do-it ices oces ifile ofile))))
52 0)
53
54 (define (do-it ices oces infile outfile)
55 (file-filter (^[in out]
56 (let ([inp (wrap-with-input-conversion in ices)]
57 [outp (wrap-with-output-conversion out oces)])
58 (copy-port inp outp :unit 'char)
59 (close-input-port inp)
60 (close-output-port outp)))
61 :input infile
62 :output outfile
63 :temporary-file #t))
64
65
66 ;; Local variables:
67 ;; mode: scheme
68 ;; end: