fbpx

C++: CSV to HTML Translation

Bjarne-stroustrup
 

Consider a simplified CSV format where all rows are separated by a newline and all columns are separated by commas. No commas are allowed as field data, but the data may contain other characters and character sequences that would normally be escaped when converted to HTML

The task is to create a function that takes a string representation of the CSV data and returns a text string of an HTML table representing the CSV data. Use the following data as the CSV text to convert, and show your output.

Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He’s not the messiah; he’s a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I’m his mother; that’s who!
The multitude,Behold his mother! Behold his mother!
#include <string>
#include <boost/regex.hpp>
#include <iostream>

std::string csvToHTML( const std::string & ) ;

int main( ) {
	std::string text = "Character,Speech\n" 
	"The multitude,The messiah! Show us the messiah!\n" 
	"Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>\n" 
	"The multitude,Who are you?\n" 
	"Brians mother,I'm his mother; that's who!\n" 
	"The multitude,Behold his mother! Behold his mother!\n" ;
	std::cout << csvToHTML( text ) ;
	return 0 ;
}

std::string csvToHTML( const std::string & csvtext ) {
	//the order of the regexes and the replacements is decisive!
	std::string regexes[ 5 ] = { "<" , ">" , "^(.+?)\\b" , "," , "\n" } ;
	const char* replacements [ 5 ] = { "&lt;" , "&gt;" , "    <TR><TD>$1" , "</TD><TD>", "</TD></TR>\n"  } ;  
	boost::regex e1( regexes[ 0 ] ) ; 
	std::string tabletext = boost::regex_replace( csvtext , e1 ,
	replacements[ 0 ] , boost::match_default | boost::format_all ) ;
	for ( int i = 1 ; i < 5 ; i++ ) {
		e1.assign( regexes[ i ] ) ;
		tabletext = boost::regex_replace( tabletext , e1 , replacements[ i ] , boost::match_default | boost::format_all ) ;
	}
	tabletext = std::string( "<TABLE>\n" ) + tabletext ;
	tabletext.append( "</TABLE>\n" ) ;
	return tabletext ;
}
Output:
 
<TABLE>
    <TR><TD>Character</TD><TD>Speech</TD></TR>
    <TR><TD>The multitude</TD><TD>The messiah! Show us the messiah!</TD></TR>
    <TR><TD>Brians mother</TD><TD>&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt;</TD></TR>
    <TR><TD>The multitude</TD><TD>Who are you?</TD></TR>
    <TR><TD>Brians mother</TD><TD>I'm his mother; that's who!</TD></TR>
    <TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR>
</TABLE>

SOURCE

Content is available under GNU Free Documentation License 1.2.