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
|
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-03-03 00:19:06 -05:00
|
|
|
io.encodings.iana fry simple-flat-file ;
|
2008-03-21 12:30:13 -04:00
|
|
|
IN: io.encodings.8-bit
|
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
2009-02-23 22:40:17 -05:00
|
|
|
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" }
|
2009-11-05 01:35:16 -05:00
|
|
|
{ "windows-1250" "windows-1250" "CP1250" }
|
2009-02-03 18:32:05 -05:00
|
|
|
{ "windows-1252" "windows-1252" "CP1252" }
|
|
|
|
{ "ebcdic" "IBM037" "CP037" }
|
|
|
|
{ "mac-roman" "macintosh" "ROMAN" }
|
2009-02-23 22:40:17 -05:00
|
|
|
}
|
2008-03-21 12:30:13 -04:00
|
|
|
|
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-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 )
|
|
|
|
"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 ]
|
2009-03-03 00:19:06 -05:00
|
|
|
[ encoding-file flat-file>biassoc 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
|