Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

XMLBinaryFile.hpp

Go to the documentation of this file.
00001 /* ImLib3D
00002  * Copyright (c) 2001, ULP-IPB Strasbourg.
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or (at
00007  * your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00022 #ifndef _XMLBinaryFile_hpp
00023 #define _XMLBinaryFile_hpp
00024 
00025 //  #ifndef _Image3D_hpp
00026 //  #error "FileParser_hpp must be included from _Image3D_hpp"
00027 //  #endif // _Image3D_hpp
00028 
00029 #include <stdio.h>
00030 #include<ImLib3D/XMLTools.hpp>
00031 #include<ImLib3D/CppTools.hpp>
00032 #include<ImLib3D/Vect3D.hpp>
00033 
00034 
00035 // ********************** FileParser ********************
00044 class XMLBinaryFile
00045 {
00046 public:
00047     // Create XMLBinaryFile. If mode is set, it will also open the file.
00048     XMLBinaryFile(const string &_fileName="",const string &mode="",bool _exceptionOnFail=false);
00049     // Closes the file (if necesary)
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     // Write some binary data. Use this only after ReadBinaryNode.
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

Generated on Fri Jun 17 13:36:10 2005 for ImLib3D by  doxygen 1.4.2