2009-11-12 17:38:21 -05:00
|
|
|
! Copyright (C) 2008 Daniel Ehrenberg, Doug Coleman.
|
2008-03-21 12:30:13 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-03-29 00:00:20 -04:00
|
|
|
USING: math.parser arrays io.encodings sequences kernel assocs
|
2008-03-29 04:34:48 -04:00
|
|
|
hashtables io.encodings.ascii generic parser classes.tuple words
|
2008-12-17 19:22:48 -05:00
|
|
|
words.symbol io io.files splitting namespaces math
|
2009-02-03 18:32:05 -05:00
|
|
|
compiler.units accessors classes.singleton classes.mixin
|
2009-11-12 17:38:21 -05:00
|
|
|
io.encodings.iana fry simple-flat-file lexer ;
|
2008-03-21 12:30:13 -04:00
|
|
|
IN: io.encodings.8-bit
|
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
2008-04-06 20:03:00 -04:00
|
|
|
: encoding-file ( file-name -- stream )
|
2009-02-23 22:40:17 -05:00
|
|
|
"vocab:io/encodings/8-bit/" ".TXT" surround ;
|
2008-03-21 14:01:50 -04:00
|
|
|
|
2008-06-19 00:29:56 -04:00
|
|
|
SYMBOL: 8-bit-encodings
|
2009-11-12 19:36:20 -05:00
|
|
|
8-bit-encodings [ H{ } clone ] initialize
|
2008-06-19 00:29:56 -04:00
|
|
|
|
2009-03-03 00:19:06 -05:00
|
|
|
TUPLE: 8-bit biassoc ;
|
2008-03-21 12:30:13 -04:00
|
|
|
|
2008-03-26 14:41:09 -04:00
|
|
|
: encode-8-bit ( char stream assoc -- )
|
2009-03-03 00:19:06 -05:00
|
|
|
swapd value-at
|
|
|
|
[ swap stream-write1 ] [ encode-error ] if* ; inline
|
2008-03-21 12:30:13 -04:00
|
|
|
|
2009-03-03 00:19:06 -05:00
|
|
|
M: 8-bit encode-char biassoc>> encode-8-bit ;
|
2008-03-21 12:30:13 -04:00
|
|
|
|
2009-03-03 00:19:06 -05:00
|
|
|
: decode-8-bit ( stream assoc -- char/f )
|
|
|
|
swap stream-read1
|
|
|
|
[ swap at [ replacement-char ] unless* ]
|
|
|
|
[ drop f ] if* ; inline
|
2008-03-21 12:30:13 -04:00
|
|
|
|
2009-03-03 00:19:06 -05:00
|
|
|
M: 8-bit decode-char biassoc>> decode-8-bit ;
|
2008-03-21 12:30:13 -04:00
|
|
|
|
2009-02-03 18:32:05 -05:00
|
|
|
MIXIN: 8-bit-encoding
|
2008-06-12 04:49:29 -04:00
|
|
|
|
2008-06-19 00:29:56 -04:00
|
|
|
M: 8-bit-encoding <encoder>
|
|
|
|
8-bit-encodings get-global at <encoder> ;
|
2008-06-12 04:49:29 -04:00
|
|
|
|
2008-06-19 00:29:56 -04:00
|
|
|
M: 8-bit-encoding <decoder>
|
|
|
|
8-bit-encodings get-global at <decoder> ;
|
2008-06-12 04:49:29 -04:00
|
|
|
|
2009-02-03 18:32:05 -05:00
|
|
|
: create-encoding ( name -- word )
|
2009-11-12 19:36:20 -05:00
|
|
|
create-in
|
2009-02-03 18:32:05 -05:00
|
|
|
[ define-singleton-class ]
|
|
|
|
[ 8-bit-encoding add-mixin-instance ]
|
|
|
|
[ ] tri ;
|
|
|
|
|
2009-11-12 17:38:21 -05:00
|
|
|
: load-encoding ( name iana-name file-name -- )
|
|
|
|
[ create-encoding dup ]
|
|
|
|
[ register-encoding ]
|
|
|
|
[ encoding-file flat-file>biassoc 8-bit boa ] tri*
|
|
|
|
swap 8-bit-encodings get-global set-at ;
|
|
|
|
|
2008-03-21 12:30:13 -04:00
|
|
|
PRIVATE>
|
|
|
|
|
2009-11-12 17:38:21 -05:00
|
|
|
SYNTAX: 8-BIT: scan scan scan load-encoding ;
|