Thursday, March 28, 2024
HomeC ProgrammingCorrectly Padding Areas and Tab Widths

Correctly Padding Areas and Tab Widths


The duty for final week’s Lesson was to transform tabs in addition to areas. The issue is that tab stops aren’t thought-about: On the terminal, a tab character generates a variable variety of areas primarily based on the place the following tab cease place is positioned. It isn’t a set worth.

The default tab cease on a Linux terminal is ready to eight areas. Like on a typewriter or phrase processor, when a tab character is encountered, the terminal generates areas vital to maneuver the cursor to the following tab cease. In Determine 1, you see how the tab stops are set, and the way spacing is calculated to place the cursor when a tab character is encountered.

Determine 1. How tab stops work on the terminal. Areas are added to line-up textual content on the subsequent interval.

It’s potential to change the terminal’s tab stops. The tabs command units the interval, which suggests a consumer can alter the tab stops to a worth aside from eight. For my sconvert utility, nevertheless, I assume the default worth. Numerous trickery will be employed to acquire the present tab cease worth, however that isn’t the purpose of this train.

Two updates are required for the code to calculate the correct variety of areas to output to line up textual content at a tab cease. The primary is to trace the present cursor place or offset from the beginning of the road. The second is to find out what number of areas are vital to maneuver the cursor to the following tab cease.

For the cursor offset, I take advantage of int variable offset. It’s initialized to zero at the beginning of every line, then incremented every time a personality is output, which incorporates the   HTML code for a non-breakable area. The newline ('n') and carriage return ('r') characters are additionally intercepted to reset the offset worth to zero.

The expression I take advantage of to calculate areas to the following tab cease is:

areas = TAB_STOP-(offsetpercentTAB_STOP);

The offset MOD TAB_STOP calculation should be subtracted from the TAB_STOP worth to acquire the variety of areas to insert for the cursor to succeed in the following tab cease. The areas worth is then used to generate area characters ( ) to fill the hole. Right here is my up to date sconvert code:

2023_04_15-Lesson.c


#embody <stdio.h>


#outline TAB_STOP 8
#outline LINE_LEN 80

int fundamental()
{
    int ch,offset,areas,x;

    offset = 0;
    whereas(1)
    {
        ch = getchar();
        if( ch==EOF )
            break;
        change(ch)
        {
            case ' ':
                printf("&nbsp;");
                offset++;
                break;
            case 't':
                areas = TAB_STOP-(offsetpercentTAB_STOP);
                for( x=0; x<areas; x++ )
                {
                    offset++;
                    printf("&nbsp;");
                }
                break;
            case 'n':
            case 'r':
                putchar(ch);
                offset = 0;
                break;
            default:
                putchar(ch);
                offset++;
        }
        if( offset==LINE_LEN )
            offset = 0;
    }

    return(0);
}

The 2 outlined constants, TAB_STOP and LINE_LEN, are assumptions primarily based on normal terminal settings: eight-character tab stops and an 80-character line width.

Variable offset is initialized earlier than the countless whereas loop.

The code’s switch-case construction is now extra concerned. Every character output modifies the offset worth, with the newline and carriage return characters resetting the worth.

For the tab, variable areas represents the areas to maneuver ahead the cursor by utilizing the expression outlined earlier on this publish. It’s vital to make use of this worth and never offset straight as variable offset is modified throughout the for loop that outputs the areas (or &nbsp; HTML codes).

After the switch-case construction, a take a look at is made to see whether or not offset is bigger than the road size, which occurs for an extended line that overflows. If that’s the case, variable offset is once more reset to zero.

Right here’s a pattern run, filtering output from the days program utilized in final week’s Lesson:

Monday  0
Tuesday 1
Wednesday       2
Thursday        3
Friday  4
Saturday        5
Sunday  6

Screenshot

Determine 1. Textual content output with tabs could not line up completely. (Tab stops set each eight positions.)

For reference, Determine 1 is proven close by. The output is correctly transformed, which suggests the sconvert program is one step nearer to being full. All that continues to be is accounting for the particular characters: <, >, &. I cowl this last replace in subsequent week’s Lesson.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments