/************************************************************************* * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * Copyright 2008 by Sun Microsystems, Inc. * * OpenOffice.org - a multi-platform office productivity suite * * $RCSfile: SubSequence.hxx,v $ * $Revision: 1.5 $ * * This file is part of OpenOffice.org. * * OpenOffice.org is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 3 * only, as published by the Free Software Foundation. * * OpenOffice.org is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License version 3 for more details * (a copy is included in the LICENSE file that accompanied this code). * * You should have received a copy of the GNU Lesser General Public License * version 3 along with OpenOffice.org. If not, see * * for a copy of the LGPLv3 License. * ************************************************************************/ #ifndef INCLUDED_SUB_SEQUENCE_HXX #define INCLUDED_SUB_SEQUENCE_HXX #include #include #include #include #include #include "exceptions.hxx" #include #include namespace writerfilter { using namespace ::std; template class SubSequence; template void dumpLine(OutputWithDepth & o, SubSequence & rSeq, sal_uInt32 nOffset, sal_uInt32 nStep); template class WRITERFILTER_DLLPUBLIC SubSequence { typedef boost::shared_ptr > SequencePointer; SequencePointer mpSequence; sal_uInt32 mnOffset; sal_uInt32 mnCount; public: typedef boost::shared_ptr Pointer_t; SubSequence() : mpSequence(new ::com::sun::star::uno::Sequence()), mnOffset(0), mnCount(0) { } SubSequence(SequencePointer pSequence, sal_uInt32 nOffset_, sal_uInt32 nCount_) : mpSequence(pSequence), mnOffset(nOffset_), mnCount(nCount_) { } SubSequence(SequencePointer pSequence) : mpSequence(pSequence), mnOffset(0), mnCount(pSequence->getLength()) { } SubSequence(const SubSequence & rSubSequence, sal_uInt32 nOffset_, sal_uInt32 nCount_) : mpSequence(rSubSequence.mpSequence), mnOffset(rSubSequence.mnOffset + nOffset_), mnCount(nCount_) { } SubSequence(const T * pStart, sal_uInt32 nCount_) : mpSequence(new com::sun::star::uno::Sequence(pStart, nCount_)), mnOffset(0), mnCount(nCount_) { } SubSequence(sal_Int32 nCount_) : mpSequence(new com::sun::star::uno::Sequence(nCount_)), mnOffset(0), mnCount(nCount_) { } ::com::sun::star::uno::Sequence & getSequence() { return *mpSequence; } const ::com::sun::star::uno::Sequence & getSequence() const { return *mpSequence; } void reset() { mnOffset = 0; mnCount = mpSequence->getLength(); } sal_uInt32 getOffset() const { return mnOffset; } sal_uInt32 getCount() const { return mnCount; } const T & operator[] (sal_uInt32 nIndex) const { if (mnOffset + nIndex >= sal::static_int_cast(mpSequence->getLength())) throw ExceptionOutOfBounds("SubSequence::operator[]"); return (*mpSequence)[mnOffset + nIndex]; } void dump(ostream & o) const { { char sBuffer[256]; snprintf(sBuffer, sizeof(sBuffer), "", mpSequence.get(), mnOffset, mnCount); o << sBuffer << endl; } sal_uInt32 n = 0; sal_uInt32 nStep = 16; while (n < getCount()) { char sBuffer[256]; o << ""; snprintf(sBuffer, 255, "%08lx: ", n); o << sBuffer; for (sal_uInt32 i = 0; i < nStep; i++) { if (n + i < getCount()) { snprintf(sBuffer, 255, "%02x ", operator[](n + i)); o << sBuffer; } else o << " "; if (i % 8 == 7) o << " "; } { for (sal_uInt32 i = 0; i < nStep; i++) { if (n + i < getCount()) { unsigned char c = static_cast(operator[](n + i)); if (c=='&') o << "&"; else if (c=='<') o << "<"; else if (c=='>') o << ">"; else if (c < 128 && isprint(c)) o << c; else o << "."; } } } o << "" << endl; n += nStep; } o << "" << endl; } void dump(OutputWithDepth & o) { { char sBuffer[256]; snprintf(sBuffer, sizeof(sBuffer), "", mpSequence.get(), mnOffset, mnCount); o.addItem(sBuffer); } sal_uInt32 n = 0; sal_uInt32 nStep = 16; try { sal_uInt32 nCount = getCount(); while (n < nCount) { sal_uInt32 nBytes = nCount - n; if (nBytes > nStep) nBytes = nStep; SubSequence aSeq(*this, n, nBytes); dumpLine(o, aSeq, n, nStep); n += nBytes; } } catch (...) { o.addItem(""); } o.addItem(""); } string toString() const { sal_uInt32 n = 0; sal_uInt32 nStep = 16; string sResult; while (n < getCount()) { char sBuffer[256]; snprintf(sBuffer, 255, "%08" SAL_PRIxUINT32 ": ", n); sResult += sBuffer; for (sal_uInt32 i = 0; i < nStep; i++) { if (n + i < getCount()) { snprintf(sBuffer, 255, "%02x ", operator[](n + i)); sResult += sBuffer; } else sResult += " "; if (i % 8 == 7) sResult += " "; } { for (sal_uInt32 i = 0; i < nStep; i++) { if (n + i < getCount()) { unsigned char c = static_cast(operator[](n + i)); if (c=='&') sResult += "&"; else if (c=='<') sResult += "<"; else if (c=='>') sResult += ">"; else if (c < 128 && isprint(c)) sResult += c; else sResult += "."; } } } sResult += "\n"; n += nStep; } return sResult; } }; template void dumpLine(OutputWithDepth & o, SubSequence & rSeq, sal_uInt32 nOffset, sal_uInt32 nStep) { sal_uInt32 nCount = rSeq.getCount(); char sBuffer[256]; string tmpStr = ""; snprintf(sBuffer, 255, "%08" SAL_PRIxUINT32 ": ", nOffset); tmpStr += sBuffer; for (sal_uInt32 i = 0; i < nStep; i++) { if (i < nCount) { snprintf(sBuffer, 255, "%02x ", rSeq[i]); tmpStr += sBuffer; } else tmpStr += " "; if (i % 8 == 7) tmpStr += " "; } { for (sal_uInt32 i = 0; i < nStep; i++) { if (i < nCount) { unsigned char c = static_cast(rSeq[i]); if (c=='&') tmpStr += "&"; else if (c=='<') tmpStr += "<"; else if (c=='>') tmpStr += ">"; else if (c < 128 && isprint(c)) tmpStr += c; else tmpStr += "."; } } } tmpStr += ""; o.addItem(tmpStr); } } #endif // INCLUDED_SUB_SEQUENCE_HXX