RoodyXML.cxx
Go to the documentation of this file.00001
00002
00003
00004
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
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
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 += "<";
00069 break;
00070 case '>':
00071 result += ">";
00072 break;
00073 case '&':
00074 result += "&";
00075 break;
00076 case '\"':
00077 result += """;
00078 break;
00079 case '\'':
00080 result += "'";
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
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164