Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / writerfilter / inc / resourcemodel / SubSequence.hxx
blobf4e08c7b7ab11a1123b0442442d6c5759d9009d6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_SUB_SEQUENCE_HXX
21 #define INCLUDED_SUB_SEQUENCE_HXX
23 #include <com/sun/star/uno/Sequence.hxx>
25 #include <boost/shared_ptr.hpp>
26 #include <iostream>
27 #include <stdio.h>
28 #include <ctype.h>
29 #include "exceptions.hxx"
30 #include <WriterFilterDllApi.hxx>
31 #include <resourcemodel/OutputWithDepth.hxx>
33 namespace writerfilter {
34 using namespace ::std;
36 template <class T>
37 class SubSequence;
39 template <typename T>
40 void dumpLine(OutputWithDepth<string> & o, SubSequence<T> & rSeq,
41 sal_uInt32 nOffset, sal_uInt32 nStep);
43 template <class T>
44 class SubSequence
46 typedef boost::shared_ptr<com::sun::star::uno::Sequence<T> >
47 SequencePointer;
49 SequencePointer mpSequence;
50 sal_uInt32 mnOffset;
51 sal_uInt32 mnCount;
53 public:
54 typedef boost::shared_ptr<SubSequence> Pointer_t;
56 SubSequence() : mpSequence(new ::com::sun::star::uno::Sequence<T>()),
57 mnOffset(0), mnCount(0)
61 SubSequence(SequencePointer pSequence, sal_uInt32 nOffset_,
62 sal_uInt32 nCount_)
63 : mpSequence(pSequence), mnOffset(nOffset_), mnCount(nCount_)
67 SubSequence(SequencePointer pSequence)
68 : mpSequence(pSequence), mnOffset(0), mnCount(pSequence->getLength())
72 SubSequence(const SubSequence & rSubSequence, sal_uInt32 nOffset_,
73 sal_uInt32 nCount_)
74 : mpSequence(rSubSequence.mpSequence),
75 mnOffset(rSubSequence.mnOffset + nOffset_),
76 mnCount(nCount_)
80 SubSequence(const T * pStart, sal_uInt32 nCount_)
81 : mpSequence(new com::sun::star::uno::Sequence<T>(pStart, nCount_)),
82 mnOffset(0), mnCount(nCount_)
86 SubSequence(sal_Int32 nCount_)
87 : mpSequence(new com::sun::star::uno::Sequence<T>(nCount_)), mnOffset(0),
88 mnCount(nCount_)
92 ::com::sun::star::uno::Sequence<T> & getSequence()
94 return *mpSequence;
97 const ::com::sun::star::uno::Sequence<T> & getSequence() const
99 return *mpSequence;
102 void reset() {
103 mnOffset = 0;
104 mnCount = mpSequence->getLength();
107 sal_uInt32 getOffset() const { return mnOffset; }
108 sal_uInt32 getCount() const { return mnCount; }
110 const T & operator[] (sal_uInt32 nIndex) const
112 if (mnOffset + nIndex >=
113 sal::static_int_cast<sal_uInt32>(mpSequence->getLength()))
114 throw ExceptionOutOfBounds("SubSequence::operator[]");
116 return (*mpSequence)[mnOffset + nIndex];
119 void dump(ostream & o) const
122 char sBuffer[256];
124 snprintf(sBuffer, sizeof(sBuffer),
125 "<sequence id='%p' offset='%lx' count='%lx'>",
126 mpSequence.get(), mnOffset, mnCount);
127 o << sBuffer << endl;
130 sal_uInt32 n = 0;
131 sal_uInt32 nStep = 16;
133 while (n < getCount())
135 char sBuffer[256];
137 o << "<line>";
139 snprintf(sBuffer, 255, "%08lx: ", static_cast<unsigned long>(n));
141 o << sBuffer;
143 for (sal_uInt32 i = 0; i < nStep; i++)
145 if (n + i < getCount())
147 snprintf(sBuffer, 255, "%02x ", operator[](n + i));
148 o << sBuffer;
150 else
151 o << " ";
153 if (i % 8 == 7)
154 o << " ";
158 for (sal_uInt32 i = 0; i < nStep; i++)
160 if (n + i < getCount())
162 unsigned char c =
163 static_cast<unsigned char>(operator[](n + i));
165 if (c=='&')
166 o << "&amp;";
167 else if (c=='<')
168 o << "&lt;";
169 else if (c=='>')
170 o << "&gt;";
171 else if (c < 128 && isprint(c))
172 o << c;
173 else
174 o << ".";
180 o << "</line>" << endl;
182 n += nStep;
185 o << "</sequence>" << endl;
188 void dump(OutputWithDepth<string> & o)
191 char sBuffer[256];
193 snprintf(sBuffer, sizeof(sBuffer),
194 "<sequence id='%p' offset='%" SAL_PRIxUINT32 "' count='%" SAL_PRIxUINT32 "'>",
195 mpSequence.get(), mnOffset, mnCount);
196 o.addItem(sBuffer);
199 sal_uInt32 n = 0;
200 sal_uInt32 nStep = 16;
204 sal_uInt32 nCount = getCount();
205 while (n < nCount)
207 sal_uInt32 nBytes = nCount - n;
209 if (nBytes > nStep)
210 nBytes = nStep;
212 SubSequence<T> aSeq(*this, n, nBytes);
213 dumpLine(o, aSeq, n, nStep);
215 n += nBytes;
218 catch (...)
220 o.addItem("<exception/>");
223 o.addItem("</sequence>");
226 string toString() const
228 sal_uInt32 n = 0;
229 sal_uInt32 nStep = 16;
231 string sResult;
233 while (n < getCount())
235 char sBuffer[256];
237 snprintf(sBuffer, 255, "<line>%08" SAL_PRIxUINT32 ": ", n);
239 sResult += sBuffer;
241 for (sal_uInt32 i = 0; i < nStep; i++)
243 if (n + i < getCount())
245 snprintf(sBuffer, 255, "%02x ", operator[](n + i));
246 sResult += sBuffer;
248 else
249 sResult += " ";
251 if (i % 8 == 7)
252 sResult += " ";
256 for (sal_uInt32 i = 0; i < nStep; i++)
258 if (n + i < getCount())
260 unsigned char c =
261 static_cast<unsigned char>(operator[](n + i));
263 if (c=='&')
264 sResult += "&amp;";
265 else if (c=='<')
266 sResult += "&lt;";
267 else if (c=='>')
268 sResult += "&gt;";
269 else if (c < 128 && isprint(c))
270 sResult += c;
271 else
272 sResult += ".";
277 sResult += "</line>\n";
279 n += nStep;
282 return sResult;
286 template <typename T>
287 void dumpLine(OutputWithDepth<string> & o, SubSequence<T> & rSeq,
288 sal_uInt32 nOffset, sal_uInt32 nStep)
290 sal_uInt32 nCount = rSeq.getCount();
291 char sBuffer[256];
293 string tmpStr = "<line>";
295 snprintf(sBuffer, 255, "%08" SAL_PRIxUINT32 ": ", nOffset);
297 tmpStr += sBuffer;
299 for (sal_uInt32 i = 0; i < nStep; i++)
301 if (i < nCount)
303 snprintf(sBuffer, 255, "%02x ", rSeq[i]);
304 tmpStr += sBuffer;
306 else
307 tmpStr += " ";
309 if (i % 8 == 7)
310 tmpStr += " ";
314 for (sal_uInt32 i = 0; i < nStep; i++)
316 if (i < nCount)
318 unsigned char c =
319 static_cast<unsigned char>(rSeq[i]);
321 if (c=='&')
322 tmpStr += "&amp;";
323 else if (c=='<')
324 tmpStr += "&lt;";
325 else if (c=='>')
326 tmpStr += "&gt;";
327 else if (c < 128 && isprint(c))
328 tmpStr += c;
329 else
330 tmpStr += ".";
336 tmpStr += "</line>";
338 o.addItem(tmpStr);
343 #endif // INCLUDED_SUB_SEQUENCE_HXX
345 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */