// man what a friggin mess just to deal with ANY
// kind of mangled input and spit out some kinda lines
// uhmm...
        static XString FilterLines(ref object scratch, XString buffer)
        {
            //*data = (struct my_scratch_data*)scratch[0];
            my_scratch_data data = scratch as my_scratch_data;
            int             total_length;
            int             n;
            int             thisskip;

            if (data == null)
            {
                data          = new my_scratch_data();
                data.skip     = 0;
                data.linebuf  = null;
                data.lastread = false;
                scratch       = data;
            }
            thisskip = data.skip; // skip N characters in first buffer.
            if (buffer != null)
            {
                data.lastread = false;
                data.linebuf.Add(buffer);
            }
            else if (buffer == null)
            {
                if (data.lastread)
                {
                    XString final = null;
                    if (data.linebuf != null)
                    {
                        final = new XString(data.linebuf.firstseg.Text.Substring(data.skip));
                    }
                    data.linebuf = null;
                    scratch      = null;
#if LOG_LINES_READ
                    Log.log("Returning buffer [%s]", GetText(final));
#endif
                    return(final);
                }
            }

            buffer       = data.linebuf;
            total_length = 0;
            while (buffer != null)
            {
                // full new buffer, which may or may not add to prior segments...
                int    end      = 0;
                int    length   = buffer.firstseg.Text.Length;
                String chardata = buffer.firstseg.Text;
                //Log.log(  "Considering buffer %s" , GetText( buffer ) + data.skip );
                if ((length - thisskip) == 0)
                {
                    //buffer
                    buffer.Remove(buffer.firstseg);
                }
                else
                {
                    for (n = thisskip; n < length; n++)
                    {
                        if (chardata[n] == '\n' ||
                            chardata[n] == '\r')
                        {
                            if (chardata[n] == '\n')
                            {
                                end = 1;
                                n++;         // include this character.
                                //Log.log( "BLANK LINE - CONSUMED" );
                                break;
                            }
                            if (end > 0)      // \r\r is two lines too
                            {
                                break;
                            }
                            end = 1;
                        }
                        else if (end > 0)
                        {
                            // any other character... after a \r.. don't include the character.
                            break;
                        }
                    }
                }
                total_length += n - thisskip;
                if (end > 0)
                {
                    // new character, trim at -1 from here...
                    result = SegCreate(total_length);
                    int ofs;
                    buffer   = data.linebuf;
                    thisskip = data.skip;
                    n        = thisskip;
                    ofs      = 0;
                    while (ofs < total_length)
                    {
                        int len = GetTextSize(buffer);
                        if (len > (len - thisskip))
                        {
                            len = len - thisskip;
                        }
                        if (len > (total_length - ofs))
                        {
                            len = total_length - ofs;
                        }

                        MemCpy(GetText(result) + ofs
                               , GetText(buffer) + thisskip
                               , sizeof(TEXTCHAR) * len);
                        ofs += len;
                        n   += len;
                        if (ofs < total_length)
                        {
                            n        = 0;
                            thisskip = 0;
                            buffer   = NEXTLINE(buffer);
                        }
                    }
                    if (buffer)
                    {
                        data.skip = n;
                        LineRelease(SegBreak(buffer));
                        data.linebuf = buffer;
                    }
                    else
                    {
                        data.skip = 0;
                    }
                    GetText(result)[total_length] = 0;
                    //Log.log( "Considering buffer %s", GetText( result ) );
#ifdef LOG_LINES_READ
                    Log.log("Returning buffer [%s]", GetText(result));
#endif
                    return(result);
                }
                else
                {
                    //Log.log( "Had no end within the buffer, waiting for another..." );
                }
                thisskip = 0; // no more skips.
                buffer   = NEXTLINE(buffer);
            }
            data.lastread = TRUE;
            return(NULL);
        }