Peeking and unreading

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Peeking and unreading

Dirk Laurie-2
Many file formats divide naturally into groups of lines, such that you
only know that the group is done when you look at the first line of the
next group.

Is there a way to read a few characters of the next line without
advancing the file position, and then to re-read them with the rest
of the line when you know how to deal with it?

Or alternatively, is there a way to unread a line when you realize
it's not meant for the current group?

Or must I just maintain my own buffer?

Reply | Threaded
Open this post in threaded view
|

Re: Peeking and unreading

Paul K-2
Hi Dirk,

> Or alternatively, is there a way to unread a line when you realize
> it's not meant for the current group?

You can use fh:seek() to get the position before reading and then set
to that position after reading, so the next reading operation starts
there.

Using your own buffer is likely to be noticeably faster if you need to
read and seek many times.

Paul.

Reply | Threaded
Open this post in threaded view
|

Re: Peeking and unreading

Sean Conner
It was thus said that the Great Paul K once stated:
> Hi Dirk,
>
> > Or alternatively, is there a way to unread a line when you realize
> > it's not meant for the current group?
>
> You can use fh:seek() to get the position before reading and then set
> to that position after reading, so the next reading operation starts
> there.

  That only works if what you are reading from *is* a file.  Anything else
(io.stdin, a network connection, etc.) won't work as you cannot seek on
those types of devices.

  Buffering a line works for both seekable and non-seekable data sources.

  -spc (There is ungetc() in C, but you are only guarenteed one character of
        pushback ... )


Reply | Threaded
Open this post in threaded view
|

Re: Peeking and unreading

Dirk Laurie-2
2016-07-17 9:31 GMT+02:00 Sean Conner <[hidden email]>:

> It was thus said that the Great Paul K once stated:
>> Hi Dirk,
>>
>> > Or alternatively, is there a way to unread a line when you realize
>> > it's not meant for the current group?
>>
>> You can use fh:seek() to get the position before reading and then set
>> to that position after reading, so the next reading operation starts
>> there.
>
>   That only works if what you are reading from *is* a file.  Anything else
> (io.stdin, a network connection, etc.) won't work as you cannot seek on
> those types of devices.
>
>   Buffering a line works for both seekable and non-seekable data sources.
>
>   -spc (There is ungetc() in C, but you are only guarenteed one character of
>         pushback ... )

It has dawned on me that the specific format I need it for has lines starting
with a number, and if you know that number you know whether the group
is done, so I only need to buffer the number if I use file:read'n' before
file:read().

Reply | Threaded
Open this post in threaded view
|

Re: Peeking and unreading

Jorge Visca
Do you think that property is by chance, or it's some pattern? Ideas for
easy framing of messages in streams are always welcome...

Jorge

On 17/07/16 05:57, Dirk Laurie wrote:
> It has dawned on me that the specific format I need it for has lines
> starting with a number, and if you know that number you know whether
> the group is done, so I only need to buffer the number if I use
> file:read'n' before file:read().


Reply | Threaded
Open this post in threaded view
|

Re: Peeking and unreading

Dirk Laurie-2
2016-07-17 19:49 GMT+02:00 Jorge <[hidden email]>:

> Do you think that property is by chance, or it's some pattern? Ideas for
> easy framing of messages in streams are always welcome...

It's on purpose. It represents the depth-first traversal of a tree. The
number is the level of the node (i.e. distance from the root). Then
follows a tag to say what kind of node it is, and then the real data.
Some nodes have an identifier so that you can reach it by direct
access instead of traversal.