From: <sg...@us...> - 2003-11-17 20:38:08
|
Update of /cvsroot/libfunutil/libfunutil/client/s11nconvert In directory sc8-pr-cvs1:/tmp/cvs-serv32552/client/s11nconvert Added Files: in.maser maser.cpp Log Message: egg --- NEW FILE: in.maser --- <!DOCTYPE SerialTree> <build_rules class="NONE"> <formatter class="as_is"> <text>### this is as-is data.</text> </formatter> <assign class="NONE"> <key>foo</key> <value>var</value> <append>y</append> </assign> <commenter class="NONE"> <prefix># </prefix> <text>this is a comment</text> </commenter> <rule class="NONE"> <deps>all</deps> <name>default</name> </rule> <rule class="NONE"> <name>all</name> <command class="NONE"> <minus>true</minus> <code>echo hello world</code> </command> <command class="NONE"> <plus>true</plus> <code>echo ${MAKE}</code> </command> <command class="NONE"> <at>false</at> <code>$(CXX) --help</code> </command> </rule> <include class="NONE"> <ignore_error>y</ignore_error> <path>/path/to/file</path> </include> <rule class="NONE"> <name>no-op</name> </rule> </build_rules> --- NEW FILE: maser.cpp --- /** A demo/proof-of-concept/test application for s11n, demonstrating the use of s11n_node is a poor man's replacement for an XML --> XSLT transformation. */ #include <cassert> #include <iostream> #include <fstream> #include <string> #include <stack> #include <list> #include <sstream> #include <algorithm> #include <memory> // auto_ptr #include <sstream> #include <stdlib.h> // random() #include <time.h> // time() #include <unistd.h> // isatty() #include <s11n/argv_parser.h> #include <s11n/string_util.h> // translate_entities() // #include <iterator> // for use with std::copy() // #include <s11n/s11n_io.h> #include <s11n/s11n-macros.h> // COUT/CERR #include <s11n/s11n_globals.h> // library_version() #include <s11n/s11n_node.h> #include <s11n/node_loader.h> #include <s11n/Serializable.h> #include <s11n/Serializer.h> #if HAVE_CONFIG_H # include "config.h" // S11N_LIBRARY_VERSION, PACKAGE_{VERSION,NAME} #endif #ifndef SERIALIZABLE_TYPE # define SERIALIZABLE_TYPE s11n::Serializable #endif #ifndef SERIALIZABLE_TYPE_STRING # define SERIALIZABLE_TYPE_STRING "s11n::Serializable" #endif using namespace s11n; using namespace s11n; using namespace std; #define APP_VERSION std::string("$Revision: 1.1 $") #define APP_NAME "s11nconvert" #define VERBOSE if( ARGV::args().get_bool( "v", ARGV::args().get_bool( "verbose", false ) ) ) CERR typedef s11n::argv_parser ARGV; #define QUIETLY if( ! ARGV::args().get_bool( "q", ARGV::args().get_bool( "quiet", false ) ) ) CERR // ^^^^ output unless -q is set class MakeSerializer : public basic_serializer { public: MakeSerializer(){}; virtual ~MakeSerializer(){}; virtual s11n_node * deserialize( std::istream & ) { return NULL; } void serialize_command( const s11n_node & node, std::ostream & os ) const { std::string prefix; if( node.get_bool( "at", true ) ) prefix += '@'; if( node.get_bool( "plus", false ) ) prefix += '+'; if( node.get_bool( "minus", false ) ) prefix += '-'; os << "\t" << prefix << node.get( "code", "ERROR" ) << std::endl; } void serialize_rule( const s11n_node & node, std::ostream & os ) const { os << node.get( "name", "ERROR" )<<":"; const s11n_node::child_list_type & childs = node.children(); std::ostringstream depbuff; std::ostringstream cmdbuff; if( node.is_set( "deps" ) ) { depbuff << " " << node.get_string( "deps" ); } else if( 0 == childs.size() ) { os << std::endl; return; } typedef s11n_node::child_list_type::const_iterator IT; IT it = childs.begin(); IT et = childs.end(); const s11n_node * ch = 0; for( ; it != et; ++it ) { ch = (*it); if( "command" == ch->name() ) { serialize_command( *ch, cmdbuff ); continue; } } os << depbuff.str() << "\n"; os << cmdbuff.str(); os << std::endl; } void serialize_comment( const std::string & comment, std::ostream & os, const std::string & prefix ) const { if( comment.empty() ) return; std::string cmt = prefix + comment; static std::map<std::string,std::string> xmap; static bool donethat = false; if( (!donethat) && (donethat=true) ) { xmap["\n"] = std::string("\n") + prefix; } s11n::translate_entities( cmt, xmap ); os << cmt << std::endl; } virtual bool serialize( const s11n_node & node, std::ostream & os ) const { const s11n_node::child_list_type & childs = node.children(); static std::string make; static bool donethat = false; if( (!donethat) && (donethat=true) ) { path_finder bin; const char * p = getenv( "PATH" ); if( p ) bin.path( p ); make = argv_parser::args().get_string( "make" ); if( make.empty() ) make = bin.find( "make" ); if( make.empty() ) make = bin.find( "gmake" ); if( make.empty() ) make = "/path/to/make"; } os << "#!" << make << "\n"; os << "# generated by MakeSerializer\n"; // if( node.is_set( "usercode" ) ) // { // os << node.get_string( "usercode" ) << std::endl; // } typedef s11n_node::child_list_type::const_iterator IT; IT it = childs.begin(); IT et = childs.end(); const s11n_node * ch = 0; std::string name; std::string impl; std::string tmpstr; for( ; it != et; ++it ) { ch = (*it); name = ch->name(); impl = ch->impl_class(); if( "rule" == name ) { this->serialize_rule( *ch, os ); continue; } if( "assign" == name ) { if( ch->get_bool( "append", false ) ) tmpstr = " += "; else tmpstr = " = "; os << ch->get( "key", "ERROR" ) << tmpstr << ch->get( "value", "ERROR" ) << std::endl; continue; } if( "include" == name ) { if( ch->get_bool( "ignore_error", false ) ) os << '-'; os << "include " << ch->get( "path", "ERROR" ) << std::endl; continue; } if( "commenter" == name ) { serialize_comment( ch->get_string( "text" ) , os, ch->get( "prefix", "# " ) ); continue; } if( "formatter" == name ) { if( "as_is" == impl ) { os << ch->get_string( "text" ) << std::endl; continue; } CERR << "warning: unknown formatter: " << name << std::endl; continue; } if( ch->is_set("as_is") ) { os << ch->get( "as_is", "ERROR" ) << std::endl; continue; } CERR << "warning: unknown element: " << name << std::endl; } os << std::endl; return true; } }; void show_version( std::ostream & out ) { out << APP_NAME << " " << APP_VERSION << std::endl; out << "s11n library version: " << std::endl; out << "\tCompiled for: " << S11N_LIBRARY_VERSION << std::endl; out << "\tRunning with: " << s11n::library_version() << std::endl; out << "Serializer interface: " << SERIALIZABLE_TYPE_STRING << std::endl; out << "License: " << PACKAGE_LICENSE << std::endl; } int main( int argc, char **argv ) { srandom( ::time(NULL) ); if( S11N_LIBRARY_VERSION != s11n::library_version() ) { CERR << "Warning: your libs11n does not match the one this was compiled against. " << "This might not work at all!" << std::endl; show_version( std::cerr ); } enum Errors { NoError = 0, ErrorHelp = 0, ErrorVersion = 0, ErrorLoadFailed = 1, ErrorSaveFailed = 2, ErrorDeserializeFailed = 3, ErrorNoFileSpecified = 4 }; s11n::argv_parser & args = s11n::argv_parser::args( argc, argv ); args.set_help( "f filename", "input filename. Same as --file." ); args.set_help( "o filename", "output filename. Filename of - means stdout. Remember to also use -oFORMAT" ); args.set_help( "q, quiet", "disable some warnings." ); args.set_help( "v, verbose", "enable some extra output." ); if( "s11n::Serializable" == SERIALIZABLE_TYPE_STRING ) { args.set_help( "sopath path", "Sets the classloader path. Use a :-delimited list." ); } if( args.is_set( "version" ) ) { show_version( std::cout ); return ErrorVersion; } if ( args.is_help_set() ) { std::cout << args.dump_help() << std::endl; std::cout << "\nPossible exit codes:\n"; #define ERRUR(C,Desc) std::cout << "\t" << C << " = " << Desc << std::endl; ERRUR(ErrorLoadFailed,"Loading input file failed."); ERRUR(ErrorSaveFailed,"Saving output file failed."); ERRUR(ErrorDeserializeFailed,"Deserialization failed (only with -d flag)."); ERRUR(ErrorNoFileSpecified,"No input file was specified."); std::cout << std::endl; return ErrorHelp; } s11n_node::debug_level( args.get_bool( "nodedebug", false ) ? 1 : 0 ); class_loader_debug_level( args.get_bool( "cldebug", false ) ? 1 : 0 ); if( args.is_set( "sopath" ) ) { class_loader<MakeSerializer>().path(true).path( args.get_string( "sopath" ) ); } // CERR << "debug level: " << s11n_node::debug_level() << std::endl; string infilename; if( 2 == argc ) infilename = argv[1]; else infilename = args.get_string( "file", args.get_string( "f", "" ) ); if( infilename.empty() ) { CERR << "You must supply a file name using -f or --file." << std::endl; return ErrorNoFileSpecified; } typedef auto_ptr<s11n_node> NODE; NODE node = NODE( s11n::node_loader::load_node( infilename ) ); if( ! node.get() ) { CERR << "Error loading file '"<<infilename<<"'!"<<std::endl; return ErrorLoadFailed; } MakeSerializer outer; outer.serialize( *node, std::cout ); // std::ostream * os = 0; // string ofilename = args.get_string( "o", "" ); return NoError; }; /** notes: grep class names from SerialTree txt files: grep class= INFILE | cut -d'=' -f2 | sort -u | \ perl -ne \ 'chomp; print "CLASSLOADER\::register_factory( \"".$_."\", FACTORY_CLASS::new_instance );\n";' */ |