1 ! Copyright (C) 2008 Daniel Ehrenberg, Doug Coleman.
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 lexer ;
12 : encoding-file ( file-name -- stream )
13 "vocab:io/encodings/8-bit/" ".TXT" surround ;
15 SYMBOL: 8-bit-encodings
16 8-bit-encodings [ H{ } clone ] initialize
18 TUPLE: 8-bit biassoc ;
20 : encode-8-bit ( char stream assoc -- )
22 [ swap stream-write1 ] [ encode-error ] if* ; inline
24 M: 8-bit encode-char biassoc>> encode-8-bit ;
26 : decode-8-bit ( stream assoc -- char/f )
28 [ swap at [ replacement-char ] unless* ]
29 [ drop f ] if* ; inline
31 M: 8-bit decode-char biassoc>> decode-8-bit ;
35 M: 8-bit-encoding <encoder>
36 8-bit-encodings get-global at <encoder> ;
38 M: 8-bit-encoding <decoder>
39 8-bit-encodings get-global at <decoder> ;
41 : create-encoding ( name -- word )
43 [ define-singleton-class ]
44 [ 8-bit-encoding add-mixin-instance ]
47 : load-encoding ( name iana-name file-name -- )
48 [ create-encoding dup ]
50 [ encoding-file flat-file>biassoc 8-bit boa ] tri*
51 swap 8-bit-encodings get-global set-at ;
55 SYNTAX: 8-BIT: scan scan scan load-encoding ;