Debian Codegraph @

lib/compat/jfilter.scm

raw
1 ;;;
2 ;;; compat.jfilter - jfilter compatibility interface
3 ;;;
4 ;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 ;; This file defines a set of character-code conversion routine with
35 ;; the same API as Dai INUKAI's Jfilter module
36 ;; http://www.sci.toyama-u.ac.jp/~iwao/Scheme/Jfilter/index.html
37 ;;
38 ;; The intention is to ease porting the existing code using Jfilter
39 ;; to Gauche.
40 ;;
41 ;; The handling of string differs between Gauche and other non-multibyte
42 ;; Scheme implementation, and most of lower-level routines in Jfilter
43 ;; is irrelevant. I only implement the higher-level routines.
44
45 (define-module compat.jfilter
46 (use gauche.charconv)
47 (use srfi.13)
48 (export cv-file
49 cv-string
50 judge-file))
51
52 (select-module compat.jfilter)
53
54 (define (ces-name->symbol name)
55 (let ((n (string-upcase (string-delete name #[-_]))))
56 (cond ((equal? n "EUCJP") 'eucj)
57 ((member n '("CSISO2022JP" "ISO2022JP")) 'jis)
58 ((member n '("SJIS" "SHIFTJIS")) 'sjis)
59 ((equal? n "UTF8") 'utf8)
60 (else (error "unsupported encoding name:" name)))))
61
62 (define (ces-symbol->name sym default)
63 (case sym
64 ((eucj) "EUCJP")
65 ((sjis) "SJIS")
66 ((utf8) "UTF-8")
67 ((jis) "CSISO2022JP")
68 ((() #f) default)
69 (else (error "unsupported encoding symbol:" sym))))
70
71 (define (judge-file input :optional (prefetch 5000))
72 (define (judge-port port)
73 (let ((str (read-block prefetch port)))
74 (ces-name->symbol (ces-guess-from-string str "*JP"))))
75
76 (cond ((string? input)
77 (call-with-input-file input judge-port))
78 ((input-port? input)
79 (judge-port input))
80 (else
81 (error "input must be a file name or an input port, but got" input)))
82 )
83
84 (define (cv-string str from-code to-code)
85 (ces-convert str
86 (ces-symbol->name from-code "*JP")
87 (ces-symbol->name to-code "EUCJP")))
88
89 (define (cv-file input output from-code to-code :optional
90 (remove-cr #f) (add-cr #f) (check-length 5000))
91 (let ((from (ces-symbol->name from-code "*JP"))
92 (to (ces-symbol->name to-code "EUCJP")))
93
94 (define (cv-block iport oport)
95 (copy-port (open-input-conversion-port iport from
96 :to-code to
97 :buffer-size check-length)
98 oport))
99
100 (define (cv-line iport oport)
101 (let loop ((line (read-line iport)))
102 (if (eof-object? line)
103 (flush oport)
104 (begin
105 (display (ces-convert line from to) oport)
106 (when add-cr (display #\return oport))
107 (newline oport)
108 (loop (read-line))))))
109
110 (define (cv-out iport)
111 (cond ((string? output)
112 (call-with-output-file output
113 (lambda (out)
114 ((if add-cr cv-line cv-block) iport out))))
115 ((output-port? output)
116 ((if add-cr cv-line cv-block) iport output))
117 (else "output must be a file name or an output port: ~s"
118 output)))
119
120 (cond ((string? input)
121 (call-with-input-file input cv-out))
122 ((input-port? input)
123 (cv-out input))
124 (else "input must be a file name or an input port: ~s"
125 input))
126 ))