! Copyright (C) 2008 Daniel Ehrenberg ! See http://factorcode.org/license.txt for BSD license. USING: math.parser arrays io.encodings sequences kernel assocs hashtables io.encodings.ascii generic parser classes.tuple words io io.files splitting namespaces math compiler.units accessors ; IN: io.encodings.8-bit ; : process-contents ( lines -- assoc ) [ "#" split1 drop ] map harvest [ "\t" split 2 head [ 2 short tail hex> ] map ] map ; : byte>ch ( assoc -- array ) 256 replacement-char [ [ swapd set-nth ] curry assoc-each ] keep ; : ch>byte ( assoc -- newassoc ) [ swap ] assoc-map >hashtable ; : parse-file ( path -- byte>ch ch>byte ) lines process-contents [ byte>ch ] [ ch>byte ] bi ; SYMBOL: 8-bit-encodings TUPLE: 8-bit decode encode ; : encode-8-bit ( char stream assoc -- ) swapd at* [ swap stream-write1 ] [ nip encode-error ] if ; inline M: 8-bit encode-char encode>> encode-8-bit ; : decode-8-bit ( stream array -- char/f ) swap stream-read1 dup [ swap nth [ replacement-char ] unless* ] [ 2drop f ] if ; inline M: 8-bit decode-char decode>> decode-8-bit ; PREDICATE: 8-bit-encoding < word 8-bit-encodings get-global key? ; M: 8-bit-encoding 8-bit-encodings get-global at ; M: 8-bit-encoding 8-bit-encodings get-global at ; PRIVATE> [ mappings [ [ "io.encodings.8-bit" create ] [ encoding-file parse-file 8-bit boa ] bi* ] assoc-map [ keys [ define-symbol ] each ] [ 8-bit-encodings set-global ] bi ] with-compilation-unit