| 1 |
;;; Ikarus Scheme -- A compiler for R6RS Scheme. |
| 2 |
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum |
| 3 |
;;; |
| 4 |
;;; This program is free software: you can redistribute it and/or modify |
| 5 |
;;; it under the terms of the GNU General Public License version 3 as |
| 6 |
;;; published by the Free Software Foundation. |
| 7 |
;;; |
| 8 |
;;; This program is distributed in the hope that it will be useful, but |
| 9 |
;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
;;; General Public License for more details. |
| 12 |
;;; |
| 13 |
;;; You should have received a copy of the GNU General Public License |
| 14 |
;;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
|
| 17 |
(library (ikarus codecs) |
| 18 |
(export latin-1-codec utf-8-codec utf-16-codec native-eol-style |
| 19 |
make-transcoder native-transcoder buffer-mode? |
| 20 |
transcoder-codec transcoder-eol-style |
| 21 |
transcoder-error-handling-mode) |
| 22 |
(import |
| 23 |
(except (ikarus) latin-1-codec utf-8-codec utf-16-codec |
| 24 |
native-eol-style make-transcoder native-transcoder |
| 25 |
buffer-mode? transcoder-codec |
| 26 |
transcoder-eol-style transcoder-error-handling-mode) |
| 27 |
(ikarus system $transcoders)) |
| 28 |
(define (latin-1-codec) 'latin-1-codec) |
| 29 |
(define (utf-8-codec) 'utf-8-codec) |
| 30 |
(define (utf-16-codec) 'utf-16-codec) |
| 31 |
(define (native-eol-style) 'none) |
| 32 |
|
| 33 |
(define error-handling-mode-alist |
| 34 |
'([ignore . #b01] |
| 35 |
[raise . #b10] |
| 36 |
[replace . #b11])) |
| 37 |
(define error-handling-mode-mask #b11) |
| 38 |
|
| 39 |
(define eol-style-alist |
| 40 |
'([none . #b00000] |
| 41 |
[lf . #b00100] |
| 42 |
[cr . #b01000] |
| 43 |
[crlf . #b01100] |
| 44 |
[nel . #b10000] |
| 45 |
[crnel . #b10100] |
| 46 |
[ls . #b11000])) |
| 47 |
(define eol-style-mask #b11100) |
| 48 |
|
| 49 |
(define codec-alist |
| 50 |
'([latin-1-codec . #b0100000] |
| 51 |
[utf-8-codec . #b1000000] |
| 52 |
[utf-16-codec . #b1100000])) |
| 53 |
(define codec-mask #b11100000) |
| 54 |
|
| 55 |
(define (rev-lookup n ls) |
| 56 |
(cond |
| 57 |
[(null? ls) #f] |
| 58 |
[(= (cdar ls) n) (caar ls)] |
| 59 |
[else (rev-lookup n (cdr ls))])) |
| 60 |
|
| 61 |
(define (codec->fixnum x who) |
| 62 |
(cond |
| 63 |
[(assq x codec-alist) => cdr] |
| 64 |
[else (die who "not a valid coded" x)])) |
| 65 |
|
| 66 |
(define (eol-style->fixnum x who) |
| 67 |
(cond |
| 68 |
[(assq x eol-style-alist) => cdr] |
| 69 |
[else (die who "not a valid eol-style" x)])) |
| 70 |
|
| 71 |
(define (error-handling-mode->fixnum x who) |
| 72 |
(cond |
| 73 |
[(assq x error-handling-mode-alist) => cdr] |
| 74 |
[else (die who "not a valid error-handling mode" x)])) |
| 75 |
|
| 76 |
(define make-transcoder |
| 77 |
(case-lambda |
| 78 |
[(codec eol-style handling-mode) |
| 79 |
($data->transcoder |
| 80 |
(fxior |
| 81 |
(error-handling-mode->fixnum handling-mode 'make-transcoder) |
| 82 |
(eol-style->fixnum eol-style 'make-transcoder) |
| 83 |
(codec->fixnum codec 'make-transcoder)))] |
| 84 |
[(codec eol-style) |
| 85 |
(make-transcoder codec eol-style 'replace)] |
| 86 |
[(codec) |
| 87 |
(make-transcoder codec 'none 'replace)])) |
| 88 |
|
| 89 |
(define (native-transcoder) |
| 90 |
(make-transcoder 'utf-8-codec 'none 'replace)) |
| 91 |
|
| 92 |
(define (transcoder-codec x) |
| 93 |
(define who 'transcoder-codec) |
| 94 |
(if (transcoder? x) |
| 95 |
(let ([tag (fxlogand ($transcoder->data x) codec-mask)]) |
| 96 |
(or (rev-lookup tag codec-alist) |
| 97 |
(die who "transcoder has no codec" x))) |
| 98 |
(die who "not a transcoder" x))) |
| 99 |
|
| 100 |
(define (transcoder-eol-style x) |
| 101 |
(define who 'transcoder-eol-style) |
| 102 |
(if (transcoder? x) |
| 103 |
(let ([tag (fxlogand ($transcoder->data x) eol-style-mask)]) |
| 104 |
(or (rev-lookup tag eol-style-alist) |
| 105 |
(die who "transcoder has no eol-style" x))) |
| 106 |
(die who "not a transcoder" x))) |
| 107 |
|
| 108 |
(define (transcoder-error-handling-mode x) |
| 109 |
(define who 'transcoder-error-handling-mode) |
| 110 |
(if (transcoder? x) |
| 111 |
(let ([tag (fxlogand ($transcoder->data x) error-handling-mode-mask)]) |
| 112 |
(or (rev-lookup tag error-handling-mode-alist) |
| 113 |
(die who "transcoder has no error-handling mode" x))) |
| 114 |
(die who "not a transcoder" x))) |
| 115 |
|
| 116 |
(define (buffer-mode? x) |
| 117 |
(and (memq x '(none line block)) #t)) |
| 118 |
|
| 119 |
) |
| 120 |
|