]> gitweb.factorcode.org Git - factor.git/blob - basis/io/encodings/8-bit/8-bit.factor
Add vocab: for vocab-relative paths
[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 ;
8 IN: io.encodings.8-bit
9
10 <PRIVATE
11
12 : 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-1252" "windows-1252" "CP1252" }
31     { "ebcdic" "IBM037" "CP037" }
32     { "mac-roman" "macintosh" "ROMAN" }
33 } ;
34
35 : encoding-file ( file-name -- stream )
36     "vocab:io/encodings/8-bit/" swap ".TXT"
37     3append ascii <file-reader> ;
38
39 : process-contents ( lines -- assoc )
40     [ "#" split1 drop ] map harvest
41     [ "\t" split 2 head [ 2 short tail hex> ] map ] map ;
42
43 : byte>ch ( assoc -- array )
44     256 replacement-char <array>
45     [ [ swapd set-nth ] curry assoc-each ] keep ;
46
47 : ch>byte ( assoc -- newassoc )
48     [ swap ] assoc-map >hashtable ;
49
50 : parse-file ( stream -- byte>ch ch>byte )
51     lines process-contents
52     [ byte>ch ] [ ch>byte ] bi ;
53
54 SYMBOL: 8-bit-encodings
55
56 TUPLE: 8-bit decode encode ;
57
58 : encode-8-bit ( char stream assoc -- )
59     swapd at*
60     [ swap stream-write1 ] [ nip encode-error ] if ; inline
61
62 M: 8-bit encode-char encode>> encode-8-bit ;
63
64 : decode-8-bit ( stream array -- char/f )
65     swap stream-read1 dup
66     [ swap nth [ replacement-char ] unless* ] [ 2drop f ] if ; inline
67
68 M: 8-bit decode-char decode>> decode-8-bit ;
69
70 MIXIN: 8-bit-encoding
71
72 M: 8-bit-encoding <encoder>
73     8-bit-encodings get-global at <encoder> ;
74
75 M: 8-bit-encoding <decoder>
76     8-bit-encodings get-global at <decoder> ;
77
78 : create-encoding ( name -- word )
79     "io.encodings.8-bit" create
80     [ define-singleton-class ]
81     [ 8-bit-encoding add-mixin-instance ]
82     [ ] tri ;
83
84 PRIVATE>
85
86 [
87     mappings [
88         first3
89         [ create-encoding ]
90         [ dupd register-encoding ]
91         [ encoding-file parse-file 8-bit boa ]
92         tri*
93     ] H{ } map>assoc
94     8-bit-encodings set-global
95 ] with-compilation-unit