re-enabled user-defined numeric fields for dBase export
[LibreOffice.git] / sc / source / filter / starcalc / collect.cxx
blobd6c3c5de049a34214ece446924b23a29caebd424
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 #include "collect.hxx"
22 #include <string.h>
24 #define MAXCOLLECTIONSIZE 16384
25 #define MAXDELTA 1024
27 // -----------------------------------------------------------------------
29 ScDataObject::~ScDataObject()
33 //------------------------------------------------------------------------
34 // Collection
35 //------------------------------------------------------------------------
37 static void lcl_DeleteScDataObjects( ScDataObject** p, sal_uInt16 nCount )
39 if ( p )
41 for (sal_uInt16 i = 0; i < nCount; i++) delete p[i];
42 delete[] p;
43 p = NULL;
47 ScCollection::ScCollection(sal_uInt16 nLim, sal_uInt16 nDel) :
48 nCount ( 0 ),
49 nLimit ( nLim ),
50 nDelta ( nDel ),
51 pItems ( NULL )
53 if (nDelta > MAXDELTA)
54 nDelta = MAXDELTA;
55 else if (nDelta == 0)
56 nDelta = 1;
57 if (nLimit > MAXCOLLECTIONSIZE)
58 nLimit = MAXCOLLECTIONSIZE;
59 else if (nLimit < nDelta)
60 nLimit = nDelta;
61 pItems = new ScDataObject*[nLimit];
64 ScCollection::ScCollection(const ScCollection& rCollection)
65 : ScDataObject(),
66 nCount ( 0 ),
67 nLimit ( 0 ),
68 nDelta ( 0 ),
69 pItems ( NULL )
71 *this = rCollection;
74 //------------------------------------------------------------------------
76 ScCollection::~ScCollection()
78 lcl_DeleteScDataObjects( pItems, nCount );
81 //------------------------------------------------------------------------
82 sal_uInt16 ScCollection::GetCount() const { return nCount; }
84 //------------------------------------------------------------------------
86 sal_Bool ScCollection::AtInsert(sal_uInt16 nIndex, ScDataObject* pScDataObject)
88 if ((nCount < MAXCOLLECTIONSIZE) && (nIndex <= nCount) && pItems)
90 if (nCount == nLimit)
92 ScDataObject** pNewItems = new ScDataObject*[nLimit + nDelta];
93 if (!pNewItems)
94 return false;
95 nLimit = sal::static_int_cast<sal_uInt16>( nLimit + nDelta );
96 memcpy(pNewItems, pItems, nCount * sizeof(ScDataObject*));
97 delete[] pItems;
98 pItems = pNewItems;
100 if (nCount > nIndex)
101 memmove(&pItems[nIndex + 1], &pItems[nIndex], (nCount - nIndex) * sizeof(ScDataObject*));
102 pItems[nIndex] = pScDataObject;
103 nCount++;
104 return sal_True;
106 return false;
109 //------------------------------------------------------------------------
111 sal_Bool ScCollection::Insert(ScDataObject* pScDataObject)
113 return AtInsert(nCount, pScDataObject);
116 //------------------------------------------------------------------------
118 ScDataObject* ScCollection::At(sal_uInt16 nIndex) const
120 if (nIndex < nCount)
121 return pItems[nIndex];
122 else
123 return NULL;
126 //------------------------------------------------------------------------
128 sal_uInt16 ScCollection::IndexOf(ScDataObject* pScDataObject) const
130 sal_uInt16 nIndex = 0xffff;
131 for (sal_uInt16 i = 0; ((i < nCount) && (nIndex == 0xffff)); i++)
133 if (pItems[i] == pScDataObject) nIndex = i;
135 return nIndex;
138 //------------------------------------------------------------------------
140 ScCollection& ScCollection::operator=( const ScCollection& r )
142 // Check for self-assignment
143 if (this == &r)
144 return *this;
146 lcl_DeleteScDataObjects( pItems, nCount );
148 nCount = r.nCount;
149 nLimit = r.nLimit;
150 nDelta = r.nDelta;
151 pItems = new ScDataObject*[nLimit];
152 for ( sal_uInt16 i=0; i<nCount; i++ )
153 pItems[i] = r.pItems[i]->Clone();
155 return *this;
158 //------------------------------------------------------------------------
160 ScDataObject* ScCollection::Clone() const
162 return new ScCollection(*this);
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */