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
34 WINE_DEFAULT_DEBUG_CHANNEL(dsa
);
45 /**************************************************************************
46 * DSA_Create [COMCTL32.320]
48 * Creates a dynamic storage array
51 * nSize [I] size of the array elements
52 * nGrow [I] number of elements by which the array grows when it is filled
55 * Success: pointer to an array control structure. Use this like a handle.
59 * The DSA_ functions can be used to create and manipulate arrays of
60 * fixed-size memory blocks. These arrays can store any kind of data
61 * (e.g. strings and icons).
63 HDSA WINAPI
DSA_Create (INT nSize
, INT nGrow
)
67 TRACE("(size=%d grow=%d)\n", nSize
, nGrow
);
69 hdsa
= Alloc (sizeof(*hdsa
));
75 hdsa
->nItemSize
= nSize
;
76 hdsa
->nGrow
= max(1, nGrow
);
83 /**************************************************************************
84 * DSA_Destroy [COMCTL32.321]
86 * Destroys a dynamic storage array
89 * hdsa [I] pointer to the array control structure
95 BOOL WINAPI
DSA_Destroy (HDSA hdsa
)
97 TRACE("(%p)\n", hdsa
);
102 if (hdsa
->pData
&& (!Free (hdsa
->pData
)))
109 /**************************************************************************
110 * DSA_GetItem [COMCTL32.322]
112 * Copies the specified item into a caller-supplied buffer.
115 * hdsa [I] pointer to the array control structure
116 * nIndex [I] number of the Item to get
117 * pDest [O] destination buffer. Has to be >= dwElementSize.
123 BOOL WINAPI
DSA_GetItem (HDSA hdsa
, INT nIndex
, LPVOID pDest
)
127 TRACE("(%p %d %p)\n", hdsa
, nIndex
, pDest
);
131 if ((nIndex
< 0) || (nIndex
>= hdsa
->nItemCount
))
134 pSrc
= (char *) hdsa
->pData
+ (hdsa
->nItemSize
* nIndex
);
135 memmove (pDest
, pSrc
, hdsa
->nItemSize
);
141 /**************************************************************************
142 * DSA_GetItemPtr [COMCTL32.323]
144 * Retrieves a pointer to the specified item.
147 * hdsa [I] pointer to the array control structure
148 * nIndex [I] index of the desired item
151 * Success: pointer to an item
154 LPVOID WINAPI
DSA_GetItemPtr (HDSA hdsa
, INT nIndex
)
158 TRACE("(%p %d)\n", hdsa
, nIndex
);
162 if ((nIndex
< 0) || (nIndex
>= hdsa
->nItemCount
))
165 pSrc
= (char *) hdsa
->pData
+ (hdsa
->nItemSize
* nIndex
);
167 TRACE("-- ret=%p\n", pSrc
);
173 /**************************************************************************
174 * DSA_SetItem [COMCTL32.325]
176 * Sets the contents of an item in the array.
179 * hdsa [I] pointer to the array control structure
180 * nIndex [I] index for the item
181 * pSrc [I] pointer to the new item data
187 BOOL WINAPI
DSA_SetItem (HDSA hdsa
, INT nIndex
, LPVOID pSrc
)
189 INT nSize
, nNewItems
;
190 LPVOID pDest
, lpTemp
;
192 TRACE("(%p %d %p)\n", hdsa
, nIndex
, pSrc
);
194 if ((!hdsa
) || nIndex
< 0)
197 if (hdsa
->nItemCount
<= nIndex
) {
198 /* within the old array */
199 if (hdsa
->nMaxCount
> nIndex
) {
200 /* within the allocated space, set a new boundary */
201 hdsa
->nItemCount
= nIndex
+ 1;
204 /* resize the block of memory */
206 hdsa
->nGrow
* ((((nIndex
+ 1) - 1) / hdsa
->nGrow
) + 1);
207 nSize
= hdsa
->nItemSize
* nNewItems
;
209 lpTemp
= ReAlloc (hdsa
->pData
, nSize
);
213 hdsa
->nMaxCount
= nNewItems
;
214 hdsa
->nItemCount
= nIndex
+ 1;
215 hdsa
->pData
= lpTemp
;
219 /* put the new entry in */
220 pDest
= (char *) hdsa
->pData
+ (hdsa
->nItemSize
* nIndex
);
221 TRACE("-- move dest=%p src=%p size=%d\n",
222 pDest
, pSrc
, hdsa
->nItemSize
);
223 memmove (pDest
, pSrc
, hdsa
->nItemSize
);
229 /**************************************************************************
230 * DSA_InsertItem [COMCTL32.324]
232 * Inserts an item into the array at the specified index.
235 * hdsa [I] pointer to the array control structure
236 * nIndex [I] index for the new item
237 * pSrc [I] pointer to the element
240 * Success: position of the new item
243 INT WINAPI
DSA_InsertItem (HDSA hdsa
, INT nIndex
, LPVOID pSrc
)
245 INT nNewItems
, nSize
;
246 LPVOID lpTemp
, lpDest
;
248 TRACE("(%p %d %p)\n", hdsa
, nIndex
, pSrc
);
250 if ((!hdsa
) || nIndex
< 0)
253 /* when nIndex >= nItemCount then append */
254 if (nIndex
>= hdsa
->nItemCount
)
255 nIndex
= hdsa
->nItemCount
;
257 /* do we need to resize ? */
258 if (hdsa
->nItemCount
>= hdsa
->nMaxCount
) {
259 nNewItems
= hdsa
->nMaxCount
+ hdsa
->nGrow
;
260 nSize
= hdsa
->nItemSize
* nNewItems
;
262 lpTemp
= ReAlloc (hdsa
->pData
, nSize
);
266 hdsa
->nMaxCount
= nNewItems
;
267 hdsa
->pData
= lpTemp
;
270 /* do we need to move elements ? */
271 if (nIndex
< hdsa
->nItemCount
) {
272 lpTemp
= (char *) hdsa
->pData
+ (hdsa
->nItemSize
* nIndex
);
273 lpDest
= (char *) lpTemp
+ hdsa
->nItemSize
;
274 nSize
= (hdsa
->nItemCount
- nIndex
) * hdsa
->nItemSize
;
275 TRACE("-- move dest=%p src=%p size=%d\n",
276 lpDest
, lpTemp
, nSize
);
277 memmove (lpDest
, lpTemp
, nSize
);
280 /* ok, we can put the new Item in */
282 lpDest
= (char *) hdsa
->pData
+ (hdsa
->nItemSize
* nIndex
);
283 TRACE("-- move dest=%p src=%p size=%d\n",
284 lpDest
, pSrc
, hdsa
->nItemSize
);
285 memmove (lpDest
, pSrc
, hdsa
->nItemSize
);
291 /**************************************************************************
292 * DSA_DeleteItem [COMCTL32.326]
294 * Deletes the specified item from the array.
297 * hdsa [I] pointer to the array control structure
298 * nIndex [I] index for the element to delete
301 * Success: number of the deleted element
304 INT WINAPI
DSA_DeleteItem (HDSA hdsa
, INT nIndex
)
309 TRACE("(%p %d)\n", hdsa
, nIndex
);
313 if (nIndex
< 0 || nIndex
>= hdsa
->nItemCount
)
316 /* do we need to move ? */
317 if (nIndex
< hdsa
->nItemCount
- 1) {
318 lpDest
= (char *) hdsa
->pData
+ (hdsa
->nItemSize
* nIndex
);
319 lpSrc
= (char *) lpDest
+ hdsa
->nItemSize
;
320 nSize
= hdsa
->nItemSize
* (hdsa
->nItemCount
- nIndex
- 1);
321 TRACE("-- move dest=%p src=%p size=%d\n",
322 lpDest
, lpSrc
, nSize
);
323 memmove (lpDest
, lpSrc
, nSize
);
329 if ((hdsa
->nMaxCount
- hdsa
->nItemCount
) >= hdsa
->nGrow
) {
330 nSize
= hdsa
->nItemSize
* hdsa
->nItemCount
;
332 lpDest
= ReAlloc (hdsa
->pData
, nSize
);
336 hdsa
->nMaxCount
= hdsa
->nItemCount
;
337 hdsa
->pData
= lpDest
;
344 /**************************************************************************
345 * DSA_DeleteAllItems [COMCTL32.327]
347 * Removes all items and reinitializes the array.
350 * hdsa [I] pointer to the array control structure
356 BOOL WINAPI
DSA_DeleteAllItems (HDSA hdsa
)
358 TRACE("(%p)\n", hdsa
);
362 if (hdsa
->pData
&& (!Free (hdsa
->pData
)))
365 hdsa
->nItemCount
= 0;
373 /**************************************************************************
374 * DSA_EnumCallback [COMCTL32.387]
376 * Enumerates all items in a dynamic storage array.
379 * hdsa [I] handle to the dynamic storage array
386 VOID WINAPI
DSA_EnumCallback (HDSA hdsa
, PFNDSAENUMCALLBACK enumProc
,
391 TRACE("(%p %p %p)\n", hdsa
, enumProc
, lParam
);
395 if (hdsa
->nItemCount
<= 0)
398 for (i
= 0; i
< hdsa
->nItemCount
; i
++) {
399 LPVOID lpItem
= DSA_GetItemPtr (hdsa
, i
);
400 if ((enumProc
)(lpItem
, lParam
) == 0)
408 /**************************************************************************
409 * DSA_DestroyCallback [COMCTL32.388]
411 * Enumerates all items in a dynamic storage array and destroys it.
414 * hdsa [I] handle to the dynamic storage array
421 void WINAPI
DSA_DestroyCallback (HDSA hdsa
, PFNDSAENUMCALLBACK enumProc
,
424 TRACE("(%p %p %p)\n", hdsa
, enumProc
, lParam
);
426 DSA_EnumCallback (hdsa
, enumProc
, lParam
);