! 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 ] 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 ( file-name -- byte>ch ch>byte ) ascii file-lines process-contents [ byte>ch ] [ ch>byte ] bi ; TUPLE: 8-bit name decode encode ; : encode-8-bit ( char stream assoc -- ) swapd at* [ encode-error ] unless swap stream-write1 ; 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* ] [ nip ] if ; M: 8-bit decode-char decode>> decode-8-bit ; : make-8-bit ( word byte>ch ch>byte -- ) [ 8-bit construct-boa ] 2curry dupd curry define ; : define-8-bit-encoding ( name path -- ) >r in get create r> parse-file make-8-bit ; PRIVATE> [ "io.encodings.8-bit" in [ mappings [ full-path define-8-bit-encoding ] assoc-each ] with-variable ] with-compilation-unit