dpa.c 26.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Dynamic pointer array (DPA) implementation
 *
 * Copyright 1998 Eric Kohl
 *           1998 Juergen Schmied <j.schmied@metronet.de>
 *           2000 Eric Kohl for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 *
 * NOTES
 *     These functions were involuntarily documented by Microsoft in 2002 as
 *     the outcome of an anti-trust suit brought by various U.S. governments.
 *     As a result the specifications on MSDN are inaccurate, incomplete 
 *     and misleading. A much more complete (unofficial) documentation is
 *     available at:
 *
 *     http://members.ozemail.com.au/~geoffch/samples/win32/shell/comctl32  
 */

#define COBJMACROS

#include <stdarg.h>
#include <limits.h>

#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "commctrl.h"
#include "objbase.h"

#include "comctl32.h"
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(dpa);

48
typedef struct _DPA
49 50 51 52 53 54
{
    INT    nItemCount;
    LPVOID   *ptrs;
    HANDLE hHeap;
    INT    nGrow;
    INT    nMaxCount;
55
} DPA;
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

typedef struct _STREAMDATA
{
    DWORD dwSize;
    DWORD dwData2;
    DWORD dwItems;
} STREAMDATA, *PSTREAMDATA;

/**************************************************************************
 * DPA_LoadStream [COMCTL32.9]
 *
 * Loads a dynamic pointer array from a stream
 *
 * PARAMS
 *     phDpa    [O] pointer to a handle to a dynamic pointer array
 *     loadProc [I] pointer to a callback function
 *     pStream  [I] pointer to a stream
73
 *     pData    [I] pointer to callback data
74 75
 *
 * RETURNS
76 77
 *     Success: S_OK, S_FALSE - partial success
 *     Failure: HRESULT error code
78 79 80 81
 *
 * NOTES
 *     No more information available yet!
 */
82 83
HRESULT WINAPI DPA_LoadStream (HDPA *phDpa, PFNDPASTREAM loadProc,
                               IStream *pStream, LPVOID pData)
84 85 86
{
    HRESULT errCode;
    LARGE_INTEGER position;
87
    ULARGE_INTEGER initial_pos;
88
    STREAMDATA  streamData;
89
    DPASTREAMINFO streamInfo;
90 91 92 93
    ULONG ulRead;
    HDPA hDpa;
    PVOID *ptr;

94
    TRACE ("phDpa=%p loadProc=%p pStream=%p pData=%p\n",
95
           phDpa, loadProc, pStream, pData);
96 97 98 99

    if (!phDpa || !loadProc || !pStream)
        return E_INVALIDARG;

100
    *phDpa = NULL;
101 102 103

    position.QuadPart = 0;

104
    errCode = IStream_Seek (pStream, position, STREAM_SEEK_CUR, &initial_pos);
105 106 107
    if (errCode != S_OK)
        return errCode;

108
    memset(&streamData, 0, sizeof(STREAMDATA));
109 110 111 112
    errCode = IStream_Read (pStream, &streamData, sizeof(STREAMDATA), &ulRead);
    if (errCode != S_OK)
        return errCode;

113
    TRACE ("dwSize=%lu dwData2=%lu dwItems=%lu\n",
114 115
           streamData.dwSize, streamData.dwData2, streamData.dwItems);

116 117 118 119 120 121
    if (ulRead < sizeof(STREAMDATA) ||
        streamData.dwSize < sizeof(STREAMDATA) || streamData.dwData2 != 1) {
        /* back to initial position */
        position.QuadPart = initial_pos.QuadPart;
        IStream_Seek (pStream, position, STREAM_SEEK_SET, NULL);
        return E_FAIL;
122 123 124 125 126 127 128 129 130 131
    }

    if (streamData.dwItems > (UINT_MAX / 2 / sizeof(VOID*))) /* 536870911 */
        return E_OUTOFMEMORY;

    /* create the dpa */
    hDpa = DPA_Create (streamData.dwItems);
    if (!hDpa)
        return E_OUTOFMEMORY;

132 133
    if (!DPA_Grow (hDpa, streamData.dwItems)) {
        DPA_Destroy (hDpa);
134
        return E_OUTOFMEMORY;
135
    }
136 137 138

    /* load data from the stream into the dpa */
    ptr = hDpa->ptrs;
139 140
    for (streamInfo.iPos = 0; streamInfo.iPos < streamData.dwItems; streamInfo.iPos++) {
        errCode = (loadProc)(&streamInfo, pStream, pData);
141 142 143 144 145
        if (errCode != S_OK) {
            errCode = S_FALSE;
            break;
        }

146
        *ptr = streamInfo.pvItem;
147 148 149 150
        ptr++;
    }

    /* set the number of items */
151
    hDpa->nItemCount = streamInfo.iPos;
152 153 154

    /* store the handle to the dpa */
    *phDpa = hDpa;
155
    TRACE ("new hDpa=%p, errorcode %lx\n", hDpa, errCode);
156 157 158 159 160 161 162 163 164 165 166 167

    return errCode;
}


