mshtml/tests: Ignore calls missing in IE6.
[wine/multimedia.git] / dlls / comctl32 / dpa.c
blob96823442864586b4cbfb163b1a4afc7d0512a97a
1 /*
2 * Dynamic pointer array (DPA) 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
22 * NOTES
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
27 * available at:
29 * http://members.ozemail.com.au/~geoffch/samples/win32/shell/comctl32
32 #define COBJMACROS
34 #include <stdarg.h>
35 #include <limits.h>
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winuser.h"
40 #include "commctrl.h"
41 #include "objbase.h"
43 #include "comctl32.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(dpa);
48 typedef struct _DPA
50 INT nItemCount;
51 LPVOID *ptrs;
52 HANDLE hHeap;
53 INT nGrow;
54 INT nMaxCount;
55 } DPA;
57 typedef struct _STREAMDATA
59 DWORD dwSize;
60 DWORD dwData2;
61 DWORD dwItems;
62 } STREAMDATA, *PSTREAMDATA;
64 /**************************************************************************
65 * DPA_LoadStream [COMCTL32.9]
67 * Loads a dynamic pointer array from a stream
69 * PARAMS
70 * phDpa [O] pointer to a handle to a dynamic pointer array
71 * loadProc [I] pointer to a callback function
72 * pStream [I] pointer to a stream
73 * pData [I] pointer to callback data
75 * RETURNS
76 * Success: S_OK, S_FALSE - partial success
77 * Failure: HRESULT error code
79 * NOTES
80 * No more information available yet!
82 HRESULT WINAPI DPA_LoadStream (HDPA *phDpa, PFNDPASTREAM loadProc,
83 IStream *pStream, LPVOID pData)
85 HRESULT errCode;
86 LARGE_INTEGER position;
87 ULARGE_INTEGER initial_pos;
88 STREAMDATA streamData;
89 DPASTREAMINFO streamInfo;
90 ULONG ulRead;
91 HDPA hDpa;
92 PVOID *ptr;
94 TRACE ("phDpa=%p loadProc=%p pStream=%p pData=%p\n",
95 phDpa, loadProc, pStream, pData);
97 if (!phDpa || !loadProc || !pStream)
98 return E_INVALIDARG;
100 *phDpa = NULL;
102 position.QuadPart = 0;
104 errCode = IStream_Seek (pStream, position, STREAM_SEEK_CUR, &initial_pos);
105 if (errCode != S_OK)
106 return errCode;
108 memset(&streamData, 0, sizeof(STREAMDATA));
109 errCode = IStream_Read (pStream, &streamData, sizeof(STREAMDATA), &ulRead);
110 if (errCode != S_OK)
111 return errCode;
113 TRACE ("dwSize=%u dwData2=%u dwItems=%u\n",
114 streamData.dwSize, streamData.dwData2, streamData.dwItems);
116 if (ulRead < sizeof(STREAMDATA) ||
117 streamData.dwSize < sizeof(STREAMDATA) || streamData.dwData2 != 1) {
118 /* back to initial position */
119 position.QuadPart = initial_pos.QuadPart;
120 IStream_Seek (pStream, position, STREAM_SEEK_SET, NULL);
121 return E_FAIL;
124 if (streamData.dwItems > (UINT_MAX / 2 / sizeof(VOID*))) /* 536870911 */
125 return E_OUTOFMEMORY;
127 /* create the dpa */
128 hDpa = DPA_Create (streamData.dwItems);
129 if (!hDpa)
130 return E_OUTOFMEMORY;
132 if (!DPA_Grow (hDpa, streamData.dwItems))
133 return E_OUTOFMEMORY;
135 /* load data from the stream into the dpa */
136 ptr = hDpa->ptrs;
137 for (streamInfo.iPos = 0; streamInfo.iPos < streamData.dwItems; streamInfo.iPos++) {
138 errCode = (loadProc)(&streamInfo, pStream, pData);
139 if (errCode != S_OK) {
140 errCode = S_FALSE;
141 break;
144 *ptr = streamInfo.pvItem;
145 ptr++;
148 /* set the number of items */
149 hDpa->nItemCount = streamInfo.iPos;
151 /* store the handle to the dpa */
152 *phDpa = hDpa;
153 TRACE ("new hDpa=%p, errorcode=%x\n", hDpa, errCode);
155 return errCode;
159 /**************************************************************************
160 * DPA_SaveStream [COMCTL32.10]
162 * Saves a dynamic pointer array to a stream
164 * PARAMS
165 * hDpa [I] handle to a dynamic pointer array
166 * saveProc [I] pointer to a callback function
167 * pStream [I] pointer to a stream
168 * pData [I] pointer to callback data
170 * RETURNS
171 * Success: S_OK, S_FALSE - partial success
172 * Failure: HRESULT error code
174 * NOTES
175 * No more information available yet!
177 HRESULT WINAPI DPA_SaveStream (const HDPA hDpa, PFNDPASTREAM saveProc,
178 IStream *pStream, LPVOID pData)
180 LARGE_INTEGER position;
181 ULARGE_INTEGER initial_pos, curr_pos;
182 STREAMDATA streamData;
183 DPASTREAMINFO streamInfo;
184 HRESULT hr;
185 PVOID *ptr;
187 TRACE ("hDpa=%p saveProc=%p pStream=%p pData=%p\n",
188 hDpa, saveProc, pStream, pData);
190 if (!hDpa || !saveProc || !pStream) return E_INVALIDARG;
192 /* save initial position to write header after completion */
193 position.QuadPart = 0;
194 hr = IStream_Seek (pStream, position, STREAM_SEEK_CUR, &initial_pos);
195 if (hr != S_OK)
196 return hr;
198 /* write empty header */
199 streamData.dwSize = sizeof(streamData);
200 streamData.dwData2 = 1;
201 streamData.dwItems = 0;
203 hr = IStream_Write (pStream, &streamData, sizeof(streamData), NULL);
204 if (hr != S_OK) {
205 position.QuadPart = initial_pos.QuadPart;
206 IStream_Seek (pStream, position, STREAM_SEEK_SET, NULL);
207 return hr;
210 /* no items - we're done */
211 if (hDpa->nItemCount == 0) return S_OK;
213 ptr = hDpa->ptrs;
214 for (streamInfo.iPos = 0; streamInfo.iPos < hDpa->nItemCount; streamInfo.iPos++) {
215 streamInfo.pvItem = *ptr;
216 hr = (saveProc)(&streamInfo, pStream, pData);
217 if (hr != S_OK) {
218 hr = S_FALSE;
219 break;
221 ptr++;
224 /* write updated header */
225 position.QuadPart = 0;
226 IStream_Seek (pStream, position, STREAM_SEEK_CUR, &curr_pos);
228 streamData.dwSize = curr_pos.QuadPart - initial_pos.QuadPart;
229 streamData.dwData2 = 1;
230 streamData.dwItems = streamInfo.iPos;
232 position.QuadPart = initial_pos.QuadPart;
233 IStream_Seek (pStream, position, STREAM_SEEK_SET, NULL);
234 IStream_Write (pStream, &streamData, sizeof(streamData), NULL);
236 position.QuadPart = curr_pos.QuadPart;
237 IStream_Seek (pStream, position, STREAM_SEEK_SET, NULL);
239 return hr;
243 /**************************************************************************
244 * DPA_Merge [COMCTL32.11]
246 * Merge two dynamic pointers arrays.
248 * PARAMS
249 * hdpa1 [I] handle to a dynamic pointer array
250 * hdpa2 [I] handle to a dynamic pointer array
251 * dwFlags [I] flags
252 * pfnCompare [I] pointer to sort function
253 * pfnMerge [I] pointer to merge function
254 * lParam [I] application specific value
256 * RETURNS
257 * Success: TRUE
258 * Failure: FALSE
260 * NOTES
261 * No more information available yet!
263 BOOL WINAPI DPA_Merge (HDPA hdpa1, HDPA hdpa2, DWORD dwFlags,
264 PFNDPACOMPARE pfnCompare, PFNDPAMERGE pfnMerge,
265 LPARAM lParam)
267 INT nCount;
268 LPVOID *pWork1, *pWork2;
269 INT nResult, i;
270 INT nIndex;
272 TRACE("%p %p %08x %p %p %08lx)\n",
273 hdpa1, hdpa2, dwFlags, pfnCompare, pfnMerge, lParam);
275 if (IsBadWritePtr (hdpa1, sizeof(*hdpa1)))
276 return FALSE;
278 if (IsBadWritePtr (hdpa2, sizeof(*hdpa2)))
279 return FALSE;
281 if (IsBadCodePtr ((FARPROC)pfnCompare))
282 return FALSE;
284 if (IsBadCodePtr ((FARPROC)pfnMerge))
285 return FALSE;
287 if (!(dwFlags & DPAM_SORTED)) {
288 TRACE("sorting dpa's!\n");
289 if (hdpa1->nItemCount > 0)
290 DPA_Sort (hdpa1, pfnCompare, lParam);
291 TRACE ("dpa 1 sorted!\n");
292 if (hdpa2->nItemCount > 0)
293 DPA_Sort (hdpa2, pfnCompare, lParam);
294 TRACE ("dpa 2 sorted!\n");
297 if (hdpa2->nItemCount < 1)
298 return TRUE;
300 TRACE("hdpa1->nItemCount=%d hdpa2->nItemCount=%d\n",
301 hdpa1->nItemCount, hdpa2->nItemCount);
304 /* working but untrusted implementation */
306 pWork1 = &(hdpa1->ptrs[hdpa1->nItemCount - 1]);
307 pWork2 = &(hdpa2->ptrs[hdpa2->nItemCount - 1]);
309 nIndex = hdpa1->nItemCount - 1;
310 nCount = hdpa2->nItemCount - 1;
314 if (nIndex < 0) {
315 if ((nCount >= 0) && (dwFlags & DPAM_UNION)) {
316 /* Now insert the remaining new items into DPA 1 */
317 TRACE("%d items to be inserted at start of DPA 1\n",
318 nCount+1);
319 for (i=nCount; i>=0; i--) {
320 PVOID ptr;
322 ptr = (pfnMerge)(DPAMM_INSERT, *pWork2, NULL, lParam);
323 if (!ptr)
324 return FALSE;
325 DPA_InsertPtr (hdpa1, 0, ptr);
326 pWork2--;
329 break;
331 nResult = (pfnCompare)(*pWork1, *pWork2, lParam);
332 TRACE("compare result=%d, dpa1.cnt=%d, dpa2.cnt=%d\n",
333 nResult, nIndex, nCount);
335 if (nResult == 0)
337 PVOID ptr;
339 ptr = (pfnMerge)(DPAMM_MERGE, *pWork1, *pWork2, lParam);
340 if (!ptr)
341 return FALSE;
343 nCount--;
344 pWork2--;
345 *pWork1 = ptr;
346 nIndex--;
347 pWork1--;
349 else if (nResult > 0)
351 /* item in DPA 1 missing from DPA 2 */
352 if (dwFlags & DPAM_INTERSECT)
354 /* Now delete the extra item in DPA1 */
355 PVOID ptr;
357 ptr = DPA_DeletePtr (hdpa1, nIndex);
359 (pfnMerge)(DPAMM_DELETE, ptr, NULL, lParam);
361 nIndex--;
362 pWork1--;
364 else
366 /* new item in DPA 2 */
367 if (dwFlags & DPAM_UNION)
369 /* Now insert the new item in DPA 1 */
370 PVOID ptr;
372 ptr = (pfnMerge)(DPAMM_INSERT, *pWork2, NULL, lParam);
373 if (!ptr)
374 return FALSE;
375 DPA_InsertPtr (hdpa1, nIndex+1, ptr);
377 nCount--;
378 pWork2--;
382 while (nCount >= 0);
384 return TRUE;
388 /**************************************************************************
389 * DPA_Destroy [COMCTL32.329]
391 * Destroys a dynamic pointer array
393 * PARAMS
394 * hdpa [I] handle (pointer) to the pointer array
396 * RETURNS
397 * Success: TRUE
398 * Failure: FALSE
400 BOOL WINAPI DPA_Destroy (const HDPA hdpa)
402 TRACE("(%p)\n", hdpa);
404 if (!hdpa)
405 return FALSE;
407 if (hdpa->ptrs && (!HeapFree (hdpa->hHeap, 0, hdpa->ptrs)))
408 return FALSE;
410 return HeapFree (hdpa->hHeap, 0, hdpa);
414 /**************************************************************************
415 * DPA_Grow [COMCTL32.330]
417 * Sets the growth amount.
419 * PARAMS
420 * hdpa [I] handle (pointer) to the existing (source) pointer array
421 * nGrow [I] number of items by which the array grows when it's too small
423 * RETURNS
424 * Success: TRUE
425 * Failure: FALSE
427 BOOL WINAPI DPA_Grow (HDPA hdpa, INT nGrow)
429 INT items;
430 TRACE("(%p %d)\n", hdpa, nGrow);
432 if (!hdpa)
433 return FALSE;
435 nGrow = max( 8, nGrow );
436 items = nGrow * (((hdpa->nMaxCount - 1) / nGrow) + 1);
437 if (items > hdpa->nMaxCount)
439 void *ptr;
441 if (hdpa->ptrs)
442 ptr = HeapReAlloc( hdpa->hHeap, HEAP_ZERO_MEMORY, hdpa->ptrs, items * sizeof(LPVOID) );
443 else
444 ptr = HeapAlloc( hdpa->hHeap, HEAP_ZERO_MEMORY, items * sizeof(LPVOID) );
445 if (!ptr) return FALSE;
446 hdpa->nMaxCount = items;
447 hdpa->ptrs = ptr;
449 hdpa->nGrow = nGrow;
451 return TRUE;
455 /**************************************************************************
456 * DPA_Clone [COMCTL32.331]
458 * Copies a pointer array to an other one or creates a copy
460 * PARAMS
461 * hdpa [I] handle (pointer) to the existing (source) pointer array
462 * hdpaNew [O] handle (pointer) to the destination pointer array
464 * RETURNS
465 * Success: pointer to the destination pointer array.
466 * Failure: NULL
468 * NOTES
469 * - If the 'hdpaNew' is a NULL-Pointer, a copy of the source pointer
470 * array will be created and it's handle (pointer) is returned.
471 * - If 'hdpa' is a NULL-Pointer, the original implementation crashes,
472 * this implementation just returns NULL.
474 HDPA WINAPI DPA_Clone (const HDPA hdpa, const HDPA hdpaNew)
476 INT nNewItems, nSize;
477 HDPA hdpaTemp;
479 if (!hdpa)
480 return NULL;
482 TRACE("(%p %p)\n", hdpa, hdpaNew);
484 if (!hdpaNew) {
485 /* create a new DPA */
486 hdpaTemp = HeapAlloc (hdpa->hHeap, HEAP_ZERO_MEMORY,
487 sizeof(*hdpaTemp));
488 hdpaTemp->hHeap = hdpa->hHeap;
489 hdpaTemp->nGrow = hdpa->nGrow;
491 else
492 hdpaTemp = hdpaNew;
494 if (hdpaTemp->ptrs) {
495 /* remove old pointer array */
496 HeapFree (hdpaTemp->hHeap, 0, hdpaTemp->ptrs);
497 hdpaTemp->ptrs = NULL;
498 hdpaTemp->nItemCount = 0;
499 hdpaTemp->nMaxCount = 0;
502 /* create a new pointer array */
503 nNewItems = hdpaTemp->nGrow *
504 (((hdpa->nItemCount - 1) / hdpaTemp->nGrow) + 1);
505 nSize = nNewItems * sizeof(LPVOID);
506 hdpaTemp->ptrs = HeapAlloc (hdpaTemp->hHeap, HEAP_ZERO_MEMORY, nSize);
507 hdpaTemp->nMaxCount = nNewItems;
509 /* clone the pointer array */
510 hdpaTemp->nItemCount = hdpa->nItemCount;
511 memmove (hdpaTemp->ptrs, hdpa->ptrs,
512 hdpaTemp->nItemCount * sizeof(LPVOID));
514 return hdpaTemp;
518 /**************************************************************************
519 * DPA_GetPtr [COMCTL32.332]
521 * Retrieves a pointer from a dynamic pointer array
523 * PARAMS
524 * hdpa [I] handle (pointer) to the pointer array
525 * nIndex [I] array index of the desired pointer
527 * RETURNS
528 * Success: pointer
529 * Failure: NULL
531 LPVOID WINAPI DPA_GetPtr (const HDPA hdpa, INT nIndex)
533 TRACE("(%p %d)\n", hdpa, nIndex);
535 if (!hdpa)
536 return NULL;
537 if (!hdpa->ptrs) {
538 WARN("no pointer array.\n");
539 return NULL;
541 if ((nIndex < 0) || (nIndex >= hdpa->nItemCount)) {
542 WARN("not enough pointers in array (%d vs %d).\n",nIndex,hdpa->nItemCount);
543 return NULL;
546 TRACE("-- %p\n", hdpa->ptrs[nIndex]);
548 return hdpa->ptrs[nIndex];
552 /**************************************************************************
553 * DPA_GetPtrIndex [COMCTL32.333]
555 * Retrieves the index of the specified pointer
557 * PARAMS
558 * hdpa [I] handle (pointer) to the pointer array
559 * p [I] pointer
561 * RETURNS
562 * Success: index of the specified pointer
563 * Failure: -1
565 INT WINAPI DPA_GetPtrIndex (HDPA hdpa, LPCVOID p)
567 INT i;
569 if (!hdpa || !hdpa->ptrs)
570 return -1;
572 for (i = 0; i < hdpa->nItemCount; i++) {
573 if (hdpa->ptrs[i] == p)
574 return i;
577 return -1;
581 /**************************************************************************
582 * DPA_InsertPtr [COMCTL32.334]
584 * Inserts a pointer into a dynamic pointer array
586 * PARAMS
587 * hdpa [I] handle (pointer) to the array
588 * i [I] array index
589 * p [I] pointer to insert
591 * RETURNS
592 * Success: index of the inserted pointer
593 * Failure: -1
595 INT WINAPI DPA_InsertPtr (const HDPA hdpa, INT i, LPVOID p)
597 TRACE("(%p %d %p)\n", hdpa, i, p);
599 if (!hdpa || i < 0) return -1;
601 /* append item if index is out of bounds */
602 i = min(hdpa->nItemCount, i);
604 /* create empty spot at the end */
605 if (!DPA_SetPtr(hdpa, hdpa->nItemCount, 0)) return -1;
607 if (i != hdpa->nItemCount - 1)
608 memmove (hdpa->ptrs + i + 1, hdpa->ptrs + i,
609 (hdpa->nItemCount - i - 1) * sizeof(LPVOID));
611 hdpa->ptrs[i] = p;
612 return i;
616 /**************************************************************************
617 * DPA_SetPtr [COMCTL32.335]
619 * Sets a pointer in the pointer array
621 * PARAMS
622 * hdpa [I] handle (pointer) to the pointer array
623 * i [I] index of the pointer that will be set
624 * p [I] pointer to be set
626 * RETURNS
627 * Success: TRUE
628 * Failure: FALSE
630 BOOL WINAPI DPA_SetPtr (const HDPA hdpa, INT i, LPVOID p)
632 LPVOID *lpTemp;
634 TRACE("(%p %d %p)\n", hdpa, i, p);
636 if (!hdpa || i < 0)
637 return FALSE;
639 if (hdpa->nItemCount <= i) {
640 /* within the old array */
641 if (hdpa->nMaxCount <= i) {
642 /* resize the block of memory */
643 INT nNewItems =
644 hdpa->nGrow * ((((i+1) - 1) / hdpa->nGrow) + 1);
645 INT nSize = nNewItems * sizeof(LPVOID);
647 if (hdpa->ptrs)
648 lpTemp = HeapReAlloc (hdpa->hHeap, HEAP_ZERO_MEMORY, hdpa->ptrs, nSize);
649 else
650 lpTemp = HeapAlloc (hdpa->hHeap, HEAP_ZERO_MEMORY, nSize);
652 if (!lpTemp)
653 return FALSE;
655 hdpa->nMaxCount = nNewItems;
656 hdpa->ptrs = lpTemp;
658 hdpa->nItemCount = i+1;
661 /* put the new entry in */
662 hdpa->ptrs[i] = p;
664 return TRUE;
668 /**************************************************************************
669 * DPA_DeletePtr [COMCTL32.336]
671 * Removes a pointer from the pointer array.
673 * PARAMS
674 * hdpa [I] handle (pointer) to the pointer array
675 * i [I] index of the pointer that will be deleted
677 * RETURNS
678 * Success: deleted pointer
679 * Failure: NULL
681 LPVOID WINAPI DPA_DeletePtr (const HDPA hdpa, INT i)
683 LPVOID *lpDest, *lpSrc, lpTemp = NULL;
684 INT nSize;
686 TRACE("(%p %d)\n", hdpa, i);
688 if ((!hdpa) || i < 0 || i >= hdpa->nItemCount)
689 return NULL;
691 lpTemp = hdpa->ptrs[i];
693 /* do we need to move ?*/
694 if (i < hdpa->nItemCount - 1) {
695 lpDest = hdpa->ptrs + i;
696 lpSrc = lpDest + 1;
697 nSize = (hdpa->nItemCount - i - 1) * sizeof(LPVOID);
698 TRACE("-- move dest=%p src=%p size=%x\n",
699 lpDest, lpSrc, nSize);
700 memmove (lpDest, lpSrc, nSize);
703 hdpa->nItemCount --;
705 /* free memory ?*/
706 if ((hdpa->nMaxCount - hdpa->nItemCount) >= hdpa->nGrow) {
707 INT nNewItems = max(hdpa->nGrow * 2, hdpa->nItemCount);
708 nSize = nNewItems * sizeof(LPVOID);
709 lpDest = HeapReAlloc (hdpa->hHeap, HEAP_ZERO_MEMORY,
710 hdpa->ptrs, nSize);
711 if (!lpDest)
712 return NULL;
714 hdpa->nMaxCount = nNewItems;
715 hdpa->ptrs = lpDest;
718 return lpTemp;
722 /**************************************************************************
723 * DPA_DeleteAllPtrs [COMCTL32.337]
725 * Removes all pointers and reinitializes the array.
727 * PARAMS
728 * hdpa [I] handle (pointer) to the pointer array
730 * RETURNS
731 * Success: TRUE
732 * Failure: FALSE
734 BOOL WINAPI DPA_DeleteAllPtrs (const HDPA hdpa)
736 TRACE("(%p)\n", hdpa);
738 if (!hdpa)
739 return FALSE;
741 if (hdpa->ptrs && (!HeapFree (hdpa->hHeap, 0, hdpa->ptrs)))
742 return FALSE;
744 hdpa->nItemCount = 0;
745 hdpa->nMaxCount = hdpa->nGrow * 2;
746 hdpa->ptrs = HeapAlloc (hdpa->hHeap, HEAP_ZERO_MEMORY,
747 hdpa->nMaxCount * sizeof(LPVOID));
749 return TRUE;
753 /**************************************************************************
754 * DPA_QuickSort [Internal]
756 * Ordinary quicksort (used by DPA_Sort).
758 * PARAMS
759 * lpPtrs [I] pointer to the pointer array
760 * l [I] index of the "left border" of the partition
761 * r [I] index of the "right border" of the partition
762 * pfnCompare [I] pointer to the compare function
763 * lParam [I] user defined value (3rd parameter in compare function)
765 * RETURNS
766 * NONE
768 static VOID DPA_QuickSort (LPVOID *lpPtrs, INT l, INT r,
769 PFNDPACOMPARE pfnCompare, LPARAM lParam)
771 INT m;
772 LPVOID t;
774 TRACE("l=%i r=%i\n", l, r);
776 if (l==r) /* one element is always sorted */
777 return;
778 if (r<l) /* oops, got it in the wrong order */
780 DPA_QuickSort(lpPtrs, r, l, pfnCompare, lParam);
781 return;
783 m = (l+r)/2; /* divide by two */
784 DPA_QuickSort(lpPtrs, l, m, pfnCompare, lParam);
785 DPA_QuickSort(lpPtrs, m+1, r, pfnCompare, lParam);
787 /* join the two sides */
788 while( (l<=m) && (m<r) )
790 if(pfnCompare(lpPtrs[l],lpPtrs[m+1],lParam)>0)
792 t = lpPtrs[m+1];
793 memmove(&lpPtrs[l+1],&lpPtrs[l],(m-l+1)*sizeof(lpPtrs[l]));
794 lpPtrs[l] = t;
796 m++;
798 l++;
803 /**************************************************************************
804 * DPA_Sort [COMCTL32.338]
806 * Sorts a pointer array using a user defined compare function
808 * PARAMS
809 * hdpa [I] handle (pointer) to the pointer array
810 * pfnCompare [I] pointer to the compare function
811 * lParam [I] user defined value (3rd parameter of compare function)
813 * RETURNS
814 * Success: TRUE
815 * Failure: FALSE
817 BOOL WINAPI DPA_Sort (const HDPA hdpa, PFNDPACOMPARE pfnCompare, LPARAM lParam)
819 if (!hdpa || !pfnCompare)
820 return FALSE;
822 TRACE("(%p %p 0x%lx)\n", hdpa, pfnCompare, lParam);
824 if ((hdpa->nItemCount > 1) && (hdpa->ptrs))
825 DPA_QuickSort (hdpa->ptrs, 0, hdpa->nItemCount - 1,
826 pfnCompare, lParam);
828 return TRUE;
832 /**************************************************************************
833 * DPA_Search [COMCTL32.339]
835 * Searches a pointer array for a specified pointer
837 * PARAMS
838 * hdpa [I] handle (pointer) to the pointer array
839 * pFind [I] pointer to search for
840 * nStart [I] start index
841 * pfnCompare [I] pointer to the compare function
842 * lParam [I] user defined value (3rd parameter of compare function)
843 * uOptions [I] search options
845 * RETURNS
846 * Success: index of the pointer in the array.
847 * Failure: -1
849 INT WINAPI DPA_Search (const HDPA hdpa, LPVOID pFind, INT nStart,
850 PFNDPACOMPARE pfnCompare, LPARAM lParam, UINT uOptions)
852 if (!hdpa || !pfnCompare || !pFind)
853 return -1;
855 TRACE("(%p %p %d %p 0x%08lx 0x%08x)\n",
856 hdpa, pFind, nStart, pfnCompare, lParam, uOptions);
858 if (uOptions & DPAS_SORTED) {
859 /* array is sorted --> use binary search */
860 INT l, r, x, n;
861 LPVOID *lpPtr;
863 /* for binary search ignore start index */
864 l = 0;
865 r = hdpa->nItemCount - 1;
866 lpPtr = hdpa->ptrs;
867 while (r >= l) {
868 x = (l + r) / 2;
869 n = (pfnCompare)(pFind, lpPtr[x], lParam);
870 if (n == 0)
871 return x;
872 else if (n < 0)
873 r = x - 1;
874 else /* (n > 0) */
875 l = x + 1;
877 if (uOptions & (DPAS_INSERTBEFORE|DPAS_INSERTAFTER)) return l;
879 else {
880 /* array is not sorted --> use linear search */
881 LPVOID *lpPtr;
882 INT nIndex;
884 nIndex = (nStart == -1)? 0 : nStart;
885 lpPtr = hdpa->ptrs;
886 for (; nIndex < hdpa->nItemCount; nIndex++) {
887 if ((pfnCompare)(pFind, lpPtr[nIndex], lParam) == 0)
888 return nIndex;
892 return -1;
896 /**************************************************************************
897 * DPA_CreateEx [COMCTL32.340]
899 * Creates a dynamic pointer array using the specified size and heap.
901 * PARAMS
902 * nGrow [I] number of items by which the array grows when it is filled
903 * hHeap [I] handle to the heap where the array is stored
905 * RETURNS
906 * Success: handle (pointer) to the pointer array.
907 * Failure: NULL
909 * NOTES
910 * The DPA_ functions can be used to create and manipulate arrays of
911 * pointers.
913 HDPA WINAPI DPA_CreateEx (INT nGrow, HANDLE hHeap)
915 HDPA hdpa;
917 TRACE("(%d %p)\n", nGrow, hHeap);
919 if (hHeap)
920 hdpa = HeapAlloc (hHeap, HEAP_ZERO_MEMORY, sizeof(*hdpa));
921 else
922 hdpa = Alloc (sizeof(*hdpa));
924 if (hdpa) {
925 hdpa->nGrow = max(8, nGrow);
926 hdpa->hHeap = hHeap ? hHeap : GetProcessHeap();
927 hdpa->nMaxCount = hdpa->nGrow * 2;
928 hdpa->ptrs = HeapAlloc (hdpa->hHeap, HEAP_ZERO_MEMORY,
929 hdpa->nMaxCount * sizeof(LPVOID));
932 TRACE("-- %p\n", hdpa);
934 return hdpa;
938 /**************************************************************************
939 * DPA_Create [COMCTL32.328]
941 * Creates a dynamic pointer array.
943 * PARAMS
944 * nGrow [I] number of items by which the array grows when it is filled
946 * RETURNS
947 * Success: handle (pointer) to the pointer array.
948 * Failure: NULL
950 * NOTES
951 * The DPA_ functions can be used to create and manipulate arrays of
952 * pointers.
954 HDPA WINAPI DPA_Create (INT nGrow)
956 return DPA_CreateEx( nGrow, 0 );
960 /**************************************************************************
961 * DPA_EnumCallback [COMCTL32.385]
963 * Enumerates all items in a dynamic pointer array.
965 * PARAMS
966 * hdpa [I] handle to the dynamic pointer array
967 * enumProc [I]
968 * lParam [I]
970 * RETURNS
971 * none
973 VOID WINAPI DPA_EnumCallback (HDPA hdpa, PFNDPAENUMCALLBACK enumProc,
974 LPVOID lParam)
976 INT i;
978 TRACE("(%p %p %p)\n", hdpa, enumProc, lParam);
980 if (!hdpa)
981 return;
982 if (hdpa->nItemCount <= 0)
983 return;
985 for (i = 0; i < hdpa->nItemCount; i++) {
986 if ((enumProc)(hdpa->ptrs[i], lParam) == 0)
987 return;
990 return;
994 /**************************************************************************
995 * DPA_DestroyCallback [COMCTL32.386]
997 * Enumerates all items in a dynamic pointer array and destroys it.
999 * PARAMS
1000 * hdpa [I] handle to the dynamic pointer array
1001 * enumProc [I]
1002 * lParam [I]
1004 * RETURNS
1005 * none
1007 void WINAPI DPA_DestroyCallback (HDPA hdpa, PFNDPAENUMCALLBACK enumProc,
1008 LPVOID lParam)
1010 TRACE("(%p %p %p)\n", hdpa, enumProc, lParam);
1012 DPA_EnumCallback (hdpa, enumProc, lParam);
1013 DPA_Destroy (hdpa);
1016 /**************************************************************************
1017 * DPA_GetSize [COMCTL32.@]
1019 * Returns all array allocated memory size
1021 * PARAMS
1022 * hdpa [I] handle to the dynamic pointer array
1024 * RETURNS
1025 * Size in bytes
1027 ULONGLONG WINAPI DPA_GetSize(HDPA hdpa)
1029 TRACE("(%p)\n", hdpa);
1031 if (!hdpa) return 0;
1033 return sizeof(DPA) + hdpa->nMaxCount*sizeof(PVOID);