FileUtil Class Reference

This class contains a library of static functions for use on Windows platforms. The functions perform general file manipulation operations, rather than single file data IO. If you want the size of a file, to locate a particular file on disc etc, then here's where you'll find all those useful routines to do it. More...

#include <fileutil.h>

List of all members.

Static Public Member Functions

static BOOL FindResourceFile (TCHAR *SearchFile, TCHAR *FoundFile)
 The function searches for a matching file in the following directories in the following sequence: 1. The directory from which the application loaded. 2. The current directory. 3. Windows 95: The Windows system directory. Windows NT: The 32-bit Windows system directory. 4. Windows NT only: The 16-bit Windows system directory. 5. The Windows directory. 6. The directories that are listed in the PATH environment variable.
static BOOL StartFindingFiles (String_256 *FileSpecifier)
 Scans a given directory for files matching a particular name.
static BOOL FindNextFile (String_256 *FoundFile)
 Finds the next file, if any. See StartFindingFiles for example search code.
static void StopFindingFiles (void)
 Stops a directory scan.
static BOOL GetLongFileName (LPTSTR lpszPath, LPTSTR lpszOutput, size_t cchMaxOutLen)
 Given a full 8.3 pathname, extract the full filename.
static UINT32 GetTemporaryFileName (const TCHAR *PathName, const TCHAR *Prefix, UINT32 Unique, TCHAR *FileName)
 Creates a unique file name to be used as temporary file. The format of the file name is path.TMP, where 'path' is specified by PathName, 'pre' are the first three characters pointed by Prefix, 'uuuu' is the hexadecimal Unique parameter. Pass 0 for Unique to be certain the temp file name is really unique.
static DWORD GetTemporaryPath (UINT32 BufferLength, TCHAR *Buffer)
 Gets the temporary directory.
static PathName GetTemporaryPathName ()
 Simplified interface to GetTempFileName().
static BOOL GetTemporaryPathName (const TCHAR *pExt, PathName *pTempPath)
 Simplified interface to GetTempFileName(). Call this function when you want to create a new temporary file with a particular extension.
static BOOL DeleteFile (PathName *FileToRemove)
 Removes a file.
static BOOL GetCurrentDirectory (String_256 *pCurrentPath)
 Set up the operating system current directory.
static BOOL SetCurrentDirectory (const PathName &NewCurrentPath)
 Set up the operating system current directory.
static BOOL SetCurrentDirectory (const String_256 &NewCurrentPath)
 Set up the operating system current directory.
static BOOL RecursiveCreateDirectory (const String_256 &strDirPath)
 Recursivly create directory path.
static DWORD FindFileAttributes (LPCTSTR lpFileName)

Static Private Attributes

static BOOL SearchActive = FALSE
static bool s_fStarted = false
static String_256 SearchPath = TEXT("")
static wxDir s_dirSearch


Detailed Description

This class contains a library of static functions for use on Windows platforms. The functions perform general file manipulation operations, rather than single file data IO. If you want the size of a file, to locate a particular file on disc etc, then here's where you'll find all those useful routines to do it.

Author:
Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com>
Date:
24/07/96
See also:

Definition at line 123 of file fileutil.h.


Member Function Documentation

BOOL FileUtil::DeleteFile PathName FileToRemove  )  [static]
 

Removes a file.

Author:
Stefan_Stoykov (Xara Group Ltd) <camelotdev@xara.com> (based on code by Neville Humphrys)
Date:
11/4/97
Parameters:
FileToRemove - the path name of the file to be removed. [INPUTS]
Returns:
TRUE if succesful, FALSE otherwise

Definition at line 573 of file fileutil.cpp.

00574 {
00575     if (FileToRemove == NULL)
00576         return FALSE;
00577     
00578     BOOL Exists = TRUE;
00579     BOOL status = TRUE;
00580     
00581     // check if the file exists
00582     Exists = SGLibOil::FileExists(FileToRemove);
00583 
00584     // remove it
00585     if (Exists)
00586         status = wxRemoveFile( FileToRemove->GetPath() );
00587 
00588     return !status;
00589 }

