00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00022 #ifndef _XMLBinaryFile_hpp
00023 #define _XMLBinaryFile_hpp
00024
00025
00026
00027
00028
00029 #include <stdio.h>
00030 #include<ImLib3D/XMLTools.hpp>
00031 #include<ImLib3D/CppTools.hpp>
00032 #include<ImLib3D/Vect3D.hpp>
00033
00034
00035
00044 class XMLBinaryFile
00045 {
00046 public:
00047
00048 XMLBinaryFile(const string &_fileName="",const string &mode="",bool _exceptionOnFail=false);
00049
00050 ~XMLBinaryFile(){if(isOpen){Close();}}
00051 void OpenRead(const string &_fileName);
00052 void OpenWrite(const string &_fileName);
00053 void Close();
00054
00055 xmlpp::Element *AddBinaryNode(xmlpp::Element *parent,int size=-1);
00056
00058 template<class T,class iterator>
00059 void BinaryWrite(iterator begin,iterator end);
00061 template<class T,class iterator>
00062 void BinaryRead(iterator begin,iterator end);
00064 void BinaryWrite(void *data,size_t size);
00066 void BinaryWrite(xmlpp::Element *binNode,void *data,size_t size);
00067 void ReadBinaryNode(xmlpp::Element *parent);
00068 void EndBinaryWrite(xmlpp::Element *binNode);
00069
00070 void BinaryRead(void *data,size_t size);
00071 bool CheckBinaryReadComplete();
00072
00074 xmlpp::Element *AddChild(xmlpp::Element *parent,const string &name,const string &a1="",const string &a2="",const string &a3="",const string &a4="");
00075 xmlpp::Element *GetChild(xmlpp::Element *parent,const string &name);
00076 bool HasChild(xmlpp::Element *parent,const string &name);
00077 int GetIntAttribute (xmlpp::Element *node,const string &name){return (int)LongInt(GetAttribute(node,name));}
00078 double GetDoubleAttribute(xmlpp::Element *node,const string &name){return Double(GetAttribute(node,name));}
00079 string GetAttribute (xmlpp::Element *node,const string &name);
00080 bool HasAttribute (xmlpp::Element *node,const string &name);
00082 static string Quote(const string &src);
00084 static string UnQuote(const string &src);
00086
00087
00088 protected:
00090
00091 int AllocInBinary(size_t size);
00093 string GetBinFileName(){return binFileName;}
00094 string GetFileName(){return fileName;}
00095 int GetBinaryBegining(){return binaryBegining;}
00096 public:
00097 xmlpp::Element *GetRoot(const string &rootName="");
00098 xmlpp::Element *SetRoot(const string &rootName)
00099 {
00100 return document->create_root_node(rootName);
00101 }
00103
00104 public:
00106 void ShowXML(xmlpp::Element *node=NULL,int depth=0);
00107
00108 int debug;
00109 protected:
00110
00112 string fileName;
00114 string binFileName;
00116 FILE *binaryFile;
00117
00118 xmlpp::DomParser *parser;
00119 xmlpp::Document *document;
00121 size_t binaryCurrentAllocPos;
00122 size_t posInsideCurrentAllocBlock;
00123 size_t currentAllocBlockSize;
00125
00126 size_t binaryBegining;
00127
00128
00129 bool isWrite;
00130 bool isOpen;
00131 bool exceptionOnFail;
00133
00136 string ReadXMLHeader();
00137 void Error(const char *fmt, ...);
00138
00139 public:
00140 void ExceptionOnFail();
00141
00142
00143 };
00144
00145
00146 template<class T,class iterator>
00147 void
00148 XMLBinaryFile::BinaryWrite(iterator begin,iterator end)
00149 {
00150 uint size=end-begin;
00151 if(IsSmallEndian() || sizeof(*begin)==1)
00152 {
00153 BinaryWrite((void *)&(*begin),size*sizeof(T));
00154 }
00155 else
00156 if(typeid(T) == typeid(Vect3Df))
00157 {
00158 float *data0=(float *)(void *)&(*begin);
00159 for(uint i=0;i<size;i++)
00160 {
00161 for(int j=0;j<3;j++)
00162 {
00163 float p;
00164 ReverseEndian(data0[i*3+j],p);
00165 BinaryWrite(&p,sizeof(float));
00166 }
00167 }
00168 }
00169 else
00170 {
00171 T v;
00172 for(iterator p=begin;p!=end;++p)
00173 {
00174 ReverseEndian(*p,v);
00175 BinaryWrite(&v,sizeof(T));
00176 }
00177 }
00178 }
00179 template<class T,class iterator>
00180 void
00181 XMLBinaryFile::BinaryRead(iterator begin,iterator end)
00182 {
00183 uint size=end-begin;
00184
00185 if(IsSmallEndian() || sizeof(T)==1)
00186 {
00187 BinaryRead(begin,size*sizeof(T));
00188 }
00189 else
00190 if(typeid(T) == typeid(Vect3Df))
00191 {
00192 float *data0=(float *)(void *)begin;
00193 for(uint i=0;i<size;i++)
00194 {
00195 for(int j=0;j<3;j++)
00196 {
00197 float p;
00198 BinaryRead(&p,sizeof(float));
00199 ReverseEndian(p,data0[i*3+j]);
00200 }
00201 }
00202 }
00203 else
00204 {
00205 T v;
00206 for(iterator p=begin;p!=end;++p)
00207 {
00208 BinaryRead(&v,sizeof(T));
00209 ReverseEndian(v,*p);
00210 }
00211 }
00212 }
00213
00214 #endif // _XMLBinaryFile_hpp