RoodyXML.cxx

Go to the documentation of this file.
00001 //
00002 // RoodyXML.cxx
00003 //
00004 // $Id$
00005 //
00006 
00007 #include <iostream>
00008 #include <ios>
00009 #include <time.h>
00010 #include <string.h>
00011 
00012 #include "RoodyXML.h"
00013 
00014 RoodyXML::RoodyXML()
00015 {
00016   version_ = "1.0";
00017   encoding_ = "ISO-8859-1";
00018 }
00019 
00020 RoodyXML::~RoodyXML()
00021 {}
00022 
00023 std::ofstream &RoodyXML::OpenFileForWrite( char const *file )
00024 {
00025   if( outputFile_.is_open() )outputFile_.close();
00026   std::ios_base::openmode mode = std::ios_base::out|std::ios_base::trunc;
00027   outputFile_.open( file, mode );
00028   //
00029   if( outputFile_ )
00030   {
00031     outputFile_ << "<?xml version=\"" << version_
00032                 << "\" encoding=\"" << encoding_ << "\"?>\n";
00033     /*
00034       << "<!DOCTYPE roody [\n"
00035       << "  <!ELEMENT roody (online?,file*,graphics?,canvas*,group*)>\n"
00036       << "  <!ELEMENT online (host,port?)>\n"
00037       << "  <!ELEMENT graphics (zones?,refresh?)>\n"
00038       << "  <!ELEMENT zones (#PCDATA)>\n"
00039       << "  <!ELEMENT refresh (#PCDATA)>\n"
00040       << "  <!ELEMENT canvas (zones?,histogram*)>\n"
00041       << "  <!ELEMENT group (name,histogram*)>\n"
00042       << "  <!ELEMENT name (#PCDATA)>\n"
00043       << "  <!ELEMENT histogram (name,source)>\n"
00044       << "  <!ELEMENT source (#PCDATA)>\n"
00045       << "  <!ELEMENT host (#PCDATA)>\n"
00046       << "  <!ELEMENT port (#PCDATA)>\n"
00047       << "  <!ELEMENT file (#PCDATA)>\n"
00048       << "]>\n";
00049     */
00050     time_t now;
00051     time( &now );
00052     std::string temp( ctime(&now) );
00053     temp.erase( temp.begin()+temp.size()-1 );
00054     outputFile_ << "<!-- created by Roody on " << temp << " -->\n";
00055   }
00056   return outputFile_;
00057 }
00058 
00059 std::string RoodyXML::Encode( const std::string &src )
00060 {
00061   std::string result;
00062   std::size_t length = src.size();
00063   for( std::size_t i=0; i<length; ++i )
00064   {
00065     switch ( src[i] )
00066     {
00067       case '<':
00068         result += "&lt;";
00069         break;
00070       case '>':
00071         result += "&gt;";
00072         break;
00073       case '&':
00074         result += "&amp;";
00075         break;
00076       case '\"':
00077         result += "&quot;";
00078         break;
00079       case '\'':
00080         result += "&apos;";
00081         break;
00082       default:
00083         result += src[i];
00084     }
00085   }  
00086   return result;
00087 }
00088 
00089 bool RoodyXML::OpenFileForRead( const char *file )
00090 {
00091   char error[256];
00092   int errline = 0;
00093   documentNode_ = mxml_parse_file( const_cast<char*>(file), error, sizeof(error), &errline );
00094   if( !documentNode_ )
00095   {
00096     std::cerr << error << std::endl;
00097     return false;
00098   }
00099   int i = FindNode( documentNode_, 0, "roody" );
00100   if( i < 0 )
00101   {
00102     std::cerr << "roody node not found in file: \"" << file << "\"" << std::endl;
00103     return false;
00104   }
00105   roodyNode_ = &documentNode_->child[i];
00106   return true;
00107 }
00108 
00109 int RoodyXML::FindNode( PMXML_NODE parent, int startIndex, char const *nameToFind )
00110 {
00111   int numberOfSiblings = parent->n_children;
00112   for( int i=startIndex; i<numberOfSiblings; ++i )
00113   {
00114     if( !strcmp(parent->child[i].name,nameToFind) )return i;
00115   }
00116   return -1;
00117 }
00118 
00119 PMXML_NODE RoodyXML::GetNode(PMXML_NODE parent, int index)
00120 {
00121   if (parent==0 || index<0)
00122     return 0;
00123 
00124   return &parent->child[index];
00125 }
00126 
00127 std::string RoodyXML::GetNodeString(PMXML_NODE parent, int index)
00128 {
00129   if (parent==0 || index<0)
00130     return "";
00131 
00132   const char* s = parent->child[index].value;
00133   if (!s)
00134     s = "";
00135 
00136   return s;
00137 }
00138 
00139 /*
00140 void RoodyXML::PrintTree( int level )
00141 {
00142   PrintTree( level, documentNode_ );
00143 }
00144 
00145 void RoodyXML::PrintTree( int level, PMXML_NODE current )
00146 {
00147   while( current )
00148   {
00149     for( int i=0; i<level; ++i )std::cout << "  ";
00150     std::cout << "current " << current->name << ", type " << current->node_type
00151               << ", children " << (void*)current << "\n";
00152     if( current->children )PrintTree( level+1, current->children );
00153     current = current->next;
00154   }
00155 }
00156 */
00157 // end of file
00158 /* emacs
00159  * Local Variables:
00160  * tab-width: 8
00161  * c-basic-offset: 3
00162  * indent-tabs-mode: nil
00163  * End:
00164  */
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends

Generated on 15 Nov 2016 for Roody by  doxygen 1.6.1