Hello!

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;
        }
        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');

    return Length > 0;
}

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();
        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?
            //
            
            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;
}

------------------------------------
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