Encoding and Decoding, Part V

You don’t want your decoding program to waste time attempting to process input that isn’t in the proper format. That’s why the encoding program bothers to put a specific header as the first line of text. Therefore, one of the first tasks for the decoding program is to check for that specific header.

The following code builds upon the filter created in last week’s Lesson. Standard input is processed one line at a time. In this modification, however, special attention is paid to the first line of input. Here is the code, which includes the addition of the check_file() function:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LINE_LENGTH 73
#define TRUE 1
#define FALSE 0

void check_file(char *line);

int main()
{
    char line_buffer[LINE_LENGTH];
    char c;
    int buffer_index = 0;
    int first_line = TRUE;

    while(1)
    {
        c = getchar();
        if( c == EOF)
            break;
        line_buffer[buffer_index] = c;
        buffer_index++;
        if( buffer_index > LINE_LENGTH)
        {
            /* overflow condition */
            puts("\nInvalid hexcode line format");
            return(1);
        }
        if( c == '\n')
        {
            /* terminate string */
            line_buffer[buffer_index] = '\0';
            /* process first line separately */
            if(first_line)
            {
                check_file(line_buffer);
                first_line = FALSE;
            }
            /* display contents */
            printf("%s",line_buffer);
            /* reset index */
            buffer_index = 0;
        }
    }

    return(0);
}

void check_file(char *line)
{
    if( strncmp(line,"START HEX CODE",14) == 0 )
    {
        /* check version number here */
        /* offset 17 bytes */
        return;
    }
    else
    {
        puts("Improper hexcode file format");
        exit(1);
    }
}

This version of the code requires a few more header files, defines TRUE and FALSE (Lines 6 and 7), and declares the check_file() prototype (Line 9).

Line 16 sets the first_line variable to TRUE, indicating that the first line must be processed. An if test is made at Line 36, so the first line processed is sent off to the check_file() function. Then the first_file variable is reset to FALSE so that the first line processing doesn’t take place again.

I didn’t need to write check_file() as a function as it’s called only once. I made it a function to keep the main() function’s while loop more readable; it fits on a single screen.

The check_file() function starts at Line 51. An strncmp() function (Line 53) compares the first line of standard input to the text that should appear at the start of a hexcode-formatted document. If the test is positive, then the version number could be checked; I’ve placed comments in the code at Lines 55 and 56 where that process would take place. Otherwise the function returns at Line 57.

When the header doesn’t match, the else statements process. An error message is displayed at Line 61 and the program terminates at Line 62.

At this point, the input isn’t decoded. Before I do that modification to the code, I’ll add another tidbit, which is to check for the final line of formatted data. That code is presented in next week’s Lesson.

Leave a Reply