static DWORD FileUtil::FindFileAttributes LPCTSTR  lpFileName  )  [inline, static]
 

Definition at line 150 of file fileutil.h.

00151     {
00152         TRACE( wxT("Warning - FileUtil::FindFileAttributes called\n") );
00153 #if defined(__WXMSW__)
00154         return GetFileAttributes( lpFileName );
00155 #else
00156         return 0;
00157 #endif
00158         }

BOOL FileUtil::FindNextFile String_256 FoundFile  )  [static]
 

Finds the next file, if any. See StartFindingFiles for example search code.

Author:
Jason_Williams (Xara Group Ltd) <camelotdev@xara.com>
Date:
12/9/96
Parameters:
If return value is TRUE, FoundFile will be filled in with [OUTPUTS] the LEAF filename ("bob.xar") of a found file - else, it will be set to an empty string.
(I was going to return a PathName - it seemed logical - but.... An attempt to set the location/path ("c:\directory") followed by the leaf-name ("bob.xar") resulted in a PathName of "bob.xar.xar" as opposed to "c:\directory\bob.xar".

Returns:
TRUE if it found a(nother) file matching the filespec. FALSE if there are no (more) matching files
Notes: ** Note that this will not return system/hidden files or directories **

See also:
FileUtil::StartFindingFiles; FileUtil::StopFindingFiles

Definition at line 268 of file fileutil.cpp.

00269 {
00270     ERROR3IF(FoundFile == NULL, "Illegal NULL param");
00271 
00272     if (!SearchActive)
00273     {
00274         ERROR3("FindNextFile called when StartFindingFiles not called/failed");
00275         return(FALSE);
00276     }
00277 
00278     BOOL result = TRUE;
00279 
00280     while (result)
00281     {
00282         wxString    strFileName;
00283         
00284         if( !s_fStarted )
00285         {
00286             // find first
00287             PathName    path( SearchPath );
00288             result = wxDir::Exists( path.GetLocation() );
00289             result = result && s_dirSearch.Open( path.GetLocation() );
00290             result = result && s_dirSearch.GetFirst( &strFileName, (PCTSTR)path.GetFileName(), wxDIR_FILES );
00291             s_fStarted = result;
00292         }
00293         else
00294         {
00295             // find next
00296             result = s_dirSearch.GetNext( &strFileName );
00297         }
00298 
00299         if (result)
00300         {
00301 PORTNOTE("other", "This is part of initial GetFirst" )
00302 #if !defined(EXCLUDE_FROM_XARALX)
00303             // Only return "files" which are not system or hidden, and which aren't directories!
00304             const DWORD AttrMask = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
00305 
00306             if ((SearchData.dwFileAttributes & AttrMask) == 0)
00307 #endif
00308             {
00309                 // OK, it's a safe file to return, so return it
00310                 *FoundFile = (PCTSTR)strFileName;
00311                 return(TRUE);
00312             }
00313         }
00314     }
00315 
00316     *FoundFile = TEXT("");
00317     return(FALSE);
00318 }

BOOL FileUtil::FindResourceFile TCHAR SearchFile,
TCHAR FoundFile
[static]
 

The function searches for a matching file in the following directories in the following sequence: 1. The directory from which the application loaded. 2. The current directory. 3. Windows 95: The Windows system directory. Windows NT: The 32-bit Windows system directory. 4. Windows NT only: The 16-bit Windows system directory. 5. The Windows directory. 6. The directories that are listed in the PATH environment variable.

Author:
Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com>
Date:
8/5/96
Parameters:
SearchFile = the name of the file to search for [INPUTS]
FoundFile = the pathname of the first file to match SearchFile. [OUTPUTS]
Returns:
TRUE if the file has been found FALSE if not

Definition at line 142 of file fileutil.cpp.

00143 {
00144     // Try to find the printer.cms file
00145     BOOL Found = FALSE;
00146     
00147     #if WIN16
00148 
00149         // 16 bit Windows - look in the PATH variable and current
00150         // directory using _searchenv.
00151         _tsearchenv((char*)SearchFile, "PATH", (char*)FoundFile);
00152         Found = (FoundFile[0] != '\0');
00153 
00154     #else
00155         
00156         // 32-bit Windows - use Win32 API SearchPath, which will look in all kinds 
00157         // of places, and hopefully should find the 16-bit Windows directory if it's
00158         // on the user's path, and use the ini file in there.
00159         LPTSTR FirstChar;
00160         DWORD BytesRead = ::SearchPath(NULL,          // Use default search paths (see help file)
00161                                      SearchFile, 
00162                                      NULL,          // Filename already has an extension
00163                                      _MAX_PATH - 1, // Destination buffer size
00164                                      FoundFile,     // Destination buffer
00165                                      &FirstChar);
00166         
00167         Found = (BytesRead > 0) && (BytesRead < _MAX_PATH);
00168 
00169     #endif
00170 
00171     return Found;
00172 }

BOOL FileUtil::GetCurrentDirectory String_256 pCurrentPath  )  [static]
 

