const type: lineFile is sub null_file struct
var file: baseFile is STD_NULL;
var string: line is "";
end struct;
const func file: openLine (in file: aFile) is func
result
var file: newFile is STD_NULL;
local
var lineFile: new_lineFile is lineFile.value;
begin
new_lineFile.baseFile := aFile;
newFile := toInterface(new_lineFile);
end func;
const proc: write (inout lineFile: outFile, in string: stri) is func
begin
outFile.line &:= stri;
end func;
const proc: writeln (inout lineFile: outFile) is func
begin
writeln(outFile.baseFile, outFile.line);
flush(outFile.baseFile);
outFile.line := "";
end func;
const proc: flush (inout lineFile: outFile) is func
begin
write(outFile.baseFile, outFile.line);
flush(outFile.baseFile);
outFile.line := "";
end func;
const func string: getln (inout lineFile: inFile) is func
result
var string: stri is "";
local
var char: ch is ' ';
begin
if inFile.line = "" then
ch := getc(inFile.baseFile);
while ch <> '\n' and ch <> EOF do
if ch = '\b' then
if stri <> "" then
stri := stri[ .. pred(length(stri))];
end if;
else
stri &:= str(ch);
end if;
ch := getc(inFile.baseFile);
end while;
inFile.bufferChar := ch;
else
if inFile.line[length(inFile.line)] = '\n' then
stri := inFile.line[ .. pred(length(inFile.line))];
inFile.bufferChar := '\n';
else
stri := inFile.line;
inFile.bufferChar := EOF;
end if;
inFile.line := "";
end if;
end func;
const func char: getc (inout lineFile: inFile) is func
result
var char: charRead is ' ';
begin
if inFile.line = "" then
inFile.line := getln(inFile) & "\n";
end if;
if inFile.line = "\n" and inFile.bufferChar = EOF then
charRead := EOF;
else
charRead := inFile.line[1];
inFile.line := inFile.line[2 .. ];
end if;
end func;
const func string: gets (inout lineFile: inFile, in integer: maxLength) is func
result
var string: striRead is "";
begin
if maxLength <= 0 then
if maxLength <> 0 then
raise RANGE_ERROR;
end if;
else
while length(inFile.line) < maxLength and inFile.bufferChar <> EOF do
inFile.line &:= getln(inFile) & "\n";
end while;
striRead := inFile.line[ .. maxLength];
inFile.line := inFile.line[succ(maxLength) .. ];
end if;
end func;