Task: CSV to HTML translation


C++: #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 ) {
   
   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 ;
}









Fortran:       SUBROUTINE CSVTEXT2HTML(FNAME,HEADED)	
Converts without checking field counts, or noting special characters.
       CHARACTER*(*) FNAME	
       LOGICAL HEADED		
       INTEGER MANY		
       PARAMETER (MANY=666)	
       CHARACTER*(MANY) ALINE	
       INTEGER MARK(0:MANY + 1)	
       INTEGER I,L,N		
       CHARACTER*2 WOT(2)	
       PARAMETER (WOT = (/"th","td"/))	
       INTEGER IT		
       INTEGER KBD,MSG,IN		
       COMMON /IOUNITS/ KBD,MSG,IN	
        OPEN(IN,FILE=FNAME,STATUS="OLD",ACTION="READ",ERR=661)	
        WRITE (MSG,1)			
    1   FORMAT ("<Table border=1>")	
        MARK(0) = 0		
        N = 0			

   10   READ (IN,11,END = 20) L,ALINE(1:MIN(L,MANY))	
   11   FORMAT (Q,A)		
        N = N + 1		
        IF (L.GT.MANY) THEN	
          WRITE (MSG,12) N,L,MANY	
   12     FORMAT ("Line ",I0," has length ",I0,"
          L = MANY			
        END IF			
        IF (N.EQ.1 .AND. HEADED) THEN	
          WRITE (MSG,*) "<tHead>"	
          IT = 1			
         ELSE				
          IT = 2			
        END IF			
        NCOLS = 0		
        DO I = 1,L		
          IF (ICHAR(ALINE(I:I)).EQ.ICHAR(",")) THEN	
            NCOLS = NCOLS + 1		
            MARK(NCOLS) = I		
          END IF			
        END DO			
        NCOLS = NCOLS + 1	
        MARK(NCOLS) = L + 1	
        WRITE (MSG,13)		
     1   (WOT(IT),				
     2    ALINE(MARK(I - 1) + 1:MARK(I) - 1),	
     3    WOT(IT),				
     4    I = 1,NCOLS),			
     5   "/tr"			
   13   FORMAT (" <tr>",666("<",A,">",A,"</",A,">"))	
        IF (N.EQ.1 .AND. HEADED) WRITE (MSG,*) "</tHead>"	
        GO TO 10		

   20   CLOSE (IN)		
        WRITE (MSG,21)		
   21   FORMAT ("</Table>")	
       RETURN			
Confusions.
  661   WRITE (MSG,*) "Can't open file ",FNAME	
      END			

      INTEGER KBD,MSG,IN
      COMMON /IOUNITS/ KBD,MSG,IN
      KBD = 5	
      MSG = 6	
      IN = 10	

      CALL CSVTEXT2HTML("Text.csv",.FALSE.)	
      WRITE (MSG,*)
      CALL CSVTEXT2HTML("Text.csv",.TRUE.)	
      END