home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / mslang / filesys / filesys.cpp next >
Encoding:
C/C++ Source or Header  |  1994-03-03  |  17.5 KB  |  718 lines

  1.  
  2. //***************************************************************************
  3. //
  4. // Class:                    CFileSystem
  5. //
  6. // Purpose:                To encapsulate access to the file system.
  7. //
  8. // Copyright:            Copyright 1993, 1994 Scott P. Leslie, All Rights Reserved.
  9. //
  10. // Version:                Version 1.0 for MS Windows 3.1 and MSVC 1.0.
  11. //
  12. // Shareware:            This class is ShareWare.  If you use it, you should register
  13. //                                it with the author.  If you do not register it after 14 days
  14. //                                of use, you must discontinue using it.
  15. //
  16. //                                Contact the author at ScotLeslie@aol.com for more info.
  17. //
  18. // Distribution:    You may distribute this class unmodified as long as all
  19. //                                accompanying documentation and notes are also distributed.
  20. //                                Object code generated from this class (or any derivation
  21. //                                of this class) can only be distributed by registered users.
  22. //
  23. // Disclaimer:        This class is provided as is with no implied or express
  24. //                                warranties.  You should test this class for your particular
  25. //                                use on non-critical data before using it in a production
  26. //                                system.
  27. //
  28. //***************************************************************************
  29.  
  30. //***************************************************************************
  31. //    Include Files
  32. //***************************************************************************
  33.  
  34. #include "stdafx.h"
  35.  
  36. #include "filesys.h"
  37.  
  38.  
  39. //***************************************************************************
  40. //
  41. // Name:        CFileSystem
  42. //
  43. // Purpose:    Constructor.
  44. //
  45. // Notes:        None.
  46. //
  47. //***************************************************************************
  48. CFileSystem::CFileSystem()
  49. {
  50.     MaxFileNameLength = 256;
  51. } // CFileSystem
  52.  
  53.  
  54. //***************************************************************************
  55. //
  56. // Name:        ~CFileSystem
  57. //
  58. // Purpose:    Destructor.
  59. //
  60. // Notes:        None.
  61. //
  62. //***************************************************************************
  63. CFileSystem::~CFileSystem()
  64. {
  65. } // ~CFileSystem
  66.  
  67.  
  68. //***************************************************************************
  69. //
  70. // Name:        GetDirectoryEntry
  71. //
  72. // Purpose:    To return the next directory entry based on a wildcard, etc...
  73. //
  74. // Notes:        None.
  75. //
  76. //***************************************************************************
  77. CString *
  78. CFileSystem::GetDirectoryEntry(CString Wildcard, Attribute eFileAttrib)
  79. {
  80.     int nRetVal;
  81.  
  82.     // If they passed in a Wildcard, we want to lookup the first entry.
  83.     if (Wildcard != "")
  84.     {
  85.         nRetVal = _dos_findfirst((const char *) Wildcard, eFileAttrib, &m_FileInfo);
  86.     } // if
  87.     else
  88.     {
  89.         nRetVal = _dos_findnext(&m_FileInfo);
  90.     } // else
  91.  
  92.     if (nRetVal != 0)
  93.     {
  94.         // Error occurred, return NULL.
  95.         return NULL;
  96.     } // if
  97.  
  98.     // Create a string and copy the name to it.
  99.     CString *pString = new CString();
  100.     memcpy(pString->GetBufferSetLength(MaxFileNameLength), m_FileInfo.name, 13);
  101.     pString->ReleaseBuffer(-1);
  102.  
  103.     return pString;
  104. } // GetDirectoryEntry
  105.  
  106.  
  107. //***************************************************************************
  108. //
  109. // Name:        GetCurrentDirectory
  110. //
  111. // Purpose:    To get the current working directory.
  112. //
  113. // Notes:        None
  114. //
  115. //***************************************************************************
  116. CString
  117. CFileSystem::GetCurrentDirectory()
  118. {
  119.     CString String;
  120.     char *pRetVal = _getcwd(String.GetBufferSetLength(MaxFileNameLength), MaxFileNameLength);
  121.     String.ReleaseBuffer(-1);
  122.  
  123.     // If an error occured, clean up and return NULL.
  124.     if (pRetVal == 0)
  125.     {
  126.         String = "";
  127.     } // if
  128.  
  129.     return String;
  130. } // GetCurrentDirectory
  131.  
  132.  
  133. //***************************************************************************
  134. //
  135. // Name:        ChangeDirectory
  136. //
  137. // Purpose:    To change the current working directory.
  138. //
  139. // Notes:        None.
  140. //
  141. //***************************************************************************
  142. BOOL
  143. CFileSystem::ChangeDirectory(CString NewDirectory)
  144. {
  145.     int nRetVal = _chdir((const char *) NewDirectory);
  146.     if (nRetVal == -1)
  147.     {
  148.         return FALSE;
  149.     } // if
  150.  
  151.     return TRUE;
  152. } // ChangeDirectory
  153.  
  154.  
  155. //***************************************************************************
  156. //
  157. // Name:        MakeDirectory
  158. //
  159. // Purpose:    To make a directory.
  160. //
  161. // Notes:        None
  162. //
  163. //***************************************************************************
  164. BOOL
  165. CFileSystem::MakeDirectory(CString NewDirectory)
  166. {
  167. #ifdef _WINNT_
  168.     ASSERT(FALSE);    // Untested
  169.     return CreateDirectory(NewDirectory);
  170.  
  171. #else // _WINNT_
  172.  
  173.     int nRetVal = _mkdir((const char *) NewDirectory);
  174.     if (nRetVal == -1)
  175.     {
  176.         return FALSE;
  177.     } // if
  178.  
  179.     return TRUE;
  180.  
  181. #endif // _WINNT_
  182. } // MakeDirectory
  183.  
  184.  
  185. //***************************************************************************
  186. //
  187. // Name:        MakePath
  188. //
  189. // Purpose:    To make all the directories in a given path.  If any of the
  190. //                    directories exist, the creation continues with lower level
  191. //                    directories.
  192. //
  193. // Example:    MakePath("c:\\foo\\bar\\what")
  194. //
  195. // Notes:        None
  196. //
  197. //***************************************************************************
  198. BOOL
  199. CFileSystem::MakePath(CString NewDirectory)
  200. {
  201.     CString    DirName;
  202.     BOOL        bRetVal = TRUE;
  203.  
  204.     // Make sure the directory name ends in a slash
  205.     if (NewDirectory[NewDirectory.GetLength() - 1] != '\\')
  206.     {
  207.         NewDirectory = NewDirectory + '\\';
  208.     } // if
  209.  
  210.     // Create each directory in the path
  211.     UINT    nIndex = 0;
  212.     BOOL    bDone = FALSE;
  213.     while (!bDone)
  214.     {
  215.         // Extract one directory
  216.         nIndex = NewDirectory.Find('\\');
  217.         if (nIndex != -1)
  218.         {
  219.             DirName = DirName + NewDirectory.Left(nIndex);
  220.             NewDirectory = NewDirectory.Right(NewDirectory.GetLength() - nIndex - 1);
  221.  
  222.             // The first time through, we might have a drive name
  223.             if (DirName.GetLength() >= 1  &&  DirName[DirName.GetLength() - 1] != ':')
  224.             {
  225.                 bRetVal = MakeDirectory(DirName);
  226.             } // if
  227.             DirName = DirName + '\\';
  228.         } // if
  229.         else
  230.         {
  231.             // We're finished
  232.             bDone = TRUE;
  233.         } // else
  234.     } // while
  235.  
  236.     // Return the last MakeDirectory() return value.
  237.     return bRetVal;
  238. } // MakePath
  239.  
  240.  
  241. //***************************************************************************
  242. //
  243. // Name:        DeleteDirectory
  244. //
  245. // Purpose:    To delete a directory.  The directory must be empty first.
  246. //
  247. // Notes:        None
  248. //
  249. //***************************************************************************
  250. BOOL
  251. CFileSystem::DeleteDirectory(CString Directory)
  252. {
  253.     int nRetVal = _rmdir((const char *) Directory);
  254.     if (nRetVal == -1)
  255.     {
  256.         return FALSE;
  257.     } // if
  258.  
  259.     return TRUE;
  260. } // DeleteDirectory
  261.  
  262.  
  263. //***************************************************************************
  264. //
  265. // Name:        GetCurrentFileSystem
  266. //
  267. // Purpose:    To return a string containing the current file system name.
  268. //
  269. // Notes:        None
  270. //
  271. //***************************************************************************
  272. CString
  273. CFileSystem::GetCurrentFileSystem()
  274. {
  275.     unsigned int    nDrive = 0;
  276.     char                    cDrive = 'A';
  277.  
  278.     _dos_getdrive(&nDrive);
  279.  
  280.     cDrive = (char) ('A' + nDrive - 1);
  281.     CString String = cDrive;
  282.     String += ":\\";
  283.  
  284.     return String;
  285. } // GetCurrentFileSystem
  286.  
  287.  
  288. //***************************************************************************
  289. //
  290. // Name:        ChangeFileSystem
  291. //
  292. // Purpose:    To change the current file system.
  293. //
  294. // Notes:        None
  295. //
  296. //***************************************************************************
  297. BOOL
  298. CFileSystem::ChangeFileSystem(char cFileSystem)
  299. {
  300.     unsigned int nNumDrives;        // ignored return value
  301.     unsigned int nNewDrive;
  302.  
  303.     nNewDrive = toupper(cFileSystem) - 'A' + 1;
  304.  
  305.     if (nNewDrive >= 0  &&  nNewDrive <= 26)
  306.     {
  307.         _dos_setdrive(nNewDrive, &nNumDrives);
  308.         return TRUE;
  309.     } // if
  310.  
  311.     return FALSE;
  312. } // SetCurrentFileSystem
  313.  
  314.  
  315. //***************************************************************************
  316. //
  317. // Name:        GetFileSystemList
  318. //
  319. // Purpose:    To return a list of available file systems (ie. drives).
  320. //
  321. // Notes:        None
  322. //
  323. //***************************************************************************
  324. CStringList    *
  325. CFileSystem::GetFileSystemList()
  326. {
  327.     CStringList *pStringList = new CStringList();
  328.  
  329.     for (int nDriveNum = 0; nDriveNum < 26; nDriveNum++)
  330.     {
  331.         if (GetDriveType(nDriveNum) != 0)
  332.         {
  333.             CString DriveName = (char)('A' + nDriveNum);
  334.             DriveName = DriveName + ":\\";
  335.             pStringList->AddTail((const char *)DriveName);
  336.         } // if
  337.     } // for
  338.  
  339.     return pStringList;
  340. } // GetFileSystemList
  341.  
  342.  
  343. //***************************************************************************
  344. //
  345. // Name:        RenameFile
  346. //
  347. // Purpose:    To rename a file.
  348. //
  349. // Notes:        None
  350. //
  351. //***************************************************************************
  352. BOOL
  353. CFileSystem::RenameFile(CString OldFileName, CString NewFileName)
  354. {
  355.     TRY
  356.     {
  357.         CFile::Rename((const char *) OldFileName, (const char *) NewFileName);
  358.     } // TRY
  359.     CATCH(CFileException, Exception)
  360.     {
  361.         return FALSE;
  362.     } // CATCH
  363.     END_CATCH
  364.  
  365.     return TRUE;
  366. } // RenameFile
  367.  
  368.  
  369. //***************************************************************************
  370. //
  371. // Name:        DeleteFile
  372. //
  373. // Purpose:    To delete a file.
  374. //
  375. // Notes:        None
  376. //
  377. //***************************************************************************
  378. BOOL
  379. CFileSystem::DeleteFile(CString FileName)
  380. {
  381.     TRY
  382.     {
  383.         CFile::Remove((const char *) FileName);
  384.     } // TRY
  385.     CATCH(CFileException, Exception)
  386.     {
  387.         return FALSE;
  388.     } // CATCH
  389.     END_CATCH
  390.  
  391.     return TRUE;
  392. } // DeleteFile
  393.  
  394.  
  395. //***************************************************************************
  396. //
  397. // Name:        CloseFile
  398. //
  399. // Purpose:    To close a file.
  400. //
  401. // Notes:        None
  402. //
  403. //***************************************************************************
  404. BOOL
  405. CFileSystem::CloseFile(CFile *pFile) const
  406. {
  407.     BOOL bRetVal = TRUE;
  408.  
  409.     TRY
  410.     {
  411.         pFile->Close();
  412.     } // TRY
  413.     CATCH(CFileException, e)
  414.     {
  415.         bRetVal = FALSE;
  416.     } // CATCH
  417.     END_CATCH
  418.  
  419.     return bRetVal;
  420. } // CloseFile
  421.  
  422.  
  423. //***************************************************************************
  424. //
  425. // Name:        CopyFile
  426. //
  427. // Purpose:    To copy a file.
  428. //
  429. // Notes:        If the destination file exists, it will be truncated and
  430. //                    overwritten.
  431. //
  432. //***************************************************************************
  433. BOOL
  434. CFileSystem::CopyFile(CString SourceFileName, CString DestFileName, unsigned long lBuffSize)
  435. {
  436.     BOOL        bRetVal    = TRUE;
  437.     char *    pBuff        = new char[lBuffSize];
  438.     CFile Source;
  439.     CFile Dest;
  440.  
  441.     // Open the files, creating the destination.
  442.     if (Source.Open((const char *) SourceFileName, CFile::modeRead))
  443.     {
  444.         if (Dest.Open((const char *) DestFileName, CFile::modeCreate | CFile::modeWrite))
  445.         {
  446.             DWORD    dwLength = Source.GetLength();
  447.         
  448.             // Copy the data in the file.
  449.             while (dwLength > 0)
  450.             {
  451.                 UINT nRead = Source.Read(pBuff, (UINT) lBuffSize);
  452.                 if (nRead)
  453.                 {
  454.                     Dest.Write(pBuff, nRead);
  455.                 } // if
  456.             
  457.                 dwLength -= nRead;
  458.             } // while
  459.         
  460.             CloseFile(&Source);
  461.             CloseFile(&Dest);
  462.         } // if
  463.         else
  464.         {
  465.             CloseFile(&Source);
  466.  
  467.             bRetVal = FALSE;
  468.         } // else
  469.     } // if
  470.  
  471.     delete [] pBuff;
  472.  
  473.     return bRetVal;
  474. } // CopyFile
  475.  
  476.  
  477. //***************************************************************************
  478. //
  479. // Name:        GetFileName
  480. //
  481. // Purpose:    Extract a file name from a path.
  482. //
  483. // Notes:        None
  484. //
  485. //***************************************************************************
  486. CString
  487. CFileSystem::GetFileName(CString PathAndFileName)
  488. {
  489.     // Find the last "\" in the string and return everything after it.
  490.     int nIndex = PathAndFileName.Find('\\');
  491.     while(nIndex != -1)
  492.     {
  493.         PathAndFileName = PathAndFileName.Right(PathAndFileName.GetLength() - nIndex - 1);
  494.  
  495.         nIndex = PathAndFileName.Find('\\');
  496.     } // while
  497.  
  498.     return PathAndFileName;
  499. } // GetFileName
  500.  
  501.  
  502. //***************************************************************************
  503. //
  504. // Name:        GetPath
  505. //
  506. // Purpose:    To return the directory from a path.
  507. //
  508. // Example:    Input:    c:\foo\bar\what.txt
  509. //                    Output:    c:\foo\bar
  510. //
  511. // Notes:        None
  512. //
  513. //***************************************************************************
  514. CString
  515. CFileSystem::GetPath(CString PathAndFileName)
  516. {
  517.     CString    Path = "";
  518.  
  519.     // Find the last "\" in the string and return everything up to and including it.
  520.     int nIndex = PathAndFileName.Find('\\');
  521.     while(nIndex != -1)
  522.     {
  523.         Path = Path + PathAndFileName.Left(nIndex + 1);
  524.         PathAndFileName = PathAndFileName.Right(PathAndFileName.GetLength() - nIndex - 1);
  525.  
  526.         nIndex = PathAndFileName.Find('\\');
  527.     } // while
  528.  
  529.     return Path;
  530. } // GetPath
  531.  
  532.  
  533. //***************************************************************************
  534. //
  535. // Name:        GetDirectory
  536. //
  537. // Purpose:    To return a list of files based on a search string, etc...
  538. //
  539. // Notes:        None
  540. //
  541. //***************************************************************************
  542. CStringList    *
  543. CFileSystem::GetDirectory(CString SearchString, Attribute eFileAttrib, BOOL bRecurseSubDirs, CStringList *pStringList)
  544. {
  545.     // If they don't pass in a list, create one.
  546.     if (pStringList == NULL)
  547.     {
  548.         pStringList = new CStringList();
  549.     } // if
  550.  
  551.     // Read the file list
  552.     CStringList *pFileList = GetFileList(SearchString, eFileAttrib);
  553.     pStringList->AddTail(pFileList);
  554.     delete pFileList;
  555.     pFileList = NULL;
  556.  
  557.     if (bRecurseSubDirs)
  558.     {
  559.         CString                CurDir = GetPath(SearchString);
  560.         CStringList *    pDirList = GetSubdirList(CurDir);
  561.  
  562.         // Go through the directories we just got and recurse through them too.
  563.         for (POSITION pos=pDirList->GetHeadPosition(); pos != 0; )
  564.         {
  565.             CString String = pDirList->GetNext(pos);
  566.  
  567.             // Get file name part of search path
  568.             CString    SearchSpec = GetFileName(SearchString);
  569.  
  570.             // Do the recursion.
  571.             GetDirectory(String + "\\" + SearchSpec, eFileAttrib, bRecurseSubDirs, pStringList);
  572.         } // for
  573.  
  574.         delete pDirList;
  575.         pDirList = NULL;
  576.     } // if
  577.  
  578.     return pStringList;
  579. } // GetDirectory
  580.  
  581.  
  582. //***************************************************************************
  583. //
  584. // Name:        GetSubdirList
  585. //
  586. // Purpose:    To return a list of subdirectories of another directory.
  587. //
  588. // Notes:        None
  589. //
  590. //***************************************************************************
  591. CStringList *
  592. CFileSystem::GetSubdirList(CString SearchDir, BOOL bPathInName)
  593. {
  594.     // Read the directory list
  595.     CStringList *    pDirList = new CStringList();
  596.  
  597.     // We assume (for now) that the SearchDir has a "\" on the end.
  598.     SearchDir = SearchDir + "*.*";
  599.     CString *    pString = GetDirectoryEntry(SearchDir, directory);
  600.     while (pString != NULL)
  601.     {
  602.         CString    String;
  603.         CString    FullPath;
  604.         CString    CurDir = GetPath(SearchDir);
  605.         sprintf(FullPath.GetBufferSetLength(1024), "%s%s", (const char *)CurDir, (const char *)(*pString));
  606.         FullPath.ReleaseBuffer(-1);
  607.         if (bPathInName)
  608.         {
  609.             sprintf(String.GetBufferSetLength(1024), "%s%s", (const char *)CurDir, (const char *)(*pString));
  610.             String.ReleaseBuffer(-1);
  611.         } // if
  612.         else
  613.         {
  614.             sprintf(String.GetBufferSetLength(1024), "%s", (const char *)(*pString));
  615.             String.ReleaseBuffer(-1);
  616.         } // else
  617.  
  618.         // If it's not one of the special directories.
  619.         if (*pString != "."  &&  *pString != "..")
  620.         {
  621.             // Get the file type and make sure it's a directory before we add it to the list.
  622.             CFileStatus FileStatus;
  623.             CFile::GetStatus((const char *) FullPath, FileStatus);
  624.             if (FileStatus.m_attribute == directory)
  625.             {
  626.                 pDirList->AddTail((const char *) String);
  627.             } // if
  628.         } // if
  629.  
  630.         // Delete the string we got back from GetDirectoryEntry
  631.         delete pString;
  632.         pString = NULL;
  633.     
  634.         pString = GetDirectoryEntry();
  635.     } // while
  636.  
  637.     return pDirList;
  638. } // GetSubdirList
  639.  
  640.  
  641. //***************************************************************************
  642. //
  643. // Name:        GetFileList
  644. //
  645. // Purpose:    Return a list of files given a search path and attribute.
  646. //
  647. // Notes:        This only searches the specified directory.
  648. //                    Use GetDirectory() to recurse subdirectories.
  649. //
  650. // Example:    GetFileList("c:\\foo\\bar\\*.cpp", Normal)
  651. //
  652. //***************************************************************************
  653. CStringList *
  654. CFileSystem::GetFileList(CString SearchString, Attribute eFileAttrib)
  655. {
  656.     CStringList *pDirList = new CStringList();
  657.  
  658.     // Read the file list
  659.     CString        CurDir = GetPath(SearchString);
  660.     CString *    pString = GetDirectoryEntry(SearchString, eFileAttrib);
  661.     CString    String;
  662.     while (pString != NULL)
  663.     {
  664.         sprintf(String.GetBufferSetLength(1024), "%s%s", (const char *)CurDir, (const char *)(*pString));
  665.         String.ReleaseBuffer(-1);
  666.         pDirList->AddTail((const char *) String);
  667.  
  668.         // Delete the string we got back from GetDirectoryEntry
  669.         delete pString;
  670.         pString = NULL;
  671.  
  672.         pString = GetDirectoryEntry();
  673.     } // while
  674.  
  675.     return pDirList;
  676. } // GetFileList
  677.  
  678.  
  679. //***************************************************************************
  680. //
  681. // Name:        LoadListBox
  682. //
  683. // Purpose:    To load a list box with the given string list.
  684. //
  685. // Notes:        None
  686. //
  687. //***************************************************************************
  688. void
  689. CFileSystem::LoadListBox(CListBox *pListBox, CStringList * pStringList)
  690. {
  691.     for (POSITION pos=pStringList->GetHeadPosition(); pos != 0; )
  692.     {
  693.         CString& String = pStringList->GetNext(pos);
  694.         pListBox->AddString(String);
  695.     } // for
  696. } // DirLoadListBox
  697.  
  698.  
  699. //***************************************************************************
  700. //
  701. // Name:        LoadComboBox
  702. //
  703. // Purpose:    To load a combo box with the given string list.
  704. //
  705. // Notes:        None
  706. //
  707. //***************************************************************************
  708. void
  709. CFileSystem::LoadComboBox(CComboBox *pComboBox, CStringList * pStringList)
  710. {
  711.     for (POSITION pos=pStringList->GetHeadPosition(); pos != 0; )
  712.     {
  713.         CString& String = pStringList->GetNext(pos);
  714.         pComboBox->AddString(String);
  715.     } // for
  716. } // LoadComboBox
  717.  
  718.