/**************************************************************************
 * DPA_SaveStream [COMCTL32.10]
 *
 * Saves a dynamic pointer array to a stream
 *
 * PARAMS
 *     hDpa     [I] handle to a dynamic pointer array
168
 *     saveProc [I] pointer to a callback function
169
 *     pStream  [I] pointer to a stream
170
 *     pData    [I] pointer to callback data
171 172
 *
 * RETURNS
173 174
 *     Success: S_OK, S_FALSE - partial success
 *     Failure: HRESULT error code
175 176 177 178
 *
 * NOTES
 *     No more information available yet!
 */
179
HRESULT WINAPI DPA_SaveStream (HDPA hDpa, PFNDPASTREAM saveProc,
180
                               IStream *pStream, LPVOID pData)
181
{
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
    LARGE_INTEGER position;
    ULARGE_INTEGER initial_pos, curr_pos;
    STREAMDATA  streamData;
    DPASTREAMINFO streamInfo;
    HRESULT hr;
    PVOID *ptr;

    TRACE ("hDpa=%p saveProc=%p pStream=%p pData=%p\n",
            hDpa, saveProc, pStream, pData);

    if (!hDpa || !saveProc || !pStream) return E_INVALIDARG;

    /* save initial position to write header after completion */
    position.QuadPart = 0;
    hr = IStream_Seek (pStream, position, STREAM_SEEK_CUR, &initial_pos);
    if (hr != S_OK)
        return hr;

    /* write empty header */
    streamData.dwSize  = sizeof(streamData);
    streamData.dwData2 = 1;
    streamData.dwItems = 0;

    hr = IStream_Write (pStream, &streamData, sizeof(streamData), NULL);
    if (hr != S_OK) {
        position.QuadPart = initial_pos.QuadPart;
        IStream_Seek (pStream, position, STREAM_SEEK_SET, NULL);
        return hr;
    }

    /* no items - we're done */
    if (hDpa->nItemCount == 0) return S_OK;

    ptr = hDpa->ptrs;
    for (streamInfo.iPos = 0; streamInfo.iPos < hDpa->nItemCount; streamInfo.iPos++) {
        streamInfo.pvItem = *ptr;
        hr = (saveProc)(&streamInfo, pStream, pData);
        if (hr != S_OK) {
            hr = S_FALSE;
            break;
        }
        ptr++;
    }

    /* write updated header */
    position.QuadPart = 0;
    IStream_Seek (pStream, position, STREAM_SEEK_CUR, &curr_pos);

    streamData.dwSize  = curr_pos.QuadPart - initial_pos.QuadPart;
    streamData.dwData2 = 1;
    streamData.dwItems = streamInfo.iPos;

    position.QuadPart = initial_pos.QuadPart;
    IStream_Seek (pStream, position, STREAM_SEEK_SET, NULL);
    IStream_Write (pStream, &streamData, sizeof(streamData), NULL);
237

238 239
    position.QuadPart = curr_pos.QuadPart;
    IStream_Seek (pStream, position, STREAM_SEEK_SET, NULL);
240

241
    return hr;
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
}


/**************************************************************************
 * DPA_Merge [COMCTL32.11]
 *
 * Merge two dynamic pointers arrays.
 *
 * PARAMS
 *     hdpa1       [I] handle to a dynamic pointer array
 *     hdpa2       [I] handle to a dynamic pointer array
 *     dwFlags     [I] flags
 *     pfnCompare  [I] pointer to sort function
 *     pfnMerge    [I] pointer to merge function
 *     lParam      [I] application specific value
 *
 * RETURNS
 *     Success: TRUE
 *     Failure: FALSE 
 *
 * NOTES
 *     No more information available yet!
 */
