Bitwise IO: Difference between revisions

2,259 bytes added ,  5 years ago
Added Seed7 example
(Added Seed7 example)
Line 2,417:
puts "fail!"
end</lang>
 
=={{header|Seed7}}==
The Seed7 library [http://seed7.sourceforge.net/libraries/bitdata.htm bitdata.s7i] defines
several functions to do bitwise I/O. Bitwise data can be read from (or written to) a string or a file.
The direction of bits can be from LSB (least significant bit) to MSB (most significant bit) or vice versa.
In the program below the functions
[http://seed7.sourceforge.net/libraries/bitdata.htm#putBitsMsb(inout_file,inout_integer,in_var_integer,in_var_integer) putBitsMsb] and [http://seed7.sourceforge.net/libraries/bitdata.htm#getBitsMsb(inout_file,inout_integer,in_var_integer) getBitsMsb] are used.
 
<lang seed7>$ 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;</lang>
 
{{out}}
<pre>
"Hello, Rosetta Code!"
</pre>
 
=={{header|Tcl}}==