FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Reader EOF?

 
Post new topic   Reply to topic     Forum Index -> Mango
View previous topic :: View next topic  
Author Message
Carlos



Joined: 19 Mar 2004
Posts: 396
Location: Canyon, TX

PostPosted: Sun May 07, 2006 7:22 am    Post subject: Reader EOF? Reply with quote

I couldn't find this in the docs. How can this be done without having to rely in exception throwing?

Code:

char c;
Reader reader = ...
try
   reader.get(c);
catch(Exception)
{
   // eof
}
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
kris



Joined: 27 Mar 2004
Posts: 1494
Location: South Pacific

PostPosted: Tue May 09, 2006 8:47 pm    Post subject: Reply with quote

The reader/writer facade is designed for IO where the content-layout is defined; for example, one might serialize a class, and then deserialize it. When the reader runs into a condition where 'expected' data is simply not available, an exceptional condition is raised.

In other words, the reader/writer pair are actually a mechanism for data formatting/parsing. To operate with a more loosly defined input protocol (where content may or may not be present), it's often more appropriate to use an Iterator instead. Alternately, one might peek at the remaining buffer content, to see if there's anything available before using the reader?

If you need something that sits in the middle between these two, let's talk Smile
Back to top
View user's profile Send private message
Carlos



Joined: 19 Mar 2004
Posts: 396
Location: Canyon, TX

PostPosted: Tue May 09, 2006 9:09 pm    Post subject: Reply with quote

The thing is I need to extract information from DBF files so I used Mango. According to some specs I read, the EOF is marked by the byte 0x1A, but in the files I was reading, that byte wasn't present so I kept getting an exception (EOF). So, I think sometimes that byte will be there but sometimes it won't, so I want to know if there's just one more byte before reading. Any ideas?
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
kris



Joined: 27 Mar 2004
Posts: 1494
Location: South Pacific

PostPosted: Tue May 09, 2006 11:52 pm    Post subject: Reply with quote

You can check the buffer to see if readable() > 0. If so, then you're still good, otherwise invoke buffer.fill() and check for Eof return. We should probably add a method that wraps this kind of thing, but there's some trickiness involved for other types of conduits such as Sockets (needs a timeout?).

Another approach is to use a MappedBuffer instead. That will memory-map your file as a virtual byte-array. That will make the Eof test simpler, since there's never any buffer.fill() needed.

A similar method is to simply read the entire file into a buffer to start with; the stock buffer is then equivalent to a MappedBuffer in many ways:

Code:
import mango.io.File;

auto file = new File ("myfile"); 
auto content = file.read ();
auto reader = new Reader (new Buffer (content, content.length));


Hope this helps Smile
Back to top
View user's profile Send private message
Carlos



Joined: 19 Mar 2004
Posts: 396
Location: Canyon, TX

PostPosted: Wed May 10, 2006 6:32 am    Post subject: Reply with quote

Quote:
You can check the buffer to see if readable() > 0. If so, then you're still good, otherwise invoke buffer.fill() and check for Eof return.


I wanted to do that but I didn't know how to call the buffer (I only have IConduit and Reader). I'm going to check the docs again.

Quote:
We should probably add a method that wraps this kind of thing, but there's some trickiness involved for other types of conduits such as Sockets (needs a timeout?).


I understand that.

Quote:
Another approach is to use a MappedBuffer instead. That will memory-map your file as a virtual byte-array. That will make the Eof test simpler, since there's never any buffer.fill() needed.

A similar method is to simply read the entire file into a buffer to start with; the stock buffer is then equivalent to a MappedBuffer in many ways:

Code:

import mango.io.File;

auto file = new File ("myfile");
auto content = file.read ();
auto reader = new Reader (new Buffer (content, content.length));



You know, I've always been reluctant to read an entire file to memory especially when I don't know how large it can be. Maybe I'm just being paranoid... hehe...

Quote:
Hope this helps Smile


Yeah, thanks.
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
kris



Joined: 27 Mar 2004
Posts: 1494
Location: South Pacific

PostPosted: Wed May 10, 2006 10:00 am    Post subject: Reply with quote

If you have a Reader, you can call getBuffer() on it Smile

I'm sometimes a bit wary of loading entire files also, which is partly why the MappedBuffer option is available.
Back to top
View user's profile Send private message
Carlos



Joined: 19 Mar 2004
Posts: 396
Location: Canyon, TX

PostPosted: Wed May 10, 2006 10:44 am    Post subject: Reply with quote

kris wrote:
If you have a Reader, you can call getBuffer() on it Smile


I must be becoming blind... Shocked Anyway, thanks.
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Mango All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group