265
BOOL WINAPI DPA_Merge (HDPA hdpa1, HDPA hdpa2, DWORD dwFlags,
266 267 268 269 270 271 272 273
                       PFNDPACOMPARE pfnCompare, PFNDPAMERGE pfnMerge,
                       LPARAM lParam)
{
    INT nCount;
    LPVOID *pWork1, *pWork2;
    INT nResult, i;
    INT nIndex;

274
    TRACE("%p, %p, %#lx, %p, %p, %#Ix\n", hdpa1, hdpa2, dwFlags, pfnCompare, pfnMerge, lParam);
275 276 277 278 279 280 281 282 283 284 285 286 287

    if (IsBadWritePtr (hdpa1, sizeof(*hdpa1)))
        return FALSE;

    if (IsBadWritePtr (hdpa2, sizeof(*hdpa2)))
        return FALSE;

    if (IsBadCodePtr ((FARPROC)pfnCompare))
        return FALSE;

    if (IsBadCodePtr ((FARPROC)pfnMerge))
        return FALSE;

288
    if (!(dwFlags & DPAM_SORTED)) {
289
        TRACE("sorting dpa's.\n");
290 291
        if (hdpa1->nItemCount > 0)
        DPA_Sort (hdpa1, pfnCompare, lParam);
292
        TRACE ("dpa 1 sorted.\n");
293 294
        if (hdpa2->nItemCount > 0)
        DPA_Sort (hdpa2, pfnCompare, lParam);
295
        TRACE ("dpa 2 sorted.\n");
296 297 298 299 300 301 302 303 304 305 306 307 308 309
    }

    if (hdpa2->nItemCount < 1)
        return TRUE;

    TRACE("hdpa1->nItemCount=%d hdpa2->nItemCount=%d\n",
           hdpa1->nItemCount, hdpa2->nItemCount);


    nIndex = hdpa1->nItemCount - 1;
    nCount = hdpa2->nItemCount - 1;

    do
    {
310 311 312
        pWork1 = &hdpa1->ptrs[nIndex];
        pWork2 = &hdpa2->ptrs[nCount];

313
        if (nIndex < 0) {
314
            if ((nCount >= 0) && (dwFlags & DPAM_UNION)) {
315 316 317 318 319 320
                /* Now insert the remaining new items into DPA 1 */
                TRACE("%d items to be inserted at start of DPA 1\n",
                      nCount+1);
                for (i=nCount; i>=0; i--) {
                    PVOID ptr;

321
                    ptr = (pfnMerge)(DPAMM_INSERT, *pWork2, NULL, lParam);
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
                    if (!ptr)
                        return FALSE;
                    DPA_InsertPtr (hdpa1, 0, ptr);
                    pWork2--;
                }
            }
            break;
        }
        nResult = (pfnCompare)(*pWork1, *pWork2, lParam);
        TRACE("compare result=%d, dpa1.cnt=%d, dpa2.cnt=%d\n",
              nResult, nIndex, nCount);

        if (nResult == 0)
        {
            PVOID ptr;

338
            ptr = (pfnMerge)(DPAMM_MERGE, *pWork1, *pWork2, lParam);
339 340 341 342 343 344 345 346 347 348
            if (!ptr)
                return FALSE;

            nCount--;
            *pWork1 = ptr;
            nIndex--;
        }
        else if (nResult > 0)
        {
            /* item in DPA 1 missing from DPA 2 */
349
            if (dwFlags & DPAM_INTERSECT)
350 351 352 353
            {
                /* Now delete the extra item in DPA1 */
                PVOID ptr;

354
                ptr = DPA_DeletePtr (hdpa1, nIndex);
355

356
                (pfnMerge)(DPAMM_DELETE, ptr, NULL, lParam);
357 358 359 360 361 362
            }
            nIndex--;
        }
        else
        {
            /* new item in DPA 2 */
363
            if (dwFlags & DPAM_UNION)
364 365 366 367
            {
                /* Now insert the new item in DPA 1 */
                PVOID ptr;

368
                ptr = (pfnMerge)(DPAMM_INSERT, *pWork2, NULL, lParam);
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
                if (!ptr)
                    return FALSE;
                DPA_InsertPtr (hdpa1, nIndex+1, ptr);
            }
            nCount--;
        }

    }
    while (nCount >= 0);

    return TRUE;
}


