RosettaCodeData/Task/Bitwise-IO/Seed7/bitwise-io.seed7

47 lines
1.1 KiB
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
$ include "seed7_05.s7i";
include "bitdata.s7i";
2025-02-27 18:35:13 -05:00
const proc: writeAscii (inout msbOutBitStream: outStream, in string: ascii) is func
2023-07-01 11:58:00 -04:00
local
var char: ch is ' ';
begin
for ch range ascii do
if ch > '\127;' then
raise RANGE_ERROR;
else
2025-02-27 18:35:13 -05:00
putBits(outStream, ord(ch), 7);
2023-07-01 11:58:00 -04:00
end if;
end for;
end func;
2025-02-27 18:35:13 -05:00
const proc: finishWriteAscii (inout msbOutBitStream: outStream) is func
2023-07-01 11:58:00 -04:00
begin
2025-02-27 18:35:13 -05:00
putBits(outStream, 0, 7); # Write a terminating NUL char.
flush(outStream);
2023-07-01 11:58:00 -04:00
end func;
2025-02-27 18:35:13 -05:00
const func string: readAscii (inout msbInBitStream: inStream) is func
2023-07-01 11:58:00 -04:00
result
var string: stri is "";
local
var char: ch is ' ';
begin
2025-02-27 18:35:13 -05:00
repeat
ch := chr(getBits(inStream, 7));
2024-04-19 16:56:29 -07:00
if ch <> '\0;' then
2023-07-01 11:58:00 -04:00
stri &:= ch;
end if;
2025-02-27 18:35:13 -05:00
until ch = '\0;';
2023-07-01 11:58:00 -04:00
end func;
const proc: main is func
local
2025-02-27 18:35:13 -05:00
var msbOutBitStream: outStream is msbOutBitStream.value;
var msbInBitStream: inStream is msbInBitStream.value;
2023-07-01 11:58:00 -04:00
begin
2025-02-27 18:35:13 -05:00
writeAscii(outStream, "Hello, Rosetta Code!");
finishWriteAscii(outStream);
inStream := openMsbInBitStream(getBytes(outStream));
writeln(literal(readAscii(inStream)));
2023-07-01 11:58:00 -04:00
end func;