September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -1,6 +1,6 @@
The aim of this task is to write functions (or create a class if your
language is Object Oriented and you prefer) for reading and writing sequences of
bits. While the output of a <tt>asciiprint "STRING"</tt> is the ASCII byte sequence
bits, most significant bit first. While the output of a <tt>asciiprint "STRING"</tt> is the ASCII byte sequence
"S", "T", "R", "I", "N", "G", the output of a "print" of the bits sequence
0101011101010 (13 bits) must be 0101011101010; real I/O is performed always
''quantized'' by byte (avoiding endianness issues and relying on underlying

View file

@ -0,0 +1,48 @@
function compress7(inio, outio)
nextwritebyte = read(inio, UInt8) & 0x7f
filled = 7
while !eof(inio)
inbyte = read(inio, UInt8)
write(outio, UInt8(nextwritebyte | inbyte << filled))
nextwritebyte = inbyte >> (8 - filled)
filled = (filled + 7) % 8
if filled == 0
if eof(inio)
break
end
nextwritebyte = read(inio, UInt8) & 0x7f
filled = 7
end
end
if filled != 0
write(outio, UInt8(nextwritebyte))
end
end
function expand7(inio, outio)
newbyte = read(inio, UInt8)
write(outio, UInt8(newbyte & 0x7f))
residualbyte::UInt8 = newbyte >> 7
filled = 1
while !eof(inio)
inbyte = read(inio, UInt8)
write(outio, UInt8((residualbyte | inbyte << filled) & 0x7f))
residualbyte = inbyte >> (7 - filled)
filled = (filled + 1) % 7
if filled == 0
write(outio, UInt8(residualbyte & 0x7f))
residualbyte = 0
end
end
end
str = b"These bit oriented I/O functions can be used to implement compressors and decompressors."
ins = IOBuffer(str)
outs = IOBuffer()
newouts = IOBuffer()
compress7(ins, outs)
seek(outs,0)
expand7(outs, newouts)
println("Initial string of length $(length(str)): ", String(ins.data))
println("Compressed to length $(length(outs.data)) on line below:\n", String(outs.data))
println("Decompressed string: ", String(newouts.data))

View file

@ -0,0 +1,103 @@
enum FN, V, BITS -- fields of a bitwiseioreader/writer
function new_bitwiseio(string filename, mode)
integer fn = open(filename,mode)
return {fn,0,0} -- ie {FN,V=0,BITS=0}
end function
function new_bitwiseiowriter(string filename)
return new_bitwiseio(filename,"wb")
end function
function new_bitwiseioreader(string filename)
return new_bitwiseio(filename,"rb")
end function
function write_bits(sequence writer, integer v, bits)
integer {fn,wv,wb} = writer,
p2 = power(2,bits), ch
if v!=and_bits(v,p2*2-1) then ?9/0 end if
wv = wv*p2+v
wb += bits
while wb>=8 do
wb -= 8
p2 = power(2,wb)
ch = floor(wv/p2)
puts(fn,ch)
wv -= ch*p2
end while
if wv>=#100 then ?9/0 end if
if wb>=8 then ?9/0 end if
writer[V]= wv
writer[BITS]= wb
return writer
end function
function close_bitwiseiowriter(sequence writer)
integer {fn,wv,wb} = writer
if wb then
if wb>=8 then ?9/0 end if -- sanity check
writer = write_bits(writer,0,8-wb)
end if
if writer[V]!=0 then ?9/0 end if
if writer[BITS]!=0 then ?9/0 end if
close(fn)
writer[FN]=-1
return writer
end function
function read_bits(sequence reader, integer bits)
integer {fn,rv,rb} = reader, ch, p2
while bits>rb do
ch = getc(fn)
if ch=-1 then return {-1,reader} end if
rv = rv*#100+ch
rb += 8
end while
rb -= bits
p2 = power(2,rb)
ch = floor(rv/p2)
rv -= ch*p2
reader[V]= rv
reader[BITS]= rb
return {ch,reader}
end function
function as_hexb(string s, fmt="%02x ")
-- helper funtion, returns hex string, or binary if fmt="%08b "
string res = ""
for i=1 to length(s) do
res &= sprintf(fmt,s[i])
end for
return trim(res)
end function
constant test = "This is a test."
--constant test = "This is a test"
--constant test = "abcdefghijk"
--constant test = "STRING"
--constant test = "This is an ascii string that will be crunched, written, read and expanded."
printf(1,"\"%s\" as bytes: %s (length %d)\n",{test,as_hexb(test),length(test)})
printf(1," original bits: %s\n",{as_hexb(test,"%08b ")})
sequence writer = new_bitwiseiowriter("test.bin")
for i=1 to length(test) do
writer = write_bits(writer,test[i],7)
end for
writer = close_bitwiseiowriter(writer)
integer fn = open("test.bin","rb")
string bytes = get_text(fn,GT_WHOLE_FILE)
printf(1,"Written bitstream: %s\n",{as_hexb(bytes,"%08b ")})
printf(1,"Written bytes: %s (length %d)\n",{as_hexb(bytes),length(bytes)})
close(fn)
sequence reader = new_bitwiseioreader("test.bin")
bytes = ""
integer ch
while true do
{ch,reader} = read_bits(reader,7)
if ch=-1 then exit end if
bytes &= ch
end while
printf(1,"\"%s\" as bytes: %s (length %d)\n",{bytes,as_hexb(bytes),length(bytes)})

View file

@ -0,0 +1,60 @@
$ include "seed7_05.s7i";
include "bitdata.s7i";
include "strifile.s7i";
const proc: initWriteAscii (inout file: outFile, inout integer: bitPos) is func
begin
outFile.bufferChar := '\0;';
bitPos := 0;
end func;
const proc: writeAscii (inout file: outFile, inout integer: bitPos, in string: ascii) is func
local
var char: ch is ' ';
begin
for ch range ascii do
if ch > '\127;' then
raise RANGE_ERROR;
else
putBitsMsb(outFile, bitPos, ord(ch), 7);
end if;
end for;
end func;
const proc: finishWriteAscii (inout file: outFile, inout integer: bitPos) is func
begin
write(outFile, chr(ord(outFile.bufferChar)));
end func;
const proc: initReadAscii (inout file: outFile, inout integer: bitPos) is func
begin
bitPos := 8;
end func;
const func string: readAscii (inout file: inFile, inout integer: bitPos, in integer: length) is func
result
var string: stri is "";
local
var char: ch is ' ';
begin
while not eof(inFile) and length(stri) < length do
ch := chr(getBitsMsb(inFile, bitPos, 7));
if inFile.bufferChar <> EOF then
stri &:= ch;
end if;
end while;
end func;
const proc: main is func
local
var file: aFile is STD_NULL;
var integer: bitPos is 0;
begin
aFile := openStrifile;
initWriteAscii(aFile, bitPos);
writeAscii(aFile, bitPos, "Hello, Rosetta Code!");
finishWriteAscii(aFile, bitPos);
seek(aFile, 1);
initReadAscii(aFile, bitPos);
writeln(literal(readAscii(aFile, bitPos, 100)));
end func;