library of assembled shared sources

http://lass.cocamware.com

xml_o_file.cpp

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 #include "io_common.h"
00044 #include "xml_o_file.h"
00045 #include "../util/bit_manip.h"
00046 
00047 namespace lass
00048 {
00049 namespace io
00050 {
00051 
00052 // --- public --------------------------------------------------------------------------------------
00053 
00054 XmlOFile::XmlOFile():
00055     XmlOStream(),
00056     file_(),
00057     root_("")
00058 {
00059 }
00060 
00061 
00062 
00063 XmlOFile::XmlOFile(const char* iFileName, std::ios::openmode iOpenMode):
00064     XmlOStream()
00065 {
00066     open(iFileName, iOpenMode);
00067 }
00068 
00069 
00070 
00071 XmlOFile::XmlOFile(const char* iFileName, const char* iRoot):
00072     XmlOStream()
00073 {
00074     open(iFileName, iRoot);
00075 }
00076 
00077 
00078 
00079 XmlOFile::XmlOFile(const std::string& iFileName, std::ios::openmode iOpenMode):
00080     XmlOStream()
00081 {
00082     open(iFileName, iOpenMode);
00083 }
00084 
00085 
00086 
00087 XmlOFile::XmlOFile(const std::string& iFileName, const std::string& iRoot):
00088     XmlOStream()
00089 {
00090     open(iFileName, iRoot);
00091 }
00092 
00093 
00094 
00095 XmlOFile::~XmlOFile()
00096 {
00097     close();
00098 }
00099 
00100 
00101 
00102 void XmlOFile::open(const char* iFilename, std::ios::openmode iOpenMode)
00103 {
00104     file_.open(iFilename, iOpenMode);
00105     root_ = "";
00106     clear(file_.rdstate()); // copy state from std::ofstream to io::StreamBase
00107 }
00108 
00109 
00110 
00111 void XmlOFile::open(const char* iFilename, const char* iRoot)
00112 {
00113     file_.open(iFilename, std::ios::out | std::ios::trunc);
00114     root_ = iRoot;
00115     file_ << "<?xml version=\"1.0\"?>\n";
00116     file_ << "<" << root_ << ">\n";
00117     clear(file_.rdstate()); // copy state from std::ofstream to io::StreamBase
00118 }
00119 
00120 
00121 
00122 void XmlOFile::open(const std::string& iFilename, std::ios::openmode iOpenMode)
00123 {
00124     open(iFilename.c_str(), iOpenMode);
00125 }
00126 
00127 
00128 
00129 void XmlOFile::open(const std::string& iFilename, const std::string& iRoot)
00130 {
00131     open(iFilename.c_str(), iRoot.c_str());
00132 }
00133 
00134 
00135 
00136 void XmlOFile::close()
00137 {
00138     if (file_.is_open() && !root_.empty())
00139     {
00140         file_ << "</" << root_ << ">\n";
00141     }
00142     file_.close();
00143     clear(file_.rdstate()); // copy state from std::ofstream to io::StreamBase
00144 }
00145 
00146 
00147 
00148 bool XmlOFile::is_open()
00149 {
00150     return file_.is_open();
00151 }
00152 
00153 
00154 
00155 #define LASS_IO_XML_O_FILE_INSERTOR( type__ )\
00156     XmlOFile& XmlOFile::operator<<( type__ iIn )\
00157     {\
00158         file_ << iIn;\
00159         clear(file_.rdstate());\
00160         return *this;\
00161     }
00162 
00163 LASS_IO_XML_O_FILE_INSERTOR( char )
00164 LASS_IO_XML_O_FILE_INSERTOR( signed char )
00165 LASS_IO_XML_O_FILE_INSERTOR( unsigned char )
00166 LASS_IO_XML_O_FILE_INSERTOR( signed short  )
00167 LASS_IO_XML_O_FILE_INSERTOR( unsigned short  )
00168 LASS_IO_XML_O_FILE_INSERTOR( signed int  )
00169 LASS_IO_XML_O_FILE_INSERTOR( unsigned int  )
00170 LASS_IO_XML_O_FILE_INSERTOR( signed long  )
00171 LASS_IO_XML_O_FILE_INSERTOR( unsigned long  )
00172 LASS_IO_XML_O_FILE_INSERTOR( float  )
00173 LASS_IO_XML_O_FILE_INSERTOR( double  )
00174 LASS_IO_XML_O_FILE_INSERTOR( long double  )
00175 LASS_IO_XML_O_FILE_INSERTOR( bool  )
00176 LASS_IO_XML_O_FILE_INSERTOR( const void*  )
00177 LASS_IO_XML_O_FILE_INSERTOR( const char*  )
00178 LASS_IO_XML_O_FILE_INSERTOR( const std::string& )
00179 
00180 XmlOFile& XmlOFile::operator<<( std::ostream& (*iIn) (std::ostream&) )
00181 {
00182     file_ << iIn;
00183     clear(file_.rdstate()); // copy state from std::ofstream to io::StreamBase
00184     return *this;
00185 }
00186 
00187 
00188 
00189 
00190 // --- protected -----------------------------------------------------------------------------------
00191 
00192 
00193 
00194 // --- private -------------------------------------------------------------------------------------
00195 
00196 
00197 
00198 // --- free ----------------------------------------------------------------------------------------
00199 
00200 
00201 
00202 }
00203 
00204 }
00205 
00206 // EOF

Generated on Mon Nov 10 14:22:02 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo