RoodyXML.cxx

Go to the documentation of this file.
00001 /********************************************************************
00002   RoodyXML
00003 ********************************************************************/
00004 
00005 #include <iostream>
00006 #include <ios>
00007 #include <time.h>
00008 
00009 #include "RoodyXML.h"
00010 
00011 RoodyXML::RoodyXML()
00012 {
00013   version_ = "1.0";
00014   encoding_ = "ISO-8859-1";
00015 }
00016 
00017 RoodyXML::~RoodyXML()
00018 {}
00019 
00020 std::ofstream &RoodyXML::OpenFileForWrite( char const *file )
00021 {
00022   if( outputFile_.is_open() )outputFile_.close();
00023   std::ios_base::openmode mode = std::ios_base::out|std::ios_base::trunc;
00024   outputFile_.open( file, mode );
00025   //
00026   if( outputFile_ )
00027   {
00028     outputFile_ << "<?xml version=\"" << version_
00029                 << "\" encoding=\"" << encoding_ << "\"?>\n";
00030     /*
00031       << "<!DOCTYPE roody [\n"
00032       << "  <!ELEMENT roody (online?,file*,graphics?,canvas*,group*)>\n"
00033       << "  <!ELEMENT online (host,port?)>\n"
00034       << "  <!ELEMENT graphics (zones?,refresh?)>\n"
00035       << "  <!ELEMENT zones (#PCDATA)>\n"
00036       << "  <!ELEMENT refresh (#PCDATA)>\n"
00037       << "  <!ELEMENT canvas (zones?,histogram*)>\n"
00038       << "  <!ELEMENT group (name,histogram*)>\n"
00039       << "  <!ELEMENT name (#PCDATA)>\n"
00040       << "  <!ELEMENT histogram (name,source)>\n"
00041       << "  <!ELEMENT source (#PCDATA)>\n"
00042       << "  <!ELEMENT host (#PCDATA)>\n"
00043       << "  <!ELEMENT port (#PCDATA)>\n"
00044       << "  <!ELEMENT file (#PCDATA)>\n"
00045       << "]>\n";
00046     */
00047     time_t now;
00048     time( &now );
00049     std::string temp( ctime(&now) );
00050     temp.erase( temp.begin()+temp.size()-1 );
00051     outputFile_ << "<!-- created by Roody on " << temp << " -->\n";
00052   }
00053   return outputFile_;
00054 }
00055 
00056 std::string RoodyXML::Encode( std::string const &src )
00057 {
00058   std::string result;
00059   std::size_t length = src.size();
00060   for( std::size_t i=0; i<length; ++i )
00061   {
00062     switch ( src[i] )
00063     {
00064       case '<':
00065         result += "&lt;";
00066         break;
00067       case '>':
00068         result += "&gt;";
00069         break;
00070       case '&':
00071         result += "&amp;";
00072         break;
00073       case '\"':
00074         result += "&quot;";
00075         break;
00076       case '\'':
00077         result += "&apos;";
00078         break;
00079       default:
00080         result += src[i];
00081     }
00082   }  
00083   return result;
00084 }
00085 
00086 bool RoodyXML::OpenFileForRead( char const *file )
00087 {
00088   char error[256];
00089   documentNode_ = mxml_parse_file( const_cast<char*>(file), error, static_cast<int>(sizeof(error)) );
00090   if( !documentNode_ )
00091   {
00092     std::cerr << error << std::endl;
00093     return false;
00094   }
00095   int i = FindNode( documentNode_, 0, "roody" );
00096   if( i < 0 )
00097   {
00098     std::cerr << "roody node not found in file: \"" << file << "\"" << std::endl;
00099     return false;
00100   }
00101   roodyNode_ = &documentNode_->child[i];
00102   fileIndex_ = 0;
00103   canvasIndex_ = 0;
00104   groupIndex_ = 0;
00105   histIndex_ = 0;
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 bool RoodyXML::GetOnlineNode()
00120 {
00121   int i = FindNode( roodyNode_, 0, "online" );
00122   if( i >= 0 )
00123   {
00124     PMXML_NODE onlineNode = &roodyNode_->child[i];
00125     int host = FindNode( onlineNode, 0, "host" );
00126     if( host < 0 )
00127     {
00128       std::cerr << "ERROR: host node not found\n";
00129       return false;
00130     }
00131     int port = FindNode( onlineNode, 0, "port" );
00132     onlineHostName_ = std::string(onlineNode->child[host].value) +
00133                       std::string(":") +
00134                       std::string(port>=0?onlineNode->child[port].value:"9090");
00135     return true;
00136   }
00137   return false;
00138 }
00139 
00140 std::string RoodyXML::GetOnlineInfo()
00141 { return onlineHostName_; }
00142 
00143 bool RoodyXML::GetFileNode()
00144 {
00145   int i = FindNode( roodyNode_, fileIndex_, "file" ); 
00146   if( i >= 0 )
00147   {
00148     fileName_ = roodyNode_->child[i].value;
00149     fileIndex_ = i+1;
00150     return true;
00151   }
00152   return false;
00153 }
00154 
00155 std::string RoodyXML::GetFileInfo()
00156 { return fileName_; }
00157 
00158 bool RoodyXML::GetGraphicsNode()
00159 {
00160   int i = FindNode( roodyNode_, 0, "graphics" );
00161   if( i >= 0 )
00162   {
00163     PMXML_NODE graphicsNode = &roodyNode_->child[i];
00164     int zones = FindNode( graphicsNode, 0, "zones" );
00165     std::string s = zones>=0?graphicsNode->child[zones].value:"1x1";
00166     std::size_t x = s.find("x");
00167     if( x == s.npos ) // "x" not found in string
00168     {
00169       columns_ = 1;
00170       rows_ = 1;
00171     }
00172     else
00173     {
00174       if( !StringTo<int>(columns_,s.substr(0,x),std::dec) )
00175       {
00176         columns_ = 1;
00177         std::cerr << "invalid number of columns: \"" << s.substr(0,x) << "\"\n"
00178                   << "columns set to 1\n";
00179       }
00180       if( !StringTo<int>(rows_,s.substr(x+1,s.size()-x-1),std::dec) )
00181       {
00182         rows_ = 1;
00183         std::cerr << "invalid number of rows: \"" << s.substr(x+1,s.size()-x-1) << "\"\n"
00184                   << "rows set to 1\n";
00185       }
00186     }
00187     int refresh = FindNode( graphicsNode, 0, "refresh" );
00188     refreshRate_ = refresh>=0?atoi(graphicsNode->child[refresh].value):0;
00189     return true;
00190   }
00191   return false;
00192 }
00193 
00194 void RoodyXML::GetZones( int &columns, int &rows )
00195 {
00196   columns = columns_;
00197   rows = rows_;
00198 }
00199 
00200 int RoodyXML::GetRefreshRate()
00201 { return refreshRate_; }
00202 
00203 bool RoodyXML::GetCanvasNode()
00204 {
00205   int i = FindNode( roodyNode_, canvasIndex_, "canvas" ); 
00206   if( i >= 0 )
00207   {
00208     canvasIndex_ = i+1;
00209     PMXML_NODE canvasNode = &roodyNode_->child[i];
00210     int zones = FindNode( canvasNode, 0, "zones" );
00211     std::string s = zones>=0?canvasNode->child[zones].value:"1x1";
00212     std::size_t x = s.find("x");
00213     if( x == s.npos ) // "x" not found in string
00214     {
00215       columns_ = 1;
00216       rows_ = 1;
00217     }
00218     if( !StringTo<int>(columns_,s.substr(0,x),std::dec) )
00219     {
00220       columns_ = 1;
00221       std::cerr << "invalid number of columns: \"" << s.substr(0,x) << "\"\n"
00222                 << "columns set to 1\n";
00223     }
00224     if( !StringTo<int>(rows_,s.substr(x+1,s.size()-x-1),std::dec) )
00225     {
00226       rows_ = 1;
00227       std::cerr << "invalid number of rows: \"" << s.substr(x+1,s.size()-x-1) << "\"\n"
00228                 << "rows set to 1\n";
00229     }
00230     histParent_ = canvasNode;
00231     histIndex_ = 0;
00232     return true;
00233   }
00234   return false;
00235 }
00236 
00237 bool RoodyXML::GetHistogramNode()
00238 {
00239   int i = FindNode( histParent_, histIndex_, "histogram" ); 
00240   if( i >= 0 )
00241   {
00242     histIndex_ = i+1;
00243     PMXML_NODE histNode = &histParent_->child[i];
00244     int name = FindNode(histNode,0,"name");
00245     if( name < 0 )
00246     {
00247       std::cerr << "ERROR: histogram name not found\n";
00248       return false;
00249     }
00250     histName_ = histNode->child[name].value;
00251     int source = FindNode(histNode,0,"source");
00252     if( source < 0 )
00253     {
00254       std::cerr << "ERROR: histogram source not found\n";
00255       return false;
00256     }
00257     histSource_ = histNode->child[source].value;
00258     return true;
00259   }
00260   return false;
00261 }
00262 
00263 std::string RoodyXML::GetHistogramName()
00264 { return histName_; }
00265 
00266 std::string RoodyXML::GetHistogramSource()
00267 { return histSource_; }
00268 
00269 bool RoodyXML::GetGroupNode()
00270 {
00271   int i = FindNode( roodyNode_, groupIndex_, "group" ); 
00272   if( i >= 0 )
00273   {
00274     groupIndex_ = i+1;
00275     PMXML_NODE groupNode = &roodyNode_->child[i];
00276     int name = FindNode( groupNode, 0, "name" );
00277     if( name < 0 )
00278     {
00279       std::cerr << "ERROR: group name not found\n";
00280       return false;
00281     }
00282     groupName_ = groupNode->child[name].value;
00283     histParent_ = groupNode;
00284     histIndex_ = 0;
00285     return true;
00286   }
00287   return false;
00288 }
00289   
00290 std::string RoodyXML::GetGroupName()
00291 { return groupName_; }
00292 /*
00293 void RoodyXML::PrintTree( int level )
00294 {
00295   PrintTree( level, documentNode_ );
00296 }
00297 
00298 void RoodyXML::PrintTree( int level, PMXML_NODE current )
00299 {
00300   while( current )
00301   {
00302     for( int i=0; i<level; ++i )std::cout << "  ";
00303     std::cout << "current " << current->name << ", type " << current->node_type
00304               << ", children " << (void*)current << "\n";
00305     if( current->children )PrintTree( level+1, current->children );
00306     current = current->next;
00307   }
00308 }
00309 */
00310 // end of file

Roody DOC Version 1.0.1 ---- TRIUMF ----
Contributions: Pierre-Andre Amaudruz - Joe Chuma - Doxygen - Greg King - Konstantin Olchanski - Matthias Schneebeli