InputSource.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) Riegel         <juergen.riegel@web.de>                  *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 
00026 #ifndef _PreComp_
00027 # include <xercesc/sax/SAXParseException.hpp>
00028 # include <xercesc/sax/SAXException.hpp>
00029 # include <xercesc/sax2/XMLReaderFactory.hpp>
00030 #endif
00031 
00032 // ---------------------------------------------------------------------------
00033 //  Includes
00034 // ---------------------------------------------------------------------------
00035 #include <xercesc/util/Janitor.hpp>
00036 #include <xercesc/util/PlatformUtils.hpp>
00037 #include <xercesc/util/XMLExceptMsgs.hpp>
00038 #include <xercesc/util/XMLString.hpp>
00039 #include <xercesc/util/PlatformUtils.hpp>
00040 #include <xercesc/util/XMLString.hpp>
00041 #include <xercesc/util/XMLUniDefs.hpp>
00042 
00044 #include "InputSource.h"
00045 #include "Exception.h"
00046 #include "XMLTools.h"
00047 
00048 XERCES_CPP_NAMESPACE_USE
00049 
00050 using namespace Base;
00051 using namespace std;
00052 
00053 
00054 // ---------------------------------------------------------------------------
00055 //  StdInputStream: Constructors and Destructor
00056 // ---------------------------------------------------------------------------
00057 StdInputStream::StdInputStream( std::istream& Stream, XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* const manager ) 
00058   : stream(Stream), fMemoryManager(manager)
00059 {
00060 }
00061 
00062 
00063 StdInputStream::~StdInputStream()
00064 {
00065 }
00066 
00067 
00068 // ---------------------------------------------------------------------------
00069 //  StdInputStream: Implementation of the input stream interface
00070 // ---------------------------------------------------------------------------
00071 #if (XERCES_VERSION_MAJOR == 2)
00072 unsigned int StdInputStream::curPos() const
00073 {
00074   return stream.tellg();
00075 }
00076 
00077 unsigned int StdInputStream::readBytes( XMLByte* const  toFill, const unsigned int maxToRead )
00078 {
00079   //
00080   //  Read up to the maximum bytes requested. We return the number
00081   //  actually read.
00082   //
00083   
00084   stream.read((char *)toFill,maxToRead);
00085   XMLSize_t len = stream.gcount();
00086 
00087   // See http://de.wikipedia.org/wiki/UTF-8#Kodierung
00088   for (XMLSize_t i=0; i<len; i++) {
00089       XMLByte& b = toFill[i];
00090       int seqlen = 0;
00091 
00092       if ((b & 0x80) == 0) {
00093           seqlen = 1;
00094       }
00095       else if ((b & 0xE0) == 0xC0) {
00096           seqlen = 2;
00097           if (b == 0xC0 || b == 0xC1)
00098               b = '?'; // these both values are not allowed
00099       }
00100       else if ((b & 0xF0) == 0xE0) {
00101           seqlen = 3;
00102       }
00103       else if ((b & 0xF8) == 0xF0) {
00104           seqlen = 4;
00105       }
00106       else {
00107           b = '?';
00108       }
00109 
00110       for(int j = 1; j < seqlen; ++j) {
00111           i++;
00112           XMLByte& c = toFill[i];
00113           // range of second, third or fourth byte
00114           if ((c & 0xC0) != 0x80) {
00115               b = '?';
00116               c = '?';
00117           }
00118       }
00119   }
00120 
00121   return len;
00122 }
00123 #else
00124 XMLFilePos StdInputStream::curPos() const
00125 {
00126   return stream.tellg();
00127 }
00128 
00129 XMLSize_t StdInputStream::readBytes( XMLByte* const  toFill, const XMLSize_t maxToRead )
00130 {
00131   //
00132   //  Read up to the maximum bytes requested. We return the number
00133   //  actually read.
00134   //
00135   
00136   stream.read((char *)toFill,maxToRead);
00137   XMLSize_t len = stream.gcount();
00138 
00139   // See http://de.wikipedia.org/wiki/UTF-8#Kodierung
00140   for (XMLSize_t i=0; i<len; i++) {
00141       XMLByte& b = toFill[i];
00142       int seqlen = 0;
00143 
00144       if ((b & 0x80) == 0) {
00145           seqlen = 1;
00146       }
00147       else if ((b & 0xE0) == 0xC0) {
00148           seqlen = 2;
00149           if (b == 0xC0 || b == 0xC1)
00150               b = '?'; // these both values are not allowed
00151       }
00152       else if ((b & 0xF0) == 0xE0) {
00153           seqlen = 3;
00154       }
00155       else if ((b & 0xF8) == 0xF0) {
00156           seqlen = 4;
00157       }
00158       else {
00159           b = '?';
00160       }
00161 
00162       for(int j = 1; j < seqlen; ++j) {
00163           i++;
00164           XMLByte& c = toFill[i];
00165           // range of second, third or fourth byte
00166           if ((c & 0xC0) != 0x80) {
00167               b = '?';
00168               c = '?';
00169           }
00170       }
00171   }
00172 
00173   return len;
00174 }
00175 #endif
00176 
00177 
00178 // ---------------------------------------------------------------------------
00179 //  StdInputSource: Constructors and Destructor
00180 // ---------------------------------------------------------------------------
00181 StdInputSource::StdInputSource ( std::istream& Stream, const char* filePath, XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* const manager )
00182   : InputSource(manager),stream(Stream)
00183 {
00184   // we have to set the file name in case an error occurs
00185   XStr tmpBuf(filePath);
00186   setSystemId(tmpBuf.unicodeForm());
00187 }
00188 
00189 
00190 StdInputSource::~StdInputSource()
00191 {
00192 }
00193 
00194 
00195 // ---------------------------------------------------------------------------
00196 //  StdInputSource: InputSource interface implementation
00197 // ---------------------------------------------------------------------------
00198 BinInputStream* StdInputSource::makeStream() const
00199 {
00200   StdInputStream* retStrm = new StdInputStream(stream /*, getMemoryManager()*/);
00201   return retStrm;
00202 }
00203 

Generated on Wed Nov 23 19:00:19 2011 for FreeCAD by  doxygen 1.6.1