- A filter is a program that reads standard input,
modifies that input,
and then generates standard output.
It's something the C language does really well,
and of all the code I've produced for this course,
I feel that my code for this solution
is the most C-like.
For this code, input must be chopped up
and based on how the text is terminated.
In a CSV file the terminators
are a comma for table cell,
and an end of line character for a table row.
Because output is determined by the terminator,
the input must be stored as it's received.
Only when a terminating character floats in
can the code generate proper output,
say a table row instead of a table cell.
To store input,
I use a ring buffer
carefully called ring_buffer in the code.
The buffer size is set to 256 characters
which should be enough for a table cell,
and if not the constant size can be adjusted.
Two pointers start and end
track the ring buffer.
A while loop reads standard input.
For the first row,
starting HTML table tags are output.
Otherwise, the terminating character in the CSV file
is checked in a switch structure.
That character can be either a carriage return,
line feed, or a combination of both.
If a comma is found,
the HTML close and open cell tags are output.
Otherwise, the character input is stored
in the ring buffer
and the while loop continues
as long as input arrives.
Because the CSV file may not end
on a line termination character,
an if test checks to see whether or not any
remaining characters linger in the ring buffer.
If so, they're flushed out
then the table tags are output
and the filters stops.
The show_text function
dumps the text in the ring buffer,
because the end pointer
references a null character.
A while loop increments
the start pointer to that position,
displaying characters along the way.
If the start pointer exceeds the ring buffer size,
it's reset back to the buffer's base address.
This is pretty much how a ring buffer works.
On the input side, back in the main function,
the end pointer is also reset
to the buffer's base address
when it exceeds the buffer's size.
The HTML tag output function generates table tags
and displays them at proper indentation
by using tab characters.
To make the code readable,
I use enumerated constants
for the HTML tag names.
These are defined earlier
in the code as shown here,
and are used in the main functions while loop
which again makes the code more readable.
My observation is that
anything you can do on your C code
to make it more readable is good thing,
yet I recognize
that when it comes to C language programmers,
my opinion here might not be in the majority.