/**************************************************************************
 * DPA_Destroy [COMCTL32.329]
 *
 * Destroys a dynamic pointer array
 *
 * PARAMS
 *     hdpa [I] handle (pointer) to the pointer array
 *
 * RETURNS
 *     Success: TRUE
 *     Failure: FALSE
 */
395
BOOL WINAPI DPA_Destroy (HDPA hdpa)
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
{
    TRACE("(%p)\n", hdpa);

    if (!hdpa)
        return FALSE;

    if (hdpa->ptrs && (!HeapFree (hdpa->hHeap, 0, hdpa->ptrs)))
        return FALSE;

    return HeapFree (hdpa->hHeap, 0, hdpa);
}


/**************************************************************************
 * DPA_Grow [COMCTL32.330]
 *
 * Sets the growth amount.
 *
 * PARAMS
 *     hdpa  [I] handle (pointer) to the existing (source) pointer array
 *     nGrow [I] number of items by which the array grows when it's too small
 *
 * RETURNS
 *     Success: TRUE
 *     Failure: FALSE
 */
422
BOOL WINAPI DPA_Grow (HDPA hdpa, INT nGrow)
423
{
424
    INT items;
425 426 427 428 429
    TRACE("(%p %d)\n", hdpa, nGrow);

    if (!hdpa)
        return FALSE;

430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
    nGrow = max( 8, nGrow );
    items = nGrow * (((hdpa->nMaxCount - 1) / nGrow) + 1);
    if (items > hdpa->nMaxCount)
    {
        void *ptr;

        if (hdpa->ptrs)
            ptr = HeapReAlloc( hdpa->hHeap, HEAP_ZERO_MEMORY, hdpa->ptrs, items * sizeof(LPVOID) );
        else
            ptr = HeapAlloc( hdpa->hHeap, HEAP_ZERO_MEMORY, items * sizeof(LPVOID) );
        if (!ptr) return FALSE;
        hdpa->nMaxCount = items;
        hdpa->ptrs = ptr;
    }
    hdpa->nGrow = nGrow;
445 446 447 448 449 450 451 452

    return TRUE;
}


/**************************************************************************
 * DPA_Clone [COMCTL32.331]
 *
453
 * Copies a pointer array to another one or creates a copy
454 455 456 457 458 459 460 461 462 463 464
 *
 * PARAMS
 *     hdpa    [I] handle (pointer) to the existing (source) pointer array
 *     hdpaNew [O] handle (pointer) to the destination pointer array
 *
 * RETURNS
 *     Success: pointer to the destination pointer array.
 *     Failure: NULL
 *
 * NOTES
 *     - If the 'hdpaNew' is a NULL-Pointer, a copy of the source pointer
465
 *       array will be created and its handle (pointer) is returned.
466 467 468
 *     - If 'hdpa' is a NULL-Pointer, the original implementation crashes,
 *       this implementation just returns NULL.
 */
469
HDPA WINAPI DPA_Clone (const HDPA hdpa, HDPA hdpaNew)
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
{
    INT nNewItems, nSize;
    HDPA hdpaTemp;

    if (!hdpa)
        return NULL;

    TRACE("(%p %p)\n", hdpa, hdpaNew);

    if (!hdpaNew) {
        /* create a new DPA */
        hdpaTemp = HeapAlloc (hdpa->hHeap, HEAP_ZERO_MEMORY,
                                    sizeof(*hdpaTemp));
        hdpaTemp->hHeap = hdpa->hHeap;
        hdpaTemp->nGrow = hdpa->nGrow;
    }
    else
        hdpaTemp = hdpaNew;

    if (hdpaTemp->ptrs) {
        /* remove old pointer array */
        HeapFree (hdpaTemp->hHeap, 0, hdpaTemp->ptrs);
        hdpaTemp->ptrs = NULL;
        hdpaTemp->nItemCount = 0;
        hdpaTemp->nMaxCount = 0;
    }

    /* create a new pointer array */
    nNewItems = hdpaTemp->nGrow *
499
                (((hdpa->nItemCount - 1) / hdpaTemp->nGrow) + 1);
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
    nSize = nNewItems * sizeof(LPVOID);
    hdpaTemp->ptrs = HeapAlloc (hdpaTemp->hHeap, HEAP_ZERO_MEMORY, nSize);
    hdpaTemp->nMaxCount = nNewItems;

    /* clone the pointer array */
    hdpaTemp->nItemCount = hdpa->nItemCount;
    memmove (hdpaTemp->ptrs, hdpa->ptrs,
             hdpaTemp->nItemCount * sizeof(LPVOID));

    return hdpaTemp;
}


