home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / pm / dragdrop / dragheap.c < prev    next >
C/C++ Source or Header  |  1999-05-11  |  18KB  |  581 lines

  1. /**************************************************************************
  2.  *  File name  :  dragheap.c
  3.  *
  4.  *  Description:  This file contains all file and memory management
  5.  *                functions for the Drag/Drop sample.
  6.  *
  7.  *                This source file contains the following functions:
  8.  *
  9.  *                FindFilePurge()
  10.  *                FindFileFirst(pszFilename, ulAttributes)
  11.  *                FindFileNext(pInput)
  12.  *                FindFileName(pszName)
  13.  *                AllocAppDraginfo(hwndClient, pDraginfo)
  14.  *                FreeAppDraginfo(pAppDraginfo)
  15.  *                FindAppDraginfo(pDraginfo)
  16.  *                AllocAppDragitem(pDraginfo, pFile)
  17.  *                FreeAppDragitem(pAppDragitem)
  18.  *                FindDraginfo(pAppDragitem)
  19.  *                FindDragitemFile(pAppDragitem)
  20.  *
  21.  *  Concepts   :  memory management
  22.  *
  23.  *  API's      :  DosSubFreeMem
  24.  *                DosGetNamedSharedMem
  25.  *                DosAllocSharedMem
  26.  *                DosSubSetMem
  27.  *                DosFindFirst
  28.  *                DosSubAllocMem
  29.  *                DosFindNext
  30.  *                DosFindClose
  31.  *                DrgQueryDragitemCount
  32.  *
  33.  *  Required
  34.  *    Files    :  OS2.H, STRING.H, DRAGDROP.H
  35.  *
  36.  *  Copyright (C) 1991 IBM Corporation
  37.  *
  38.  *      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  39.  *      sample code created by IBM Corporation. This sample code is not
  40.  *      part of any standard or IBM product and is provided to you solely
  41.  *      for  the purpose of assisting you in the development of your
  42.  *      applications.  The code is provided "AS IS", without
  43.  *      warranty of any kind.  IBM shall not be liable for any damages
  44.  *      arising out of your use of the sample code, even if they have been
  45.  *      advised of the possibility of such damages.                                                    *
  46.  *************************************************************************/
  47.  
  48. /*
  49.  *        Include the required sections from the PM header file
  50.  */
  51. #define INCL_DOSERRORS
  52. #define INCL_DOSFILEMGR
  53. #define INCL_WIN
  54. #define INCL_WINSTDDRAG
  55.  
  56. /*
  57.  *                     Include the PM header file
  58.  */
  59. #include <os2.h>
  60.  
  61. /*
  62.  *                  Include the C library header files
  63.  */
  64. #include <string.h>
  65.  
  66. /*
  67.  *                 Include the application header files
  68.  */
  69. #include "dragdrop.h"
  70.  
  71. /*
  72.  * Variables global to file
  73.  */
  74.  
  75. static PBYTE            hHeap     = NULL;   /* heap from which elements are */
  76.                                             /* allocated                    */
  77. static PPUBFILEFINDBUF  pFirst    = NULL;   /* anchor to list of elements   */
  78. static PAPPDRAGINFO     pFirstAppDraginfo = NULL;
  79.                                             /* pointer to chain of source   */
  80.                                             /* that contain information     */
  81.                                             /* for each drag in progress    */
  82.  
  83. /**************************************************************************
  84.  *
  85.  *  Name       : FindFilePurge
  86.  *
  87.  *  Description: Removes all entries in the list.  The function is
  88.  *               invoked from FileFindFirst to remove the old list if
  89.  *               one is present.
  90.  *
  91.  *  Concepts   :
  92.  *
  93.  *  API's      :  DosSubFreeMem
  94.  *
  95.  *  Parameters :  [none]
  96.  *
  97.  *  Return     :  TRUE  = list was purged
  98.  *                FALSE = purge of list was unsuccessful
  99.  *************************************************************************/
  100. BOOL FindFilePurge(VOID)
  101. {
  102.    PPUBFILEFINDBUF pList, pListNext;
  103.  
  104.    for (pList = pFirst; pList != NULL; pList = pListNext)
  105.    {
  106.       pListNext = pList->pNext;
  107.       if(NO_ERROR !=
  108.             DosSubFreeMem(hHeap, (NPBYTE)pList, sizeof(PUBFILEFINDBUF)))
  109.          return FALSE;
  110.    }
  111.    pFirst = NULL;                                  /* indicate list is empty */
  112.    return TRUE;
  113. }
  114.  
  115.  
  116. /**************************************************************************
  117.  *
  118.  *  Name       : FindFileFirst
  119.  *
  120.  *  Description: Finds all files in the input subdirectory, creating
  121.  *               a list of these files in the heap.  It then returns
  122.  *               the first file found onthe queue that matches the
  123.  *               input attributes.
  124.  *
  125.  *  Concepts   :
  126.  *
  127.  *  API's      :  DosGetNamedSharedMem
  128.  *                DosAllocSharedMem
  129.  *                DosSubSetMem
  130.  *                DosFindFirst
  131.  *                DosSubAllocMem
  132.  *                DosFindNext
  133.  *                DosFindClose
  134.  *
  135.  *  Parameters :  pszFilename = the name of the file
  136.  *                ulAttributes = attribute flags
  137.  *
  138.  *  Return     :  address of the first list element that matches the
  139.  *                input attributes, or
  140.  *                NULL = no file matched the input attributes
  141.  *************************************************************************/
  142. PPUBFILEFINDBUF FindFileFirst (PSZ pszFilename, ULONG ulAttributes)
  143. {
  144.    FILEFINDBUF3    ffbFile;
  145.    ULONG           rc;
  146.    ULONG           cFile;
  147.    PPUBFILEFINDBUF pAlloc, pTail;
  148.    NPBYTE          npBuffer;
  149.    HDIR            hDir;
  150.    PVOID           phHeap;
  151.  
  152.    if (!hHeap)
  153.    {
  154.      /*
  155.       * Initialize the heap that will be used to contain the queue elements
  156.       * for the files in the subdirectory
  157.       */
  158.      rc = DosGetNamedSharedMem((PPVOID)&hHeap, (PSZ)"\\sharemem\\dragmem",
  159.                                PAG_READ | PAG_WRITE);
  160.      if (rc != 0L)
  161.      {
  162.         rc = DosAllocSharedMem((PPVOID)&hHeap, (PSZ)"\\sharemem\\dragmem",
  163.                              65535LU,  PAG_COMMIT | PAG_READ | PAG_WRITE);
  164.  
  165.         if(rc != 0L ||    /* Initialize the heap.                 */
  166.            (NO_ERROR != DosSubSetMem((PVOID)hHeap, 1, 65535)))
  167.         {
  168.           /*
  169.            * Heap initialization failed.
  170.            */
  171.           MessageBox(HWND_DESKTOP,
  172.                     IDMSG_HEAPERROR,
  173.                     MB_OK | MB_ERROR,
  174.                     TRUE);
  175.           return NULL;
  176.         }
  177.          /*
  178.           * Heap is initialized - set anchor to NULL.
  179.           */
  180.         pFirst = NULL;
  181.      }
  182.    }
  183.  
  184.    /*
  185.     * heap now initialized
  186.     *
  187.     * free the entries currently on the list and create the new list
  188.     */
  189.    if(!FindFilePurge())                        /* empty the current list   */
  190.    {
  191.       MessageBox(HWND_DESKTOP,
  192.                     IDMSG_MEMFREEERROR,
  193.                     MB_OK | MB_ERROR,
  194.                     TRUE);
  195.       return NULL;
  196.    }
  197.  
  198.    cFile = 1;                                  /* buffer is large enough   */
  199.                                                /* to contain info for 1    */
  200.                                                /* file                     */
  201.    hDir = HDIR_CREATE;                         /* allocate DosFind handle  */
  202.    rc = DosFindFirst(pszFilename, &hDir, ulAttributes, &ffbFile,
  203.                      sizeof(ffbFile), &cFile, 1);
  204.    while (!rc)
  205.    {
  206.     /*
  207.      * if we couldn't allocate a buffer from the heap, display an
  208.      * error message and set the flag that will terminate the loop
  209.      */
  210.       if((NO_ERROR !=
  211.         DosSubAllocMem((PVOID)hHeap, (PPVOID)&npBuffer, sizeof(PUBFILEFINDBUF)))
  212.                ||     !npBuffer)
  213.       {
  214.          MessageBox(HWND_DESKTOP,
  215.                     IDMSG_HEAPALLOCERROR,
  216.                     MB_OK | MB_ERROR,
  217.                     TRUE);
  218.          rc = 1;
  219.       }
  220.  
  221.     /*
  222.      * otherwise, add the found entry to the list
  223.      */
  224.       else
  225.       {
  226.          phHeap = hHeap;
  227.          pAlloc = (PPUBFILEFINDBUF) npBuffer;
  228.          pAlloc->pNext = NULL;
  229.  
  230.       /*
  231.        * if this is the first one in the list, update the head pointer
  232.        */
  233.          if (pFirst == NULL)
  234.             pFirst = pAlloc;
  235.  
  236.       /*
  237.        * otherwise, add it to the end of the list
  238.        */
  239.          else
  240.            pTail ->pNext = pAlloc;
  241.  
  242.          pTail = pAlloc;                    /* update the tail pointer      */
  243.  
  244.       /*
  245.        * copy portions of file find buffer that will be kept to the
  246.        * buffer that we allocated.
  247.        */
  248.          if (ffbFile.attrFile & FILE_DIRECTORY)
  249.             strcpy (pAlloc->achName, "\\");     /* add '\\' before directory  */
  250.          else
  251.             pAlloc->achName[0] = '\0';          /* just a file                */
  252.  
  253.          strcat (pAlloc->achName, ffbFile.achName);
  254.          pAlloc->fdateLastWrite = ffbFile.fdateLastWrite;
  255.          pAlloc->ftimeLastWrite = ffbFile.ftimeLastWrite;
  256.          pAlloc->cbFileAlloc    = ffbFile.cbFileAlloc;
  257.          pAlloc->attrFile       = ffbFile.attrFile;
  258.  
  259.       /*
  260.        * get next file
  261.        */
  262.          rc = DosFindNext (hDir, &ffbFile, (LONG)(sizeof(ffbFile)), &cFile);
  263.       }
  264.    }
  265.  
  266.   /*
  267.    * All files have been found. Free the file find handle and send the
  268.    * appropriate return code.
  269.    */
  270.    DosFindClose (hDir);
  271.    return (pFirst);
  272. }
  273.  
  274.  
  275. /**************************************************************************
  276.  *
  277.  *  Name       : FindFileNext
  278.  *
  279.  *  Description: Finds the next file int eh input subdirectory
  280.  *
  281.  *  Concepts   :
  282.  *
  283.  *  API's      :  [none]
  284.  *
  285.  *  Parameters :  pInput = a pointer to the input file-find buffer
  286.  *
  287.  *  Return     :  address of the next file that matches the
  288.  *                input attributes, or
  289.  *                NULL = no file matched the input attributes before
  290.  *                       reaching the end of the list
  291.  *************************************************************************/
  292. PPUBFILEFINDBUF FindFileNext(PPUBFILEFINDBUF pInput)
  293. {
  294.    return ((pInput != NULL) ? pInput->pNext : (PPUBFILEFINDBUF)NULL);
  295. }
  296.  
  297. /**************************************************************************
  298.  *
  299.  *  Name       : FindFileName
  300.  *
  301.  *  Description: Finds the next file in the input subdirectory
  302.  *
  303.  *  Concepts   :
  304.  *
  305.  *  API's      :  [none]
  306.  *
  307.  *  Parameters :  pszName = a pointer to the file name string
  308.  *
  309.  *  Return     :  address of the next file that matches the
  310.  *                input file name, or
  311.  *                NULL = no file matched the input name
  312.  *************************************************************************/
  313. PPUBFILEFINDBUF FindFileName (PSZ pszName)
  314. {
  315.    PPUBFILEFINDBUF       pList;
  316.  
  317.    pList = pFirst;
  318.  
  319.    while (pList != NULL)
  320.    {
  321.       if (strcmp (pList->achName, pszName) == 0)
  322.          break;
  323.  
  324.       pList = pList -> pNext;
  325.    }
  326.  
  327.    return pList;
  328. }
  329.  
  330. /**************************************************************************
  331.  *
  332.  *  Name       : AllocAppDraginfo
  333.  *
  334.  *  Description: Allocates an APPDRAGINFO control block and places
  335.  *               it on a queue to be found by the application later.
  336.  *               The APPDRAGINFO is initialized.
  337.  *
  338.  *  Concepts   : drag information
  339.  *
  340.  *  API's      :  DosSubAllocMem
  341.  *                DrgQueryDragitemCount
  342.  *
  343.  *  Parameters :  pszName = a pointer to the file name string
  344.  *
  345.  *  Return     :  address of the APPDRAGINFO, or
  346.  *                NULL = the allocation failed
  347.  *************************************************************************/
  348. PAPPDRAGINFO AllocAppDraginfo (HWND hwndClient, PDRAGINFO pDraginfo)
  349. {
  350.    NPBYTE          npBuffer;
  351.    PAPPDRAGINFO    pAlloc;
  352.    PVOID           phHeap;
  353.  
  354.   /*
  355.    * if we couldn't allocate a buffer from
  356.    * the heap, display a message and abort
  357.    */
  358.    if(NO_ERROR != DosSubAllocMem(hHeap, (PPVOID)&npBuffer, sizeof(APPDRAGINFO)))
  359.    {
  360.       MessageBox(HWND_DESKTOP,
  361.                  IDMSG_ALLOCDRAGINFO,
  362.                  MB_OK | MB_ERROR,
  363.                  TRUE);
  364.       return NULL;
  365.    }
  366.    else
  367.    {
  368.     /*
  369.      * Add entry to list.
  370.      */
  371.       phHeap = hHeap;
  372.       pAlloc = (PAPPDRAGINFO) npBuffer;
  373.       pAlloc->pNextAppDraginfo = pFirstAppDraginfo;
  374.       pFirstAppDraginfo = pAlloc;
  375.  
  376.     /*
  377.      * Initialize the new entry.
  378.      */
  379.       pAlloc->pDraginfo      = pDraginfo;
  380.       pAlloc->hwndClient     = hwndClient;
  381.       pAlloc->usItemsDragged = DrgQueryDragitemCount(pDraginfo);
  382.       return pAlloc;
  383.    }
  384. }
  385.  
  386. /**************************************************************************
  387.  *
  388.  *  Name       : FreeAppDraginfo
  389.  *
  390.  *  Description: Removes an APPDRAGINFO from the queue and
  391.  *               deallocates the memory it was using.
  392.  *
  393.  *  Concepts   : drag information
  394.  *
  395.  *  API's      :  DosSubFreeMem
  396.  *
  397.  *  Parameters :  pAppDraginfo = pointer to the drag information
  398.  *
  399.  *  Return     :  TRUE = memory was successfullly deallocated
  400.  *                FALSE = the deallocation failed
  401.  *************************************************************************/
  402. BOOL FreeAppDraginfo(PAPPDRAGINFO pAppDraginfo)
  403. {
  404.    PAPPDRAGINFO    pPrevious, pCurrent;
  405.  
  406.    pPrevious = NULL;
  407.    pCurrent = pFirstAppDraginfo;
  408.  
  409.    while (pCurrent != NULL)
  410.    {
  411.       if (pCurrent == pAppDraginfo)
  412.          break;
  413.       else
  414.       {
  415.          pPrevious = pCurrent;
  416.          pCurrent = pCurrent->pNextAppDraginfo;
  417.       }
  418.    }
  419.  
  420.    if (pCurrent != NULL)
  421.    {
  422.       if (pPrevious == NULL)
  423.          pFirstAppDraginfo = pCurrent->pNextAppDraginfo;
  424.       else
  425.          pPrevious->pNextAppDraginfo = pCurrent->pNextAppDraginfo;
  426.  
  427.       if (NO_ERROR ==
  428.              DosSubFreeMem(hHeap, (NPBYTE)pAppDraginfo, sizeof(APPDRAGINFO)))
  429.          return TRUE;
  430.       else
  431.          return FALSE;
  432.    }
  433.    else
  434.    {
  435.       return FALSE;
  436.    }
  437. }
  438.  
  439. /**************************************************************************
  440.  *
  441.  *  Name       : FindAppDraginfo
  442.  *
  443.  *  Description: Finds an APPDRAGINFO, given the address of a DRAGINFO
  444.  *
  445.  *  Concepts   : drag information
  446.  *
  447.  *  API's      :  [none]
  448.  *
  449.  *  Parameters :  pDraginfo = pointer to the drag information
  450.  *
  451.  *  Return     :  NULL = unable to find the APPDRAGINFO
  452.  *                otherwise, the address of the APPDRAGINFO
  453.  *************************************************************************/
  454. PAPPDRAGINFO FindAppDraginfo(PDRAGINFO pDraginfo)
  455. {
  456.    PAPPDRAGINFO    pCurrent;
  457.  
  458.    pCurrent = pFirstAppDraginfo;
  459.  
  460.    while (pCurrent != NULL)
  461.    {
  462.      if (pCurrent->pDraginfo == pDraginfo)
  463.         break;
  464.      else
  465.         pCurrent = pCurrent->pNextAppDraginfo;
  466.    }
  467.  
  468.    return pCurrent;
  469. }
  470.  
  471.  
  472. /**************************************************************************
  473.  *
  474.  *  Name       : AllocAppDragitem
  475.  *
  476.  *  Description: Allocates an APPDRAGITEM control block and initializes it
  477.  *
  478.  *  Concepts   : drag information
  479.  *
  480.  *  API's      :  DosSubAllocMem
  481.  *
  482.  *  Parameters :  pDraginfo = pointer to the drag information
  483.  *                pFile = file pointer
  484.  *
  485.  *  Return     :  NULL = allocation failed
  486.  *                otherwise, the address of the APPDRAGINFO
  487.  *************************************************************************/
  488. PAPPDRAGITEM AllocAppDragitem(PDRAGINFO pDraginfo, PPUBFILEFINDBUF pFile)
  489. {
  490.    NPBYTE          npBuffer;
  491.    PAPPDRAGITEM    pAlloc;
  492.    PVOID           phHeap;
  493.  
  494.    if(NO_ERROR != DosSubAllocMem(hHeap, (PPVOID)&npBuffer, sizeof(APPDRAGITEM)))
  495.    {
  496.        MessageBox(HWND_DESKTOP,
  497.                     IDMSG_HEAPALLOCERROR,
  498.                     MB_OK | MB_ERROR,
  499.                     TRUE);
  500.        return NULL;
  501.    }
  502.    else
  503.    {
  504.      /*
  505.       * initialize the new entry
  506.       */
  507.       phHeap = &hHeap;
  508.       pAlloc = (PAPPDRAGITEM)npBuffer;
  509.       pAlloc->pDraginfo = pDraginfo;
  510.       pAlloc->pFile     = pFile;
  511.       return pAlloc;
  512.    }
  513. }
  514.  
  515. /**************************************************************************
  516.  *
  517.  *  Name       : FreeAppDragitem
  518.  *
  519.  *  Description: Frees an APPDRAGITEM
  520.  *
  521.  *  Concepts   : drag information
  522.  *
  523.  *  API's      :  DosSubFreeMem
  524.  *
  525.  *  Parameters :  pAppDragitem = pointer to the drag item
  526.  *
  527.  *  Return     :  TRUE = memory was successfully deallocated
  528.  *                FALSE = an error occurred
  529.  *************************************************************************/
  530. BOOL FreeAppDragitem(PAPPDRAGITEM pAppDragitem)
  531. {
  532.    return (NO_ERROR ==
  533.          DosSubFreeMem(hHeap, (NPBYTE)pAppDragitem, sizeof(APPDRAGITEM)));
  534. }
  535.  
  536.  
  537. /**************************************************************************
  538.  *
  539.  *  Name       : FindDragInfo
  540.  *
  541.  *  Description: Finds the PDRAGINFO for a drag operation, given the
  542.  *               address of an APPDRAGITEM
  543.  *
  544.  *  Concepts   : drag information
  545.  *
  546.  *  API's      :  [none]
  547.  *
  548.  *  Parameters :  pAppDragitem = pointer to the application drag item
  549.  *
  550.  *  Return     :  NULL = unable to find the drag information
  551.  *                otherwise, the address of the drag information used
  552.  *                to drag this item
  553.  *************************************************************************/
  554. PDRAGINFO FindDraginfo(PAPPDRAGITEM pAppDragitem)
  555. {
  556.    return pAppDragitem->pDraginfo;
  557. }
  558.  
  559. /**************************************************************************
  560.  *
  561.  *  Name       : FindDragItemFile
  562.  *
  563.  *  Description: Finds the PPUBFILEFINDBUF for a drag operation, given the
  564.  *               address of an APPDRAGITEM
  565.  *
  566.  *  Concepts   : drag information
  567.  *
  568.  *  API's      :  [none]
  569.  *
  570.  *  Parameters :  pAppDragitem = pointer to the drag item
  571.  *
  572.  *  Return     :  NULL = unable to find the PPUBFILEFINDBUF
  573.  *                otherwise, the address of the PPUBFILEFINDBUF for the
  574.  *                item being dragged
  575.  *************************************************************************/
  576. PPUBFILEFINDBUF FindDragitemFile (PAPPDRAGITEM pAppDragitem)
  577. {
  578.    return pAppDragitem->pFile;
  579. }
  580. /***************************  End of dragheap.c  *************************/
  581.