2 * Dynamic structure array (DSA) implementation
4 * Copyright 1998 Eric Kohl
5 * 1998 Juergen Schmied <j.schmied@metronet.de>
6 * 2000 Eric Kohl for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * These functions were involuntarily documented by Microsoft in 2002 as
24 * the outcome of an anti-trust suit brought by various U.S. governments.
25 * As a result the specifications on MSDN are inaccurate, incomplete
26 * and misleading. A much more complete (unofficial) documentation is
29 * http://members.ozemail.com.au/~geoffch/samples/win32/shell/comctl32
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(dsa
);
53 /**************************************************************************
54 * DSA_Create [COMCTL32.320]
56 * Creates a dynamic storage array
59 * nSize [I] size of the array elements
60 * nGrow [I] number of elements by which the array grows when it is filled
63 * Success: pointer to an array control structure. Use this like a handle.
67 * The DSA_ functions can be used to create and manipulate arrays of
68 * fixed-size memory blocks. These arrays can store any kind of data
69 * (e.g. strings and icons).
71 HDSA WINAPI
DSA_Create (INT nSize
, INT nGrow
)
75 TRACE("(size=%d grow=%d)\n", nSize
, nGrow
);
77 hdsa
= Alloc (sizeof(*hdsa
));
83 hdsa
->nItemSize
= nSize
;
84 hdsa
->nGrow
= max(1, nGrow
);
91 /**************************************************************************
92 * DSA_Destroy [COMCTL32.321]
94 * Destroys a dynamic storage array
97 * hdsa [I] pointer to the array control structure
103 BOOL WINAPI
DSA_Destroy (HDSA hdsa
)
105 TRACE("(%p)\n", hdsa
);
110 if (hdsa
->pData
&& (!Free (hdsa
->pData
)))
117 /**************************************************************************
118 * DSA_GetItem [COMCTL32.322]
120 * Copies the specified item into a caller-supplied buffer.
123 * hdsa [I] pointer to the array control structure
124 * nIndex [I] number of the Item to get
125 * pDest [O] destination buffer. Has to be >= dwElementSize.
131 BOOL WINAPI
DSA_GetItem (HDSA hdsa
, INT nIndex
, LPVOID pDest
)
135 TRACE("(%p %d %p)\n", hdsa
, nIndex
, pDest
);
139 if ((nIndex
< 0) || (nIndex
>= hdsa
->nItemCount
))
142 pSrc
= (char *) hdsa
->pData
+ (hdsa
->nItemSize
* nIndex
);
143 memmove (pDest
, pSrc
, hdsa
->nItemSize
);
149 /**************************************************************************
150 * DSA_GetItemPtr [COMCTL32.323]
152 * Retrieves a pointer to the specified item.
155 * hdsa [I] pointer to the array control structure
156 * nIndex [I] index of the desired item
159 * Success: pointer to an item
162 LPVOID WINAPI
DSA_GetItemPtr (HDSA hdsa
, INT nIndex
)
166 TRACE("(%p %d)\n", hdsa
, nIndex
);
170 if ((nIndex
< 0) || (nIndex
>= hdsa
->nItemCount
))
173 pSrc
= (char *) hdsa
->pData
+ (hdsa
->nItemSize
* nIndex
);
175 TRACE("-- ret=%p\n", pSrc
);
181 /**************************************************************************
182 * DSA_SetItem [COMCTL32.325]
184 * Sets the contents of an item in the array.
187 * hdsa [I] pointer to the array control structure
188 * nIndex [I] index for the item
189 * pSrc [I] pointer to the new item data
195 BOOL WINAPI
DSA_SetItem (HDSA hdsa
, INT nIndex
, LPVOID pSrc
)
197 INT nSize
, nNewItems
;
198 LPVOID pDest
, lpTemp
;
200 TRACE("(%p %d %p)\n", hdsa
, nIndex
, pSrc
);
202 if ((!hdsa
) || nIndex
< 0)
205 if (hdsa
->nItemCount
<= nIndex
) {
206 /* within the old array */
207 if (hdsa
->nMaxCount
> nIndex
) {
208 /* within the allocated space, set a new boundary */
209 hdsa
->nItemCount
= nIndex
+ 1;
212 /* resize the block of memory */
214 hdsa
->nGrow
* ((((nIndex
+ 1) - 1) / hdsa
->nGrow
) + 1);
215 nSize
= hdsa
->nItemSize
* nNewItems
;
217 lpTemp
= ReAlloc (hdsa
->pData
, nSize
);
221 hdsa
->nMaxCount
= nNewItems
;
222 hdsa
->nItemCount
= nIndex
+ 1;
223 hdsa
->pData
= lpTemp
;
227 /* put the new entry in */
228 pDest
= (char *) hdsa
->pData
+ (hdsa
->nItemSize
* nIndex
);
229 TRACE("-- move dest=%p src=%p size=%d\n",
230 pDest
, pSrc
, hdsa
->nItemSize
);
231 memmove (pDest
, pSrc
, hdsa
->nItemSize
);
237 /**************************************************************************
238 * DSA_InsertItem [COMCTL32.324]
240 * Inserts an item into the array at the specified index.
243 * hdsa [I] pointer to the array control structure
244 * nIndex [I] index for the new item
245 * pSrc [I] pointer to the element
248 * Success: position of the new item
251 INT WINAPI
DSA_InsertItem (HDSA hdsa
, INT nIndex
, LPVOID pSrc
)
253 INT nNewItems
, nSize
;
254 LPVOID lpTemp
, lpDest
;
256 TRACE("(%p %d %p)\n", hdsa
, nIndex
, pSrc
);
258 if ((!hdsa
) || nIndex
< 0)
261 /* when nIndex >= nItemCount then append */
262 if (nIndex
>= hdsa
->nItemCount
)
263 nIndex
= hdsa
->nItemCount
;
265 /* do we need to resize ? */
266 if (hdsa
->nItemCount
>= hdsa
->nMaxCount
) {
267 nNewItems
= hdsa
->nMaxCount
+ hdsa
->nGrow
;
268 nSize
= hdsa
->nItemSize
* nNewItems
;
270 if (nSize
/ hdsa
->nItemSize
!= nNewItems
)
273 lpTemp
= ReAlloc (hdsa
->pData
, nSize
);
277 hdsa
->nMaxCount
= nNewItems
;
278 hdsa
->pData
= lpTemp
;
281 /* do we need to move elements ? */
282 if (nIndex
< hdsa
->nItemCount
) {
283 lpTemp
= (char *) hdsa
->pData
+ (hdsa
->nItemSize
* nIndex
);
284 lpDest
= (char *) lpTemp
+ hdsa
->nItemSize
;
285 nSize
= (hdsa
->nItemCount
- nIndex
) * hdsa
->nItemSize
;
286 TRACE("-- move dest=%p src=%p size=%d\n",
287 lpDest
, lpTemp
, nSize
);
288 memmove (lpDest
, lpTemp
, nSize
);
291 /* ok, we can put the new Item in */
293 lpDest
= (char *) hdsa
->pData
+ (hdsa
->nItemSize
* nIndex
);
294 TRACE("-- move dest=%p src=%p size=%d\n",
295 lpDest
, pSrc
, hdsa
->nItemSize
);
296 memmove (lpDest
, pSrc
, hdsa
->nItemSize
);
302 /**************************************************************************
303 * DSA_DeleteItem [COMCTL32.326]
305 * Deletes the specified item from the array.
308 * hdsa [I] pointer to the array control structure
309 * nIndex [I] index for the element to delete
312 * Success: number of the deleted element
315 INT WINAPI
DSA_DeleteItem (HDSA hdsa
, INT nIndex
)
320 TRACE("(%p %d)\n", hdsa
, nIndex
);
324 if (nIndex
< 0 || nIndex
>= hdsa
->nItemCount
)
327 /* do we need to move ? */
328 if (nIndex
< hdsa
->nItemCount
- 1) {
329 lpDest
= (char *) hdsa
->pData
+ (hdsa
->nItemSize
* nIndex
);
330 lpSrc
= (char *) lpDest
+ hdsa
->nItemSize
;
331 nSize
= hdsa
->nItemSize
* (hdsa
->nItemCount
- nIndex
- 1);
332 TRACE("-- move dest=%p src=%p size=%d\n",
333 lpDest
, lpSrc
, nSize
);
334 memmove (lpDest
, lpSrc
, nSize
);
340 if ((hdsa
->nMaxCount
- hdsa
->nItemCount
) >= hdsa
->nGrow
) {
341 nSize
= hdsa
->nItemSize
* hdsa
->nItemCount
;
343 lpDest
= ReAlloc (hdsa
->pData
, nSize
);
347 hdsa
->nMaxCount
= hdsa
->nItemCount
;
348 hdsa
->pData
= lpDest
;
355 /**************************************************************************
356 * DSA_DeleteAllItems [COMCTL32.327]
358 * Removes all items and reinitializes the array.
361 * hdsa [I] pointer to the array control structure
367 BOOL WINAPI
DSA_DeleteAllItems (HDSA hdsa
)
369 TRACE("(%p)\n", hdsa
);
373 if (hdsa
->pData
&& (!Free (hdsa
->pData
)))
376 hdsa
->nItemCount
= 0;
384 /**************************************************************************
385 * DSA_EnumCallback [COMCTL32.387]
387 * Enumerates all items in a dynamic storage array.
390 * hdsa [I] handle to the dynamic storage array
397 VOID WINAPI
DSA_EnumCallback (HDSA hdsa
, PFNDSAENUMCALLBACK enumProc
,
402 TRACE("(%p %p %p)\n", hdsa
, enumProc
, lParam
);
406 if (hdsa
->nItemCount
<= 0)
409 for (i
= 0; i
< hdsa
->nItemCount
; i
++) {
410 LPVOID lpItem
= DSA_GetItemPtr (hdsa
, i
);
411 if ((enumProc
)(lpItem
, lParam
) == 0)
419 /**************************************************************************
420 * DSA_DestroyCallback [COMCTL32.388]
422 * Enumerates all items in a dynamic storage array and destroys it.
425 * hdsa [I] handle to the dynamic storage array
432 void WINAPI
DSA_DestroyCallback (HDSA hdsa
, PFNDSAENUMCALLBACK enumProc
,
435 TRACE("(%p %p %p)\n", hdsa
, enumProc
, lParam
);
437 DSA_EnumCallback (hdsa
, enumProc
, lParam
);
441 /**************************************************************************
442 * DSA_Clone [COMCTL32.@]
444 * Creates a copy of a dsa
447 * hdsa [I] handle to the dynamic storage array
452 HDSA WINAPI
DSA_Clone(HDSA hdsa
)
457 TRACE("(%p)\n", hdsa
);
462 dest
= DSA_Create (hdsa
->nItemSize
, hdsa
->nGrow
);
466 for (i
= 0; i
< hdsa
->nItemCount
; i
++) {
467 void *ptr
= DSA_GetItemPtr (hdsa
, i
);
468 if (DSA_InsertItem (dest
, DA_LAST
, ptr
) == -1) {
477 /**************************************************************************
478 * DSA_GetSize [COMCTL32.@]
480 * Returns allocated memory size for this array
483 * hdsa [I] handle to the dynamic storage array
488 ULONGLONG WINAPI
DSA_GetSize(HDSA hdsa
)
490 TRACE("(%p)\n", hdsa
);
494 return sizeof(*hdsa
) + (ULONGLONG
)hdsa
->nMaxCount
*hdsa
->nItemSize
;