Commits

Neil Butterworth committed 11cafa5

added expressions to template command

  • Participants
  • Parent commits 9c5a7d4

Comments (0)

Files changed (5)

File csvfix/inc/csved_template.h

 									const CSVRow & row,
 									std::string & out );
 
+		std::string Eval( const CSVRow & row, const std::string & expr );
 		void ReadTemplate( const ALib::CommandLine & cmd );
 		std::string mTemplate;
 

File csvfix/src/csved_template.cpp

 #include "csved_template.h"
 #include "csved_strings.h"
 #include "csved_except.h"
+#include "a_expr.h"
 #include <fstream>
 
 using std::string;
 
 const char PINTRO = '{';
 const char POUTRO = '}';
+const char EVALCHR = '@';
 
 //----------------------------------------------------------------------------
 // Help text
 // a formatting placeholder. For exanple {2} will be replaced by column
 // two from the input row. Append placeholder value to the 'out'
 // parameter. Also updates the 'pos' param with new position in template.
+//
+// If the string in braces begins with the special EVALCHR character, it is
+// an expression in the expression language and needs to be evaluated.
 //---------------------------------------------------------------------------
 
 void TemplateCommand :: HandleSpecialChars( char c, unsigned int & pos,
 			t = mTemplate[ pos++ ];
 		}
 
+		if( ns.size() && ns[0] == EVALCHR ) {    // it's an expression
+			out += Eval( row, ns );
+			return;
+		}
+
 		if ( ! ALib::IsInteger( ns ) ) {
 			CSVTHROW( "Invalid placeholder: " << "{" << ns << "}" );
 		}
 	}
 }
 
+//----------------------------------------------------------------------------
+// Evaluate a string as an expression. The string will start with EVALCHR
+// which needs to be removed.
+//----------------------------------------------------------------------------
+
+string TemplateCommand :: Eval( const CSVRow & row, const string & expr ) {
+	ALib::Expression ex;
+	for( unsigned int i = 0; i < row.size(); i++ ) {
+		ex.AddPosParam( row[i] );
+	}
+	return ex.Evaluate( expr.substr( 1 ) );
+}
+
 //---------------------------------------------------------------------------
 // Replace all placeholders enclosed in braces in the template with the
 // coresponding column from therow. If there is no such column, ignore.

File csvfix/tests/correct/template.test

 Forename is George and surname is Elliot
 Forename is Virginia and surname is Woolf
 Forename is Oscar and surname is Wilde
+Field #1: Charles
+Concat #1 and #2: Charles Dickens
+Random number: 0.910744
+Field #1: Jane
+Concat #1 and #2: Jane Austen
+Random number: 0.464085
+Field #1: Herman
+Concat #1 and #2: Herman Melville
+Random number: 0.554779
+Field #1: Flann
+Concat #1 and #2: Flann O'Brien
+Random number: 0.434718
+Field #1: George
+Concat #1 and #2: George Elliot
+Random number: 0.929075
+Field #1: Virginia
+Concat #1 and #2: Virginia Woolf
+Random number: 0.28888
+Field #1: Oscar
+Concat #1 and #2: Oscar Wilde
+Random number: 0.509514

File csvfix/tests/data/tplexpr.txt

+Field #1: {1}
+Concat #1 and #2: {@$1 . " " .  $2}
+Random number: {@random()}

File csvfix/tests/tests/template.test

 # test template
 $CSVED template -tf data/template.txt  data/names.csv 
+$CSVED template -tf data/tplexpr.txt  data/names.csv