Utilities/ExpandTab [ Functions ]

FUNCTION

Expand the tabs in a line of text.

SYNOPSIS

char               *ExpandTab(
    char *line )

INPUTS

line -- the line to be expanded tab_size -- global. RETURN pointer to the expanded line.

NOTE

This function is not reentrant.

SOURCE

{
    char               *cur_char = line;
    int                 n = 0;
    int                 jump = 0;
    char               *newLine = NULL;
    int                 lineBufLen = 1;
    int                 actual_tab = 0;

    lineBufLen = strlen( line ) + 1;

    if ( ( newLine = malloc( lineBufLen * sizeof( char ) ) ) == NULL )
    {
        RB_Panic( "Out of memory! ExpandTab()\n" );
    }

    for ( ; *cur_char; ++cur_char )
    {
        if ( *cur_char == '\t' )
        {
            int                 i;

            // Seek to actual tab stop position in tabstop table
            while ( ( tab_stops[actual_tab] <= n )
                    && ( actual_tab < ( MAX_TABS - 1 ) ) )
            {
                actual_tab++;
            }

            jump = tab_stops[actual_tab] - n;

            // If jump gets somehow negative fix it...
            if ( jump < 0 )
            {
                jump = 1;
            }

            lineBufLen += jump;
            if ( ( newLine = realloc( newLine, sizeof( char ) * lineBufLen ) )
                 == NULL )
            {
                RB_Panic( "Out of memory! ExpandTab()\n" );
            }
            for ( i = 0; i < jump; i++ )
            {
                newLine[n] = ' ';
                ++n;
            }
        }
        else
        {
            newLine[n] = *cur_char;
            ++n;
        }
    }
    newLine[n] = '\0';

    return newLine;
}