CTTL on
SourceForge |
Download
Latest |
Documentation
Index |
Library
News |
CVS
Repository |
Other
Links |
This sample utility removes html tags from the input file specified on the command line, and prints modified text on the screen. Program demonstrates search-and-replace parser in action. The parser compiles against mutable universe, and makes changes to the underlying text while parsing the input.
// search_replace.cpp //#define NDEBUG // define before assert.h to stop assertions from being compiled //#define CTTL_TRACE_EVERYTHING //define to turn tracing on //#define CTTL_TRACE_RULES //define to turn light tracing on #include <iostream> #include "cttl/cttl.h" #include "utils/fileio.h" using namespace cttl; template< typename UniverseT > struct search_replace_parser { typedef UniverseT universe_T; typedef typename UniverseT::strict_edge_T strict_universe_T; typedef typename UniverseT::node_T node_T; typedef typename UniverseT::string_T string_T; static size_t search( UniverseT& universe_ ) { return ( '<' + !symbol( '>' ) ).runtime_match( universe_ ); } static size_t replace( strict_universe_T& universe_ ) { universe_.text( "" ); return 0; } static size_t search_and_replace( UniverseT& universe_ ) { return ( *!! ( CTTL_STATIC_RULE( search ) & CTTL_STATIC_RULE( replace ) ) ).match( universe_ ); } }; // search_replace_parser< UniverseT > int main(int argc, char* argv[]) { if ( argc != 2 ) { std::cout << "Usage: program removes html tags from a file and prints the result:" << std::endl << '\t' << argv[ 0 ] << " path/filename.html" << std::endl ; return 1; } // construct input input<> inp; // load data from file file2string( argv[ 1 ], inp.text() ); assert( inp.length() ); // construct mutable universe typedef edge<> universe_T; universe_T universe( new_edge( inp ) ); // search for tags and delete them from the input string search_replace_parser< universe_T >::search_and_replace( universe ); // send modified input to the standard output std::cout << inp.text(); return 0; }
Another library sample, located in example/lambda/misc/inline_replace.cpp
, simplifies implementation of html tag removal program by using lambda expression
scalar( &universe_ ) = scalar( string_T( "" ) )
Lambda code is easier to write and maintain:
// inline_replace.cpp //#define NDEBUG // define before assert.h to stop assertions from being compiled //#define CTTL_TRACE_EVERYTHING //define to turn tracing on //#define CTTL_TRACE_RULES //define to turn light tracing on #include <iostream> #include "cttl/cttl.h" #include "lambda/lambda.h" #include "utils/fileio.h" using namespace cttl; template< typename UniverseT > struct inline_replace_parser { typedef typename UniverseT::string_T string_T; static size_t search_and_replace( UniverseT& universe_ ) { return ( *!!( ( '<' + !symbol( '>' ) ) & *( scalar( &universe_ ) = scalar( string_T( "" ) ) ) ) ).match( universe_ ); } }; // inline_replace_parser< UniverseT > int main(int argc, char* argv[]) { if ( argc != 2 ) { std::cout << "Usage: program removes html tags from a file and prints the result:" << std::endl << '\t' << argv[ 0 ] << " path/filename.html" << std::endl ; return 1; } // construct input input<> inp; // load data from file file2string( argv[ 1 ], inp.text() ); assert( inp.length() ); // construct mutable universe typedef edge<> universe_T; universe_T universe( new_edge( inp ) ); // search for tags and delete them from the input string inline_replace_parser< universe_T >::search_and_replace( universe ); // send modified input to the standard output std::cout << inp.text(); return 0; }
Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.