/**************************************************************************
 * DPA_GetPtr [COMCTL32.332]
 *
 * Retrieves a pointer from a dynamic pointer array
 *
 * PARAMS
 *     hdpa   [I] handle (pointer) to the pointer array
 *     nIndex [I] array index of the desired pointer
 *
 * RETURNS
 *     Success: pointer
 *     Failure: NULL
 */
526
LPVOID WINAPI DPA_GetPtr (HDPA hdpa, INT_PTR nIndex)
527
{
528
    TRACE("%p, %Id\n", hdpa, nIndex);
529 530 531 532 533 534 535 536

    if (!hdpa)
        return NULL;
    if (!hdpa->ptrs) {
        WARN("no pointer array.\n");
        return NULL;
    }
    if ((nIndex < 0) || (nIndex >= hdpa->nItemCount)) {
537
        WARN("not enough pointers in array (%Id vs %d).\n",nIndex,hdpa->nItemCount);
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
        return NULL;
    }

    TRACE("-- %p\n", hdpa->ptrs[nIndex]);

    return hdpa->ptrs[nIndex];
}


/**************************************************************************
 * DPA_GetPtrIndex [COMCTL32.333]
 *
 * Retrieves the index of the specified pointer
 *
 * PARAMS
 *     hdpa   [I] handle (pointer) to the pointer array
 *     p      [I] pointer
 *
 * RETURNS
 *     Success: index of the specified pointer
 *     Failure: -1
 */
560
INT WINAPI DPA_GetPtrIndex (HDPA hdpa, LPCVOID p)
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
{
    INT i;

    if (!hdpa || !hdpa->ptrs)
        return -1;

    for (i = 0; i < hdpa->nItemCount; i++) {
        if (hdpa->ptrs[i] == p)
            return i;
    }

    return -1;
}


/**************************************************************************
 * DPA_InsertPtr [COMCTL32.334]
 *
 * Inserts a pointer into a dynamic pointer array
 *
 * PARAMS
 *     hdpa [I] handle (pointer) to the array
 *     i    [I] array index
 *     p    [I] pointer to insert
 *
 * RETURNS
 *     Success: index of the inserted pointer
 *     Failure: -1
 */
590
INT WINAPI DPA_InsertPtr (HDPA hdpa, INT i, LPVOID p)
591 592 593 594 595
{
    TRACE("(%p %d %p)\n", hdpa, i, p);

    if (!hdpa || i < 0) return -1;

596 597
    /* append item if index is out of bounds */
    i = min(hdpa->nItemCount, i);
598 599 600

    /* create empty spot at the end */
    if (!DPA_SetPtr(hdpa, hdpa->nItemCount, 0)) return -1;
601 602 603 604 605
    
    if (i != hdpa->nItemCount - 1)
        memmove (hdpa->ptrs + i + 1, hdpa->ptrs + i, 
                 (hdpa->nItemCount - i - 1) * sizeof(LPVOID));
    
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
    hdpa->ptrs[i] = p;
    return i;
}


/**************************************************************************
 * DPA_SetPtr [COMCTL32.335]
 *
 * Sets a pointer in the pointer array
 *
 * PARAMS
 *     hdpa [I] handle (pointer) to the pointer array
 *     i    [I] index of the pointer that will be set
 *     p    [I] pointer to be set
 *
 * RETURNS
 *     Success: TRUE
 *     Failure: FALSE
 */