Set up the operating system current directory.

Author:
Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
Date:
31/10/97
Parameters:
NewCurrentPath - the new current path to be used. [INPUTS]
Returns:
TRUE if succesful, FALSE otherwise

Definition at line 645 of file fileutil.cpp.

00646 {
00647     ERROR2IF(pCurrentPath == NULL,FALSE,"FileUtil::GetCurrentDirectory Bad params");
00648 
00649     // Call the OS to get the new current directory
00650     // DWORD nBufferLength  size, in characters, of directory buffer 
00651     // LPTSTR lpBuffer      address of buffer for current directory 
00652     // If the function succeeds, the return value specifies the number of characters
00653     // written to the buffer, not including the terminating null character. 
00654     // If the function fails, the return value is zero.
00655     const INT32 MaxSize = 256;
00656     TCHAR Buffer[MaxSize];
00657     DWORD chars = ::GetCurrentDirectory( MaxSize, Buffer);
00658     if (chars > 0)
00659     {
00660         *pCurrentPath = Buffer;
00661     }
00662     else
00663         pCurrentPath->Empty();
00664 
00665     return (chars > 0);
00666 }

BOOL FileUtil::GetLongFileName LPTSTR  lpszPath,
LPTSTR  lpszOutput,
size_t  cchMaxOutLen
[static]
 

Given a full 8.3 pathname, extract the full filename.

Author:
Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
Date:
7/8/96
Parameters:
lpszPath the full pathname of the file (short, or long) [INPUTS] cchMaxLen the size of the output buffer (if not big enough, we WILL fail and return FALSE)
lpszOutput contains the full, long, filename of the file [OUTPUTS]
Returns:
TRUE if successful, FALSE otherwise.

Errors: -

See also:
MakeShortPath (which is currently in helpuser.cpp for some bizarre reason...)

Definition at line 371 of file fileutil.cpp.

00372 {
00373     if(lpszPath == NULL || lpszOutput == NULL || cchMaxOutLen < 1)
00374     {
00375         ERROR3("FileUtil::GetLongFileName given dodgy params - come on, play the game...");
00376         return FALSE;
00377     }
00378 
00379     WIN32_FIND_DATA FileData;
00380 
00381     // Perform a search for the specified file
00382     HANDLE hSearch = ::FindFirstFile(lpszPath, &FileData);
00383     if (hSearch == INVALID_HANDLE_VALUE)
00384     {
00385         //ERROR3_PF(("FileUtil::GetLongFileName Corresponding long filename version of %s not found", lpszPath));
00386         return FALSE;
00387     }
00388 
00389     // The find_data structure will hopefully now contain the long filename for the file
00390     if(camStrlen(FileData.cFileName) < (INT32)cchMaxOutLen)
00391     {
00392         camStrcpy(lpszOutput, FileData.cFileName);
00393         return TRUE;
00394     }
00395 
00396     return FALSE;
00397 }

