Making a Table Formatter ------ - ----- --------- Olivia went to work for State Report Writers Inc. and found herself writing a lot of plain ASCII text files containing tables that looked like: +-------------+--------+--------+--------+ | Year | 2001 | 2002 | 2003 | +-------------+--------+--------+--------+ | Population | 37,452 | 37,459 | 37,620 | +-------------+--------+--------+--------+ | Number of | 13,645 | 13,652 | 13,684 | | Households | | | | +-------------+--------+--------+--------+ Getting things all lined up was becoming a real pain, so she wants a program to do it for her. The program takes as input a text file whose tables are defective, and outputs the file with the defects corrected. The permitted defects are: (1) Lines beginning with + can have any garbage after the +, and will be converted into proper row separating lines. (2) If the first character of a table line is not + or |, it will be assumed that an initial | is missing from the line. (5) The |'s and the text in a line need not be properly aligned, except a | that should not appear at the very beginning of a line may not be moved to the beginning of the line. (4) A trailing sequence of |'s and spaces may be missing from a line. (5) There may be empty columns in the table (columns all of whose entries are blank). These empty columns should be deleted. (6) The last + line of the table may be omitted. The program reads its input, outputs table lines after reformatting them, adds a table end + line if necessary, and outputs exact copies of any non-table lines. A table begins with a line whose first character is +, and ends just before the next blank line or file end. Text in a table box should be separated by at least one space from any |. Within each line, the text in a box should be left adjusted if it contains any letters, and right adjusted otherwise. Note that text in a box may be left adjusted on one line and right adjusted on another line; each line is treated independently as far as text adjustment is concerned. Any spaces embedded inside the text (i.e., not next to |'s or line ends) should be preserved. All table lines should have the same length, and this should be the minimum length con- sistent with the given rules. To simplify coding the only whitespace character allowed in a line is the single space character, and lines are not permitted to end with space characters. Thus a blank line will be completely empty. This rule applies to input lines, and you must apply it to output lines. If you get only the spacing wrong, your program will be given the `Formatting Error' score. Input ----- The text to be reformatted. This text can contain any number of tables. No input line will be longer than 80 characters. The text ends with an end of file. The input will be such that every table contains at least one non-empty column. The input ends with an end of file. Output ------ The input text after it is reformatted. Note that output lines may be longer than 80 columns due to reformatting, but the input will be such that no output line is longer than 132 characters. Sample Input ------ ----- FLEAS IN THE MIDDLE COUNTIES by Dr. Troubledstat edited by Olivia Oliviana + County | 2001 | 2002 | 2003 + Upper Middle | 4,589,290 | 2,976,384 | 10,000,000 + Middle Middle | 7,671,004 | 5,804,027 | 20,000,000 + Lower Middle | 8,612,920 | 5,790,468 | 1,764,893 + This is a TEST TABLE +98435632-6987234899-8g544285-9245724||-- |0123456789 ||0123456789 123 || $789.87 | A123 +++++++++++++++++++++ | H21 || x y z ||| foo |||*&^%$#@ Sample Output ------ ------ FLEAS IN THE MIDDLE COUNTIES by Dr. Troubledstat edited by Olivia Oliviana +---------------+-----------+-----------+------------+ | County | 2001 | 2002 | 2003 | +---------------+-----------+-----------+------------+ | Upper Middle | 4,589,290 | 2,976,384 | 10,000,000 | +---------------+-----------+-----------+------------+ | Middle Middle | 7,671,004 | 5,804,027 | 20,000,000 | +---------------+-----------+-----------+------------+ | Lower Middle | 8,612,920 | 5,790,468 | 1,764,893 | +---------------+-----------+-----------+------------+ This is a TEST TABLE +------------+------------+ | 0123456789 | 0123456789 | | 123 | $789.87 | | A123 | | +------------+------------+ | H21 | x y z | | foo | | | | *&^%$#@ | +------------+------------+ File: tableformatter.txt Author: Bob Walton Date: Wed Oct 15 09:21:16 EDT 2008 The authors have placed this file in the public domain; they make no warranty and accept no liability for this file. RCS Info (may not be true date or author): $Author: walton $ $Date: 2008/10/15 13:25:02 $ $RCSfile: tableformatter.txt,v $ $Revision: 1.9 $