625
BOOL WINAPI DPA_SetPtr (HDPA hdpa, INT i, LPVOID p)
626 627 628 629 630
{
    LPVOID *lpTemp;

    TRACE("(%p %d %p)\n", hdpa, i, p);

631
    if (!hdpa || i < 0)
632 633 634 635 636 637 638
        return FALSE;

    if (hdpa->nItemCount <= i) {
        /* within the old array */
        if (hdpa->nMaxCount <= i) {
            /* resize the block of memory */
            INT nNewItems =
639
                hdpa->nGrow * ((((i+1) - 1) / hdpa->nGrow) + 1);
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
            INT nSize = nNewItems * sizeof(LPVOID);

            if (hdpa->ptrs)
                lpTemp = HeapReAlloc (hdpa->hHeap, HEAP_ZERO_MEMORY, hdpa->ptrs, nSize);
            else
                lpTemp = HeapAlloc (hdpa->hHeap, HEAP_ZERO_MEMORY, nSize);
            
            if (!lpTemp)
                return FALSE;

            hdpa->nMaxCount = nNewItems;
            hdpa->ptrs = lpTemp;
        }
        hdpa->nItemCount = i+1;
    }

    /* put the new entry in */
    hdpa->ptrs[i] = p;

    return TRUE;
}


/**************************************************************************
 * DPA_DeletePtr [COMCTL32.336]
 *
 * Removes a pointer from the pointer array.
 *
 * PARAMS
 *     hdpa [I] handle (pointer) to the pointer array
 *     i    [I] index of the pointer that will be deleted
 *
 * RETURNS
 *     Success: deleted pointer
 *     Failure: NULL
 */
676
LPVOID WINAPI DPA_DeletePtr (HDPA hdpa, INT i)
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
{
    LPVOID *lpDest, *lpSrc, lpTemp = NULL;
    INT  nSize;

    TRACE("(%p %d)\n", hdpa, i);

    if ((!hdpa) || i < 0 || i >= hdpa->nItemCount)
        return NULL;

    lpTemp = hdpa->ptrs[i];

    /* do we need to move ?*/
    if (i < hdpa->nItemCount - 1) {
        lpDest = hdpa->ptrs + i;
        lpSrc = lpDest + 1;
        nSize = (hdpa->nItemCount - i - 1) * sizeof(LPVOID);
        TRACE("-- move dest=%p src=%p size=%x\n",
               lpDest, lpSrc, nSize);
        memmove (lpDest, lpSrc, nSize);
    }

    hdpa->nItemCount --;

    /* free memory ?*/
    if ((hdpa->nMaxCount - hdpa->nItemCount) >= hdpa->nGrow) {
        INT nNewItems = max(hdpa->nGrow * 2, hdpa->nItemCount);
        nSize = nNewItems * sizeof(LPVOID);
        lpDest = HeapReAlloc (hdpa->hHeap, HEAP_ZERO_MEMORY,
                                      hdpa->ptrs, nSize);
        if (!lpDest)
            return NULL;

        hdpa->nMaxCount = nNewItems;
710
        hdpa->ptrs = lpDest;
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
    }

    return lpTemp;
}


/**************************************************************************
 * DPA_DeleteAllPtrs [COMCTL32.337]
 *
 * Removes all pointers and reinitializes the array.
 *
 * PARAMS
 *     hdpa [I] handle (pointer) to the pointer array
 *
 * RETURNS
 *     Success: TRUE
 *     Failure: FALSE
 */
729
BOOL WINAPI DPA_DeleteAllPtrs (HDPA hdpa)
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
{
    TRACE("(%p)\n", hdpa);

    if (!hdpa)
        return FALSE;

    if (hdpa->ptrs && (!HeapFree (hdpa->hHeap, 0, hdpa->ptrs)))
        return FALSE;

    hdpa->nItemCount = 0;
    hdpa->nMaxCount = hdpa->nGrow * 2;
    hdpa->ptrs = HeapAlloc (hdpa->hHeap, HEAP_ZERO_MEMORY,
                                     hdpa->nMaxCount * sizeof(LPVOID));

    return TRUE;
}


/**************************************************************************
 * DPA_QuickSort [Internal]
 *
 * Ordinary quicksort (used by DPA_Sort).
 *
 * PARAMS
 *     lpPtrs     [I] pointer to the pointer array
 *     l          [I] index of the "left border" of the partition
 *     r          [I] index of the "right border" of the partition
 *     pfnCompare [I] pointer to the compare function
 *     lParam     [I] user defined value (3rd parameter in compare function)
 *
 * RETURNS
 *     NONE
 */