UINT32 FileUtil::GetTemporaryFileName const TCHAR PathName,
const TCHAR Prefix,
UINT32  Unique,
TCHAR FileName
[static]
 

Creates a unique file name to be used as temporary file. The format of the file name is path.TMP, where 'path' is specified by PathName, 'pre' are the first three characters pointed by Prefix, 'uuuu' is the hexadecimal Unique parameter. Pass 0 for Unique to be certain the temp file name is really unique.

Author:
Stefan_Stoykov (Xara Group Ltd) <camelotdev@xara.com>
Date:
2/5/97
Parameters:
pPathName address of directory name for temporary file [INPUTS] Prefix address of filename prefix Unique number used to create temporary filename
FileName address of buffer that receives the new filename [OUTPUTS]
Returns:
The unique numeric value used in the temporary filenamem, or 0 if unsuccessful.

Errors: -

See also:
FileUtil::GetTemporaryDir

Definition at line 515 of file fileutil.cpp.

00516 {
00517     if(PathName == NULL || Prefix == NULL || FileName == NULL)
00518     {
00519         ERROR3("FileUtil::GetTempFileName given dodgy params - come on, play the game...");
00520         return FALSE;
00521     }
00522 
00523     wxString            str( PathName );
00524 //  str += Prefix;
00525 
00526     str = wxFileName::CreateTempFileName( str );
00527     camStrcpy( FileName, str.c_str() );
00528     return Unique != 0 ? Unique : 0xD7334;  // Random non-zero value
00529 }

DWORD FileUtil::GetTemporaryPath UINT32  BufferLength,
TCHAR Buffer
[static]
 

Gets the temporary directory.

Author:
Stefan_Stoykov (Xara Group Ltd) <camelotdev@xara.com>
Date:
2/5/97
Parameters:
BufferLength the lenght of the buffer addressed by the second parameter [INPUTS]
Buffer points to a character buffer, to receive the directory name [OUTPUTS]
Returns:
The actual length of the returned string

Errors: -

See also:
FileUtil::GetTemporaryFileName

Definition at line 547 of file fileutil.cpp.

00548 {
00549     if(Buffer == NULL || BufferLength == 0)
00550     {
00551         ERROR3("FileUtil::GetTempPath given dodgy params - come on, play the game...");
00552         return FALSE;
00553     }
00554 
00555     return ::GetTempPath(BufferLength, Buffer);
00556 }

BOOL FileUtil::GetTemporaryPathName const TCHAR pExt,
PathName pTempPath
[static]
 

Simplified interface to GetTempFileName(). Call this function when you want to create a new temporary file with a particular extension.

Author:
Stefan_Stoykov (Xara Group Ltd) <camelotdev@xara.com>
Date:
30/6/97
Parameters:
pExt - pointer to the extension for the new file [INPUTS]
pTempPath - a path for a new temporary file [OUTPUTS]
Returns:
TRUE, if successful, FALSE otherwise

Errors: -

See also:
- GetTemporaryPathName()

Definition at line 460 of file fileutil.cpp.

