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

95 lines
2.7 KiB
Factor
Raw Normal View History

2008-03-21 12:30:13 -04:00
! Copyright (C) 2008 Daniel Ehrenberg
! 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
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
io.encodings.iana fry ;
2008-03-21 12:30:13 -04:00
IN: io.encodings.8-bit
<PRIVATE
CONSTANT: mappings {
2009-02-03 18:32:05 -05:00
! encoding-name iana-name file-name
{ "latin1" "ISO_8859-1:1987" "8859-1" }
{ "latin2" "ISO_8859-2:1987" "8859-2" }
{ "latin3" "ISO_8859-3:1988" "8859-3" }
{ "latin4" "ISO_8859-4:1988" "8859-4" }
{ "latin/cyrillic" "ISO_8859-5:1988" "8859-5" }
{ "latin/arabic" "ISO_8859-6:1987" "8859-6" }
{ "latin/greek" "ISO_8859-7:1987" "8859-7" }
{ "latin/hebrew" "ISO_8859-8:1988" "8859-8" }
{ "latin5" "ISO_8859-9:1989" "8859-9" }
{ "latin6" "ISO-8859-10" "8859-10" }
{ "latin/thai" "TIS-620" "8859-11" }
{ "latin7" "ISO-8859-13" "8859-13" }
{ "latin8" "ISO-8859-14" "8859-14" }
{ "latin9" "ISO-8859-15" "8859-15" }
{ "latin10" "ISO-8859-16" "8859-16" }
{ "koi8-r" "KOI8-R" "KOI8-R" }
{ "windows-1252" "windows-1252" "CP1252" }
{ "ebcdic" "IBM037" "CP037" }
{ "mac-roman" "macintosh" "ROMAN" }
}
2008-03-21 12:30:13 -04:00
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-03-21 12:30:13 -04:00
: process-contents ( lines -- assoc )
2008-05-14 00:36:45 -04:00
[ "#" split1 drop ] map harvest
2008-06-19 00:29:56 -04:00
[ "\t" split 2 head [ 2 short tail hex> ] map ] map ;
2008-03-21 12:30:13 -04:00
: byte>ch ( assoc -- array )
256 replacement-char <array>
[ '[ swap _ set-nth ] assoc-each ] keep ;
2008-03-21 12:30:13 -04:00
: ch>byte ( assoc -- newassoc )
[ swap ] assoc-map >hashtable ;
: parse-file ( path -- byte>ch ch>byte )
ascii file-lines process-contents
2008-03-21 12:30:13 -04:00
[ byte>ch ] [ ch>byte ] bi ;
2008-06-19 00:29:56 -04:00
SYMBOL: 8-bit-encodings
TUPLE: 8-bit decode encode ;
2008-03-21 12:30:13 -04:00
: encode-8-bit ( char stream assoc -- )
2008-08-22 23:07:59 -04:00
swapd at*
[ swap stream-write1 ] [ nip encode-error ] if ; inline
2008-03-21 12:30:13 -04:00
2008-06-19 00:29:56 -04:00
M: 8-bit encode-char encode>> encode-8-bit ;
2008-03-21 12:30:13 -04:00
: decode-8-bit ( stream array -- char/f )
2008-08-22 23:07:59 -04:00
swap stream-read1 dup
[ swap nth [ replacement-char ] unless* ] [ 2drop f ] if ; inline
2008-03-21 12:30:13 -04:00
2008-06-19 00:29:56 -04:00
M: 8-bit decode-char decode>> 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 )
"io.encodings.8-bit" create
[ define-singleton-class ]
[ 8-bit-encoding add-mixin-instance ]
[ ] tri ;
2008-03-21 12:30:13 -04:00
PRIVATE>
2008-03-27 00:32:41 -04:00
[
2008-06-19 00:29:56 -04:00
mappings [
2009-02-03 18:32:05 -05:00
first3
[ create-encoding ]
[ dupd register-encoding ]
2008-06-19 00:29:56 -04:00
[ encoding-file parse-file 8-bit boa ]
2009-02-03 18:32:05 -05:00
tri*
] H{ } map>assoc
8-bit-encodings set-global
2008-03-27 00:32:41 -04:00
] with-compilation-unit