[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: GCC 3.2 replacement for streammarker?



On Sunday 27 October 2002 02:35, Steve M. Robbins wrote:
> Hi,
>
> The geomview package needs the ability to seek backwards on
> a file regardless of whether it is a pipe, socket, tty, etc.
> Basically: fseek() that never fails.
>
> A pure C-code solution would be best.  Any suggestions?
>
>
> The current solution is to link in some C++ code that uses
> a streammarker object, which does exactly what is needed:
>
>     The GNU iostream library allows you to ask a streambuf to remember
          ^^^^^^^^^^^^
It's a non-portable extension, in other words. A quick grep revealed no such 
thing as a streammarker in 3.2 (part of 'old' stuff are still available in 
separated namespaces, but not that one).

However, standard C++ IOstreams provide means to seek, though there is no 
guarantee that the operation will not fail. The problem is especially when 
using non-storage devices like pipes or ttys, cause those have no way to 
seek. However, here is some code:

istream::pos_type const pos = mystream.tellg();
// do something with mystream
mystream.clear();
if(! mystream.seekg(pos))
    ...// handle error

>     This allows you to go back to this position
>     later, after reading further. You can back up arbitrary amounts,
>     even on unbuffered files or multiple buffers' worth, as long as
>     you tell the library in advance.
Doesn't work with this method. Still, there is a simple way of doing that 
which might be sufficient: copy parts of the istream to a stringstream and 
read from that one. stringstream it probably seekable without limit. This 
requires knowing in advance how many bytes are needed though ... 

I just took a look at the sources. It seems that the only thing there that 
uses C++ IOstreams is some weird way of wrapping access to a piece of memory 
in a FILE*. It does so in a way that makes me cringe. However, I don't think 
you should need that hack: in <stdio.h> is a function called fmemopen. This 
function however requires that you #define __USE_GNU ( because it is 
non-standard) to become visible. This should provide unlimited seeking.

What I don't get is where/why geomview operates on pipes/ttys ... maybe it 
doesn't and you're lucky? :)

hth
Uli



Reply to: