Skip to content

Commit

Permalink
FixSyntax amended to cope with the nightmare that is the previously u…
Browse files Browse the repository at this point in the history
…ncleaned (by me, manually) HTML files. Works a lot better now!
  • Loading branch information
NormanDunbar committed Aug 16, 2016
1 parent 21a30b8 commit 5f05963
Showing 1 changed file with 58 additions and 148 deletions.
206 changes: 58 additions & 148 deletions tools/fixSyntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@
//
// Of course, sometimes Location: and Syntax: happen to be on the
// same line, which is, to be blunt, a bit of a bugger!
//
// So far I've seen the following:
//
// 1. Syntax: blah Location: Blah<eol>
//
// 2. Syntax: blah<eol>
// Location: Blah<eol>
//
// 3. Syntax: blah<eol>
// blah blah blah Location: Blah<eol>
//
// 4. Syntax: blah<eol>
// blah blah blah<eol>
// blah blah blah<eol>
// blah blah blah Location: Blah<eol>
//
// In any of the above, Location's line may or may not be followed by a blank.


using std::string;
using std::getline;
Expand All @@ -31,7 +49,8 @@ void doSyntax(const size_t pos);
void doLocation(const size_t pos);
void doBoth(const size_t posSyntax, const size_t posLocation);

string a_line;
string a_line; // Storage for each line, without a line feed.
string buffer; // Storage for entire Syntax: + Location: section, with linefeeds.

string tableSeparator =
"+----------+-------------------------------------------------------------------+";
Expand All @@ -51,161 +70,52 @@ int main (int argc, char *argv[])
size_t posSyntax = a_line.find("Syntax:", 0, 7);
size_t posLocation = a_line.find("Location:", 0, 9);

// Did we find both of them in one line?
if ((posSyntax != string::npos) && (posLocation != string::npos)) {
doBoth(posSyntax, posLocation);

// We continue because we do Syntax first, then Location
// And doLocation writes out all the lines it reads.
// Did we find Syntax?
if (posSyntax == string::npos) {
// Not found, write the line to output and continue reading.
// We should trim leading spaces though.
cout << a_line << endl;
continue;
}

// Try just the Syntax: line then? If we find a Location line
// in doSyntax, it gets correctly processed so we need a new line.
if (posSyntax != string::npos) {
doSyntax(posSyntax);
continue;
}

// Check for "Location:". We need a new line afterwards.
posLocation = a_line.find("Location:", 0, 9);
if (posLocation != string::npos) {
doLocation(posLocation);
continue;
}

// Just write everything else out.
cout << a_line << endl;
}
}


void doSyntax(const size_t pos)
{
// Process the Syntax: part of the fixup.
// Read lines until we get a blank, (??) or a line
// with "Location:" in it. Keep everything then
// slice out each individual option for syntax.

string buffer;
size_t posLocation;

cout << tableSeparator << endl;
cout << "| Syntax | "; // 13 characters, no linefeed.

// Keep the rest of the Syntax: line.
buffer = a_line.substr(pos + 7, string::npos);

while (1) {
// Read a few more lines. We shouldn't hit EOF here! (FLW!)
getline(cin, a_line);
posLocation = a_line.find("Location:", 0, 9);
if ((posLocation != string::npos) || (a_line.empty())) {
break;
} else {
buffer += (" " + a_line);
}
}

// We have the full text of the syntax line in our buffer.
if (buffer.size() < 80-2-13) {
// Just write the whole line out.

cout << buffer;
cout << setw(80-13-buffer.size()) << "|" << endl;
} else {
// Need to split the buffer at any " or " text.

while (1) {
posLocation = buffer.find(" or ", 0, 4);
if (posLocation != string::npos) {
// We have a split.
string temp = buffer.substr(0, posLocation + 3);
cout << temp;
cout << setw(80-13-temp.size()) << "|" << endl;
buffer = buffer.substr(posLocation + 4, string::npos);

// Do we need another line?
if (buffer.find(" or ", 0, 4) != string::npos) {
cout << "| | ";
}
} else {
// No splitter. Write out the rest of the buffer.
cout << "| | " << buffer;
cout << setw(80-13-buffer.size()) << "|" << endl;
// We must have found Syntax:, read until we find Location: too.
buffer = "";
while (posLocation == string::npos) {
buffer += a_line += " ";
getline(cin, a_line);

// Should never happen ..... (FLW!)
if (cin.eof()) {
break;
}

posLocation = a_line.find("Location:", 0, 9);
}

// Add the last line to the buffer.
buffer += a_line;

// posSyntax is fine.
// PosLocation is relevant to the final line only.
// We need to find it in relation to buffer.
posLocation = buffer.find("Location:", posSyntax + 7, 9);

// Write out the syntax/location table.
// We assume the whole lot fits in 80 characters.
cout << tableSeparator << endl;
string temp = buffer.substr(posSyntax + 7, posLocation - posSyntax - 7);
cout << "| Syntax | " << temp;
cout << setw(80 - 13 - temp.size()) << "|" << endl;
cout << tableSeparator << endl;
temp = buffer.substr(posLocation + 9, string::npos);
cout << "| Location | " << temp;
cout << setw(80 - 13 - temp.size()) << "|" << endl;
cout << tableSeparator << endl << endl;



}

cout << tableSeparator << endl;

// Did we find a "Location:" line? If so, it's in a_line
// so process it because we "continue" on return from here
// and would lose the newly read line.
if (posLocation != string::npos) {
doLocation(posLocation);
}

}


void doLocation(const size_t pos)
{
// Process the Location: part of the fixup.
// There should be a tableSeparator just above from Syntax.

string buffer;

cout << "| Location | "; // 13 characters, no linefeed.

// Keep the rest of the Location: line.
buffer = a_line.substr(pos + 9, string::npos);

while (1) {
// Read a few more lines. We shouldn't hit EOF here! (FLW!)
getline(cin, a_line);
if (a_line.empty()) {
break;
} else {
buffer += (" " + a_line);
}
}

// We have the full text of the Location line in our buffer.
if (buffer.size() < 80-2-13) {
// Just write the whole line out.
cout << buffer;
cout << setw(80-13-buffer.size()) << "|" << endl;
} else {
// Need to split the buffer at WHAT EXACTLY???.
cout << buffer << " |" << endl;
}

cout << tableSeparator << endl;

// And write out the blank line we just read.
cout << a_line << endl;
}


void doBoth(const size_t posSyntax, const size_t posLocation)
{
// We have Syntax: and Location: on the same line.
// Split off the Location: part, keeping "Location:".
string locBuffer = a_line.substr(posLocation, string::npos);

// Keep just the Syntax: part.
a_line = a_line.substr(posSyntax + 7, posLocation - 8);

// Write out the syntax table cell and the syntax details.
cout << tableSeparator << endl;
cout << "| Syntax | " << a_line;
cout << setw(80-13-a_line.size()) << "|" << endl;
cout << tableSeparator << endl;

// Then do the Location details.
a_line = locBuffer;
doLocation(0);
}

0 comments on commit 5f05963

Please sign in to comment.