factor/library/io/stdio-binary.factor

66 lines
1.5 KiB
Factor
Raw Normal View History

! Copyright (C) 2003, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
2004-08-18 01:13:56 -04:00
IN: stdio
USING: kernel math ;
2004-08-18 01:13:56 -04:00
: read-little-endian-32 ( -- word )
read1
2004-08-26 19:37:22 -04:00
read1 8 shift bitor
read1 16 shift bitor
read1 24 shift bitor ;
: read-big-endian-32 ( -- word )
2004-08-26 19:37:22 -04:00
read1 24 shift
read1 16 shift bitor
read1 8 shift bitor
read1 bitor ;
: byte7 ( num -- byte ) -56 shift HEX: ff bitand ;
: byte6 ( num -- byte ) -48 shift HEX: ff bitand ;
: byte5 ( num -- byte ) -40 shift HEX: ff bitand ;
: byte4 ( num -- byte ) -32 shift HEX: ff bitand ;
2004-08-26 19:37:22 -04:00
: byte3 ( num -- byte ) -24 shift HEX: ff bitand ;
: byte2 ( num -- byte ) -16 shift HEX: ff bitand ;
: byte1 ( num -- byte ) -8 shift HEX: ff bitand ;
2004-08-18 01:13:56 -04:00
: byte0 ( num -- byte ) HEX: ff bitand ;
: write-little-endian-64 ( word -- )
2004-11-25 21:51:47 -05:00
dup byte0 write
dup byte1 write
dup byte2 write
dup byte3 write
dup byte4 write
dup byte5 write
dup byte6 write
byte7 write ;
: write-big-endian-64 ( word -- )
2004-11-25 21:51:47 -05:00
dup byte7 write
dup byte6 write
dup byte5 write
dup byte4 write
dup byte3 write
dup byte2 write
dup byte1 write
byte0 write ;
: write-little-endian-32 ( word -- )
2004-11-25 21:51:47 -05:00
dup byte0 write
dup byte1 write
dup byte2 write
byte3 write ;
2004-08-18 01:13:56 -04:00
: write-big-endian-32 ( word -- )
2004-11-25 21:51:47 -05:00
dup byte3 write
dup byte2 write
dup byte1 write
byte0 write ;
2004-08-18 01:13:56 -04:00
: write-little-endian-16 ( char -- )
2004-11-25 21:51:47 -05:00
dup byte0 write
byte1 write ;
2004-08-18 01:13:56 -04:00
: write-big-endian-16 ( char -- )
2004-11-25 21:51:47 -05:00
dup byte1 write
byte0 write ;