home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / guit01.zip / DIRLIST.CPP < prev    next >
C/C++ Source or Header  |  1993-11-30  |  5KB  |  177 lines

  1. /*********************************************************** C++ SOURCE *****
  2.  *
  3.  *               (C) Copyright JBA International Plc - 1993
  4.  *               Licensed Material - Program Property of JBA.
  5.  *      Not to be used for other than licensed and/or agreed purposes.
  6.  *-------------------------------------------------------------------------
  7.  *
  8.  * Project:         GUIDELINES
  9.  *
  10.  * Component:       Custom Controls
  11.  *
  12.  * Module:          DirList.CPP
  13.  *
  14.  * Original Author: Jon Wright
  15.  *
  16.  * Description:     This file contains an object which reads in a list
  17.  *                  of the files from a directory. This list may
  18.  *                  then be accessed by a JOT program. It is a trivial
  19.  *                  program to demonstrate how to interface to external 
  20.  *                  from within Guidelines
  21.  *
  22.  * Environment:     OS/2 V2.1 - IBM CSet++
  23.  *
  24.  * Modification history:
  25.  *
  26.  *  24nov93 : Initial release                                            jw
  27.  *
  28.  *  $Log$
  29.  *
  30.  ***************************************************************************/
  31.  
  32. /**************************************************************************
  33.  * Include Local files
  34.  *************************************************************************/
  35.  
  36. #include "dirlist.hpp"
  37.  
  38. /**************************************************************************
  39.  * DirList Methods 
  40.  *************************************************************************/
  41.  
  42. /**************************************************************************
  43.  * Function:   DirList
  44.  *
  45.  * Purpose:    Constructor: Create empty object
  46.  *
  47.  * Parameters: none
  48.  *
  49.  * Returns:    none
  50.  *
  51.  *************************************************************************/
  52.  
  53. DirList::DirList (void)
  54.     {
  55.     count = 0;
  56.     ptrList [0] = 0;
  57.     }
  58.  
  59.  
  60. /**************************************************************************
  61.  * Function:   DirList
  62.  *
  63.  * Purpose:    Constructor: Create and initialise with files list
  64.  *
  65.  * Parameters: Filespec of files to log
  66.  *
  67.  * Returns:    none
  68.  *
  69.  *************************************************************************/
  70.  
  71. DirList::DirList (char *DirName)
  72.     {
  73.     RefreshList(DirName);
  74.     }
  75.  
  76.  
  77.  
  78. /**************************************************************************
  79.  * Function:   DirList
  80.  *
  81.  * Purpose:    Destructor: Delete object and any list entries.
  82.  *
  83.  * Parameters: none
  84.  *
  85.  * Returns:    none
  86.  *
  87.  *************************************************************************/
  88.  
  89. DirList::~DirList ()
  90.     {
  91.     int i=0;
  92.  
  93.     while ((ptrList[i] != 0) && (i<LISTLEN))
  94.         {
  95.         delete ptrList[i++];
  96.         }
  97.     }
  98.  
  99.  
  100. /**************************************************************************
  101.  * Function:   RefreshList
  102.  *
  103.  * Purpose:    Allocates strings containing file names and adds them to
  104.  *             the list.
  105.  *             (note - almost no error checking!)
  106.  *
  107.  * Parameters: Filespec on which to search
  108.  *
  109.  * Returns:    -1 if error,  0 if OK.
  110.  *
  111.  *************************************************************************/
  112.  
  113. short DirList::RefreshList (char *DirName)
  114.     {
  115.     FILEFINDBUF3    findbuf;
  116.     HDIR            hDir = HDIR_CREATE;
  117.     ULONG           ulSearchCount = 1;   // Read 1 entry at a time 
  118.  
  119.     count = 0;
  120.     memset(&findbuf,0,sizeof(FILEFINDBUF3) );
  121.  
  122.     if (DosFindFirst( DirName,
  123.                       &hDir, 
  124.                       FILE_NORMAL, 
  125.                       &findbuf, 
  126.                       sizeof(findbuf),
  127.                       &ulSearchCount,
  128.                       FIL_STANDARD))
  129.         {
  130.         DosBeep(700,110);
  131.         return -1;
  132.         }
  133.  
  134.     while(ulSearchCount && (count < LISTLEN))
  135.         {
  136.         if (ptrList[count])
  137.             {
  138.             delete ptrList[count];
  139.             }
  140.  
  141.         ptrList[count] = new String(findbuf.achName);
  142.         count++;
  143.  
  144.         if (DosFindNext(hDir, &findbuf, sizeof(findbuf), &ulSearchCount))
  145.             {
  146.             break;
  147.             }
  148.         }
  149.     return 0;
  150.     }
  151.  
  152.  
  153. /**************************************************************************
  154.  * Function:   ReadItem
  155.  *
  156.  * Purpose:    Retrieves item from list
  157.  *
  158.  * Parameters: Index - index of item in list to be retrieved
  159.  *
  160.  * Returns:    Pointer to a Guidelines String, or null if invalid index.
  161.  *
  162.  *************************************************************************/
  163.  
  164. String *DirList::ReadItem (int Index)
  165.     {
  166.     if (Index < count)
  167.         {
  168.         return ptrList[Index];
  169.         }
  170.     else
  171.         {
  172.         return 0;
  173.         }
  174.     }
  175.  
  176.  
  177.