static VOID DPA_QuickSort (LPVOID *lpPtrs, INT l, INT r,
                           PFNDPACOMPARE pfnCompare, LPARAM lParam)
{
    INT m;
    LPVOID t;

    TRACE("l=%i r=%i\n", l, r);

    if (l==r)    /* one element is always sorted */
        return;
    if (r<l)     /* oops, got it in the wrong order */
        {
        DPA_QuickSort(lpPtrs, r, l, pfnCompare, lParam);
        return;
        }
    m = (l+r)/2; /* divide by two */
    DPA_QuickSort(lpPtrs, l, m, pfnCompare, lParam);
    DPA_QuickSort(lpPtrs, m+1, r, pfnCompare, lParam);

    /* join the two sides */
    while( (l<=m) && (m<r) )
    {
        if(pfnCompare(lpPtrs[l],lpPtrs[m+1],lParam)>0)
        {
            t = lpPtrs[m+1];
            memmove(&lpPtrs[l+1],&lpPtrs[l],(m-l+1)*sizeof(lpPtrs[l]));
            lpPtrs[l] = t;

            m++;
        }
        l++;
    }
}


/**************************************************************************
 * DPA_Sort [COMCTL32.338]
 *
 * Sorts a pointer array using a user defined compare function
 *
 * PARAMS
 *     hdpa       [I] handle (pointer) to the pointer array
 *     pfnCompare [I] pointer to the compare function
 *     lParam     [I] user defined value (3rd parameter of compare function)
 *
 * RETURNS
 *     Success: TRUE
 *     Failure: FALSE
 */
812
BOOL WINAPI DPA_Sort (HDPA hdpa, PFNDPACOMPARE pfnCompare, LPARAM lParam)
813 814 815 816
{
    if (!hdpa || !pfnCompare)
        return FALSE;

817
    TRACE("%p, %p, %#Ix\n", hdpa, pfnCompare, lParam);
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843

    if ((hdpa->nItemCount > 1) && (hdpa->ptrs))
        DPA_QuickSort (hdpa->ptrs, 0, hdpa->nItemCount - 1,
                       pfnCompare, lParam);

    return TRUE;
}


/**************************************************************************
 * DPA_Search [COMCTL32.339]
 *
 * Searches a pointer array for a specified pointer
 *
 * PARAMS
 *     hdpa       [I] handle (pointer) to the pointer array
 *     pFind      [I] pointer to search for
 *     nStart     [I] start index
 *     pfnCompare [I] pointer to the compare function
 *     lParam     [I] user defined value (3rd parameter of compare function)
 *     uOptions   [I] search options
 *
 * RETURNS
 *     Success: index of the pointer in the array.
 *     Failure: -1
 */
844
INT WINAPI DPA_Search (HDPA hdpa, LPVOID pFind, INT nStart,
845 846 847 848 849
                       PFNDPACOMPARE pfnCompare, LPARAM lParam, UINT uOptions)
{
    if (!hdpa || !pfnCompare || !pFind)
        return -1;

850
    TRACE("%p, %p, %d, %p, %#Ix, %#x\n", hdpa, pFind, nStart, pfnCompare, lParam, uOptions);
851 852 853 854 855 856

    if (uOptions & DPAS_SORTED) {
        /* array is sorted --> use binary search */
        INT l, r, x, n;
        LPVOID *lpPtr;

857 858
        /* for binary search ignore start index */
        l = 0;
859 860 861 862 863
        r = hdpa->nItemCount - 1;
        lpPtr = hdpa->ptrs;
        while (r >= l) {
            x = (l + r) / 2;
            n = (pfnCompare)(pFind, lpPtr[x], lParam);
864 865 866
            if (n == 0)
                return x;
            else if (n < 0)
867
                r = x - 1;
868
            else /* (n > 0) */
869 870
                l = x + 1;
        }
871
        if (uOptions & (DPAS_INSERTBEFORE|DPAS_INSERTAFTER)) return l;
872 873 874 875 876 877 878 879 880
    }
    else {
        /* array is not sorted --> use linear search */
        LPVOID *lpPtr;
        INT  nIndex;

        nIndex = (nStart == -1)? 0 : nStart;
        lpPtr = hdpa->ptrs;
        for (; nIndex < hdpa->nItemCount; nIndex++) {
881
            if ((pfnCompare)(pFind, lpPtr[nIndex], lParam) == 0)
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
                return nIndex;
        }
    }

    return -1;
}


