File: ikarus.codecs.ss

package info (click to toggle)
ikarus 0.0.3%2Bbzr.2010.01.26-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 39,868 kB
  • ctags: 9,284
  • sloc: lisp: 47,954; ansic: 13,247; sh: 4,595; java: 641; asm: 366; makefile: 264; awk: 186; perl: 66
file content (120 lines) | stat: -rw-r--r-- 4,062 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
;;; Ikarus Scheme -- A compiler for R6RS Scheme.
;;; Copyright (C) 2006,2007,2008  Abdulaziz Ghuloum
;;; 
;;; 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/>.


(library (ikarus codecs)
  (export latin-1-codec utf-8-codec utf-16-codec native-eol-style
          make-transcoder native-transcoder buffer-mode?
          transcoder-codec transcoder-eol-style
          transcoder-error-handling-mode)
  (import 
    (except (ikarus) latin-1-codec utf-8-codec utf-16-codec 
      native-eol-style make-transcoder native-transcoder
      buffer-mode? transcoder-codec
      transcoder-eol-style transcoder-error-handling-mode)
    (ikarus system $transcoders))
  (define (latin-1-codec) 'latin-1-codec)
  (define (utf-8-codec)   'utf-8-codec)
  (define (utf-16-codec)  'utf-16-codec)
  (define (native-eol-style) 'none)
  
  (define error-handling-mode-alist
    '([ignore .  #b01]
      [raise .   #b10]
      [replace . #b11]))
  (define error-handling-mode-mask #b11)

  (define eol-style-alist
    '([none .   #b00000]
      [lf .     #b00100]
      [cr .     #b01000]
      [crlf .   #b01100]
      [nel .    #b10000]
      [crnel .  #b10100]
      [ls .     #b11000]))
  (define eol-style-mask #b11100)

  (define codec-alist
    '([latin-1-codec . #b0100000]
      [utf-8-codec .   #b1000000]
      [utf-16-codec .  #b1100000]))
  (define codec-mask #b11100000)

  (define (rev-lookup n ls)
    (cond
      [(null? ls) #f]
      [(= (cdar ls) n) (caar ls)]
      [else (rev-lookup n (cdr ls))]))

  (define (codec->fixnum x who)
    (cond
      [(assq x codec-alist) => cdr]
      [else (die who "not a valid coded" x)]))

  (define (eol-style->fixnum x who)
    (cond
      [(assq x eol-style-alist) => cdr]
      [else (die who "not a valid eol-style" x)]))

  (define (error-handling-mode->fixnum x who)
    (cond
      [(assq x error-handling-mode-alist) => cdr]
      [else (die who "not a valid error-handling mode" x)]))

  (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))

  (define (transcoder-codec x) 
    (define who 'transcoder-codec)
    (if (transcoder? x) 
        (let ([tag (fxlogand ($transcoder->data x) codec-mask)])
          (or (rev-lookup tag codec-alist)
              (die who "transcoder has no codec" x)))
        (die who "not a transcoder" x)))

  (define (transcoder-eol-style x) 
    (define who 'transcoder-eol-style)
    (if (transcoder? x) 
        (let ([tag (fxlogand ($transcoder->data x) eol-style-mask)])
          (or (rev-lookup tag eol-style-alist)
              (die who "transcoder has no eol-style" x)))
        (die who "not a transcoder" x)))

  (define (transcoder-error-handling-mode x) 
    (define who 'transcoder-error-handling-mode)
    (if (transcoder? x) 
        (let ([tag (fxlogand ($transcoder->data x) error-handling-mode-mask)])
          (or (rev-lookup tag error-handling-mode-alist)
              (die who "transcoder has no error-handling mode" x)))
        (die who "not a transcoder" x)))

  (define (buffer-mode? x)
    (and (memq x '(none line block)) #t))

  )