factor/basis/io/encodings/8-bit/8-bit.factor

58 lines
1.6 KiB
Factor
Raw Normal View History

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.
USING: arrays assocs biassocs kernel io.encodings math.parser
sequences hashtables io.encodings.ascii generic parser
classes.tuple words words.symbol io io.files splitting
namespaces math compiler.units accessors classes.singleton
classes.mixin 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 )
"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
8-bit-encodings [ H{ } clone ] initialize
2008-06-19 00:29:56 -04:00
TUPLE: 8-bit { biassoc biassoc read-only } ;
2008-03-21 12:30:13 -04:00
: 8-bit-encode ( char 8-bit -- byte )
biassoc>> value-at [ encode-error ] unless* ; inline
2008-03-21 12:30:13 -04:00
M: 8-bit encode-char
swap [ 8-bit-encode ] dip stream-write1 ;
2008-03-21 12:30:13 -04:00
M: 8-bit encode-string
swap [ '[ _ 8-bit-encode ] B{ } map-as ] dip stream-write ;
2008-03-21 12:30:13 -04:00
M: 8-bit decode-char
swap stream-read1 dup
[ swap biassoc>> at [ replacement-char ] unless* ]
[ 2drop f ]
if ;
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 )
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>
SYNTAX: 8-BIT: scan-token scan-token scan-token load-encoding ;