]> gitweb.factorcode.org Git - factor.git/blob - basis/io/encodings/8-bit/8-bit.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / io / encodings / 8-bit / 8-bit.factor
1 ! Copyright (C) 2008 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: math.parser arrays io.encodings sequences kernel assocs
4 hashtables io.encodings.ascii generic parser classes.tuple words
5 words.symbol io io.files splitting namespaces math
6 compiler.units accessors classes.singleton classes.mixin
7 io.encodings.iana fry simple-flat-file ;
8 IN: io.encodings.8-bit
9
10 <PRIVATE
11
12 CONSTANT: mappings {
13     ! encoding-name iana-name file-name
14     { "latin1" "ISO_8859-1:1987" "8859-1" }
15     { "latin2" "ISO_8859-2:1987" "8859-2" }
16     { "latin3" "ISO_8859-3:1988" "8859-3" }
17     { "latin4" "ISO_8859-4:1988" "8859-4" }
18     { "latin/cyrillic" "ISO_8859-5:1988" "8859-5" }
19     { "latin/arabic" "ISO_8859-6:1987" "8859-6" }
20     { "latin/greek" "ISO_8859-7:1987" "8859-7" }
21     { "latin/hebrew" "ISO_8859-8:1988" "8859-8" }
22     { "latin5" "ISO_8859-9:1989" "8859-9" }
23     { "latin6" "ISO-8859-10" "8859-10" }
24     { "latin/thai" "TIS-620" "8859-11" }
25     { "latin7" "ISO-8859-13" "8859-13" }
26     { "latin8" "ISO-8859-14" "8859-14" }
27     { "latin9" "ISO-8859-15" "8859-15" }
28     { "latin10" "ISO-8859-16" "8859-16" }
29     { "koi8-r" "KOI8-R" "KOI8-R" }
30     { "windows-1250" "windows-1250" "CP1250" }
31     { "windows-1252" "windows-1252" "CP1252" }
32     { "ebcdic" "IBM037" "CP037" }
33     { "mac-roman" "macintosh" "ROMAN" }
34 }
35
36 : encoding-file ( file-name -- stream )
37     "vocab:io/encodings/8-bit/" ".TXT" surround ;
38
39 SYMBOL: 8-bit-encodings
40
41 TUPLE: 8-bit biassoc ;
42
43 : encode-8-bit ( char stream assoc -- )
44     swapd value-at
45     [ swap stream-write1 ] [ encode-error ] if* ; inline
46
47 M: 8-bit encode-char biassoc>> encode-8-bit ;
48
49 : decode-8-bit ( stream assoc -- char/f )
50     swap stream-read1
51     [ swap at [ replacement-char ] unless* ]
52     [ drop f ] if* ; inline
53
54 M: 8-bit decode-char biassoc>> decode-8-bit ;
55
56 MIXIN: 8-bit-encoding
57
58 M: 8-bit-encoding <encoder>
59     8-bit-encodings get-global at <encoder> ;
60
61 M: 8-bit-encoding <decoder>
62     8-bit-encodings get-global at <decoder> ;
63
64 : create-encoding ( name -- word )
65     "io.encodings.8-bit" create
66     [ define-singleton-class ]
67     [ 8-bit-encoding add-mixin-instance ]
68     [ ] tri ;
69
70 PRIVATE>
71
72 [
73     mappings [
74         first3
75         [ create-encoding ]
76         [ dupd register-encoding ]
77         [ encoding-file flat-file>biassoc 8-bit boa ]
78         tri*
79     ] H{ } map>assoc
80     8-bit-encodings set-global
81 ] with-compilation-unit