2007-10-25 16:27:34 -04:00
|
|
|
;;; Ikarus Scheme -- A compiler for R6RS Scheme.
|
2008-01-29 00:34:34 -05:00
|
|
|
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
|
2007-10-25 16:27:34 -04:00
|
|
|
;;;
|
|
|
|
;;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;;; it under the terms of the GNU General Public License version 3 as
|
|
|
|
;;; published by the Free Software Foundation.
|
|
|
|
;;;
|
|
|
|
;;; This program is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
;;; General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2007-10-11 21:28:08 -04:00
|
|
|
|
|
|
|
(library (ikarus codecs)
|
2007-10-11 22:55:20 -04:00
|
|
|
(export latin-1-codec utf-8-codec utf-16-codec native-eol-style
|
2007-10-11 23:43:25 -04:00
|
|
|
make-transcoder native-transcoder buffer-mode?
|
2008-07-24 21:58:53 -04:00
|
|
|
transcoder-codec transcoder-eol-style
|
2007-10-12 00:00:36 -04:00
|
|
|
transcoder-error-handling-mode)
|
2007-10-11 22:55:20 -04:00
|
|
|
(import
|
|
|
|
(except (ikarus) latin-1-codec utf-8-codec utf-16-codec
|
2007-10-11 23:21:55 -04:00
|
|
|
native-eol-style make-transcoder native-transcoder
|
2008-07-24 21:58:53 -04:00
|
|
|
buffer-mode? transcoder-codec
|
2007-10-12 00:00:36 -04:00
|
|
|
transcoder-eol-style transcoder-error-handling-mode)
|
2007-10-11 22:55:20 -04:00
|
|
|
(ikarus system $transcoders))
|
2007-10-11 21:28:08 -04:00
|
|
|
(define (latin-1-codec) 'latin-1-codec)
|
|
|
|
(define (utf-8-codec) 'utf-8-codec)
|
2007-10-11 21:47:11 -04:00
|
|
|
(define (utf-16-codec) 'utf-16-codec)
|
2007-10-11 22:55:20 -04:00
|
|
|
(define (native-eol-style) 'none)
|
|
|
|
|
|
|
|
(define error-handling-mode-alist
|
|
|
|
'([ignore . #b01]
|
|
|
|
[raise . #b10]
|
|
|
|
[replace . #b11]))
|
2007-10-12 00:00:36 -04:00
|
|
|
(define error-handling-mode-mask #b11)
|
2007-10-11 22:55:20 -04:00
|
|
|
|
|
|
|
(define eol-style-alist
|
|
|
|
'([none . #b00000]
|
|
|
|
[lf . #b00100]
|
|
|
|
[cr . #b01000]
|
|
|
|
[crlf . #b01100]
|
|
|
|
[nel . #b10000]
|
|
|
|
[crnel . #b10100]
|
|
|
|
[ls . #b11000]))
|
2007-10-12 00:00:36 -04:00
|
|
|
(define eol-style-mask #b11100)
|
2007-10-11 22:55:20 -04:00
|
|
|
|
|
|
|
(define codec-alist
|
|
|
|
'([latin-1-codec . #b0100000]
|
|
|
|
[utf-8-codec . #b1000000]
|
|
|
|
[utf-16-codec . #b1100000]))
|
2007-10-12 00:00:36 -04:00
|
|
|
(define codec-mask #b11100000)
|
|
|
|
|
|
|
|
(define (rev-lookup n ls)
|
|
|
|
(cond
|
|
|
|
[(null? ls) #f]
|
|
|
|
[(= (cdar ls) n) (caar ls)]
|
|
|
|
[else (rev-lookup n (cdr ls))]))
|
2007-10-11 22:55:20 -04:00
|
|
|
|
|
|
|
(define (codec->fixnum x who)
|
|
|
|
(cond
|
|
|
|
[(assq x codec-alist) => cdr]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die who "not a valid coded" x)]))
|
2007-10-11 22:55:20 -04:00
|
|
|
|
|
|
|
(define (eol-style->fixnum x who)
|
|
|
|
(cond
|
|
|
|
[(assq x eol-style-alist) => cdr]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die who "not a valid eol-style" x)]))
|
2007-10-11 22:55:20 -04:00
|
|
|
|
|
|
|
(define (error-handling-mode->fixnum x who)
|
|
|
|
(cond
|
|
|
|
[(assq x error-handling-mode-alist) => cdr]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die who "not a valid error-handling mode" x)]))
|
2007-10-11 22:55:20 -04:00
|
|
|
|
|
|
|
(define make-transcoder
|
|
|
|
(case-lambda
|
|
|
|
[(codec eol-style handling-mode)
|
|
|
|
($data->transcoder
|
|
|
|
(fxior
|
|
|
|
(error-handling-mode->fixnum handling-mode 'make-transcoder)
|
|
|
|
(eol-style->fixnum eol-style 'make-transcoder)
|
|
|
|
(codec->fixnum codec 'make-transcoder)))]
|
|
|
|
[(codec eol-style)
|
|
|
|
(make-transcoder codec eol-style 'replace)]
|
|
|
|
[(codec)
|
|
|
|
(make-transcoder codec 'none 'replace)]))
|
|
|
|
|
|
|
|
(define (native-transcoder)
|
|
|
|
(make-transcoder 'utf-8-codec 'none 'replace))
|
|
|
|
|
2007-10-12 00:00:36 -04:00
|
|
|
(define (transcoder-codec x)
|
|
|
|
(define who 'transcoder-codec)
|
2007-12-06 05:05:26 -05:00
|
|
|
(if (transcoder? x)
|
2007-10-12 00:00:36 -04:00
|
|
|
(let ([tag (fxlogand ($transcoder->data x) codec-mask)])
|
|
|
|
(or (rev-lookup tag codec-alist)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "transcoder has no codec" x)))
|
|
|
|
(die who "not a transcoder" x)))
|
2007-10-12 00:00:36 -04:00
|
|
|
|
|
|
|
(define (transcoder-eol-style x)
|
|
|
|
(define who 'transcoder-eol-style)
|
2007-12-06 05:05:26 -05:00
|
|
|
(if (transcoder? x)
|
2007-10-12 00:00:36 -04:00
|
|
|
(let ([tag (fxlogand ($transcoder->data x) eol-style-mask)])
|
|
|
|
(or (rev-lookup tag eol-style-alist)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "transcoder has no eol-style" x)))
|
|
|
|
(die who "not a transcoder" x)))
|
2007-10-12 00:00:36 -04:00
|
|
|
|
|
|
|
(define (transcoder-error-handling-mode x)
|
|
|
|
(define who 'transcoder-error-handling-mode)
|
2007-12-06 05:05:26 -05:00
|
|
|
(if (transcoder? x)
|
2007-10-12 00:00:36 -04:00
|
|
|
(let ([tag (fxlogand ($transcoder->data x) error-handling-mode-mask)])
|
|
|
|
(or (rev-lookup tag error-handling-mode-alist)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "transcoder has no error-handling mode" x)))
|
|
|
|
(die who "not a transcoder" x)))
|
2007-10-12 00:00:36 -04:00
|
|
|
|
2007-10-11 23:21:55 -04:00
|
|
|
(define (buffer-mode? x)
|
|
|
|
(and (memq x '(none line block)) #t))
|
|
|
|
|
2007-10-11 22:55:20 -04:00
|
|
|
)
|
2007-10-11 21:28:08 -04:00
|
|
|
|