00461 {
00462     ERROR3IF((pTempPath == NULL) || (pExt == NULL), "NULL param passed to FileUtil::GetTemporaryPathName");
00463     if ((pTempPath == NULL) || (pExt == NULL))
00464         return FALSE;
00465 
00466     // create a new temp file 
00467     *pTempPath = FileUtil::GetTemporaryPathName();
00468 
00469     // if tmp extension is asked - call the relevant function
00470     if (camStricmp(pExt, (const TCHAR *)"tmp") == 0)
00471         return TRUE;
00472 
00473     // remember the current name
00474     String_256 pInFile = pTempPath->GetPath();
00475 
00476     // change the extension
00477     pTempPath->SetType(pExt);
00478 
00479     // check if the file exists
00480     if (SGLibOil::FileExists(pTempPath))
00481     {
00482         // make a recursive call
00483         BOOL ok = GetTemporaryPathName(pExt, pTempPath);
00484 
00485         // remove our .tmp file
00486         wxRemoveFile( (PCTSTR)pInFile );
00487 
00488         return ok;
00489     }
00490 
00491     // try to rename the file
00492     wxString            path = (PCTSTR)pTempPath->GetPath();
00493     return wxRenameFile( (PCTSTR)pInFile, path );
00494 }

PathName FileUtil::GetTemporaryPathName  )  [static]
 

Simplified interface to GetTempFileName().

Author:
Graham_Walmsley (Xara Group Ltd) <camelotdev@xara.com>
Date:
2/5/97
Parameters:
- [INPUTS]
- [OUTPUTS]
Returns:
A path to a new temporary file
Call this function when you want to create a new temporary file and want a pathname for it.

The pathname that this function returns will be unique (i.e. will not point to an existing file), will begin with 'xar' and will be in the system's temporary directory.

Returns:
Errors: -
See also:
-

Definition at line 423 of file fileutil.cpp.

00424 {
00425     //First get the directory in which to put our temporary files
00426     TCHAR pcPath[MAX_PATH] = _T("");
00427 
00428 PORTNOTE("other", "This call is not needed, wxFileName::CreateTempFileName does the right thing" )
00429 //  FileUtil::GetTemporaryPath(MAX_PATH, pcPath);
00430 
00431     //Now get a temporary file name
00432     TCHAR pcFileName[MAX_PATH];
00433 
00434     FileUtil::GetTemporaryFileName(pcPath, _T("xar"), 0, pcFileName);
00435 
00436     //And convert the file name into a PathName
00437     String_256 strFileName=pcFileName;
00438     PathName pthFileName(strFileName);
00439 
00440     //And return it
00441     return pthFileName;
00442 
00443 }

BOOL FileUtil::RecursiveCreateDirectory const String_256 strDirPath  )  [static]
 

Recursivly create directory path.

Author:
Luke_Hart (Xara Group Ltd) <lukeh@xara.com>
Date:
18/07/2006
Parameters:
strDirPath - the path to verify and create [INPUTS]
Returns:
TRUE if succesful, FALSE otherwise

Definition at line 604 of file fileutil.cpp.

00605 {
00606     // Go down path making sure each directory exists (and creating if needed), we
00607     // start from the second character becuase we don't want to deal with '/'
00608     UINT32              ord = 1;
00609     while( _T('\0') != strDirPath[ord] )
00610     {
00611         if( _T('/') == strDirPath[ord] )
00612         {
00613             String_256  strPath;
00614             strDirPath.Left( &strPath, ord );
00615             if( !wxDir::Exists( (PCTSTR)strPath ) )
00616                 wxMkdir( (PCTSTR)strPath ); 
00617         }
00618 
00619         ++ord;
00620     }
00621 
00622     // Make sure the path as a whole exists
00623     if( !wxDir::Exists( (PCTSTR)strDirPath ) )
00624         wxMkdir( (PCTSTR)strDirPath );
00625 
00626     return TRUE;
00627 }

BOOL FileUtil::SetCurrentDirectory const String_256 NewCurrentPath  )  [static]
 

Set up the operating system current directory.

Author:
Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
Date:
31/10/97
Parameters:
NewCurrentPath - the new current path to be used. [INPUTS]
Returns:
TRUE if succesful, FALSE otherwise

Definition at line 701 of file fileutil.cpp.

00702 {
00703     // Call the OS to set the new current directory
00704     String_256 CurrentPath = NewCurrentPath;
00705     BOOL ok = ::SetCurrentDirectory( (TCHAR *)CurrentPath );
00706     return ok;
00707 }

