/*
 * @topic T00340 Apr 18 -- how to print HTML content withut tags v.1
 * @brief Program demonstrates <tt>&lt;sstream&gt</tt> header and <tt>stringstream</tt> class
*/
#include <iostream>
#include <string>
#include <sstream>
#include <cctype>

using namespace std;

int main()
{
    // Assume this is HTML that we have to parse:
    string str_html =
    "<html>"
        "<head>"
            "<title>TITLE</title>"
        "</head>"
    "<body>"
    "<p>PARAGRAPH</p>"
    "</body>"
    "</html>\n"
    ;

    // construct buffer to hold the HTML
    stringstream buffer( str_html );

    // Use unformatted input to read HTML character-by-character:
    bool flag_inside_tag = false;
    int onechar; // using int because char cannot represent EOF
    while ( ( onechar = buffer.get() ) != EOF ) {
        if ( onechar == '<') {
            // Encountered opening of an HTML tag
            flag_inside_tag = true;
            continue;
        }
        else if ( onechar == '>') {
            // Encountered closing of an HTML tag
            flag_inside_tag = false;
            cout << " ";
            continue;
        }
        else if ( flag_inside_tag == false ) {
            // Print everything outside of HTML tags
            std::cout.put( onechar );
        }
    }
    return 0;
}