home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / text_cla / cmemlib.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-12  |  66.5 KB  |  3,097 lines

  1. ////////////////////////////////////////////////////////////////////////
  2. //+
  3. //  Module Name:  cmemlib.cpp
  4. //
  5. //  Description:  MEM specific routines
  6. //
  7. //  Include Modules Referenced:  cmemlib.hpp
  8. //
  9. //  Written by:  John Tal
  10. //
  11. //
  12. //  Modification history:
  13. //
  14. //  Date         Engineer     Mod #          Modification Description
  15. //
  16. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  17. //-
  18. ////////////////////////////////////////////////////////////////////////
  19.  
  20. #include <mem.h>
  21.  
  22. #include "cmemlib.h"
  23.  
  24.  
  25.  
  26. ////////////////////////////////////////////////////////////////////////
  27. //+
  28. // Function Name:   HEAP_C
  29. //
  30. // Class:           HEAP_C
  31. //
  32. // Security:        Public Constructor
  33. // 
  34. // Description:     Initializes a heap
  35. //
  36. // Parameters      
  37. //    In:           SHORT = Number of heap elements to create
  38. //    Out:          NONE 
  39. //
  40. // Return Codes:    VOID
  41. //                   
  42. //
  43. // Written by:      John Tal
  44. //
  45. //
  46. // Modification History:
  47. //
  48. //  Date         Engineer     Mod #          Modification Description
  49. //
  50. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  51. //
  52. //-
  53. ////////////////////////////////////////////////////////////////////////
  54.  
  55. HEAP_C::HEAP_C(SHORT sElms)
  56. {
  57.    C_DEF_VMODULE("HEAP_C::HEAP_C")
  58.  
  59.    sMaxElms = sElms;
  60.  
  61.    sBottom = C_HEAP_EMPTY;
  62.  
  63.    pstHeapData = (HEAP_DATA_P) new HEAP_DATA_T[sElms];
  64.  
  65.    pCompareFunc = NULL;
  66.  
  67. }
  68.  
  69.  
  70. ////////////////////////////////////////////////////////////////////////
  71. //+
  72. // Function Name:   Enq
  73. //
  74. // Class:           HEAP_C
  75. //
  76. // Security:        Public
  77. // 
  78. // Description:     Queues and item to the heap
  79. //
  80. // Parameters      
  81. //    In:           SHORT = Priority of Item
  82. //                  PVOID = Data to store
  83. //    Out:          NONE
  84. //
  85. // Return Codes:    SHORT = C_OK = Queued Item
  86. //                        = C_NOTOK = Queue Full
  87. //               
  88. //
  89. // Written by:      John Tal
  90. //
  91. //
  92. // Modification History:
  93. //
  94. //  Date         Engineer     Mod #          Modification Description
  95. //
  96. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  97. //
  98. //-
  99. ////////////////////////////////////////////////////////////////////////
  100.  
  101. SHORT HEAP_C::Enq(SHORT sPriority, PVOID pvData)
  102. {
  103.    C_DEF_MODULE("HEAP_C::Enq")
  104.  
  105.    if(sBottom > sMaxElms)
  106.       C_LEAVE(C_NOTOK)
  107.  
  108.    sBottom++;
  109.  
  110.    pstHeapData[sBottom].sPriority = sPriority;
  111.    pstHeapData[sBottom].pvData = pvData;
  112.  
  113.    Up();
  114.  
  115. C_MODULE_EXIT:
  116.  
  117.    C_RETURN
  118. }
  119.  
  120.  
  121. ////////////////////////////////////////////////////////////////////////
  122. //+
  123. // Function Name:   Deq
  124. //
  125. // Class:           HEAP_C
  126. //
  127. // Security:        Public
  128. // 
  129. // Description:     Removes an entry from the heap
  130. //
  131. // Parameters
  132. //    In:           SHORT = Priority of Item
  133. //    Out:          PPVOID = Returned address of item
  134. //
  135. // Return Codes:    SHORT = C_OK = Got an item
  136. //                        = C_NOTOK = Heap empty
  137. //
  138. // Written by:      John Tal
  139. //
  140. //
  141. // Modification History:
  142. //
  143. //  Date         Engineer     Mod #          Modification Description
  144. //
  145. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  146. //
  147. //-
  148. ////////////////////////////////////////////////////////////////////////
  149.  
  150. SHORT HEAP_C::Deq(PSHORT pPriority, PPVOID ppvData)
  151. {
  152.    C_DEF_MODULE("HEAP_C::Deq")
  153.  
  154.    if(sBottom == C_HEAP_EMPTY)
  155.       C_LEAVE(C_NOTOK)
  156.  
  157.    *pPriority = pstHeapData[C_HEAP_TOP].sPriority;
  158.    *ppvData = pstHeapData[C_HEAP_TOP].pvData;
  159.  
  160.    memcpy(&pstHeapData[C_HEAP_TOP],
  161.       &pstHeapData[sBottom],
  162.       sizeof(HEAP_DATA_T));
  163.  
  164.    memset(&pstHeapData[sBottom],
  165.           C_NOTHING,
  166.           sizeof(HEAP_DATA_T));
  167.  
  168.    sBottom--;
  169.  
  170.    Down();
  171.  
  172. C_MODULE_EXIT:
  173.  
  174.    C_RETURN
  175. }
  176.  
  177.  
  178. ////////////////////////////////////////////////////////////////////////
  179. //+
  180. // Function Name:   Down
  181. //
  182. // Class:           HEAP_C
  183. //
  184. // Security:        Protected
  185. // 
  186. // Description:     Adjusts the heap after a deque
  187. //
  188. // Parameters:      VOID
  189. //
  190. // Return Codes:    SHORT = C_OK
  191. //
  192. // Written by:      John Tal
  193. //
  194. //
  195. // Modification History:
  196. //
  197. //  Date         Engineer     Mod #          Modification Description
  198. //
  199. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  200. //
  201. //-
  202. ////////////////////////////////////////////////////////////////////////
  203.  
  204. SHORT HEAP_C::Down(VOID)
  205. {
  206.    C_DEF_MODULE("HEAP_C::Down")
  207.  
  208.    BOOL   fHeapOk = C_FALSE;
  209.    SHORT  sRoot2;
  210.    SHORT  sMaxChild;
  211.    SHORT  sRoot = C_HEAP_TOP;
  212.  
  213.    sRoot2 = sRoot << 1;
  214.  
  215.    //
  216.    //  Process until the root value is in its correct place
  217.    //
  218.  
  219.    while(
  220.          (sRoot2 <= sBottom) &&
  221.          (!fHeapOk)
  222.         )
  223.    {
  224.  
  225.        //
  226.        //  Calculate index of the child with the larger value
  227.        //
  228.  
  229.        if(sRoot2 == sBottom)
  230.           sMaxChild = sRoot2;  // only one child 
  231.        else
  232.        {
  233.           
  234.           //
  235.           //  Select the greater of the 2 children
  236.           //
  237.  
  238.           if(pstHeapData[sRoot2].sPriority >   // left child 
  239.              pstHeapData[sRoot2 + 1].sPriority)  // right child
  240.              sMaxChild = sRoot2;
  241.           else
  242.              sMaxChild = (sRoot2) + 1;
  243.  
  244.        }
  245.  
  246.  
  247.        //
  248.        //  If heap property violated, swap values
  249.        //
  250.  
  251.        if(pstHeapData[sRoot].sPriority <
  252.           pstHeapData[sMaxChild].sPriority)
  253.        {
  254.           Swap(&pstHeapData[sRoot],
  255.                      &pstHeapData[sMaxChild]);
  256.  
  257.           sRoot = sMaxChild;
  258.           sRoot2 = sRoot << 1;
  259.  
  260.  
  261.        }
  262.        else
  263.           fHeapOk = C_TRUE;
  264.    }
  265.  
  266.    C_RETURN
  267.  
  268. }
  269.  
  270.  
  271. ////////////////////////////////////////////////////////////////////////
  272. //+
  273. // Function Name:   Up
  274. //
  275. // Class:           HEAP_C
  276. //
  277. // Security:        Protected
  278. // 
  279. // Description:     Adjusts the heap after an enq
  280. //
  281. // Parameters:      VOID
  282. //
  283. // Return Codes:    SHORT = C_OK
  284. //
  285. // Written by:      John Tal
  286. //
  287. //
  288. // Modification History:
  289. //
  290. //  Date         Engineer     Mod #          Modification Description
  291. //
  292. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  293. //
  294. //-
  295. ////////////////////////////////////////////////////////////////////////
  296.  
  297. SHORT HEAP_C::Up(VOID)
  298. {
  299.    C_DEF_MODULE("HEAP_C::Up")
  300.  
  301.    SHORT  sCur;
  302.    SHORT  sParent;
  303.    SHORT  sHeapOk;
  304.  
  305.    sHeapOk = C_FALSE;
  306.    sCur = sBottom;
  307.    sParent = sCur >> 1;
  308.  
  309.  
  310.    //
  311.    //  move last element up in heap till in correct place
  312.    //
  313.  
  314.    while((sCur > (C_HEAP_EMPTY + 1)) && !sHeapOk)
  315.    {
  316.  
  317.       //
  318.       //  compare current and parent
  319.       //
  320.  
  321.       if(pstHeapData[sParent].sPriority >=
  322.          pstHeapData[sCur].sPriority)
  323.       {
  324.          sHeapOk = C_TRUE;
  325.       }
  326.       else
  327.       {   
  328.          Swap(&pstHeapData[sParent],
  329.                     &pstHeapData[sCur]);
  330.          sCur = sParent;
  331.          sParent = sParent >> 1;
  332.       }
  333.    }  
  334.  
  335.    C_RETURN
  336. }
  337.  
  338.  
  339. ////////////////////////////////////////////////////////////////////////
  340. //+
  341. // Function Name:   Swap
  342. //
  343. // Class:           HEAP_C
  344. //
  345. // Security:        Protected
  346. // 
  347. // Description:     Swaps two HEAP_DATA_T elements
  348. //
  349. // Parameters
  350. //    In:           HEAP_DATA_P = one heap data element
  351. //                  HEAP_DATA_P = another heap data element
  352. //    Out:          SAME
  353. //
  354. // Return Codes:    VOID
  355. //
  356. // Written by:      John Tal
  357. //
  358. //
  359. // Modification History:
  360. //
  361. //  Date         Engineer     Mod #          Modification Description
  362. //
  363. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  364. //
  365. //-
  366. ////////////////////////////////////////////////////////////////////////
  367.  
  368. VOID HEAP_C::Swap(HEAP_DATA_P pstHeap1,
  369.                   HEAP_DATA_P pstHeap2)
  370. {
  371.    C_DEF_VMODULE("HEAP_C::Swap")
  372.  
  373.    HEAP_DATA_T stHeapTemp;
  374.  
  375.    memcpy(&stHeapTemp,pstHeap1,sizeof(HEAP_DATA_T));
  376.    memcpy(pstHeap1,pstHeap2,sizeof(HEAP_DATA_T));
  377.    memcpy(pstHeap2,&stHeapTemp,sizeof(HEAP_DATA_T));
  378. }
  379.  
  380.  
  381. ////////////////////////////////////////////////////////////////////////
  382. //+
  383. // Function Name:   Find 
  384. //
  385. // Class:           HEAP_C
  386. //
  387. // Security:        Public
  388. // 
  389. // Description:     Searches a heap for pvData/key
  390. //
  391. // Parameters 
  392. //    In:           PVOID = Address of key data to search for
  393. //    Out:          PPVOID = Address of returned data (NULL if not found) 
  394. //
  395. // Return Codes:    VOID 
  396. //               
  397. //
  398. // Written by:      John Tal
  399. //
  400. //
  401. // Modification History:
  402. //
  403. //  Date         Engineer     Mod #          Modification Description
  404. //
  405. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  406. //
  407. //-
  408. ////////////////////////////////////////////////////////////////////////
  409.  
  410. SHORT  HEAP_C::Find(PVOID pvData,PPVOID ppVoid)
  411. {
  412.    C_DEF_MODULE("HEAP_C::Find")
  413.  
  414.    SHORT  sCnt;
  415.    
  416.    (*ppVoid) = NULL;
  417.  
  418.    if(sBottom != C_HEAP_EMPTY)
  419.    {
  420.       for(sCnt = C_HEAP_EMPTY + 1; sCnt <= sBottom; sCnt++)
  421.       {
  422.      if(pstHeapData[sCnt].pvData != NULL)
  423.      {
  424.          if( ((*pCompareFunc)(pstHeapData[sCnt].pvData,pvData)) == 0)
  425.          (*ppVoid) = pstHeapData[sCnt].pvData;
  426.          }
  427.       }
  428.    }
  429.  
  430.    C_RETURN
  431.  
  432. }
  433.  
  434.  
  435. ////////////////////////////////////////////////////////////////////////
  436. //+
  437. // Function Name:   GetEntries
  438. //
  439. // Class:           HEAP_C
  440. //
  441. // Security:        Public
  442. //
  443. // Description:     Returns current number of entries in heap 
  444. //
  445. // Parameters      
  446. //     In:          SAME     
  447. //     Out:         PSHORT = Number of entries
  448. //
  449. // Return Codes:    SHORT = C_OK
  450. //
  451. // Written by:      John Tal
  452. //
  453. //
  454. // Modification History:
  455. //
  456. //  Date         Engineer     Mod #          Modification Description
  457. //
  458. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  459. //
  460. //-
  461. ////////////////////////////////////////////////////////////////////////
  462.  
  463. SHORT HEAP_C::GetEntries(PSHORT psEntries)
  464. {
  465.    C_DEF_MODULE("HEAP_C::GetEntries")
  466.  
  467.    *psEntries = sBottom;
  468.  
  469.    C_RETURN
  470. }
  471.  
  472.  
  473. ////////////////////////////////////////////////////////////////////////
  474. //+
  475. // Function Name:   Vacate
  476. //
  477. // Class:           HEAP_C
  478. //
  479. // Security:        Public
  480. //
  481. // Description:     Removes all items from heap
  482. //
  483. // Parameters
  484. //    In:           BOOL = Whether or not to free data pointed to
  485. //    Out:          NONE
  486. //
  487. // Return Codes:    SHORT = C_OK
  488. //
  489. // Written by:      John Tal
  490. //
  491. //
  492. // Modification History:
  493. //
  494. //  Date         Engineer     Mod #          Modification Description
  495. //
  496. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  497. //
  498. //-
  499. ////////////////////////////////////////////////////////////////////////
  500.  
  501. SHORT HEAP_C::Vacate(BOOL fFreeData)
  502. {
  503.    C_DEF_MODULE("HEAP_C::Vacate")
  504.  
  505.    SHORT   sCnt;
  506.  
  507.    //
  508.    //  If freeing data, check every [].pvData ptr and free()
  509.    //
  510.  
  511.    if(fFreeData)
  512.    {
  513.       for(sCnt = C_HEAP_EMPTY + 1; sCnt <= sBottom; sCnt++)
  514.       {
  515.      if(pstHeapData[sCnt].pvData != NULL)
  516.      {
  517.         free(pstHeapData[sCnt].pvData);
  518.         pstHeapData[sCnt].pvData = NULL;
  519.          }
  520.       }   
  521.    }   
  522.  
  523.  
  524.    C_RETURN
  525. }
  526.  
  527.  
  528. //////////////////////////////////////////////////////////////////////// 
  529. //+ 
  530. // Function Name:   SetCompareFunc
  531. //
  532. // Class:           HEAP_C
  533. //
  534. // Security         Public
  535. //
  536. // Description:     Set the compare function for link list data
  537. //
  538. // Parameters
  539. //    In:           SHORT (*)(PVOID,PVOID)
  540. //    Out:          NONE
  541. //
  542. // Return Codes:    SHORT = C_OK
  543. //
  544. // Written by:      John Tal
  545. //
  546. //
  547. // Modification History:
  548. //
  549. //  Date         Engineer     Mod #          Modification Description
  550. //
  551. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  552. //
  553. //-
  554. ////////////////////////////////////////////////////////////////////////
  555.  
  556. SHORT  HEAP_C::SetCompareFunc(SHORT (*pFunc)(PVOID,PVOID))
  557. {
  558.    C_DEF_MODULE("HEAP_C::SetCompareFunc")
  559.  
  560.    pCompareFunc = pFunc;
  561.  
  562.    C_RETURN
  563. }
  564.  
  565.  
  566.  
  567. ////////////////////////////////////////////////////////////////////////
  568. //+
  569. // Function Name:   QUEUE_C
  570. //
  571. // Class:           QUEUE_C
  572. //
  573. // Security:        Public
  574. // 
  575. // Description:     Public Constructor
  576. //
  577. // Parameters:      VOID
  578. //
  579. // Return Codes:    VOID
  580. //
  581. // Written by:      John Tal
  582. //
  583. //
  584. // Modification History:
  585. //
  586. //  Date         Engineer     Mod #          Modification Description
  587. //
  588. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  589. //
  590. //-
  591. ////////////////////////////////////////////////////////////////////////
  592.  
  593. QUEUE_C::QUEUE_C(VOID)
  594. {
  595.    C_DEF_VMODULE("QUEUE_C::QUEUE_C")
  596.  
  597.    pstTail = NULL;
  598.  
  599.    lEntries = 0;
  600.  
  601.    pCompareFunc = NULL;
  602.  
  603. }
  604.  
  605.  
  606. //////////////////////////////////////////////////////////////////////// 
  607. //+ 
  608. // Function Name:   SetCompareFunc
  609. //
  610. // Class:           QUEUE_C
  611. //
  612. // Security         Public
  613. //
  614. // Description:     Set the compare function for link list data
  615. //
  616. // Parameters
  617. //    In:           SHORT (*)(PVOID,PVOID)
  618. //    Out:          NONE
  619. //
  620. // Return Codes:    SHORT = C_OK
  621. //
  622. // Written by:      John Tal
  623. //
  624. //
  625. // Modification History:
  626. //
  627. //  Date         Engineer     Mod #          Modification Description
  628. //
  629. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  630. //
  631. //-
  632. ////////////////////////////////////////////////////////////////////////
  633.  
  634. SHORT  QUEUE_C::SetCompareFunc(SHORT (*pFunc)(PVOID,PVOID))
  635. {
  636.    C_DEF_MODULE("QUEUE_C::SetCompareFunc")
  637.  
  638.    pCompareFunc = pFunc;
  639.  
  640.    C_RETURN
  641. }
  642.  
  643.  
  644. ////////////////////////////////////////////////////////////////////////
  645. //+
  646. // Function Name:   GetEntries
  647. //
  648. // Class:           QUEUE_C
  649. //
  650. // Security:        Public
  651. // 
  652. // Description:     Returns current number of entries in queue
  653. //
  654. // Parameters
  655. //    In:           SAME
  656. //    Out:          PLONG = Number of entries
  657. //
  658. // Return Codes:    SHORT = C_OK
  659. //
  660. // Written by:      John Tal
  661. //
  662. //
  663. // Modification History:
  664. //
  665. //  Date         Engineer     Mod #          Modification Description
  666. //
  667. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  668. //
  669. //-
  670. ////////////////////////////////////////////////////////////////////////
  671.  
  672. SHORT QUEUE_C::GetEntries(PLONG plEntries)
  673. {
  674.    C_DEF_MODULE("QUEUE_C::GetEntries")
  675.  
  676.    *plEntries = lEntries;
  677.  
  678.    C_RETURN
  679. }
  680.  
  681.  
  682. ////////////////////////////////////////////////////////////////////////
  683. //+
  684. // Function Name:   Vacate
  685. //
  686. // Class:           QUEUE_C
  687. //
  688. // Security:        Public
  689. // 
  690. // Description:     Removes all items from a queue
  691. //
  692. // Parameters
  693. //    In:           BOOL = C_TRUE = free data pointed to
  694. //                       = C_FALSE = do not free data pointed to, only
  695. //                                   list itself
  696. //    Out:          NONE
  697. //
  698. // Return Codes:    SHORT = C_OK
  699. //
  700. // Written by:      John Tal
  701. //
  702. //
  703. // Modification History:
  704. //
  705. //  Date         Engineer     Mod #          Modification Description
  706. //
  707. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  708. //
  709. //-
  710. ////////////////////////////////////////////////////////////////////////
  711.  
  712. SHORT QUEUE_C::Vacate(BOOL fFreeData)
  713. {
  714.    C_DEF_MODULE("QUEUE_C::Vacate")
  715.  
  716.    C_STATUS = LLIST_C::Vacate(fFreeData);
  717.  
  718.    C_RETURN
  719. }
  720.  
  721.  
  722.  
  723. ////////////////////////////////////////////////////////////////////////
  724. //+
  725. // Function Name:   Find
  726. //
  727. // Class:           QUEUE_C
  728. //
  729. // Security:        Public
  730. // 
  731. // Description:     Find an entry
  732. //
  733. // Parameters      
  734. //    In:           PVOID = Address of key data to search for
  735. //    Out:          PPVOID = Address of returned data (NULL if not found) 
  736. //
  737. // Return Codes:    VOID 
  738. //
  739. // Written by:      John Tal
  740. //
  741. //
  742. // Modification History:
  743. //
  744. //  Date         Engineer     Mod #          Modification Description
  745. //
  746. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  747. //
  748. //-
  749. ////////////////////////////////////////////////////////////////////////
  750.  
  751. SHORT QUEUE_C::Find(PVOID pvData,PPVOID ppVoid)
  752. {
  753.    C_DEF_MODULE("QUEUE_C:Find")
  754.  
  755.    C_STATUS = LLIST_C::Find(pvData,ppVoid);
  756.  
  757.    C_RETURN
  758. }
  759.  
  760.  
  761. ////////////////////////////////////////////////////////////////////////
  762. //+
  763. // Function Name:   Enq
  764. //
  765. // Class:           QUEUE_C
  766. //
  767. // Security:        Public
  768. // 
  769. // Description:     Adds an item to a queue
  770. //
  771. // Parameters
  772. //    In:           PVOID = Address/item to add
  773. //    Out:          NONE
  774. //
  775. // Return Codes:    SHORT = C_OK
  776. //
  777. // Written by:      John Tal
  778. //
  779. //
  780. // Modification History:
  781. //
  782. //  Date         Engineer     Mod #          Modification Description
  783. //
  784. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  785. //
  786. //-
  787. ////////////////////////////////////////////////////////////////////////
  788.  
  789. SHORT QUEUE_C::Enq(PVOID pvData)
  790. {
  791.    C_DEF_MODULE("QUEUE_C::Enq")
  792.  
  793.    LLIST_P  pstNew;
  794.  
  795.    C_STATUS = Alloc(pvData,&pstNew);
  796.  
  797.    if(C_STATUS)
  798.       C_LEAVE(C_STATUS);
  799.      
  800.    if(pstHead == NULL)
  801.    {
  802.       pstHead = pstNew;
  803.       pstTail = pstNew;
  804.    }
  805.    else
  806.    {
  807.       AddAfterNode(pstTail,pstNew);
  808.       pstTail = pstNew;
  809.    }
  810.  
  811.    lEntries++;
  812.  
  813. C_MODULE_EXIT:
  814.  
  815.    C_RETURN
  816. }
  817.  
  818.  
  819. ////////////////////////////////////////////////////////////////////////
  820. //+
  821. // Function Name:   Deq
  822. //
  823. // Class:           QUEUE_C
  824. //
  825. // Security:        Public
  826. // 
  827. // Description:     Removes an entry from a queue
  828. //
  829. // Parameters
  830. //    In:           SAME
  831. //    Out:          PPVOID = Address/item retrieved from queue
  832. //
  833. // Return Codes:    SHORT = C_OK
  834. //
  835. // Written by:      John Tal
  836. //
  837. //
  838. // Modification History:
  839. //
  840. //  Date         Engineer     Mod #          Modification Description
  841. //
  842. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  843. //
  844. //-
  845. ////////////////////////////////////////////////////////////////////////
  846.  
  847. SHORT QUEUE_C::Deq(PPVOID ppvData)
  848. {
  849.    C_DEF_MODULE("QUEUE_C::Deq")
  850.  
  851.    if(pstHead != NULL)
  852.       *ppvData = pstHead -> pvData;
  853.  
  854.    DeleteNode(pstHead);
  855.  
  856.    if(pstHead == NULL)
  857.       pstTail = NULL;
  858.      
  859.  
  860.    lEntries--;
  861.  
  862.    C_RETURN
  863. }
  864.  
  865.  
  866. ////////////////////////////////////////////////////////////////////////
  867. //+
  868. // Function Name:   LLIST_C 
  869. //
  870. // Class:           LLIST_C
  871. //
  872. // Security:        Public Constructor 
  873. // 
  874. // Description:     Initializes a link list object
  875. //
  876. // Parameters
  877. //    In:           SHORT = Number of Tracks to keep
  878. //    Out:          NONE
  879. //
  880. // Return Codes:    SHORT = C_OK
  881. //
  882. // Written by:      John Tal
  883. //
  884. //
  885. // Modification History:
  886. //
  887. //  Date         Engineer     Mod #          Modification Description
  888. //
  889. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  890. //
  891. //-
  892. ////////////////////////////////////////////////////////////////////////
  893.  
  894. LLIST_C::LLIST_C(SHORT sTracks)
  895. {
  896.    C_DEF_VMODULE("LLIST_C::LLIST_C")
  897.  
  898.    SHORT  sCnt; 
  899.  
  900.  
  901.    pstTrack = NULL;
  902.    sTrackSize = 0;
  903.  
  904.    pstHead = NULL;
  905.    pstCurNode = NULL;
  906.  
  907.  
  908.    if(sTracks > 0)
  909.    {
  910.       pstTrack = (TRACK_P) new TRACK_T[sTracks];
  911.  
  912.       for(sCnt = 0; sCnt < sTracks; sCnt++)
  913.         pstTrack[sCnt].sState = TRACK_AVAIL;
  914.  
  915.       sTrackSize = sTracks;
  916.    }
  917.  
  918.    pCompareFunc = NULL;
  919.  
  920.  
  921. }
  922.  
  923.  
  924. ////////////////////////////////////////////////////////////////////////
  925. //+
  926. // Function Name:   ~LLIST_C 
  927. //
  928. // Class:           LLIST_C
  929. //
  930. // Security:        Public Destructor
  931. // 
  932. // Description:     Link list termination
  933. //
  934. // Parameters:      VOID
  935. //
  936. // Return Codes:    SHORT = C_OK
  937. //
  938. // Written by:      John Tal
  939. //
  940. //
  941. // Modification History:
  942. //
  943. //  Date         Engineer     Mod #          Modification Description
  944. //
  945. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  946. //
  947. //-
  948. ////////////////////////////////////////////////////////////////////////
  949.  
  950. LLIST_C::~LLIST_C(VOID)
  951. {
  952.    C_DEF_VMODULE("LLIST_C::~LLIST_C")
  953.  
  954.    free(pstTrack);
  955.  
  956.    if(pstHead)
  957.       Vacate(C_FALSE);
  958. }
  959.  
  960.  
  961. //////////////////////////////////////////////////////////////////////// 
  962. //+ 
  963. // Function Name:   SetCompareFunc
  964. //
  965. // Class:           LLIST_C
  966. //
  967. // Security         Public
  968. //
  969. // Description:     Set the compare function for link list data
  970. //
  971. // Parameters
  972. //    In:           SHORT (*)(PVOID,PVOID)
  973. //    Out:          NONE
  974. //
  975. // Return Codes:    SHORT = C_OK
  976. //
  977. // Written by:      John Tal
  978. //
  979. //
  980. // Modification History:
  981. //
  982. //  Date         Engineer     Mod #          Modification Description
  983. //
  984. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  985. //
  986. //-
  987. ////////////////////////////////////////////////////////////////////////
  988.  
  989. SHORT  LLIST_C::SetCompareFunc(SHORT (*pFunc)(PVOID,PVOID))
  990. {
  991.    C_DEF_MODULE("LLIST_C::SetCompareFunc")
  992.  
  993.    pCompareFunc = pFunc;  
  994.  
  995.    C_RETURN
  996. }
  997.  
  998.  
  999. ////////////////////////////////////////////////////////////////////////
  1000. //+
  1001. // Function Name:   SetMark
  1002. //
  1003. // Class:           LLIST_C
  1004. //
  1005. // Security:        Public
  1006. // 
  1007. // Description:     Set a link list marker, return to caller
  1008. //
  1009. //                  A mark is like a bookmark, it allows to to leave
  1010. //                  a mark at the current member, to go elsewhere
  1011. //                  in the list for processing, and then to return
  1012. //                  to the marked member.
  1013. //
  1014. //                  Marks are provided because the LLIST_C objects
  1015. //                  isolate the application from the internals of
  1016. //                  the link list nodes (no address, all private).
  1017. //
  1018. //                  Multiple marks may be set.  The limit is defined
  1019. //                  by the LLIST_C constructor.
  1020. //
  1021. //                  The mark returned is like a handle since it
  1022. //                  indicates the array of the marking track where
  1023. //                  the address is stored within the LLIST_C class
  1024. //                  and not the address itself.  The application
  1025. //                  passes back the mark handle via LLIST_C::GoMark.
  1026. //                  After GoMark, the current position in the list
  1027. //                  is reset and data accessible via:
  1028. //
  1029. //                                      GetCur()
  1030. //                                      AddAfterCur()
  1031. //                                      AddBeforeCur()
  1032. //                                      DeleteCur()
  1033. //
  1034. //
  1035. // Parameters
  1036. //    In:           SAME
  1037. //    Out:          PSHORT = marker handle 
  1038. //                         = C_UNAVAIL = no marker slots left
  1039. //
  1040. // Return Codes:    SHORT = C_OK
  1041. //
  1042. // Written by:      John Tal
  1043. //
  1044. //
  1045. // Modification History:
  1046. //
  1047. //  Date         Engineer     Mod #          Modification Description
  1048. //
  1049. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  1050. //
  1051. //-
  1052. ////////////////////////////////////////////////////////////////////////
  1053.  
  1054. SHORT LLIST_C::SetMark(PSHORT psTrack)
  1055. {
  1056.    C_DEF_MODULE("LLIST_C::SetMark")
  1057.  
  1058.    *psTrack = C_UNAVAIL;
  1059.  
  1060.    for(SHORT sCnt = 0; sCnt < sTrackSize; sCnt++)
  1061.       if(pstTrack[sCnt].sState == TRACK_AVAIL)
  1062.          break;
  1063.  
  1064.    if(sCnt < sTrackSize)
  1065.    {
  1066.       pstTrack[sCnt].pstNode = pstCurNode;
  1067.       pstTrack[sCnt].sState = TRACK_USED;
  1068.       *psTrack = sCnt;
  1069.    }
  1070.  
  1071.    C_RETURN
  1072. }
  1073.  
  1074.  
  1075. ////////////////////////////////////////////////////////////////////////
  1076. //+
  1077. // Function Name:   GoMark
  1078. //
  1079. // Class:           LLIST_C
  1080. //
  1081. // Security:        Public
  1082. // 
  1083. // Description:     Set current link list node to previous mark
  1084. //
  1085. // Parameters
  1086. //    In:           Marker/track handle to set to
  1087. //    Out:          NONE
  1088. //
  1089. // Return Codes:    SHORT = C_OK = Success
  1090. //                        = C_NOTOK = bad handle
  1091. //
  1092. // Written by:      John Tal
  1093. //
  1094. //
  1095. // Modification History:
  1096. //
  1097. //  Date         Engineer     Mod #          Modification Description
  1098. //
  1099. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  1100. //
  1101. //-
  1102. ////////////////////////////////////////////////////////////////////////
  1103.  
  1104. SHORT  LLIST_C::GoMark(SHORT sTrack)
  1105. {
  1106.    C_DEF_MODULE("LLIST_C::GoMark")
  1107.  
  1108.    if(
  1109.       (sTrack > C_UNAVAIL) &&
  1110.       (sTrack < sTrackSize) &&
  1111.       (pstTrack[sTrack].sState = TRACK_USED)
  1112.      )
  1113.    {
  1114.  
  1115.       pstCurNode = pstTrack[sTrack].pstNode;
  1116.    }
  1117.    else
  1118.       C_SET_STATUS(C_NOTOK);
  1119.   
  1120.    C_RETURN
  1121. }
  1122.   
  1123.  
  1124. ////////////////////////////////////////////////////////////////////////
  1125. //+
  1126. // Function Name:   ReleaseMark
  1127. //
  1128. // Class:           LLIST_C
  1129. //
  1130. // Security:        Public
  1131. // 
  1132. // Description:     Release/Free a Mark for re-use
  1133. //
  1134. // Parameters
  1135. //    In:           Marker/track handle to set to
  1136. //    Out:          NONE
  1137. //
  1138. // Return Codes:    SHORT = C_OK = Success
  1139. //                        = C_NOTOK = bad handle
  1140. //
  1141. // Written by:      John Tal
  1142. //
  1143. //
  1144. // Modification History:
  1145. //
  1146. //  Date         Engineer     Mod #          Modification Description
  1147. //
  1148. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  1149. //
  1150. //-
  1151. ////////////////////////////////////////////////////////////////////////
  1152.  
  1153. SHORT  LLIST_C::ReleaseMark(SHORT sTrack)
  1154. {
  1155.    C_DEF_MODULE("LLIST_C::ReleaseMark")
  1156.  
  1157.    if(
  1158.       (sTrack > C_UNAVAIL) &&
  1159.       (sTrack < sTrackSize) &&
  1160.       (pstTrack[sTrack].sState = TRACK_USED)
  1161.      )
  1162.    {
  1163.  
  1164.       pstTrack[sTrack].pstNode = NULL;
  1165.       pstTrack[sTrack].sState = TRACK_AVAIL;
  1166.    }
  1167.    else
  1168.       C_SET_STATUS(C_NOTOK);
  1169.   
  1170.    C_RETURN
  1171. }
  1172.   
  1173.  
  1174. ////////////////////////////////////////////////////////////////////////
  1175. //+
  1176. // Function Name:   GetCur
  1177. //
  1178. // Class:           LLIST_C
  1179. //
  1180. // Security:        Public
  1181. // 
  1182. // Description:     Get the data for the current link list member/node
  1183. //                  Usefull after call to GoMark
  1184. //
  1185. // Parameters
  1186. //    In:           SAME
  1187. //    Out:          PPVOID = address of current item in list, does not
  1188. //                           remove item from list
  1189. //
  1190. // Return Codes:    SHORT = C_OK = Item was returned
  1191. //                        = C_NOTOK = No item to return
  1192. //
  1193. // Written by:      John Tal
  1194. //
  1195. //
  1196. // Modification History:
  1197. //
  1198. //  Date         Engineer     Mod #          Modification Description
  1199. //
  1200. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  1201. //
  1202. //-
  1203. ////////////////////////////////////////////////////////////////////////
  1204.  
  1205. SHORT  LLIST_C::GetCur(PPVOID ppVoid)
  1206. {
  1207.    C_DEF_MODULE("LLIST_C::GetCur")
  1208.  
  1209.    if(pstCurNode != NULL)
  1210.    {
  1211.       (*ppVoid) = pstCurNode -> pvData;
  1212.    }
  1213.    else
  1214.    {
  1215.       (*ppVoid) = NULL;
  1216.       C_SET_STATUS(C_NOTOK);
  1217.    }
  1218.  
  1219.    C_RETURN
  1220. }
  1221.  
  1222.  
  1223. ////////////////////////////////////////////////////////////////////////
  1224. //+
  1225. // Function Name:   AddAfterNode
  1226. //
  1227. // Class:           LLIST_C
  1228. //
  1229. // Security:        Protected
  1230. // 
  1231. // Description:     Add node after another node, for use by derived
  1232. //                  classes, not applications
  1233. //
  1234. // Parameters
  1235. //    In:           LLIST_P = node to add after
  1236. //                  LLIST_P = node to add
  1237. //    Out:          NONE
  1238. //
  1239. // Return Codes:    SHORT = C_OK = Success
  1240. //
  1241. // Written by:      John Tal
  1242. //
  1243. //
  1244. // Modification History:
  1245. //
  1246. //  Date         Engineer     Mod #          Modification Description
  1247. //
  1248. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  1249. //  16-Feb-1992  Tal          v 1.0-002      Support for empty lists
  1250. //-
  1251. ////////////////////////////////////////////////////////////////////////
  1252.  
  1253. SHORT LLIST_C::AddAfterNode(LLIST_P pstPrev, LLIST_P pstNew)
  1254. {
  1255.    C_DEF_MODULE("LLIST_C::AddAfterNode")
  1256.  
  1257.    LLIST_P  pstTemp = NULL;
  1258.  
  1259.    if(pstPrev != NULL)    //  fixed 16-Feb-92  Tal
  1260.       pstNew -> pstNext = pstPrev -> pstNext;
  1261.    pstNew -> pstPrev = pstPrev;
  1262.  
  1263.    if(pstPrev != NULL)    //  fixed 16-Feb-92  Tal
  1264.    {
  1265.      pstTemp = pstPrev -> pstNext;
  1266.      pstPrev -> pstNext = pstNew;
  1267.    }
  1268.  
  1269.    if(pstTemp != NULL)
  1270.       pstTemp -> pstPrev = pstNew;
  1271.  
  1272.    pstCurNode = pstNew;
  1273.  
  1274.    if(pstHead == NULL)    //  fixed 16-Feb-92  Tal
  1275.       pstHead = pstCurNode;
  1276.    C_RETURN
  1277. }
  1278.  
  1279.  
  1280. ////////////////////////////////////////////////////////////////////////
  1281. //+
  1282. // Function Name:   AddBeforeNode
  1283. //
  1284. // Class:           LLIST_C
  1285. //
  1286. // Security:        Protected
  1287. // 
  1288. // Description:     Add node directly before another one.  For use by
  1289. //                  derived classes.
  1290. //
  1291. // Parameters
  1292. //    In:           LLIST_P = Node to add before
  1293. //                  LLIST_P = Node to add
  1294. //    Out:          NONE
  1295. //
  1296. // Return Codes:    SHORT = C_OK
  1297. //
  1298. // Written by:      John Tal
  1299. //
  1300. //
  1301. // Modification History:
  1302. //
  1303. //  Date         Engineer     Mod #          Modification Description
  1304. //
  1305. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  1306. //
  1307. //-
  1308. ////////////////////////////////////////////////////////////////////////
  1309.  
  1310. SHORT LLIST_C::AddBeforeNode(LLIST_P pstPrev, LLIST_P pstNew)
  1311. {
  1312.    C_DEF_MODULE("LLIST_C::AddBeforeNode")
  1313.  
  1314.    LLIST_P pstTemp;
  1315.  
  1316.    if(pstPrev == pstHead)
  1317.    {
  1318.       pstHead = pstNew;
  1319.       pstPrev -> pstPrev = pstNew;
  1320.       pstNew -> pstNext = pstPrev;
  1321.    }
  1322.    else
  1323.    {
  1324.       pstTemp = pstPrev -> pstPrev;
  1325.       pstTemp -> pstNext = pstNew;
  1326.       pstPrev -> pstPrev = pstNew;
  1327.       pstNew -> pstNext = pstPrev;
  1328.    }
  1329.  
  1330.    pstCurNode = pstNew;
  1331.  
  1332.    C_RETURN
  1333. }
  1334.  
  1335.  
  1336. ////////////////////////////////////////////////////////////////////////
  1337. //+
  1338. // Function Name:   AddAfterCur
  1339. //
  1340. // Class:           LLIST_C
  1341. //
  1342. // Security:        Public
  1343. // 
  1344. // Description:     Call Find before this to set the current node 
  1345. //                  internally, then call this function
  1346. //
  1347. // Parameters
  1348. //    In:           PVOID = Data to add
  1349. //    Out:          NONE
  1350. //
  1351. // Return Codes:    SHORT = C_OK = Added
  1352. //                        = C_NOTOK = Not Added
  1353. //
  1354. // Written by:      John Tal
  1355. //
  1356. //
  1357. // Modification History:
  1358. //
  1359. //  Date         Engineer     Mod #          Modification Description
  1360. //
  1361. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  1362. //
  1363. //-
  1364. ////////////////////////////////////////////////////////////////////////
  1365.  
  1366. SHORT LLIST_C::AddAfterCur(PVOID pvData)
  1367. {
  1368.    C_DEF_MODULE("LLIST_C::AddAfterCur")
  1369.  
  1370.    LLIST_P  pstNew;
  1371.  
  1372.  
  1373.    C_STATUS = Alloc(pvData,&pstNew);
  1374.  
  1375.    if(C_STATUS)
  1376.       C_LEAVE(C_STATUS);
  1377.  
  1378.    C_STATUS = AddAfterNode(pstCurNode,pstNew);
  1379.  
  1380. C_MODULE_EXIT:
  1381.  
  1382.    C_RETURN
  1383. }
  1384.  
  1385.  
  1386. ////////////////////////////////////////////////////////////////////////
  1387. //+
  1388. // Function Name:   AddBeforeCur
  1389. //
  1390. // Class:           LLIST_C
  1391. //
  1392. // Security:        Public
  1393. // 
  1394. // Description:     Call Find before this to set the current node 
  1395. //                  internally, then call this function.
  1396. //
  1397. // Parameters
  1398. //    In:           PVOID = Address/data to add
  1399. //    Out:          NONE
  1400. //
  1401. // Return Codes:    SHORT = C_OK = Added
  1402. //                        = C_NOTOK = Not added
  1403. //
  1404. // Written by:      John Tal
  1405. //
  1406. //
  1407. // Modification History:
  1408. //
  1409. //  Date         Engineer     Mod #          Modification Description
  1410. //
  1411. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  1412. //
  1413. //-
  1414. ////////////////////////////////////////////////////////////////////////
  1415.  
  1416. SHORT LLIST_C::AddBeforeCur(PVOID pvData)
  1417. {
  1418.    C_DEF_MODULE("LLIST_C::AddBeforeCur")
  1419.  
  1420.    LLIST_P pstNew;
  1421.  
  1422.    C_STATUS = Alloc(pvData,&pstNew);
  1423.  
  1424.    if(C_STATUS)
  1425.       C_LEAVE(C_STATUS);
  1426.  
  1427.    C_STATUS = AddBeforeNode(pstCurNode,pstNew);
  1428.  
  1429. C_MODULE_EXIT:
  1430.  
  1431.    C_RETURN
  1432. }
  1433.  
  1434.  
  1435. ////////////////////////////////////////////////////////////////////////
  1436. //+
  1437. // Function Name:   DeleteNode
  1438. //
  1439. // Class:           LLIST_C
  1440. // 
  1441. // Security:        Protected
  1442. // 
  1443. // Description:     Explicitly delete a node, for use by derived classes.
  1444. //
  1445. // Parameters
  1446. //    In:           LLIST_P = Node to delete
  1447. //    Out:          NONE
  1448. //
  1449. // Return Codes:    SHORT = C_OK
  1450. //
  1451. // Written by:      John Tal
  1452. //
  1453. //
  1454. // Modification History:
  1455. //
  1456. //  Date         Engineer     Mod #          Modification Description
  1457. //
  1458. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  1459. //
  1460. //-
  1461. ////////////////////////////////////////////////////////////////////////
  1462.  
  1463. SHORT LLIST_C::DeleteNode(LLIST_P pstNode)
  1464. {
  1465.    C_DEF_MODULE("LLIST_C::DeleteNode")
  1466.  
  1467.    LLIST_P pstTemp;
  1468.  
  1469.    if(pstNode == pstHead)
  1470.    {
  1471.       pstHead = pstHead -> pstNext;
  1472.       if(pstHead != NULL)
  1473.          pstHead -> pstPrev = NULL;
  1474.    }
  1475.    else
  1476.    {   
  1477.       pstTemp = pstNode -> pstNext;
  1478.       if(pstTemp != NULL)
  1479.      pstTemp -> pstPrev = pstNode -> pstPrev;
  1480.       pstTemp = pstNode -> pstPrev;
  1481.       pstTemp -> pstNext = pstNode -> pstNext;
  1482.    }
  1483.  
  1484.    delete pstNode;
  1485.  
  1486.    C_RETURN
  1487. }
  1488.  
  1489.  
  1490. ////////////////////////////////////////////////////////////////////////
  1491. //+
  1492. // Function Name:   DeleteCur
  1493. //
  1494. // Class:           LLIST_C
  1495. //
  1496. // Security:        Public
  1497. // 
  1498. // Description:     Delete current member/node
  1499. //                  See GoMark, 
  1500. //                      Find, 
  1501. //                      FindFirst, 
  1502. //                      FindLast
  1503. //
  1504. // Parameters:      VOID
  1505. //
  1506. // Return Codes:    SHORT = C_OK = Deleted
  1507. //                        = C_NOTOK = Not deleted
  1508. //
  1509. // Written by:      John Tal
  1510. //
  1511. //
  1512. // Modification History:
  1513. //
  1514. //  Date         Engineer     Mod #          Modification Description
  1515. //
  1516. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  1517. //
  1518. //-
  1519. ////////////////////////////////////////////////////////////////////////
  1520.  
  1521. SHORT LLIST_C::DeleteCur(VOID)
  1522. {
  1523.    C_DEF_MODULE("LLIST_C::DeleteCur")
  1524.  
  1525.    LLIST_P pstTemp;
  1526.  
  1527.    if(pstCurNode != NULL)
  1528.    {
  1529.       C_STATUS = DeleteNode(pstCurNode);
  1530.       pstCurNode = NULL;
  1531.    }
  1532.    else
  1533.       C_SET_STATUS(C_NOTOK);
  1534.  
  1535.    C_RETURN
  1536. }
  1537.  
  1538.  
  1539. ////////////////////////////////////////////////////////////////////////
  1540. //+
  1541. // Function Name:   FindNode
  1542. //
  1543. // Class:           LLIST_C
  1544. //
  1545. // Security:        Protected
  1546. // 
  1547. // Description:     Search for entry in list by key, returns node
  1548. //
  1549. // Parameters
  1550. //    In:           PVOID = Pointer to key
  1551. //    Out:          LLIST_PP = Pointer to node found or NULL
  1552. //
  1553. // Return Codes:    SHORT = C_OK = Data Found
  1554. //                        = C_NOTOK = Data not found
  1555. //
  1556. // Written by:      John Tal
  1557. //
  1558. //
  1559. // Modification History:
  1560. //
  1561. //  Date         Engineer     Mod #          Modification Description
  1562. //
  1563. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  1564. //
  1565. //-
  1566. ////////////////////////////////////////////////////////////////////////
  1567.  
  1568.  
  1569. SHORT LLIST_C::FindNode(PVOID pvData, LLIST_PP ppstNode)
  1570. {
  1571.    C_DEF_MODULE("LLIST_C::FindNode")
  1572.  
  1573.    LLIST_P pstNode = NULL;
  1574.    SHORT  sTemp;
  1575.     
  1576.    (*ppstNode) = NULL;
  1577.  
  1578.    pstCurNode = NULL;    // fixup object global
  1579.  
  1580.  
  1581.    pstNode = pstHead;
  1582.  
  1583.    if(pstNode != NULL)
  1584.    {
  1585.      while ( (sTemp=((*pCompareFunc)(pvData,pstNode -> pvData)) ) != 0)
  1586.      {
  1587.      pstNode = pstNode -> pstNext;
  1588.  
  1589.      if(pstNode == NULL)
  1590.        break;
  1591.      }
  1592.    }
  1593.  
  1594.    if(pstNode != NULL)
  1595.      if( ((*pCompareFunc)(pvData,pstNode -> pvData)) != 0)
  1596.     pstNode = NULL;
  1597.      else
  1598.     pstCurNode = pstNode;    // set as found node
  1599.  
  1600.    *ppstNode = pstNode;
  1601.  
  1602. C_MODULE_EXIT: 
  1603.  
  1604.    C_RETURN
  1605. }
  1606.  
  1607.  
  1608. ////////////////////////////////////////////////////////////////////////
  1609. //+
  1610. // Function Name:   Find
  1611. //
  1612. // Class:           LLIST_C
  1613. //
  1614. // Security:        Public
  1615. // 
  1616. // Description:     Search for entry in list by key
  1617. //
  1618. // Parameters
  1619. //    In:           PVOID = Pointer to key
  1620. //    Out:          PPVOID = Pointer to data found or NULL
  1621. //
  1622. // Return Codes:    SHORT = C_OK = Data Found
  1623. //                        = C_NOTOK = Data not found
  1624. //
  1625. // Written by:      John Tal
  1626. //
  1627. //
  1628. // Modification History:
  1629. //
  1630. //  Date         Engineer     Mod #          Modification Description
  1631. //
  1632. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  1633. //
  1634. //-
  1635. ////////////////////////////////////////////////////////////////////////
  1636.  
  1637.  
  1638. SHORT LLIST_C::Find(PVOID pvData, PPVOID ppVoid)
  1639. {
  1640.    C_DEF_MODULE("LLIST_C::Find")
  1641.  
  1642.    LLIST_P pstNode = NULL;
  1643.     
  1644.    (*ppVoid) = NULL;
  1645.  
  1646.  
  1647.    C_STATUS = FindNode(pvData,&pstNode);
  1648.  
  1649.    C_IF_STATUS
  1650.  
  1651.    C_STATUS = GetCur(ppVoid);
  1652.  
  1653. C_MODULE_EXIT: 
  1654.  
  1655.    C_RETURN
  1656. }
  1657.  
  1658.  
  1659. ////////////////////////////////////////////////////////////////////////
  1660. //+
  1661. // Function Name:   FindLast
  1662. //
  1663. // Class:           LLIST_C
  1664. //
  1665. // Security:        Public
  1666. // 
  1667. // Description:     Find data for last member in list
  1668. //
  1669. // Parameters
  1670. //    In:           SAME
  1671. //    Out:          PPVOID = Returned address of data found or NULL
  1672. //
  1673. // Return Codes:    SHORT = C_OK = Data found
  1674. //                        = C_NOTOK = No data in list
  1675. //
  1676. // Written by:      John Tal
  1677. //
  1678. //
  1679. // Modification History:
  1680. //
  1681. //  Date         Engineer     Mod #          Modification Description
  1682. //
  1683. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  1684. //
  1685. //-
  1686. ////////////////////////////////////////////////////////////////////////
  1687.  
  1688. SHORT LLIST_C::FindLast(PPVOID ppVoid)
  1689. {
  1690.    C_DEF_MODULE("LLIST_C::FindLast")
  1691.  
  1692.    LLIST_P   pstNode;
  1693.  
  1694.    *ppVoid = NULL;
  1695.  
  1696.    C_STATUS = FindLastNode(&pstNode);
  1697.  
  1698.    if(pstNode != NULL)
  1699.       (*ppVoid) = pstNode -> pvData;
  1700.    else
  1701.       (*ppVoid) = NULL;
  1702.  
  1703.    C_RETURN
  1704. }
  1705.  
  1706.  
  1707. ////////////////////////////////////////////////////////////////////////
  1708. //+
  1709. // Function Name:   FindLastNode 
  1710. //
  1711. // Class:           LLIST_C
  1712. //
  1713. // Security:        Protected
  1714. // 
  1715. // Description:     Searches for last member in list, returns node
  1716. //                  For use by derived functions, not applications
  1717. //
  1718. // Parameters
  1719. //    In:           SAME
  1720. //    Out:          LLIST_PP = Pointer to Node returned or NULL if none
  1721. //
  1722. // Return Codes:    SHORT = C_OK 
  1723. //
  1724. // Written by:      John Tal
  1725. //
  1726. //
  1727. // Modification History:
  1728. //
  1729. //  Date         Engineer     Mod #          Modification Description
  1730. //
  1731. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  1732. //
  1733. //-
  1734. ////////////////////////////////////////////////////////////////////////
  1735.  
  1736. SHORT LLIST_C::FindLastNode(LLIST_PP  ppstNode)
  1737. {
  1738.    C_DEF_MODULE("LLIST_C::FindLastNode")
  1739.  
  1740.    LLIST_P pstNode = NULL;
  1741.    LLIST_P pstPrev = NULL;
  1742.  
  1743.    pstNode = pstHead;
  1744.  
  1745.    while(pstNode != NULL)
  1746.    {
  1747.       pstPrev = pstNode;
  1748.       pstNode = pstNode -> pstNext;
  1749.    }
  1750.  
  1751.    pstNode = pstPrev;
  1752.  
  1753.    pstCurNode = pstNode;
  1754.  
  1755.    (*ppstNode) = pstNode;
  1756.  
  1757.    C_RETURN
  1758. }
  1759.  
  1760.  
  1761. ////////////////////////////////////////////////////////////////////////
  1762. //+
  1763. // Function Name:   FindFirst 
  1764. //
  1765. // Class:           LLIST_C
  1766. //
  1767. // Security:        Public
  1768. // 
  1769. // Description:     Returns data/address of first member pointed to in 
  1770. //                  list.
  1771. //
  1772. // Parameters
  1773. //    In:           SAME
  1774. //    Out:          PPVOID = Address of first item tracked or NULL
  1775. //
  1776. // Return Codes:    SHORT = C_OK = Item returned
  1777. //                        = C_NOTOK = No data found
  1778. //
  1779. // Written by:      John Tal
  1780. //
  1781. //
  1782. // Modification History:
  1783. //
  1784. //  Date         Engineer     Mod #          Modification Description
  1785. //
  1786. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  1787. //
  1788. //-
  1789. ////////////////////////////////////////////////////////////////////////
  1790.  
  1791. SHORT LLIST_C::FindFirst(PPVOID ppvVoid)
  1792. {
  1793.    C_DEF_MODULE("LLIST_C::FindFirst")
  1794.  
  1795.    (*ppvVoid) = NULL;
  1796.  
  1797.    if(pstHead != NULL)
  1798.    {
  1799.       (*ppvVoid) = (PVOID) pstHead -> pvData;
  1800.  
  1801.       pstCurNode = pstHead;
  1802.  
  1803.    }
  1804.    else
  1805.       C_SET_STATUS(C_NOTOK)
  1806.  
  1807.    C_RETURN
  1808. }
  1809.  
  1810.  
  1811. ////////////////////////////////////////////////////////////////////////
  1812. //+
  1813. // Function Name:   GetNext
  1814. //
  1815. // Class:           LLIST_C
  1816. //
  1817. // Security:        Public
  1818. // 
  1819. // Description:     Returns data/address of next member pointed to in 
  1820. //                  list.
  1821. //
  1822. // Parameters
  1823. //    In:           SAME
  1824. //    Out:          PPVOID = Address of next item tracked or NULL
  1825. //
  1826. // Return Codes:    SHORT = C_OK = Item returned
  1827. //                        = C_NOTOK = No data found
  1828. //
  1829. // Written by:      John Tal
  1830. //
  1831. //
  1832. // Modification History:
  1833. //
  1834. //  Date         Engineer     Mod #          Modification Description
  1835. //
  1836. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  1837. //
  1838. //-
  1839. ////////////////////////////////////////////////////////////////////////
  1840.  
  1841. SHORT LLIST_C::GetNext(PPVOID ppvVoid)
  1842. {
  1843.    C_DEF_MODULE("LLIST_C::GetNext")
  1844.  
  1845.    (*ppvVoid) = NULL;
  1846.  
  1847.    if(pstCurNode != NULL)
  1848.    {
  1849.       pstCurNode = pstCurNode -> pstNext;
  1850.  
  1851.       (*ppvVoid) = pstCurNode;
  1852.    }
  1853.    else
  1854.       C_SET_STATUS(C_NOTOK)
  1855.  
  1856.    C_RETURN
  1857. }
  1858.  
  1859. ////////////////////////////////////////////////////////////////////////
  1860. //+
  1861. // Function Name:   GetPrev
  1862. //
  1863. // Class:           LLIST_C
  1864. //
  1865. // Security:        Public
  1866. // 
  1867. // Description:     Returns data/address of Previous member pointed to in 
  1868. //                  list.
  1869. //
  1870. // Parameters
  1871. //    In:           SAME
  1872. //    Out:          PPVOID = Address of previous item tracked or NULL
  1873. //
  1874. // Return Codes:    SHORT = C_OK = Item returned
  1875. //                        = C_NOTOK = No data found
  1876. //
  1877. // Written by:      John Tal
  1878. //
  1879. //
  1880. // Modification History:
  1881. //
  1882. //  Date         Engineer     Mod #          Modification Description
  1883. //
  1884. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  1885. //
  1886. //-
  1887. ////////////////////////////////////////////////////////////////////////
  1888.  
  1889. SHORT LLIST_C::GetPrev(PPVOID ppvVoid)
  1890. {
  1891.    C_DEF_MODULE("LLIST_C::GetPrev")
  1892.  
  1893.    (*ppvVoid) = NULL;
  1894.  
  1895.    if(pstCurNode != NULL)
  1896.    {
  1897.       if(pstCurNode -> pstPrev != NULL)
  1898.       {
  1899.      pstCurNode = pstCurNode -> pstPrev;
  1900.  
  1901.      (*ppvVoid) = pstCurNode;
  1902.       }
  1903.       else
  1904.     C_SET_STATUS(C_NOTOK);
  1905.    }
  1906.    else
  1907.       C_SET_STATUS(C_NOTOK)
  1908.  
  1909.    C_RETURN
  1910. }
  1911.  
  1912.  
  1913. ////////////////////////////////////////////////////////////////////////
  1914. //+
  1915. // Function Name:   Alloc
  1916. //
  1917. // Class:           LLIST_C
  1918. //
  1919. // Security:        Protected 
  1920. // 
  1921. // Description:     Allocates a new member
  1922. //
  1923. // Parameters
  1924. //    In:           PVDATA = Key to insert
  1925. //    Out:          LLIST_PP = New Node address
  1926. //
  1927. // Return Codes:    SHORT = C_OK = Allocated
  1928. //                        = C_NOTOK = Not allocated
  1929. //
  1930. // Written by:      John Tal
  1931. //
  1932. //
  1933. // Modification History:
  1934. //
  1935. //  Date         Engineer     Mod #          Modification Description
  1936. //
  1937. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  1938. //
  1939. //-
  1940. ////////////////////////////////////////////////////////////////////////
  1941.  
  1942. SHORT LLIST_C::Alloc(PVOID pvData, LLIST_PP ppstNode)
  1943. {
  1944.    C_DEF_MODULE("LLIST_C::Alloc")
  1945.  
  1946.    (*ppstNode) = new LLIST_T;
  1947.  
  1948.    (*ppstNode) -> pstPrev = NULL;
  1949.    (*ppstNode) -> pstNext = NULL;
  1950.  
  1951.    if(*ppstNode != NULL)
  1952.      (*ppstNode) -> pvData = pvData;
  1953.    else
  1954.      C_SET_STATUS(C_NOTOK)
  1955.  
  1956.    C_RETURN
  1957. }
  1958.  
  1959.  
  1960. ////////////////////////////////////////////////////////////////////////
  1961. //+
  1962. // Function Name:   Insert 
  1963. // 
  1964. // Class:           LLIST_C
  1965. //
  1966. // Security:        Public
  1967. //
  1968. // Description:     Insert an item into the list
  1969. //
  1970. // Parameters
  1971. //    In:           PVOID = Address of data to insert
  1972. //    Out:          NONE
  1973. //
  1974. // Return Codes:    SHORT = C_OK
  1975. //
  1976. // Written by:      John Tal
  1977. //
  1978. //
  1979. // Modification History:
  1980. //
  1981. //  Date         Engineer     Mod #          Modification Description
  1982. //
  1983. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  1984. //
  1985. //-
  1986. ////////////////////////////////////////////////////////////////////////
  1987.  
  1988. SHORT LLIST_C::Insert(PVOID pvData)
  1989. {
  1990.    C_DEF_MODULE("LLIST_C::Insert")
  1991.  
  1992.    LLIST_P pstPrev = NULL;
  1993.    LLIST_P pstNode;
  1994.  
  1995.    LLIST_P pstNew;
  1996.  
  1997.  
  1998.    C_STATUS = Alloc(pvData,&pstNew);
  1999.  
  2000.    if(C_STATUS)
  2001.       C_LEAVE(C_STATUS);
  2002.  
  2003.    pstCurNode = pstNew;
  2004.  
  2005.  
  2006.    if(pstHead == NULL)
  2007.    {
  2008.       pstHead = pstNew;
  2009.    }
  2010.    else
  2011.    {
  2012.       pstNode = pstHead;
  2013.  
  2014.       while( ((*pCompareFunc)(pstNode -> pvData, pstNew -> pvData)) < 0)
  2015.       {
  2016.          pstPrev = pstNode;
  2017.          pstNode = pstNode -> pstNext;
  2018.          if(pstNode == NULL)
  2019.             break;
  2020.       }
  2021.  
  2022.       if(pstPrev == NULL)
  2023.      AddBeforeNode(pstNode,pstNew);
  2024.       else
  2025.          AddAfterNode(pstPrev,pstNew);  // big change from binary, uses
  2026.                                       // prev ptr found here
  2027.  
  2028.    }  // if node != NULL 
  2029.  
  2030. C_MODULE_EXIT:
  2031.  
  2032.    C_RETURN
  2033. }
  2034.  
  2035.  
  2036. ////////////////////////////////////////////////////////////////////////
  2037. //+
  2038. // Function Name:   UnlinkNode
  2039. //
  2040. // Class:           LLIST_C
  2041. //
  2042. // Security:        Protected
  2043. // 
  2044. // Description:     Unlinks a node from the list, does not free()
  2045. //                  Function was in C libraries, must evaluate
  2046. //                  existing benefits for derived or public.
  2047. //
  2048. // Parameters
  2049. //    In:           LLIST_P = Node to unlink
  2050. //    Out:          NONE
  2051. //
  2052. // Return Codes:    SHORT = C_OK
  2053. //
  2054. // Written by:      John Tal
  2055. //
  2056. //
  2057. // Modification History:
  2058. //
  2059. //  Date         Engineer     Mod #          Modification Description
  2060. //
  2061. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  2062. //
  2063. //-
  2064. ////////////////////////////////////////////////////////////////////////
  2065.  
  2066. SHORT LLIST_C::UnlinkNode(LLIST_P pstNode)
  2067. {
  2068.    C_DEF_MODULE("LLIST_C::UnlinkNode")
  2069.  
  2070.    LLIST_P  pstTemp;
  2071.  
  2072.    if(pstNode == pstHead)
  2073.    {
  2074.       pstHead = pstHead -> pstNext;
  2075.       if(pstHead != NULL)
  2076.          pstHead -> pstPrev = NULL;
  2077.    }
  2078.    else
  2079.    {   
  2080.       pstTemp = pstNode -> pstNext;
  2081.       if(pstTemp != NULL)
  2082.      pstTemp -> pstPrev = pstNode -> pstPrev;
  2083.       pstTemp = pstNode -> pstPrev;
  2084.       pstTemp -> pstNext = pstNode -> pstNext;
  2085.    }
  2086.  
  2087.    C_RETURN
  2088. }
  2089.  
  2090.  
  2091. ////////////////////////////////////////////////////////////////////////
  2092. //+
  2093. // Function Name:   Vacate
  2094. //
  2095. // Class:           LLIST_C
  2096. // 
  2097. // Security:        Public
  2098. // 
  2099. // Description:     Removes all items from a list
  2100. //
  2101. // Parameters
  2102. //    In:           BOOL = C_TRUE = free data pointed to
  2103. //                       = C_FALSE = do not free data pointed to, only
  2104. //                                   list itself
  2105. //    Out:          NONE
  2106. //
  2107. // Return Codes:    SHORT = C_OK
  2108. //
  2109. // Written by:      John Tal
  2110. //
  2111. //
  2112. // Modification History:
  2113. //
  2114. //  Date         Engineer     Mod #          Modification Description
  2115. //
  2116. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  2117. //
  2118. //-
  2119. ////////////////////////////////////////////////////////////////////////
  2120.  
  2121. SHORT LLIST_C::Vacate(BOOL fFreeData)
  2122. {
  2123.    C_DEF_MODULE("LLIST_C::Vacate")
  2124.  
  2125.    LLIST_P  pstTemp;   //  working pointer 
  2126.    LLIST_P  pstFree;
  2127.  
  2128.    pstTemp = pstHead;
  2129.  
  2130.    // 
  2131.    //  Spin through list from head to end
  2132.    //
  2133.  
  2134.    while(pstTemp != NULL)
  2135.    {
  2136.       //
  2137.       //  If free the data and data to free then free
  2138.       //
  2139.  
  2140.       if(fFreeData)
  2141.         if(pstTemp -> pvData != NULL)
  2142.            delete pstTemp -> pvData;
  2143.  
  2144.  
  2145.       //
  2146.       //  Delete the member itself
  2147.       //
  2148.  
  2149.       pstFree = pstTemp;
  2150.       pstTemp = pstTemp -> pstNext;
  2151.  
  2152.       DeleteNode(pstFree);
  2153.  
  2154.    }
  2155.  
  2156.    //
  2157.    //  Reset the pointer sent in, should be NULL now
  2158.    //
  2159.  
  2160.    pstHead = pstTemp;
  2161.  
  2162.    C_RETURN
  2163. }
  2164.  
  2165.  
  2166. ////////////////////////////////////////////////////////////////////////
  2167. //+
  2168. // Function Name:   TREE_C
  2169. //
  2170. // Class:           TREE_C
  2171. //
  2172. // Security:        Public Constructor
  2173. // 
  2174. // Description:     Initializes a tree
  2175. //
  2176. // Parameters:      VOID
  2177. //
  2178. // Return Codes:    SHORT = C_OK
  2179. //
  2180. // Written by:      John Tal
  2181. //
  2182. //
  2183. // Modification History:
  2184. //
  2185. //  Date         Engineer     Mod #          Modification Description
  2186. //
  2187. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  2188. //
  2189. //-
  2190. ////////////////////////////////////////////////////////////////////////
  2191.  
  2192. TREE_C::TREE_C(VOID)
  2193. {
  2194.    C_DEF_VMODULE("TREE_C::TREE_C")
  2195.  
  2196.    pstHead = NULL;
  2197.    pstNode = NULL;
  2198.    pCompareFunc = NULL;
  2199.  
  2200. }
  2201.  
  2202.  
  2203. ////////////////////////////////////////////////////////////////////////
  2204. //+
  2205. // Function Name:   ~TREE_C
  2206. //
  2207. // Class:           TREE_C
  2208. //
  2209. // Security         Public Destructor
  2210. // 
  2211. // Description:     Removes all items from a tree
  2212. //
  2213. // Parameters
  2214. //    In:           BOOL = C_TRUE = free data pointed to
  2215. //                       = C_FALSE = free only tree itself
  2216. //    Out:          NONE
  2217. //
  2218. // Return Codes:    SHORT = C_OK
  2219. //
  2220. // Written by:      John Tal
  2221. //
  2222. //
  2223. // Modification History:
  2224. //
  2225. //  Date         Engineer     Mod #          Modification Description
  2226. //
  2227. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  2228. //
  2229. //-
  2230. ////////////////////////////////////////////////////////////////////////
  2231.  
  2232. TREE_C::~TREE_C(VOID)
  2233. {
  2234.    C_DEF_VMODULE("TREE_C::~TNODE_C")
  2235.  
  2236.    Vacate(C_FALSE);
  2237.  
  2238. }
  2239.  
  2240.  
  2241. ////////////////////////////////////////////////////////////////////////
  2242. //+
  2243. // Function Name:   SetCompareFunc
  2244. //
  2245. // Class:           TREE_C
  2246. //
  2247. // Security         Public
  2248. //
  2249. // Description:     Set the compare function for tree data
  2250. //
  2251. // Parameters
  2252. //    In:           SHORT (*)(PVOID,PVOID)
  2253. //    Out:          NONE
  2254. //
  2255. // Return Codes:    SHORT = C_OK
  2256. //
  2257. // Written by:      John Tal
  2258. //
  2259. //
  2260. // Modification History:
  2261. //
  2262. //  Date         Engineer     Mod #          Modification Description
  2263. //
  2264. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  2265. //
  2266. //-
  2267. ////////////////////////////////////////////////////////////////////////
  2268.  
  2269. SHORT  TREE_C::SetCompareFunc(SHORT (*pFunc)(PVOID,PVOID))
  2270. {
  2271.    C_DEF_MODULE("TREE_C::SetCompareFunc")
  2272.  
  2273.    pCompareFunc = pFunc; 
  2274.  
  2275.    C_RETURN
  2276. }
  2277.  
  2278.  
  2279. ////////////////////////////////////////////////////////////////////////
  2280. //+
  2281. // Function Name:   Vacate 
  2282. //
  2283. // Class:           TREE_C
  2284. //
  2285. // Security         Public
  2286. //
  2287. // Description:     Called by ~TREE_C or application
  2288. // 
  2289. //                  Performs non-recursive removal of all a node and all its
  2290. //                  children.  If used on the root of a tree, the entire
  2291. //                  tree is removed.
  2292. //
  2293. //                  Recursion is great, BUT, recursion relies heavily on
  2294. //                  the stack.  Large amounts of recursion and stack pushing
  2295. //                  will result in stack-overflow or core dump.
  2296. //
  2297. //                  Dymnamic memory from heap (or system memory) is generally
  2298. //                  more abundant than stack bytes.  So here we use the
  2299. //                  STACK_C routines which alloc from the compiler heap.
  2300. //
  2301. //
  2302. // Parameters
  2303. //    In:           BOOL = C_TRUE = free data pointed to
  2304. //                       = C_FALSE = free only tree itself
  2305. //    Out:          NONE
  2306. //
  2307. // Return Codes:    SHORT = C_OK
  2308. //
  2309. // Written by:      John Tal
  2310. //
  2311. //
  2312. // Modification History:
  2313. //
  2314. //  Date         Engineer     Mod #          Modification Description
  2315. //
  2316. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  2317. //
  2318. //-
  2319. ////////////////////////////////////////////////////////////////////////
  2320.  
  2321. SHORT TREE_C::Vacate(BOOL fFreeData)
  2322. {
  2323.    C_DEF_MODULE("TREE_C::Vacate")
  2324.  
  2325.    TNODE_P  pstNode;       //  main working TNODE_P var 
  2326.    BOOL     bEmpty;         //  whether stack is empty or not
  2327.    TNODE_P  pstDeleteNode;  //  working TNODE_P for delete 
  2328.    PVOID    pvWork;         //  hold area for TNODE_P after pop 
  2329.  
  2330.    STACK_C  clStack;        //  Stack object
  2331.    
  2332.  
  2333.  
  2334.   pstNode = pstHead;
  2335.  
  2336.   do
  2337.   { 
  2338.      while( pstNode != NULL )
  2339.      {
  2340.  
  2341.         //  For non-recursive delete on a binary tree,
  2342.         //  follow left-size of tree and find left-most node.
  2343.         //  Drop a marker of the trail down by pushing addresses
  2344.         //  of the nodes as we go down.  Then we can pop them
  2345.         //  to go back up the tree.
  2346.  
  2347.         clStack.Push((PVOID) pstNode);
  2348.     pstNode = pstNode -> pstLeft;
  2349.  
  2350.      }
  2351.  
  2352.      clStack.IsEmpty(&bEmpty);
  2353.  
  2354.      if( !bEmpty )
  2355.      {
  2356.  
  2357.         //  As far left as we can go, we are currently at a NULL
  2358.         //  so pop off the last node (furthest left)
  2359.  
  2360.         clStack.Pop(&pvWork);
  2361.  
  2362.         pstNode = (TNODE_P) pvWork;
  2363.  
  2364.  
  2365.         //  Got a node to delete, check if also free pvData;
  2366.  
  2367.         if(fFreeData && (pstNode -> pvData != NULL))
  2368.         {
  2369.        free(pstNode -> pvData);
  2370.        pstNode -> pvData = NULL;
  2371.         }
  2372.  
  2373.     pstDeleteNode = pstNode;
  2374.  
  2375.  
  2376.         //  Now try and go to the right.  We would then follow the
  2377.         //  same logic above by now falling down to the left-most
  2378.         //  node of our right sibling
  2379.         
  2380.  
  2381.         pstNode = pstNode -> pstRight;
  2382.  
  2383.         //  Free the node we were just at (left-most parent)
  2384.  
  2385.         free(pstDeleteNode);
  2386.  
  2387.      }
  2388.  
  2389.      clStack.IsEmpty(&bEmpty);
  2390.  
  2391.   } while ( (pstNode != NULL) && (! bEmpty) );
  2392.  
  2393.  
  2394.   //  Reset tree head pointer
  2395.  
  2396.   pstHead = NULL;
  2397.  
  2398.   C_RETURN
  2399. }
  2400.  
  2401.  
  2402. ////////////////////////////////////////////////////////////////////////
  2403. //+
  2404. // Function Name:   Insert
  2405. //
  2406. // Class:           TREE_C
  2407. //
  2408. // Security:        Public
  2409. // 
  2410. // Description:     Insert a node/data into tree.  It calls Ins()
  2411. //                  which then performs recursive walk down the tree.
  2412. //
  2413. // Parameters
  2414. //    In:           PVOID = data to insert
  2415. //    Out:          NONE
  2416. //
  2417. // Return Codes:    SHORT = C_OK
  2418. //               C_OK
  2419. //
  2420. // Written by:  John Tal
  2421. //
  2422. //
  2423. // Modification History:
  2424. //
  2425. //  Date         Engineer     Mod #          Modification Description
  2426. //
  2427. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  2428. //
  2429. //-
  2430. ////////////////////////////////////////////////////////////////////////
  2431.  
  2432. SHORT TREE_C::Insert(PVOID pvDataParm)
  2433. {
  2434.    C_DEF_MODULE("TREE_C::Insert")
  2435.  
  2436.    TNODE_P pstNewNode;
  2437.  
  2438.    pstNewNode = new TNODE_T;
  2439.    pstNewNode -> pstLeft = NULL;
  2440.    pstNewNode -> pstRight = NULL;
  2441.  
  2442.    pstNewNode -> pvData = pvDataParm;
  2443.  
  2444.    Ins(&pstHead,pstNewNode);
  2445.  
  2446.    C_RETURN
  2447. }
  2448.  
  2449.  
  2450. ////////////////////////////////////////////////////////////////////////
  2451. //+
  2452. // Function Name:   Ins
  2453. //
  2454. // Class:           TREE_C
  2455. //
  2456. // Security:        Private
  2457. // 
  2458. // Description:     Workhorse recursive function for insert
  2459. //
  2460. // Parameters
  2461. //    In:           TNODE_P = Working head node
  2462. //                  TNODE_P = Node to insert
  2463. //    Out:          NONE
  2464. //
  2465. // Return Codes:    VOID
  2466. //
  2467. // Written by:      John Tal
  2468. //
  2469. //
  2470. // Modification History:
  2471. //
  2472. //  Date         Engineer     Mod #          Modification Description
  2473. //
  2474. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  2475. //
  2476. //-
  2477. ////////////////////////////////////////////////////////////////////////
  2478.  
  2479. VOID TREE_C::Ins(TNODE_PP ppstWorkHead, TNODE_P pstNewNode)
  2480. {
  2481.    C_DEF_VMODULE("TREE_C::Ins")
  2482.  
  2483.    if((*ppstWorkHead) == NULL)
  2484.    {
  2485.       (*ppstWorkHead) = pstNewNode;
  2486.    }
  2487.    else
  2488.    {
  2489.       if( ((*pCompareFunc)((*ppstWorkHead) -> pvData,pstNewNode -> pvData)) >= 0)
  2490.        Ins(&(*ppstWorkHead) -> pstLeft, pstNewNode);
  2491.       else
  2492.        Ins(&(*ppstWorkHead) -> pstRight, pstNewNode);
  2493.  
  2494.    }
  2495. }
  2496.  
  2497.  
  2498. ////////////////////////////////////////////////////////////////////////
  2499. //+
  2500. // Function Name:   Leaf 
  2501. //
  2502. // Class:           TREE_C
  2503. //
  2504. // Security:        Private
  2505. // 
  2506. // Description:     Determines if a node is terminal 
  2507. //                  (no left or right children)
  2508. //
  2509. // Parameters
  2510. //    In:           TNODE_P = node to check
  2511. //    Out:          NONE
  2512. //
  2513. // Return Codes:    SHORT = C_OK
  2514. //
  2515. // Written by:      John Tal
  2516. //
  2517. //
  2518. // Modification History:
  2519. //
  2520. //  Date         Engineer     Mod #          Modification Description
  2521. //
  2522. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  2523. //
  2524. //-
  2525. ////////////////////////////////////////////////////////////////////////
  2526.  
  2527. SHORT TREE_C::Leaf(TNODE_P pstNode)
  2528. {
  2529.    C_DEF_MODULE("TREE_C::Leaf")
  2530.  
  2531.    if(
  2532.       (pstNode -> pstLeft == NULL) &&
  2533.       (pstNode -> pstRight == NULL)
  2534.      )
  2535.        C_SET_STATUS(C_TRUE)
  2536.    else
  2537.        C_SET_STATUS(C_FALSE)
  2538.  
  2539.    C_RETURN
  2540. }
  2541.  
  2542.  
  2543. ////////////////////////////////////////////////////////////////////////
  2544. //+
  2545. // Function Name:   RotateLeft 
  2546. //
  2547. // Class:           TREE_C
  2548. //
  2549. // Security:        Private
  2550. // 
  2551. // Description:     Rotates a node to the left.
  2552. //                  Called during node deletion
  2553. //
  2554. // Parameters:
  2555. //    In:           TNODE_P = Node to rotate
  2556. //    Out:          TNODE_PP = New node address  
  2557. //
  2558. // Return Codes:    VOID
  2559. //
  2560. // Written by:      John Tal
  2561. //
  2562. //
  2563. // Modification History:
  2564. //
  2565. //  Date         Engineer     Mod #          Modification Description
  2566. //
  2567. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  2568. //
  2569. //-
  2570. ////////////////////////////////////////////////////////////////////////
  2571.  
  2572. VOID TREE_C::RotateLeft(TNODE_P pstNode, TNODE_PP ppstNode)
  2573. {
  2574.    C_DEF_VMODULE("TREE_C::RotateLeft")
  2575.  
  2576.    TNODE_P pstTempNode = pstNode;
  2577.  
  2578.    if(pstNode != NULL)
  2579.    {
  2580.       pstNode = pstNode -> pstRight;
  2581.       pstTempNode -> pstRight = pstNode -> pstLeft;
  2582.       pstNode -> pstLeft = pstTempNode;
  2583.    }
  2584.  
  2585.    (*ppstNode) = pstNode;
  2586.  
  2587. }
  2588.  
  2589.  
  2590. ////////////////////////////////////////////////////////////////////////
  2591. //+
  2592. // Function Name:   RotateRight 
  2593. //
  2594. // Class:           TREE_C
  2595. //
  2596. // Security:        Private
  2597. // 
  2598. // Description:     Rotates a node to the right.
  2599. //                  Called during node deletion
  2600. // 
  2601. // Parameters:
  2602. //    In:           TNODE_P = Node to rotate
  2603. //    Out:          TNODE_PP = New node address  
  2604. //
  2605. // Return Codes:    VOID
  2606. //
  2607. // Written by:      John Tal
  2608. //
  2609. //
  2610. // Modification History:
  2611. //
  2612. //  Date         Engineer     Mod #          Modification Description
  2613. //
  2614. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  2615. //
  2616. //-
  2617. ////////////////////////////////////////////////////////////////////////
  2618.  
  2619. VOID TREE_C::RotateRight(TNODE_P pstNode, TNODE_PP ppstNode)
  2620. {
  2621.    C_DEF_VMODULE("TREE_C::RotateRight")
  2622.  
  2623.    TNODE_P pTempNode = pstNode; 
  2624.  
  2625.    if(pstNode != NULL)
  2626.    { 
  2627.       pstNode = pstNode -> pstLeft; 
  2628.       pTempNode -> pstLeft = pstNode -> pstRight; 
  2629.       pstNode -> pstRight = pTempNode;
  2630.    }
  2631.  
  2632.    (*ppstNode) = pstNode;
  2633. }
  2634.  
  2635.  
  2636. ////////////////////////////////////////////////////////////////////////
  2637. //+
  2638. // Function Name:   Delete 
  2639. //
  2640. // Class:           TREE_C
  2641. //
  2642. // Security:        Public
  2643. // 
  2644. // Description:     Node deletion based on key/data.
  2645. //                  Calls Del()
  2646. //
  2647. // Parameters
  2648. //    In:           PVOID = data to remove
  2649. //    Out:          NONE
  2650. //
  2651. // Return Codes:    VOID
  2652. //
  2653. // Written by:      John Tal
  2654. //
  2655. //
  2656. // Modification History:
  2657. //
  2658. //  Date         Engineer     Mod #          Modification Description
  2659. //
  2660. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  2661. //
  2662. //-
  2663. ////////////////////////////////////////////////////////////////////////
  2664.  
  2665. SHORT TREE_C::Delete(PVOID pvData)
  2666. {
  2667.    C_DEF_MODULE("TREE_C::Delete")
  2668.  
  2669.    C_STATUS = Del(pvData,pstHead,&pstHead);
  2670.  
  2671.    C_RETURN
  2672. }
  2673.  
  2674.  
  2675. ////////////////////////////////////////////////////////////////////////
  2676. //+
  2677. // Function Name:   Del
  2678. //
  2679. // Class:           TREE_C
  2680. //
  2681. // Security:        Private
  2682. // 
  2683. // Description:     Performs recursive node deletion
  2684. //
  2685. // Parameters
  2686. //    In:           PVOID = data to remove
  2687. //                  TNODE_P = Current node/head
  2688. //    Out:          TNODE_PP = New node/head
  2689. //
  2690. // Return Codes:    SHORT = C_OK = success
  2691. //                        = C_NOTOK = failed, key not in tree
  2692. //
  2693. // Written by:      John Tal
  2694. //
  2695. //
  2696. // Modification History:
  2697. //
  2698. //  Date         Engineer     Mod #          Modification Description
  2699. //
  2700. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  2701. //
  2702. //-
  2703. ////////////////////////////////////////////////////////////////////////
  2704.  
  2705. SHORT TREE_C::Del(PVOID pvData,TNODE_P pstNode, TNODE_PP ppstNode )
  2706. {
  2707.    C_DEF_MODULE("TREE_C::Del")
  2708.  
  2709.    if(pstNode == NULL)
  2710.    {
  2711.       (*ppstNode) = NULL;
  2712.       return C_NOTOK;
  2713.    }
  2714.  
  2715.    if( ((*pCompareFunc)(pvData,pstNode -> pvData)) == 0)
  2716.    {
  2717.       if(Leaf(pstNode))
  2718.       {
  2719.          delete pstNode;
  2720.          (*ppstNode) = NULL;
  2721.  
  2722.          return C_OK;
  2723.       }
  2724.       else
  2725.       {   
  2726.      if(pstNode -> pstLeft != NULL)
  2727.          {
  2728.             RotateRight(pstNode,&pstNode);
  2729.         Del(pvData,pstNode,&pstNode);
  2730.      }
  2731.      else
  2732.      {
  2733.         RotateLeft(pstNode,&pstNode);
  2734.         Del(pvData,pstNode,&pstNode);
  2735.      }
  2736.  
  2737.       }
  2738.    }
  2739.    else
  2740.    {
  2741.       if( ((*pCompareFunc)(pvData, pstNode -> pvData)) < 0)
  2742.      Del(pvData, pstNode -> pstLeft,&pstNode -> pstLeft);
  2743.       else
  2744.      Del(pvData, pstNode -> pstRight,&pstNode -> pstRight);
  2745.    }
  2746.  
  2747.    (*ppstNode) = pstNode;
  2748.  
  2749.    C_RETURN
  2750. }
  2751.  
  2752.  
  2753. ////////////////////////////////////////////////////////////////////////
  2754. //+
  2755. // Function Name:   Find
  2756. //
  2757. // Class:           TREE_C
  2758. //
  2759. // Security:        Private
  2760. // 
  2761. // Description:     Finds a node based on key
  2762. //
  2763. // Parameters
  2764. //    In:           PVOID = Key of Data to find
  2765. //    Out:          PPVOID = Pointed to data returned
  2766. //
  2767. // Return Codes:    VOID
  2768. //
  2769. // Written by:      John Tal
  2770. //
  2771. //
  2772. // Modification History:
  2773. //
  2774. //  Date         Engineer     Mod #          Modification Description
  2775. //
  2776. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  2777. //
  2778. //-
  2779. ////////////////////////////////////////////////////////////////////////
  2780.  
  2781. SHORT TREE_C::Find(PVOID pvData,PPVOID ppvData)
  2782. {
  2783.    C_DEF_MODULE("TREE_C::Find")
  2784.  
  2785.    TNODE_P pstNode;
  2786.    SHORT   sCmp;
  2787.  
  2788.    (*ppvData) = NULL;     //  16-Feb-92  Tal, support empty tree
  2789.  
  2790.    pstNode = pstHead;
  2791.  
  2792.    if(pstNode != NULL)
  2793.    {
  2794.       while(pstNode != NULL)
  2795.       {
  2796.          sCmp = (*pCompareFunc)(pvData,pstNode -> pvData);
  2797.  
  2798.      if( sCmp == 0 )
  2799.              break;
  2800.          else if( sCmp > 0 )
  2801.              pstNode = pstNode -> pstRight;
  2802.          else
  2803.              pstNode = pstNode -> pstLeft;
  2804.       }
  2805.    }
  2806.  
  2807.    if(pstNode != NULL)    //  16-Feb-92   Tal, support empty tree
  2808.       (*ppvData) = pstNode -> pvData;
  2809.  
  2810.    C_RETURN
  2811. }
  2812.  
  2813.  
  2814. ////////////////////////////////////////////////////////////////////////
  2815. //+
  2816. // Function Name:   IsEmpty
  2817. //
  2818. // Class:           STACK_C
  2819. //
  2820. // Security:        Public
  2821. //
  2822. // Description:     Checks if the stack is empty
  2823. //
  2824. // Parameters
  2825. //    In:           SAME
  2826. //    Out:          PBOOL = C_TRUE = Is Empty
  2827. //                        = C_FALSE = Not Empty
  2828. //
  2829. // Return Codes:    SHORT = C_OK
  2830. //
  2831. // Written by:      John Tal
  2832. //
  2833. //
  2834. // Modification History:
  2835. //
  2836. //  Date         Engineer     Mod #          Modification Description
  2837. //
  2838. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  2839. //
  2840. //-
  2841. ////////////////////////////////////////////////////////////////////////
  2842.  
  2843. SHORT STACK_C::IsEmpty(PBOOL fIsEmpty)
  2844. {
  2845.    C_DEF_MODULE("STACK_C::IsEmpty")
  2846.  
  2847.    if(pstHead == NULL)
  2848.       *fIsEmpty = C_TRUE;
  2849.    else
  2850.       *fIsEmpty = C_FALSE;
  2851.  
  2852.  
  2853.    C_RETURN
  2854. }
  2855.  
  2856.  
  2857. ////////////////////////////////////////////////////////////////////////
  2858. //+
  2859. // Function Name:   Pop
  2860. //
  2861. // Class:           STACK_C
  2862. //
  2863. // Security:        Public
  2864. //
  2865. // Description:     Removes an element from stack
  2866. //
  2867. // Parameters
  2868. //    In:           SAME
  2869. //    Out:          PPVOID = Address of returned item or NULL
  2870. //
  2871. // Return Codes:    SHORT = C_OK
  2872. //
  2873. // Written by:      John Tal
  2874. //
  2875. //
  2876. // Modification History:
  2877. //
  2878. //  Date         Engineer     Mod #          Modification Description
  2879. //
  2880. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  2881. //
  2882. //-
  2883. ////////////////////////////////////////////////////////////////////////
  2884.  
  2885. SHORT  STACK_C::Pop(PPVOID ppvData)
  2886. {
  2887.    C_DEF_MODULE("STACK_C::Pop")
  2888.  
  2889.    LLIST_P  pstOldHead;
  2890.  
  2891.  
  2892.    if( pstHead != NULL )
  2893.    {
  2894.       //  To modify a pointer inside of a function requires
  2895.       //  sending in the address of the pointer.
  2896.  
  2897.       *ppvData = pstHead -> pvData;
  2898.  
  2899.  
  2900.       //  Reset to new stack head pointer
  2901.  
  2902.       pstOldHead = pstHead;
  2903.  
  2904.       pstHead = pstHead -> pstNext;
  2905.  
  2906.  
  2907.       //    Free the old stack head pointer
  2908.       
  2909.       free(pstOldHead);
  2910.  
  2911.   }
  2912.   else
  2913.   {
  2914.       //   Stack is empty, return NULL
  2915.  
  2916.       *ppvData = NULL;
  2917.   }
  2918.  
  2919.   C_RETURN
  2920. }
  2921.  
  2922.  
  2923. ////////////////////////////////////////////////////////////////////////
  2924. //+
  2925. // Function Name:   Push
  2926. //
  2927. // Class:           STACK_C
  2928. //
  2929. // Security:        Public
  2930. //
  2931. // Description:     Places an item on the stack
  2932. //
  2933. // Parameters
  2934. //    In:           PVOID = Address of data to store
  2935. //    Out:          SAME
  2936. //
  2937. // Return Codes:    SHORT = C_OK
  2938. //
  2939. // Written by:      John Tal
  2940. //
  2941. //
  2942. // Modification History:
  2943. //
  2944. //  Date         Engineer     Mod #          Modification Description
  2945. //
  2946. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  2947. //
  2948. //-
  2949. ////////////////////////////////////////////////////////////////////////
  2950.  
  2951. SHORT  STACK_C::Push(PVOID pvData)
  2952. {
  2953.    C_DEF_MODULE("STACK_C::Push")
  2954.  
  2955.    LLIST_P  pstNew;
  2956.  
  2957.    C_STATUS = Alloc(pvData,&pstNew);  // allocate new member
  2958.  
  2959.    if(C_STATUS)
  2960.       C_LEAVE(C_STATUS);
  2961.  
  2962.    pstNew -> pstNext = pstHead;
  2963.  
  2964.    pstHead = pstNew;
  2965.  
  2966. C_MODULE_EXIT:
  2967.  
  2968.    C_RETURN
  2969. }
  2970.  
  2971.  
  2972. ////////////////////////////////////////////////////////////////////////
  2973. //+
  2974. // Function Name:   Vacate
  2975. //
  2976. // Class:           STACK_C
  2977. //
  2978. // Security:        Public
  2979. //
  2980. // Description:     Removes all items from stack
  2981. //
  2982. // Parameters
  2983. //    In:           BOOL = C_FALSE = Free only stack, not pointed to data
  2984. //                       = C_TRUE = Free stack and pointed to data
  2985. //    Out:          NONE
  2986. //
  2987. // Return Codes:    SHORT = C_OK
  2988. //
  2989. // Written by:      John Tal
  2990. //
  2991. //
  2992. // Modification History:
  2993. //
  2994. //  Date         Engineer     Mod #          Modification Description
  2995. //
  2996. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  2997. //
  2998. //-
  2999. ////////////////////////////////////////////////////////////////////////
  3000.  
  3001. SHORT  STACK_C::Vacate(BOOL fFreeData)
  3002. {
  3003.    C_DEF_MODULE("STACK_C::Vacate")
  3004.  
  3005.    PVOID  pvData;
  3006.  
  3007.    while(pstHead != NULL)
  3008.    {
  3009.       Pop(&pvData);
  3010.  
  3011.       if(fFreeData)
  3012.          if(pvData != NULL)
  3013.             free(pvData);
  3014.    }
  3015.  
  3016.    pstHead = NULL;
  3017.  
  3018.    C_RETURN
  3019. }
  3020.  
  3021.  
  3022. ////////////////////////////////////////////////////////////////////////
  3023. //+
  3024. // Function Name:   Find
  3025. //
  3026. // Class:           STACK_C
  3027. //
  3028. // Security:        Public
  3029. // 
  3030. // Description:     Find an entry
  3031. //
  3032. // Parameters      
  3033. //    In:           PVOID = Address of key data to search for
  3034. //    Out:          PPVOID = Address of returned data (NULL if not found) 
  3035. //
  3036. // Return Codes:    VOID 
  3037. //
  3038. // Written by:      John Tal
  3039. //
  3040. //
  3041. // Modification History:
  3042. //
  3043. //  Date         Engineer     Mod #          Modification Description
  3044. //
  3045. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  3046. //
  3047. //-
  3048. ////////////////////////////////////////////////////////////////////////
  3049.  
  3050. SHORT STACK_C::Find(PVOID pvData,PPVOID ppVoid)
  3051. {
  3052.    C_DEF_MODULE("STACK_C:Find")
  3053.  
  3054.    C_STATUS = LLIST_C::Find(pvData,ppVoid);
  3055.  
  3056.    C_RETURN
  3057. }
  3058.  
  3059.  
  3060. //////////////////////////////////////////////////////////////////////// 
  3061. //+ 
  3062. // Function Name:   SetCompareFunc
  3063. //
  3064. // Class:           STACK_C
  3065. //
  3066. // Security         Public
  3067. //
  3068. // Description:     Set the compare function for link list data
  3069. //
  3070. // Parameters
  3071. //    In:           SHORT (*)(PVOID,PVOID)
  3072. //    Out:          NONE
  3073. //
  3074. // Return Codes:    SHORT = C_OK
  3075. //
  3076. // Written by:      John Tal
  3077. //
  3078. //
  3079. // Modification History:
  3080. //
  3081. //  Date         Engineer     Mod #          Modification Description
  3082. //
  3083. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  3084. //
  3085. //-
  3086. ////////////////////////////////////////////////////////////////////////
  3087.  
  3088. SHORT  STACK_C::SetCompareFunc(SHORT (*pFunc)(PVOID,PVOID))
  3089. {
  3090.    C_DEF_MODULE("STACK_C::SetCompareFunc")
  3091.  
  3092.    pCompareFunc = pFunc;
  3093.  
  3094.    C_RETURN
  3095. }
  3096.  
  3097.