merge the formfield patch from ooo-build
[ooovba.git] / writerfilter / inc / resourcemodel / SubSequence.hxx
blob961785dd0410030f62ba4ba39b1288eeb94f7ad3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SubSequence.hxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef INCLUDED_SUB_SEQUENCE_HXX
32 #define INCLUDED_SUB_SEQUENCE_HXX
34 #include <com/sun/star/uno/Sequence.hxx>
36 #include <boost/shared_ptr.hpp>
37 #include <iostream>
38 #include <stdio.h>
39 #include <ctype.h>
40 #include "exceptions.hxx"
41 #include <WriterFilterDllApi.hxx>
42 #include <resourcemodel/OutputWithDepth.hxx>
44 namespace writerfilter {
45 using namespace ::std;
47 template <class T>
48 class SubSequence;
50 template <typename T>
51 void dumpLine(OutputWithDepth<string> & o, SubSequence<T> & rSeq,
52 sal_uInt32 nOffset, sal_uInt32 nStep);
54 template <class T>
55 class WRITERFILTER_DLLPUBLIC SubSequence
57 typedef boost::shared_ptr<com::sun::star::uno::Sequence<T> >
58 SequencePointer;
60 SequencePointer mpSequence;
61 sal_uInt32 mnOffset;
62 sal_uInt32 mnCount;
64 public:
65 typedef boost::shared_ptr<SubSequence> Pointer_t;
67 SubSequence() : mpSequence(new ::com::sun::star::uno::Sequence<T>()),
68 mnOffset(0), mnCount(0)
72 SubSequence(SequencePointer pSequence, sal_uInt32 nOffset_,
73 sal_uInt32 nCount_)
74 : mpSequence(pSequence), mnOffset(nOffset_), mnCount(nCount_)
78 SubSequence(SequencePointer pSequence)
79 : mpSequence(pSequence), mnOffset(0), mnCount(pSequence->getLength())
83 SubSequence(const SubSequence & rSubSequence, sal_uInt32 nOffset_,
84 sal_uInt32 nCount_)
85 : mpSequence(rSubSequence.mpSequence),
86 mnOffset(rSubSequence.mnOffset + nOffset_),
87 mnCount(nCount_)
91 SubSequence(const T * pStart, sal_uInt32 nCount_)
92 : mpSequence(new com::sun::star::uno::Sequence<T>(pStart, nCount_)),
93 mnOffset(0), mnCount(nCount_)
97 SubSequence(sal_Int32 nCount_)
98 : mpSequence(new com::sun::star::uno::Sequence<T>(nCount_)), mnOffset(0),
99 mnCount(nCount_)
103 ::com::sun::star::uno::Sequence<T> & getSequence()
105 return *mpSequence;
108 const ::com::sun::star::uno::Sequence<T> & getSequence() const
110 return *mpSequence;
113 void reset() {
114 mnOffset = 0;
115 mnCount = mpSequence->getLength();
118 sal_uInt32 getOffset() const { return mnOffset; }
119 sal_uInt32 getCount() const { return mnCount; }
121 const T & operator[] (sal_uInt32 nIndex) const
123 if (mnOffset + nIndex >=
124 sal::static_int_cast<sal_uInt32>(mpSequence->getLength()))
125 throw ExceptionOutOfBounds("SubSequence::operator[]");
127 return (*mpSequence)[mnOffset + nIndex];
130 void dump(ostream & o) const
133 char sBuffer[256];
135 snprintf(sBuffer, sizeof(sBuffer),
136 "<sequence id='%p' offset='%lx' count='%lx'>",
137 mpSequence.get(), mnOffset, mnCount);
138 o << sBuffer << endl;
141 sal_uInt32 n = 0;
142 sal_uInt32 nStep = 16;
144 while (n < getCount())
146 char sBuffer[256];
148 o << "<line>";
150 snprintf(sBuffer, 255, "%08lx: ", n);
152 o << sBuffer;
154 for (sal_uInt32 i = 0; i < nStep; i++)
156 if (n + i < getCount())
158 snprintf(sBuffer, 255, "%02x ", operator[](n + i));
159 o << sBuffer;
161 else
162 o << " ";
164 if (i % 8 == 7)
165 o << " ";
169 for (sal_uInt32 i = 0; i < nStep; i++)
171 if (n + i < getCount())
173 unsigned char c =
174 static_cast<unsigned char>(operator[](n + i));
176 if (c=='&')
177 o << "&amp;";
178 else if (c=='<')
179 o << "&lt;";
180 else if (c=='>')
181 o << "&gt;";
182 else if (c < 128 && isprint(c))
183 o << c;
184 else
185 o << ".";
191 o << "</line>" << endl;
193 n += nStep;
196 o << "</sequence>" << endl;
199 void dump(OutputWithDepth<string> & o)
202 char sBuffer[256];
204 snprintf(sBuffer, sizeof(sBuffer),
205 "<sequence id='%p' offset='%" SAL_PRIxUINT32 "' count='%" SAL_PRIxUINT32 "'>",
206 mpSequence.get(), mnOffset, mnCount);
207 o.addItem(sBuffer);
210 sal_uInt32 n = 0;
211 sal_uInt32 nStep = 16;
213 try
215 sal_uInt32 nCount = getCount();
216 while (n < nCount)
218 sal_uInt32 nBytes = nCount - n;
220 if (nBytes > nStep)
221 nBytes = nStep;
223 SubSequence<T> aSeq(*this, n, nBytes);
224 dumpLine(o, aSeq, n, nStep);
226 n += nBytes;
229 catch (...)
231 o.addItem("<exception/>");
234 o.addItem("</sequence>");
237 string toString() const
239 sal_uInt32 n = 0;
240 sal_uInt32 nStep = 16;
242 string sResult;
244 while (n < getCount())
246 char sBuffer[256];
248 snprintf(sBuffer, 255, "<line>%08" SAL_PRIxUINT32 ": ", n);
250 sResult += sBuffer;
252 for (sal_uInt32 i = 0; i < nStep; i++)
254 if (n + i < getCount())
256 snprintf(sBuffer, 255, "%02x ", operator[](n + i));
257 sResult += sBuffer;
259 else
260 sResult += " ";
262 if (i % 8 == 7)
263 sResult += " ";
267 for (sal_uInt32 i = 0; i < nStep; i++)
269 if (n + i < getCount())
271 unsigned char c =
272 static_cast<unsigned char>(operator[](n + i));
274 if (c=='&')
275 sResult += "&amp;";
276 else if (c=='<')
277 sResult += "&lt;";
278 else if (c=='>')
279 sResult += "&gt;";
280 else if (c < 128 && isprint(c))
281 sResult += c;
282 else
283 sResult += ".";
288 sResult += "</line>\n";
290 n += nStep;
293 return sResult;
297 template <typename T>
298 void dumpLine(OutputWithDepth<string> & o, SubSequence<T> & rSeq,
299 sal_uInt32 nOffset, sal_uInt32 nStep)
301 sal_uInt32 nCount = rSeq.getCount();
302 char sBuffer[256];
304 string tmpStr = "<line>";
306 snprintf(sBuffer, 255, "%08" SAL_PRIxUINT32 ": ", nOffset);
308 tmpStr += sBuffer;
310 for (sal_uInt32 i = 0; i < nStep; i++)
312 if (i < nCount)
314 snprintf(sBuffer, 255, "%02x ", rSeq[i]);
315 tmpStr += sBuffer;
317 else
318 tmpStr += " ";
320 if (i % 8 == 7)
321 tmpStr += " ";
325 for (sal_uInt32 i = 0; i < nStep; i++)
327 if (i < nCount)
329 unsigned char c =
330 static_cast<unsigned char>(rSeq[i]);
332 if (c=='&')
333 tmpStr += "&amp;";
334 else if (c=='<')
335 tmpStr += "&lt;";
336 else if (c=='>')
337 tmpStr += "&gt;";
338 else if (c < 128 && isprint(c))
339 tmpStr += c;
340 else
341 tmpStr += ".";
347 tmpStr += "</line>";
349 o.addItem(tmpStr);
354 #endif // INCLUDED_SUB_SEQUENCE_HXX