home *** CD-ROM | disk | FTP | other *** search
-
- //***************************************************************************
- //
- // Class: CFileSystem
- //
- // Purpose: To encapsulate access to the file system.
- //
- // Copyright: Copyright 1993, 1994 Scott P. Leslie, All Rights Reserved.
- //
- // Version: Version 1.0 for MS Windows 3.1 and MSVC 1.0.
- //
- // Shareware: This class is ShareWare. If you use it, you should register
- // it with the author. If you do not register it after 14 days
- // of use, you must discontinue using it.
- //
- // Contact the author at ScotLeslie@aol.com for more info.
- //
- // Distribution: You may distribute this class unmodified as long as all
- // accompanying documentation and notes are also distributed.
- // Object code generated from this class (or any derivation
- // of this class) can only be distributed by registered users.
- //
- // Disclaimer: This class is provided as is with no implied or express
- // warranties. You should test this class for your particular
- // use on non-critical data before using it in a production
- // system.
- //
- //***************************************************************************
-
- //***************************************************************************
- // Include Files
- //***************************************************************************
-
- #include "stdafx.h"
-
- #include "filesys.h"
-
-
- //***************************************************************************
- //
- // Name: CFileSystem
- //
- // Purpose: Constructor.
- //
- // Notes: None.
- //
- //***************************************************************************
- CFileSystem::CFileSystem()
- {
- MaxFileNameLength = 256;
- } // CFileSystem
-
-
- //***************************************************************************
- //
- // Name: ~CFileSystem
- //
- // Purpose: Destructor.
- //
- // Notes: None.
- //
- //***************************************************************************
- CFileSystem::~CFileSystem()
- {
- } // ~CFileSystem
-
-
- //***************************************************************************
- //
- // Name: GetDirectoryEntry
- //
- // Purpose: To return the next directory entry based on a wildcard, etc...
- //
- // Notes: None.
- //
- //***************************************************************************
- CString *
- CFileSystem::GetDirectoryEntry(CString Wildcard, Attribute eFileAttrib)
- {
- int nRetVal;
-
- // If they passed in a Wildcard, we want to lookup the first entry.
- if (Wildcard != "")
- {
- nRetVal = _dos_findfirst((const char *) Wildcard, eFileAttrib, &m_FileInfo);
- } // if
- else
- {
- nRetVal = _dos_findnext(&m_FileInfo);
- } // else
-
- if (nRetVal != 0)
- {
- // Error occurred, return NULL.
- return NULL;
- } // if
-
- // Create a string and copy the name to it.
- CString *pString = new CString();
- memcpy(pString->GetBufferSetLength(MaxFileNameLength), m_FileInfo.name, 13);
- pString->ReleaseBuffer(-1);
-
- return pString;
- } // GetDirectoryEntry
-
-
- //***************************************************************************
- //
- // Name: GetCurrentDirectory
- //
- // Purpose: To get the current working directory.
- //
- // Notes: None
- //
- //***************************************************************************
- CString
- CFileSystem::GetCurrentDirectory()
- {
- CString String;
- char *pRetVal = _getcwd(String.GetBufferSetLength(MaxFileNameLength), MaxFileNameLength);
- String.ReleaseBuffer(-1);
-
- // If an error occured, clean up and return NULL.
- if (pRetVal == 0)
- {
- String = "";
- } // if
-
- return String;
- } // GetCurrentDirectory
-
-
- //***************************************************************************
- //
- // Name: ChangeDirectory
- //
- // Purpose: To change the current working directory.
- //
- // Notes: None.
- //
- //***************************************************************************
- BOOL
- CFileSystem::ChangeDirectory(CString NewDirectory)
- {
- int nRetVal = _chdir((const char *) NewDirectory);
- if (nRetVal == -1)
- {
- return FALSE;
- } // if
-
- return TRUE;
- } // ChangeDirectory
-
-
- //***************************************************************************
- //
- // Name: MakeDirectory
- //
- // Purpose: To make a directory.
- //
- // Notes: None
- //
- //***************************************************************************
- BOOL
- CFileSystem::MakeDirectory(CString NewDirectory)
- {
- #ifdef _WINNT_
- ASSERT(FALSE); // Untested
- return CreateDirectory(NewDirectory);
-
- #else // _WINNT_
-
- int nRetVal = _mkdir((const char *) NewDirectory);
- if (nRetVal == -1)
- {
- return FALSE;
- } // if
-
- return TRUE;
-
- #endif // _WINNT_
- } // MakeDirectory
-
-
- //***************************************************************************
- //
- // Name: MakePath
- //
- // Purpose: To make all the directories in a given path. If any of the
- // directories exist, the creation continues with lower level
- // directories.
- //
- // Example: MakePath("c:\\foo\\bar\\what")
- //
- // Notes: None
- //
- //***************************************************************************
- BOOL
- CFileSystem::MakePath(CString NewDirectory)
- {
- CString DirName;
- BOOL bRetVal = TRUE;
-
- // Make sure the directory name ends in a slash
- if (NewDirectory[NewDirectory.GetLength() - 1] != '\\')
- {
- NewDirectory = NewDirectory + '\\';
- } // if
-
- // Create each directory in the path
- UINT nIndex = 0;
- BOOL bDone = FALSE;
- while (!bDone)
- {
- // Extract one directory
- nIndex = NewDirectory.Find('\\');
- if (nIndex != -1)
- {
- DirName = DirName + NewDirectory.Left(nIndex);
- NewDirectory = NewDirectory.Right(NewDirectory.GetLength() - nIndex - 1);
-
- // The first time through, we might have a drive name
- if (DirName.GetLength() >= 1 && DirName[DirName.GetLength() - 1] != ':')
- {
- bRetVal = MakeDirectory(DirName);
- } // if
- DirName = DirName + '\\';
- } // if
- else
- {
- // We're finished
- bDone = TRUE;
- } // else
- } // while
-
- // Return the last MakeDirectory() return value.
- return bRetVal;
- } // MakePath
-
-
- //***************************************************************************
- //
- // Name: DeleteDirectory
- //
- // Purpose: To delete a directory. The directory must be empty first.
- //
- // Notes: None
- //
- //***************************************************************************
- BOOL
- CFileSystem::DeleteDirectory(CString Directory)
- {
- int nRetVal = _rmdir((const char *) Directory);
- if (nRetVal == -1)
- {
- return FALSE;
- } // if
-
- return TRUE;
- } // DeleteDirectory
-
-
- //***************************************************************************
- //
- // Name: GetCurrentFileSystem
- //
- // Purpose: To return a string containing the current file system name.
- //
- // Notes: None
- //
- //***************************************************************************
- CString
- CFileSystem::GetCurrentFileSystem()
- {
- unsigned int nDrive = 0;
- char cDrive = 'A';
-
- _dos_getdrive(&nDrive);
-
- cDrive = (char) ('A' + nDrive - 1);
- CString String = cDrive;
- String += ":\\";
-
- return String;
- } // GetCurrentFileSystem
-
-
- //***************************************************************************
- //
- // Name: ChangeFileSystem
- //
- // Purpose: To change the current file system.
- //
- // Notes: None
- //
- //***************************************************************************
- BOOL
- CFileSystem::ChangeFileSystem(char cFileSystem)
- {
- unsigned int nNumDrives; // ignored return value
- unsigned int nNewDrive;
-
- nNewDrive = toupper(cFileSystem) - 'A' + 1;
-
- if (nNewDrive >= 0 && nNewDrive <= 26)
- {
- _dos_setdrive(nNewDrive, &nNumDrives);
- return TRUE;
- } // if
-
- return FALSE;
- } // SetCurrentFileSystem
-
-
- //***************************************************************************
- //
- // Name: GetFileSystemList
- //
- // Purpose: To return a list of available file systems (ie. drives).
- //
- // Notes: None
- //
- //***************************************************************************
- CStringList *
- CFileSystem::GetFileSystemList()
- {
- CStringList *pStringList = new CStringList();
-
- for (int nDriveNum = 0; nDriveNum < 26; nDriveNum++)
- {
- if (GetDriveType(nDriveNum) != 0)
- {
- CString DriveName = (char)('A' + nDriveNum);
- DriveName = DriveName + ":\\";
- pStringList->AddTail((const char *)DriveName);
- } // if
- } // for
-
- return pStringList;
- } // GetFileSystemList
-
-
- //***************************************************************************
- //
- // Name: RenameFile
- //
- // Purpose: To rename a file.
- //
- // Notes: None
- //
- //***************************************************************************
- BOOL
- CFileSystem::RenameFile(CString OldFileName, CString NewFileName)
- {
- TRY
- {
- CFile::Rename((const char *) OldFileName, (const char *) NewFileName);
- } // TRY
- CATCH(CFileException, Exception)
- {
- return FALSE;
- } // CATCH
- END_CATCH
-
- return TRUE;
- } // RenameFile
-
-
- //***************************************************************************
- //
- // Name: DeleteFile
- //
- // Purpose: To delete a file.
- //
- // Notes: None
- //
- //***************************************************************************
- BOOL
- CFileSystem::DeleteFile(CString FileName)
- {
- TRY
- {
- CFile::Remove((const char *) FileName);
- } // TRY
- CATCH(CFileException, Exception)
- {
- return FALSE;
- } // CATCH
- END_CATCH
-
- return TRUE;
- } // DeleteFile
-
-
- //***************************************************************************
- //
- // Name: CloseFile
- //
- // Purpose: To close a file.
- //
- // Notes: None
- //
- //***************************************************************************
- BOOL
- CFileSystem::CloseFile(CFile *pFile) const
- {
- BOOL bRetVal = TRUE;
-
- TRY
- {
- pFile->Close();
- } // TRY
- CATCH(CFileException, e)
- {
- bRetVal = FALSE;
- } // CATCH
- END_CATCH
-
- return bRetVal;
- } // CloseFile
-
-
- //***************************************************************************
- //
- // Name: CopyFile
- //
- // Purpose: To copy a file.
- //
- // Notes: If the destination file exists, it will be truncated and
- // overwritten.
- //
- //***************************************************************************
- BOOL
- CFileSystem::CopyFile(CString SourceFileName, CString DestFileName, unsigned long lBuffSize)
- {
- BOOL bRetVal = TRUE;
- char * pBuff = new char[lBuffSize];
- CFile Source;
- CFile Dest;
-
- // Open the files, creating the destination.
- if (Source.Open((const char *) SourceFileName, CFile::modeRead))
- {
- if (Dest.Open((const char *) DestFileName, CFile::modeCreate | CFile::modeWrite))
- {
- DWORD dwLength = Source.GetLength();
-
- // Copy the data in the file.
- while (dwLength > 0)
- {
- UINT nRead = Source.Read(pBuff, (UINT) lBuffSize);
- if (nRead)
- {
- Dest.Write(pBuff, nRead);
- } // if
-
- dwLength -= nRead;
- } // while
-
- CloseFile(&Source);
- CloseFile(&Dest);
- } // if
- else
- {
- CloseFile(&Source);
-
- bRetVal = FALSE;
- } // else
- } // if
-
- delete [] pBuff;
-
- return bRetVal;
- } // CopyFile
-
-
- //***************************************************************************
- //
- // Name: GetFileName
- //
- // Purpose: Extract a file name from a path.
- //
- // Notes: None
- //
- //***************************************************************************
- CString
- CFileSystem::GetFileName(CString PathAndFileName)
- {
- // Find the last "\" in the string and return everything after it.
- int nIndex = PathAndFileName.Find('\\');
- while(nIndex != -1)
- {
- PathAndFileName = PathAndFileName.Right(PathAndFileName.GetLength() - nIndex - 1);
-
- nIndex = PathAndFileName.Find('\\');
- } // while
-
- return PathAndFileName;
- } // GetFileName
-
-
- //***************************************************************************
- //
- // Name: GetPath
- //
- // Purpose: To return the directory from a path.
- //
- // Example: Input: c:\foo\bar\what.txt
- // Output: c:\foo\bar
- //
- // Notes: None
- //
- //***************************************************************************
- CString
- CFileSystem::GetPath(CString PathAndFileName)
- {
- CString Path = "";
-
- // Find the last "\" in the string and return everything up to and including it.
- int nIndex = PathAndFileName.Find('\\');
- while(nIndex != -1)
- {
- Path = Path + PathAndFileName.Left(nIndex + 1);
- PathAndFileName = PathAndFileName.Right(PathAndFileName.GetLength() - nIndex - 1);
-
- nIndex = PathAndFileName.Find('\\');
- } // while
-
- return Path;
- } // GetPath
-
-
- //***************************************************************************
- //
- // Name: GetDirectory
- //
- // Purpose: To return a list of files based on a search string, etc...
- //
- // Notes: None
- //
- //***************************************************************************
- CStringList *
- CFileSystem::GetDirectory(CString SearchString, Attribute eFileAttrib, BOOL bRecurseSubDirs, CStringList *pStringList)
- {
- // If they don't pass in a list, create one.
- if (pStringList == NULL)
- {
- pStringList = new CStringList();
- } // if
-
- // Read the file list
- CStringList *pFileList = GetFileList(SearchString, eFileAttrib);
- pStringList->AddTail(pFileList);
- delete pFileList;
- pFileList = NULL;
-
- if (bRecurseSubDirs)
- {
- CString CurDir = GetPath(SearchString);
- CStringList * pDirList = GetSubdirList(CurDir);
-
- // Go through the directories we just got and recurse through them too.
- for (POSITION pos=pDirList->GetHeadPosition(); pos != 0; )
- {
- CString String = pDirList->GetNext(pos);
-
- // Get file name part of search path
- CString SearchSpec = GetFileName(SearchString);
-
- // Do the recursion.
- GetDirectory(String + "\\" + SearchSpec, eFileAttrib, bRecurseSubDirs, pStringList);
- } // for
-
- delete pDirList;
- pDirList = NULL;
- } // if
-
- return pStringList;
- } // GetDirectory
-
-
- //***************************************************************************
- //
- // Name: GetSubdirList
- //
- // Purpose: To return a list of subdirectories of another directory.
- //
- // Notes: None
- //
- //***************************************************************************
- CStringList *
- CFileSystem::GetSubdirList(CString SearchDir, BOOL bPathInName)
- {
- // Read the directory list
- CStringList * pDirList = new CStringList();
-
- // We assume (for now) that the SearchDir has a "\" on the end.
- SearchDir = SearchDir + "*.*";
- CString * pString = GetDirectoryEntry(SearchDir, directory);
- while (pString != NULL)
- {
- CString String;
- CString FullPath;
- CString CurDir = GetPath(SearchDir);
- sprintf(FullPath.GetBufferSetLength(1024), "%s%s", (const char *)CurDir, (const char *)(*pString));
- FullPath.ReleaseBuffer(-1);
- if (bPathInName)
- {
- sprintf(String.GetBufferSetLength(1024), "%s%s", (const char *)CurDir, (const char *)(*pString));
- String.ReleaseBuffer(-1);
- } // if
- else
- {
- sprintf(String.GetBufferSetLength(1024), "%s", (const char *)(*pString));
- String.ReleaseBuffer(-1);
- } // else
-
- // If it's not one of the special directories.
- if (*pString != "." && *pString != "..")
- {
- // Get the file type and make sure it's a directory before we add it to the list.
- CFileStatus FileStatus;
- CFile::GetStatus((const char *) FullPath, FileStatus);
- if (FileStatus.m_attribute == directory)
- {
- pDirList->AddTail((const char *) String);
- } // if
- } // if
-
- // Delete the string we got back from GetDirectoryEntry
- delete pString;
- pString = NULL;
-
- pString = GetDirectoryEntry();
- } // while
-
- return pDirList;
- } // GetSubdirList
-
-
- //***************************************************************************
- //
- // Name: GetFileList
- //
- // Purpose: Return a list of files given a search path and attribute.
- //
- // Notes: This only searches the specified directory.
- // Use GetDirectory() to recurse subdirectories.
- //
- // Example: GetFileList("c:\\foo\\bar\\*.cpp", Normal)
- //
- //***************************************************************************
- CStringList *
- CFileSystem::GetFileList(CString SearchString, Attribute eFileAttrib)
- {
- CStringList *pDirList = new CStringList();
-
- // Read the file list
- CString CurDir = GetPath(SearchString);
- CString * pString = GetDirectoryEntry(SearchString, eFileAttrib);
- CString String;
- while (pString != NULL)
- {
- sprintf(String.GetBufferSetLength(1024), "%s%s", (const char *)CurDir, (const char *)(*pString));
- String.ReleaseBuffer(-1);
- pDirList->AddTail((const char *) String);
-
- // Delete the string we got back from GetDirectoryEntry
- delete pString;
- pString = NULL;
-
- pString = GetDirectoryEntry();
- } // while
-
- return pDirList;
- } // GetFileList
-
-
- //***************************************************************************
- //
- // Name: LoadListBox
- //
- // Purpose: To load a list box with the given string list.
- //
- // Notes: None
- //
- //***************************************************************************
- void
- CFileSystem::LoadListBox(CListBox *pListBox, CStringList * pStringList)
- {
- for (POSITION pos=pStringList->GetHeadPosition(); pos != 0; )
- {
- CString& String = pStringList->GetNext(pos);
- pListBox->AddString(String);
- } // for
- } // DirLoadListBox
-
-
- //***************************************************************************
- //
- // Name: LoadComboBox
- //
- // Purpose: To load a combo box with the given string list.
- //
- // Notes: None
- //
- //***************************************************************************
- void
- CFileSystem::LoadComboBox(CComboBox *pComboBox, CStringList * pStringList)
- {
- for (POSITION pos=pStringList->GetHeadPosition(); pos != 0; )
- {
- CString& String = pStringList->GetNext(pos);
- pComboBox->AddString(String);
- } // for
- } // LoadComboBox
-
-