Stdio.Buffer, need a way to revisit old positions in the output

"Stephen R. van den Berg" <[email protected]>
Newsgroups gmane.comp.lang.pike.devel,gmane.comp.lang.pike.user
Message-ID <[email protected]>
In perusing the Stdio.Buffer for pgsql NG, I find that I need
some way to:
a. Fill the output buffer.
b. Leave a spot for say a 4 byte integer.
c. Fill the output buffer some more.
d. Revisit spot (b).
e. Write out the newly calculated length.
f. Return back to the current tip of the buffer.

And all this has to work reliably even if during this, some part of
the output buffer is already flushed to a filedescriptor (but not beyond
the spot (b), the output code already takes care not to flush more
than that).

I was thinking something like:

int Stdio.Buffer->getaddpos();
Stdio.Buffer->setaddpos(int oldpos);

Whereas the numbers returned and used are only guaranteed to work
as long as the underlying buffer has not been trimmed in the meantime.

A typical sequence would then look like:

Stdio.Buffer b=Stdio.Buffer();
int oldpos,endpos;

b->add("abc");
b->add("d");
oldpos=b->getaddpos();
b->add_int32(0);                /* or: b->setaddpos(oldpos+4); */
b->add("fghijklm");             /* This part is generated dynamically */
b->output_to(fd,3);             /* Already outputs the initial abc */
endpos=b->getaddpos();
b->setaddpos(oldpos);
b->add_int32(endpos-oldpos-4);
b->setaddpos(endpos);
b->output_to(fd);

Any better ideas?
-- 
Stephen.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.