According to Alexis Mikhailov:
> Geoff Hutchison wrote:
> > 
> > At 11:10 AM -0600 12/8/99, Gilles Detillieux wrote:
> > >The ExternalParser::readLine() method has the advantage that it makes
> > >sure you don't end up with a truncated line, if an input line is bigger
> > >than your buffer.  So, maybe it's not such a bad example after all.
> > 
> > That was, actually my point. I didn't think of the cin.getline()
> > methods because, ... well, because I didn't think of them. :-)
> > 
> > Anyway, I would think that the readLine() method might not be a bad
> > base for the String >> operator.
> 
> Will this couple of functions (attached) go?
> 
> Alexis
> 
> int
> String::readLine(FILE *in)
> {
>     Length = 0;
>     allocate_fix_space(2048);
> 
>     while (fgets(Data + Length, Allocated - Length, in))
>     {
>       Length += strlen(Data + Length);
>       if (Length == 0)
>           continue;
>       if (Data[Length - 1] == '\n')
>       {
>           //
>           // A full line has been read.  Return it.
>           //
>           chop('\n');
>           return 1;

I think instead of the chop() and return above, you just want a "break;",
to be consistent with the >> operator below.

>       }
>       if (Allocated > Length + 1)
>       {
>           //
>           // Not all available space filled. Probably EOF?
>           //
>           
>           continue;
>       }
>       //
>       // Only a partial line was read. Increase available space in 
>       // string and read some more.
>       //
> 
>       reallocate_space(Allocated << 1);
>     }
>     chop('\n');

I guess the chop() is a judgement call - some may prefer to leave the '\n'
in the string, while some would prefer it taken out.  I guess for our
purposes, we always want it out, and it's easy to add it if we want.

> 
>     return Length > 0;

Should an empty line return 1 or 0?  This code will return 0 on any empty
line, not just at EOF.

> }
> 
> istream &
> operator >> (istream &in, String &line)
> {
>     line.Length = 0;
>     line.allocate_fix_space(2048);
> 
>     while (in.get(line.Data + line.Length, line.Allocated - line.Length))
>     {
>       line.Length += strlen(line.Data + line.Length);
>       int c = in.get();

I'm not clear on the semantics of ifstream::get() above, and why you need
two calls to it.  Wouldn't it make more sense to use in.getline() instead,
and keep the logic of this operator as close to the readLine() method
above as possible?  I believe ifstream::getline()'s semantics are identical
to fgets(), except that is works on an ifstream instead of a FILE.  BTW,
I think it's ifstream, and not istream.

>       if (c == '\n')
>       {
>           //
>           // A full line has been read.  Return it.
>           //
>           break;
>       }
>       if (line.Allocated > line.Length + 2)
>       {
>           //
>           // Not all available space filled. Probably EOF?
>           //
>           

Will this code add EOF to the Data?

>           line.Data[line.Length++] = char(c);
>           continue;
>       }
>       //
>       // Only a partial line was read. Increase available space in 
>       // string and read some more.
>       //
> 
>       line.reallocate_space(line.Allocated << 1);
>       line.Data[line.Length++] = char(c);
>     }
> 
>     return in;
> }


-- 
Gilles R. Detillieux              E-mail: <[EMAIL PROTECTED]>
Spinal Cord Research Centre       WWW:    http://www.scrc.umanitoba.ca/~grdetil
Dept. Physiology, U. of Manitoba  Phone:  (204)789-3766
Winnipeg, MB  R3E 3J7  (Canada)   Fax:    (204)789-3930

------------------------------------
To unsubscribe from the htdig3-dev mailing list, send a message to
[EMAIL PROTECTED] 
You will receive a message to confirm this. 

Reply via email to