/**************************************************************************
 * DPA_CreateEx [COMCTL32.340]
 *
 * Creates a dynamic pointer array using the specified size and heap.
 *
 * PARAMS
 *     nGrow [I] number of items by which the array grows when it is filled
 *     hHeap [I] handle to the heap where the array is stored
 *
 * RETURNS
 *     Success: handle (pointer) to the pointer array.
 *     Failure: NULL
 *
 * NOTES
 *     The DPA_ functions can be used to create and manipulate arrays of
 *     pointers.
 */
HDPA WINAPI DPA_CreateEx (INT nGrow, HANDLE hHeap)
{
    HDPA hdpa;

    TRACE("(%d %p)\n", nGrow, hHeap);

    if (hHeap)
        hdpa = HeapAlloc (hHeap, HEAP_ZERO_MEMORY, sizeof(*hdpa));
    else
        hdpa = Alloc (sizeof(*hdpa));

    if (hdpa) {
        hdpa->nGrow = max(8, nGrow);
        hdpa->hHeap = hHeap ? hHeap : GetProcessHeap();
        hdpa->nMaxCount = hdpa->nGrow * 2;
        hdpa->ptrs = HeapAlloc (hdpa->hHeap, HEAP_ZERO_MEMORY,
                                hdpa->nMaxCount * sizeof(LPVOID));
    }

    TRACE("-- %p\n", hdpa);

    return hdpa;
}


/**************************************************************************
 * DPA_Create [COMCTL32.328]
 *
 * Creates a dynamic pointer array.
 *
 * PARAMS
 *     nGrow [I] number of items by which the array grows when it is filled
 *
 * RETURNS
 *     Success: handle (pointer) to the pointer array.
 *     Failure: NULL
 *
 * NOTES
 *     The DPA_ functions can be used to create and manipulate arrays of
 *     pointers.
 */
HDPA WINAPI DPA_Create (INT nGrow)
{
    return DPA_CreateEx( nGrow, 0 );
}


/**************************************************************************
 * DPA_EnumCallback [COMCTL32.385]
 *
 * Enumerates all items in a dynamic pointer array.
 *
 * PARAMS
 *     hdpa     [I] handle to the dynamic pointer array
 *     enumProc [I]
 *     lParam   [I]
 *
 * RETURNS
 *     none
 */
VOID WINAPI DPA_EnumCallback (HDPA hdpa, PFNDPAENUMCALLBACK enumProc,
                              LPVOID lParam)
{
    INT i;

    TRACE("(%p %p %p)\n", hdpa, enumProc, lParam);

    if (!hdpa)
975
        return;
976
    if (hdpa->nItemCount <= 0)
977
        return;
978 979

    for (i = 0; i < hdpa->nItemCount; i++) {
980 981
        if ((enumProc)(hdpa->ptrs[i], lParam) == 0)
            return;
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
    }

    return;
}


/**************************************************************************
 * DPA_DestroyCallback [COMCTL32.386]
 *
 * Enumerates all items in a dynamic pointer array and destroys it.
 *
 * PARAMS
 *     hdpa     [I] handle to the dynamic pointer array
 *     enumProc [I]
 *     lParam   [I]
 *
 * RETURNS
 *     none
 */
void WINAPI DPA_DestroyCallback (HDPA hdpa, PFNDPAENUMCALLBACK enumProc,
                                 LPVOID lParam)
{
    TRACE("(%p %p %p)\n", hdpa, enumProc, lParam);

    DPA_EnumCallback (hdpa, enumProc, lParam);
    DPA_Destroy (hdpa);
}
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028

/**************************************************************************
 * DPA_GetSize [COMCTL32.@]
 *
 * Returns all array allocated memory size
 *
 * PARAMS
 *     hdpa     [I] handle to the dynamic pointer array
 *
 * RETURNS
 *     Size in bytes
 */
ULONGLONG WINAPI DPA_GetSize(HDPA hdpa)
{
    TRACE("(%p)\n", hdpa);

    if (!hdpa) return 0;

    return sizeof(DPA) + hdpa->nMaxCount*sizeof(PVOID);
}