Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A reallocating readline wrapper

69 views
Skip to first unread message

luser- -droog

unread,
Nov 6, 2011, 1:46:15 AM11/6/11
to
This procedure wraps readline in a stopped context and retries
with a larger and larger buffer until readline completes without
stopping. It then replaces the returned substring with the
complete accumulated line.

It assumes that readline will not encounter any other error than
rangecheck. Note that /readaline takes the /name of the buffer,
not the buffer itself.

%!

%file /str readaline str bool
/readaline { 2 dict begin /@s exch def /n 0 def
@s load
{
{
readline
} stopped { % f s
(readline error: expanding buffer...)=
@s dup load % f s @s s_
dup length 2 mul string % f s @s s_ s'
dup 0 4 -1 roll % f s @s s' s' 0 s_
putinterval % f s @s s' % s' += s_
store % f s % *@s *= 2
length /n exch n add def % f % n += sn
@s load dup n exch length n sub getinterval %f s
}{ % s bool
exch % b s
@s load exch length n add 0 exch getinterval % b s'
exch % s' b
exit
} ifelse
} loop
end } def

/f currentfile def
/s 1 string def
f /s readaline
12345678901234567890
pstack
0 new messages