BOOL FileUtil::SetCurrentDirectory const PathName NewCurrentPath  )  [static]
 

Set up the operating system current directory.

Author:
Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
Date:
31/10/97
Parameters:
NewCurrentPath - the new current path to be used. [INPUTS]
Returns:
TRUE if succesful, FALSE otherwise

Definition at line 680 of file fileutil.cpp.

00681 {
00682     String_256 CurrentPath = NewCurrentPath.GetPath();
00683 
00684     // Call the OS to set the new current directory
00685     BOOL ok = ::SetCurrentDirectory( (TCHAR *)CurrentPath );
00686     return ok;
00687 }

BOOL FileUtil::StartFindingFiles String_256 FileSpecifier  )  [static]
 

Scans a given directory for files matching a particular name.

Author:
Jason_Williams (Xara Group Ltd) <camelotdev@xara.com>
Date:
12/9/96
Parameters:
FileSpecifier - A full pathname specifying the file(s) to search for. [INPUTS] You may include ? and * wildcards
For example, c:.xar one specific file c:\*.txt all .txt files c:\*.* all files

Returns:
TRUE if the scan was successfully started, FALSE otherwise
To find files, do something like the following

PathName SearchPath("c:\\Directory\\*.*") if (FileUtil::StartFindingFiles(&SearchPath)) { String_256 LeafName; while (TRUE) { if (!FileUtilFindNextFile(&LeafName)) break;

Do something with file "LeafName" String_256 FullPathName = "c:\\Directory\"; FullPathName += LeafName; TRACE( _T("Full file path name is %s\n"), (TCHAR *)FullPathName); } FileUtil::StopFindingFiles(); }

Notes: This search is non-"re-entrant". Between calls to Start & Stop, any calls to start another search will give an Error3 and return failure.

See also:
FileUtil::FindNextFile; FileUtil::StopFindingFiles

Definition at line 221 of file fileutil.cpp.

00222 {
00223     ERROR3IF(FileSpecifier == NULL, "Duff FileSpecifier param");
00224 
00225     if (SearchActive)
00226     {
00227         ERROR3("StartFindingFiles called while already finding files! Call returns failure");
00228         return(FALSE);
00229     }
00230 
00231     SearchActive = TRUE;
00232     s_fStarted   = false;
00233     SearchPath = *FileSpecifier;
00234 
00235     return(TRUE);
00236 }

void FileUtil::StopFindingFiles void   )  [static]
 

Stops a directory scan.

Author:
Jason_Williams (Xara Group Ltd) <camelotdev@xara.com>
Date:
12/9/96
To find files, do something like the following See StartFindingFiles for example search code

Notes: This code is non-re-entrant. Between calls to Start & Stop, any calls to start another search will give an Error3 and return failure.

DO NOT CALL THIS FUNCTION if StartFindingFiles failed. Call this function exactly once for each call to StartFindingFiles.

See also:
FileUtil::StartFindingFiles; FileUtil::FindNextFile

Definition at line 344 of file fileutil.cpp.

00345 {
00346     ERROR3IF(!SearchActive, "StopFindingFiles called when StartFindingFiles not called/failed");
00347 
00348     s_fStarted   = false;
00349     SearchActive = FALSE;
00350 }


Member Data Documentation

wxDir FileUtil::s_dirSearch [static, private]
 

Definition at line 164 of file fileutil.h.

bool FileUtil::s_fStarted = false [static, private]
 

Definition at line 162 of file fileutil.h.

BOOL FileUtil::SearchActive = FALSE [static, private]
 

Definition at line 161 of file fileutil.h.

String_256 FileUtil::SearchPath = TEXT("") [static, private]
 

Definition at line 163 of file fileutil.h.


The documentation for this class was generated from the following files:
Generated on Sat Nov 10 03:54:11 2007 for Camelot by  doxygen 1.4.4