home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ocl150a.zip / OCL / Source / OFcl.cpp < prev    next >
C/C++ Source or Header  |  1996-08-12  |  8KB  |  337 lines

  1. // OCL - OS/2 Class Library
  2. // (c) Cubus 1995
  3. // All Rights Reserved
  4. // OFcl.CPP
  5.  
  6. // member functions of fcl FileCollector
  7.  
  8. /*
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Neither the name Cubus nor the name Team OCL may be used to
  15.  *    endorse or promote products derived from this software
  16.  *    without specific prior written permission.
  17.  * 3. See OCL.INF for a detailed copyright notice.
  18.  *
  19.  *              THIS SOFTWARE IS PROVIDED ``AS IS'' AND
  20.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29.  * SUCH DAMAGE.
  30.  */
  31.  
  32.  
  33. // $Header: W:/Projects/OCL/Source/rcs/OFcl.cpp 1.50 1996/08/11 23:49:15 B.STEIN Release $
  34.  
  35. #define __OCL_SOURCE__
  36.  
  37. #define OINCL_OSTRING
  38. #define OINCL_BASE
  39.  
  40. #include <ocl.hpp>
  41. #include <OFcl.hpp>
  42.  
  43.  
  44. OFcl::OFcl()    // default constructor
  45.    : ulMaxFiles(200), space(0), found(0),
  46.    checkDate(FALSE), checkTime(FALSE),
  47.    AttrMask(FILE_SYSTEM | FILE_HIDDEN | FILE_ARCHIVED | FILE_READONLY),
  48.    DateTime(NULL),
  49.    stopASAP(FALSE)
  50. {
  51.  bufSize = sizeof(FILEFINDBUF3) * ulMaxFiles;
  52.  setupFilebuf();
  53. }
  54.  
  55.  
  56. OFcl::~OFcl()   // destructor
  57. {
  58.  fclstop();
  59.  DosFreeMem(pffb);
  60.  // cleanup the Lists
  61.  dirs.reset();
  62.  files.reset();
  63. }
  64.  
  65.  
  66. PSZ OFcl::isOfType() const
  67.  return("OFcl"); 
  68. }
  69.  
  70.  
  71. void OFcl::setupFilebuf()      // allocate the filefind3 buffer
  72. {
  73.  DosAllocMem((PPVOID)&pffb,  bufSize, PAG_READ | PAG_WRITE | PAG_COMMIT);
  74. }
  75.  
  76.  
  77. void OFcl::fclinit()
  78. {
  79.  // cleanup the Lists
  80.  dirs.reset();
  81.  files.reset();
  82.  space = 0;
  83.  found = 0;
  84.  checkDate = FALSE;
  85.  checkTime = FALSE;
  86.  DateTime = NULL;
  87.  AttrMask = FILE_SYSTEM | FILE_HIDDEN | FILE_ARCHIVED | FILE_READONLY;
  88.  ulMaxFiles = 200;
  89.  stopASAP = FALSE;
  90. }
  91.  
  92.  
  93. void OFcl::setCheck(BOOL date, BOOL time)
  94. {
  95.  checkTime = time;
  96.  checkDate = date; 
  97. }
  98.  
  99.  
  100. void OFcl::fclstop()
  101. {
  102.  stopASAP = TRUE;
  103. }
  104.  
  105.  
  106. // this member handles the collect
  107.  
  108. void OFcl::collect(PCSZ spec, BOOL rec, BOOL local)
  109. {
  110.  ULONG    sDrive;
  111.  ULONG    ulDriveMap, ulDriveNum=0L;
  112.  PSZ      pathname = new CHAR[CCHMAXPATH];
  113.  OString  Spec(spec);
  114.  
  115.  strupr(Spec);
  116.  
  117.  recurse = rec;
  118.  
  119.  if (local)
  120.   {
  121.    finddrives(spec);
  122.    return;
  123.   }
  124.  
  125.  if(*(Spec.getText()+1)==':')
  126.    {
  127.     sDrive = (ULONG) ((*Spec.getText()) - '@');
  128.     if (DosSetDefaultDisk(sDrive))
  129.       return;
  130.     dirs << Spec;
  131.    }
  132.  else
  133.    {
  134.     if (strchr(Spec, '\\'))
  135.       return;
  136.     DosQueryCurrentDisk(&ulDriveNum, &ulDriveMap);
  137.     DosQueryCurrentDir(ulDriveNum, (PBYTE)pathname, &ulDriveMap);
  138.     strcat(pathname, "\\");
  139.     strcat(pathname, Spec);
  140.     dirs << pathname;
  141.    }
  142.  
  143.  ext << (PSZ) strrchr(Spec, '\\');
  144.  findfiles(dirs.last());
  145.  delete[] pathname;
  146.  return;
  147. }
  148.  
  149.  
  150. void OFcl::finddirs(PSZ dir)
  151. {
  152.  HDIR dhdir = HDIR_CREATE;
  153.  
  154.  strcpy(strrchr(dir, '\\'), "\\*");
  155.  ulMaxFiles = 200;
  156.  
  157.  rc = DosFindFirst(dir, &dhdir, MUST_HAVE_DIRECTORY,
  158.                        pffb, bufSize, &ulMaxFiles, FIL_STANDARD);
  159.  while((!rc) && (!stopASAP))
  160.    {
  161.     if (ulMaxFiles)
  162.        adddirs(dir);
  163.     ulMaxFiles = 200;
  164.     rc = DosFindNext(dhdir, pffb, bufSize, &ulMaxFiles);
  165.    }
  166.  
  167.  DosFindClose(dhdir);
  168. }
  169.  
  170.  
  171. void OFcl::findfiles(OListItem<OString> *tmp)  // find all files
  172. {
  173.  OListItem<OString> *strItem = tmp;
  174.  
  175.  while((strItem) && (!stopASAP))
  176.   {
  177.    HDIR hdir = HDIR_CREATE;
  178.  
  179.    ulMaxFiles = 200;
  180.    filespec << strItem->item->getText();
  181.  
  182.    rc = DosFindFirst(strItem->item->getText(), &hdir, (ULONG) AttrMask,
  183.                      pffb, bufSize, &ulMaxFiles, FIL_STANDARD);
  184.  
  185.    while((!rc) && (!stopASAP))
  186.     {
  187.      if (ulMaxFiles)
  188.        addfiles();
  189.      ulMaxFiles = 200;
  190.      rc = DosFindNext(hdir, pffb, bufSize, &ulMaxFiles);
  191.     }
  192.  
  193.    DosFindClose(hdir);
  194.  
  195.    if ((recurse) && (!stopASAP)) {
  196.     finddirs(strItem->item->getText());
  197.     strItem = strItem->next; }
  198.    else
  199.      break;
  200.   }
  201. }
  202.  
  203.  
  204. void OFcl::addfiles()  // add all found files to a List
  205. {
  206.  PFILEFINDBUF3   pffbFile;
  207.  ULONG           i;
  208.  PSZ             Buf = new CHAR[CCHMAXPATH];
  209.  
  210.  strcpy(Buf, filespec);
  211.  
  212.  tmp = (PBYTE) pffb;
  213.  
  214.  for(i = 0; (i < ulMaxFiles) && (!stopASAP); i++)
  215.    {
  216.     BOOL matches = TRUE;
  217.  
  218.     pffbFile = (PFILEFINDBUF3) tmp;
  219.     strcpy(strrchr(Buf, '\\')+1, pffbFile->achName);
  220.     if (checkDate)
  221.       matches = hasDate(pffbFile);
  222.     if (matches)
  223.       if (checkTime)
  224.         matches = hasTime(pffbFile);
  225.     if (matches)
  226.       files << Buf;
  227.     found++;
  228.     space += pffb->cbFileAlloc;
  229.     tmp += pffbFile->oNextEntryOffset;                // Point to the next file in the buffer
  230.    }
  231.  delete[] Buf;
  232. }
  233.  
  234.  
  235. void OFcl::adddirs(PCSZ dir)
  236. {
  237.  PFILEFINDBUF3   pffbFile;
  238.  ULONG           i;
  239.  PSZ             Buf = new CHAR[CCHMAXPATH];
  240.  
  241.  tmp = (PBYTE) pffb;
  242.  
  243.  for(i = 0; (i < ulMaxFiles) && (!stopASAP); i++)
  244.    {
  245.     pffbFile = (PFILEFINDBUF3) tmp;
  246.  
  247.     if (((BOOL)(pffbFile->attrFile&FILE_DIRECTORY)) && (pffbFile->achName[0]!='.'))
  248.      {
  249.       strcpy(Buf, dir);
  250.       strcpy(strrchr(Buf, '\\')+1, pffbFile->achName);
  251.       strcat(Buf, ext.getText());
  252.       dirs << Buf;
  253.      }
  254.     tmp += pffbFile->oNextEntryOffset;                // Point to the next file in the buffer
  255.    }
  256.  
  257.  delete[] Buf;
  258. }
  259.  
  260.  
  261. void OFcl::finddrives(PCSZ spec)
  262. {
  263.  ULONG   DriveNumber; 
  264.  ULONG   LogicalDriveMap;
  265.  UCHAR   Drive[] = "c:";
  266.  ULONG   Temp;
  267.  HFILE   hfDiskHandle;
  268.  ULONG   Action;
  269.  
  270.  if (DosQueryCurrentDisk(&DriveNumber, &LogicalDriveMap))
  271.     return;
  272.  
  273.  for(Temp=(ULONG)Drive[0]-'a', LogicalDriveMap>>=2;
  274.      Temp<=(ULONG)('z'-'a');
  275.      Temp++, Drive[0]++, LogicalDriveMap>>=1)
  276.       {
  277.        if(!(LogicalDriveMap&0x1))
  278.           continue;
  279.  
  280.        if (!DosOpen((PSZ)Drive, &hfDiskHandle, &Action, 0, FILE_NORMAL,
  281.                        OPEN_ACTION_OPEN_IF_EXISTS, OPEN_FLAGS_DASD | 
  282.                        OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NOINHERIT | 
  283.                        OPEN_SHARE_DENYREADWRITE | OPEN_ACCESS_READONLY, 0))
  284.            {
  285.             struct _PPF  
  286.              {
  287.               BYTE        bCommandInformation;
  288.               BYTE        bDriveUnit;
  289.              } PPF={0, 0};
  290.  
  291.             struct _DDF                
  292.              {
  293.               BYTE        bData;
  294.              } DDF;
  295.  
  296.             ULONG       ulParamLengthInOut=sizeof(PPF);
  297.             ULONG       ulDataLengthInOut=sizeof(DDF);
  298.  
  299.             DosDevIOCtl(hfDiskHandle, IOCTL_DISK, DSK_BLOCKREMOVABLE,
  300.                 &PPF, ulParamLengthInOut, &ulParamLengthInOut,
  301.                 &DDF, ulDataLengthInOut, &ulDataLengthInOut);
  302.             DosClose(hfDiskHandle);
  303.             if(DDF.bData)
  304.               {
  305.                OString buffer((PSZ) Drive);
  306.  
  307.                buffer + (PSZ) strchr(spec, '\\');
  308.                collect(buffer, recurse, FALSE);
  309.               }
  310.           }
  311.       }
  312.  return;
  313. }
  314.  
  315.  
  316. BOOL OFcl::hasTime(PFILEFINDBUF3 buf)
  317. {
  318.  if (!DateTime)
  319.    return(FALSE);
  320.  return((buf->ftimeCreation.minutes == DateTime->minutes) &&
  321.          (buf->ftimeCreation.hours == DateTime->hours));
  322. }
  323.  
  324.  
  325. BOOL OFcl::hasDate(PFILEFINDBUF3 buf)
  326. {
  327.  if (!DateTime)
  328.    return(FALSE);
  329.  return((buf->fdateCreation.day == DateTime->day) &&
  330.         (buf->fdateCreation.month == DateTime->month) &&
  331.         (buf->fdateCreation.year == DateTime->year));
  332. }
  333.  
  334.  
  335. // end of source
  336.