/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ #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 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: ", static_cast(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 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */