Change readstring() to use buffered input.
Change from `uread(0, c, 1)` to `read(c)`, so input goes through libpc's buffer. If input is a tty in Unix, this reduces the number of read(2) system calls from one per character to one per line. This change will become necessary in CP/M when I enable the line editor.
This commit is contained in:
parent
4a3b7be795
commit
b4be612832
|
@ -9,7 +9,6 @@ program hilo(input, output);
|
||||||
|
|
||||||
type
|
type
|
||||||
string = packed array [0..255] of char;
|
string = packed array [0..255] of char;
|
||||||
charstar = packed array [0..0] of char;
|
|
||||||
|
|
||||||
var
|
var
|
||||||
playing : Boolean;
|
playing : Boolean;
|
||||||
|
@ -30,47 +29,25 @@ function random(range : integer) : integer;
|
||||||
random := seed mod range;
|
random := seed mod range;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
{ Pascal doesn't provide string input, so we interface to the read() syscall
|
{ Pascal doesn't provide string input, so we read characters until the
|
||||||
and do it manually. But... we can't interface to read() directly because
|
end of line and put them in a string. }
|
||||||
that conflicts with a Pascal keyword. Luckily there's a private function
|
|
||||||
uread() in the ACK Pascal library that we can use instead. }
|
|
||||||
|
|
||||||
function uread(fd : integer; var buffer : charstar; count : integer) : integer;
|
|
||||||
extern;
|
|
||||||
|
|
||||||
function readchar : char;
|
|
||||||
var
|
|
||||||
c : charstar;
|
|
||||||
dummy : integer;
|
|
||||||
|
|
||||||
begin
|
|
||||||
c[0] := chr(0);
|
|
||||||
dummy := uread(0, c, 1);
|
|
||||||
readchar := c[0];
|
|
||||||
end;
|
|
||||||
|
|
||||||
procedure readstring(var buffer : string; var length : integer);
|
procedure readstring(var buffer : string; var length : integer);
|
||||||
var
|
var
|
||||||
finished : Boolean;
|
|
||||||
c : char;
|
c : char;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
write('> ');
|
write('> ');
|
||||||
|
|
||||||
length := 0;
|
length := 0;
|
||||||
finished := FALSE;
|
repeat
|
||||||
seed := 0;
|
|
||||||
while not finished do
|
|
||||||
begin
|
|
||||||
c := readchar;
|
|
||||||
if (ord(c) = 10) then
|
|
||||||
finished := true
|
|
||||||
else
|
|
||||||
begin
|
begin
|
||||||
|
read(c);
|
||||||
buffer[length] := c;
|
buffer[length] := c;
|
||||||
length := length + 1;
|
length := length + 1;
|
||||||
end
|
end
|
||||||
end;
|
until eoln;
|
||||||
|
readln; { discard end of line }
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure getname;
|
procedure getname;
|
||||||
|
|
Loading…
Reference in a new issue