Analyser/Find_End_Marker [ Functions ]

FUNCTION

Scan and store all lines from a source file until an end marker is found.

SYNOPSIS

static int Find_End_Marker(
    FILE *document,
    struct RB_header *new_header )

INPUTS

OUTPUT

RESULT

SOURCE

{
    int                 found = FALSE;
    unsigned int        no_lines = 0;
    unsigned int        max_no_lines = 10;
    struct RB_header_lines *lines = NULL;
    char               *dummy;

    lines = malloc( max_no_lines * sizeof( struct RB_header_lines ) );
    if ( lines == NULL )
    {
        RB_Panic( "Out of memory! %s()\n", "Find_End_Marker" );
    }

    while ( !feof( document ) )
    {
        RB_FreeLineBuffer(  );
        myLine = RB_ReadWholeLine( document, line_buffer, &readChars );
        ++line_number;          /* global linecounter, koessi */
        if ( RB_Is_Begin_Marker( myLine, &dummy ) )
        {
            /* Bad... found a begin marker but was expecting to
               find an end marker.  Panic... */
            found = FALSE;
            return found;
        }
        else if ( RB_Is_End_Marker( myLine ) )
        {
            RB_Say( "Found end marker \"%s\"", SAY_DEBUG, myLine );
            found = TRUE;
            break;
        }
        else
        {
            unsigned int        n;
            char               *line;

            line = RB_StrDup( myLine );
            n = strlen( line );
            assert( n > 0 );
            assert( line[n - 1] == '\n' );
            /* Strip CR */
            line[n - 1] = '\0';
            // Copy the line itself
            lines[no_lines].line = line;
            // and also save the original source line number
            lines[no_lines].line_number = line_number;
            ++no_lines;
            if ( no_lines == max_no_lines )
            {
                max_no_lines *= 2;
                lines =
                    realloc( lines,
                             max_no_lines *
                             sizeof( struct RB_header_lines ) );

                if ( lines == NULL )
                {
                    RB_Panic( "Out of memory! %s()\n", "Find_End_Marker" );
                }
            }
        }
    }

    new_header->no_lines = no_lines;
    new_header->lines = lines;

    return found;
}