Back to index

nux  3.0.0
Public Member Functions
nux::NFileManagerGeneric Class Reference

#include <NFileManagerGeneric.h>

Inheritance diagram for nux::NFileManagerGeneric:
Inheritance graph
[legend]
Collaboration diagram for nux::NFileManagerGeneric:
Collaboration graph
[legend]

List of all members.

Public Member Functions

int Copy (const TCHAR *InDestFile, const TCHAR *InSrcFile, bool OverWriteExisting, bool OverWriteReadOnly, NFileTransferMonitor *Monitor)
bool MakeDirectory (const TCHAR *Path, bool CreateCompletePath=false)
bool DeleteDirectory (const TCHAR *Path, bool DeleteContentFirst=false)
 Delete directory.
bool Move (const TCHAR *Dest, const TCHAR *Src, bool OverWriteExisting=true, bool OverWriteReadOnly=false, NFileTransferMonitor *Monitor=NULL)
int CreateUniqueFileName (const TCHAR *Filename, const TCHAR *Extension, NString &OutputFilename, unsigned int BaseIndex=0xffffffff)
 Creates a unique file name.
bool IsDrive (const TCHAR *Path)
virtual void Init (bool Startup)
virtual NSerializerCreateFileReader (const TCHAR *Filename, DWORD ReadFlags=0, LogOutputDevice &Error=GNullDevice)=0
virtual NSerializerCreateFileWriter (const TCHAR *Filename, DWORD WriteFlags=0, LogOutputDevice &Error=GNullDevice)=0
virtual long long FileSize (const TCHAR *Filename)=0
 Return TRUE if the file exist.
virtual bool FileExist (const TCHAR *Filename)=0
virtual bool Delete (const TCHAR *Filename, bool OverWriteReadOnly=false)=0
virtual bool IsReadOnly (const TCHAR *Filename)=0
virtual bool IsDirectory (const TCHAR *DirectoryName)=0
virtual bool IsHidden (const TCHAR *Filename)=0
virtual bool GetFileAttribute (const TCHAR *Filename, bool &isDirectory, bool &IsReadOnly, bool &IsHidden, long long &Size)=0
virtual void FindFiles (std::vector< NString > &FileNames, const TCHAR *Filename, bool Files, bool Directories)=0
virtual void ListFilesInDirectory (std::vector< NString > &Result, const TCHAR *DirName)=0
virtual time_t GetFileLastModified (const TCHAR *Filename)=0
virtual double GetFileAgeSeconds (const TCHAR *Filename)=0
virtual bool SetDefaultDirectory ()=0
virtual NString GetCurrentDirectory ()=0
virtual bool GetTimeStamp (const TCHAR *Path, FileTimeStamp &Timestamp)=0

Detailed Description

Definition at line 168 of file NFileManagerGeneric.h.


Member Function Documentation

int nux::NFileManagerGeneric::Copy ( const TCHAR InDestFile,
const TCHAR InSrcFile,
bool  OverWriteExisting,
bool  OverWriteReadOnly,
NFileTransferMonitor Monitor 
) [virtual]

Implements nux::NFileManager.

Reimplemented in nux::NFileManagerGNU.

Definition at line 32 of file NFileManagerGeneric.cpp.

  {
    // Direct file copier.
    if (Monitor && !Monitor->Progress (0.0) )
    {
      return COPY_CANCELED;
    }

    int           Result           = COPY_OK;
    NString SrcFile         = InSrcFile;
    NString DestFile = InDestFile;

    NSerializer *Src = CreateFileReader (SrcFile.GetTCharPtr() );

    if (!Src)
    {
      Result = COPY_READFAIL;
    }
    else
    {
      unsigned int Size = Src->GetFileSize();
      NSerializer *Dest = CreateFileWriter (DestFile.GetTCharPtr(), (OverWriteExisting ? 0 : FILEWRITE_NOREPLACEEXISTING) | (OverWriteReadOnly ? FILEWRITE_EVENIFREADONLY : 0) );

      if (!Dest)
      {
        Result = COPY_WRITEFAIL;
      }
      else
      {
        unsigned int Percent = 0, NewPercent = 0;
        BYTE Buffer[COPYBLOCKSIZE];

        for (unsigned int Total = 0; Total < Size; Total += sizeof (Buffer) )
        {
          unsigned int Count = Min<unsigned int> (Size - Total, (unsigned int) sizeof (Buffer) );
          Src->Serialize (Buffer, Count);

          if (Src->IsError() )
          {
            Result = COPY_READFAIL;
            break;
          }

          Dest->Serialize (Buffer, Count);

          if (Dest->IsError() )
          {
            Result = COPY_WRITEFAIL;
            break;
          }

          NewPercent = Total * 100 / Size;

          if (Monitor && Percent != NewPercent && !Monitor->Progress ( (float) NewPercent / 100.f) )
          {
            Result = COPY_CANCELED;
            break;
          }

          Percent = NewPercent;
        }

        if (Result == COPY_OK)
        {
          if (!Dest->Close() )
          {
            Result = COPY_WRITEFAIL;
          }
        }

        delete Dest;

        if (Result != COPY_OK)
        {
          Delete (DestFile.GetTCharPtr() );
        }
      }

      if (Result == COPY_OK)
      {
        if (!Src->Close() )
        {
          Result = COPY_READFAIL;
        }
      }

      delete Src;
    }

    if (Monitor && Result == COPY_OK && !Monitor->Progress (1.0) )
    {
      Result = COPY_CANCELED;
    }

    return Result;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

virtual NSerializer* nux::NFileManager::CreateFileReader ( const TCHAR Filename,
DWORD  ReadFlags = 0,
LogOutputDevice Error = GNullDevice 
) [pure virtual, inherited]

Implemented in nux::NFileManagerGNU.

Here is the caller graph for this function:

virtual NSerializer* nux::NFileManager::CreateFileWriter ( const TCHAR Filename,
DWORD  WriteFlags = 0,
LogOutputDevice Error = GNullDevice 
) [pure virtual, inherited]

Implemented in nux::NFileManagerGNU.

Here is the caller graph for this function:

int nux::NFileManagerGeneric::CreateUniqueFileName ( const TCHAR Filename,
const TCHAR Extension,
NString OutputFilename,
unsigned int  BaseIndex = 0xffffffff 
) [virtual]

Creates a unique file name.

The format of the name is "DirectoryPath/BaseName####.Extension" where #### is a 4-digit number in [0, 9999]. The new name is unique and does not exist in the path directory. The function returns the value of the index created for the new file name or -1 if none could be found. The return value can be saved and passed the he next call to CreateUniqueFileName in order to speed up the search. Example usage: Create a new file name for of form DirectoryPath/Filename####.ext CreateUniqueFileName(TEXT("DirectoryPath/Filename"), TEXT("ext"), Output);

Parameters:
FilenameFilename with optional path.
ExtensionExtension.
OutputFilenameNew filename.
BaseIndexBase for index search.
Returns:
Index of the new file. -1 if the file couldn't be created The index has to be in the range [0, 9999].

Implements nux::NFileManager.

Definition at line 233 of file NFileManagerGeneric.cpp.

  {
    nuxAssert (Filename);
    nuxAssert (Extension);

    NString FullPath (Filename);
    const size_t IndexMarker = FullPath.Length();                     // Marks location of the four-digit index.
    FullPath += TEXT ("0000.");
    FullPath += Extension;

    // Iterate over indices, searching for a file that doesn't exist.
    for (DWORD i = BaseIndex + 1 ; i < 10000 ; ++i)
    {
      FullPath[IndexMarker  ] = i / 1000     + TEXT ('0');
      FullPath[IndexMarker+1] = (i / 100) % 10 + TEXT ('0');
      FullPath[IndexMarker+2] = (i / 10)  % 10 + TEXT ('0');
      FullPath[IndexMarker+3] =   i     % 10 + TEXT ('0');

      if (GFileManager.FileSize (FullPath.GetTCharPtr() ) == -1)
      {
        // The file doesn't exist; output success.
        OutputFilename = FullPath;
        return static_cast<int> (i);
      }
    }

    // Can't find an empty filename slot with index in (StartVal, 9999].
    return -1;
  }

Here is the call graph for this function:

virtual bool nux::NFileManager::Delete ( const TCHAR Filename,
bool  OverWriteReadOnly = false 
) [pure virtual, inherited]

Implemented in nux::NFileManagerGNU.

Here is the caller graph for this function:

bool nux::NFileManagerGeneric::DeleteDirectory ( const TCHAR Path,
bool  DeleteContentFirst = false 
) [virtual]

Delete directory.

Delete a Directory. If DeleteContent is true, The content of the directory is deleted before the directory itself;

Parameters:
PathPath of the directory
DeleteContentFirstDelete the content of the directory before deleting the directory itself.
Returns:
TRUE if the directory was deleted.

Implements nux::NFileManager.

Reimplemented in nux::NFileManagerGNU.

Definition at line 183 of file NFileManagerGeneric.cpp.

  {
    nuxAssert (DeleteContentFirst);
    nuxAssert (Path != NULL);

    size_t PathLength = StringLength (Path);

    if (PathLength == 0)
      return false;

    NString WildcardPath = NString (Path);

    if ( (WildcardPath[PathLength - 1] != NUX_BACKSLASH_CHAR) && (WildcardPath[PathLength - 1] != NUX_SLASH_CHAR) )
      WildcardPath += NUX_BACKSLASH_CHAR;

    WildcardPath += TEXT ("*");

    std::vector<NString> List;
    FindFiles (List, *WildcardPath, 1, 0);

    for (unsigned int i = 0; i < List.size(); i++)
    {
      if (!Delete (* (NString (Path) + NUX_BACKSLASH_CHAR + List[i]), 1) )
        return 0;
    }

    List.clear();
    FindFiles (List, *WildcardPath, 0, 1);

    for (unsigned int i = 0; i < List.size(); i++)
    {
      if (!DeleteDirectory (* (NString (Path) + NUX_BACKSLASH_CHAR + List[i]), true) )
        return 0;
    }

    List.clear();
    return DeleteDirectory (Path, false);
  }

Here is the call graph for this function:

virtual bool nux::NFileManager::FileExist ( const TCHAR Filename) [pure virtual, inherited]

Implemented in nux::NFileManagerGNU.

virtual long long nux::NFileManager::FileSize ( const TCHAR Filename) [pure virtual, inherited]

Return TRUE if the file exist.

Return TRUE if the file exist.

Parameters:
Filenamethe full path of the file to search.
Returns:
TRUE if the file exist.

Implemented in nux::NFileManagerGNU.

virtual void nux::NFileManager::FindFiles ( std::vector< NString > &  FileNames,
const TCHAR Filename,
bool  Files,
bool  Directories 
) [pure virtual, inherited]

Implemented in nux::NFileManagerGNU.

Here is the caller graph for this function:

virtual NString nux::NFileManager::GetCurrentDirectory ( ) [pure virtual, inherited]

Implemented in nux::NFileManagerGNU.

virtual double nux::NFileManager::GetFileAgeSeconds ( const TCHAR Filename) [pure virtual, inherited]

Implemented in nux::NFileManagerGNU.

virtual bool nux::NFileManager::GetFileAttribute ( const TCHAR Filename,
bool &  isDirectory,
bool &  IsReadOnly,
bool &  IsHidden,
long long &  Size 
) [pure virtual, inherited]

Implemented in nux::NFileManagerGNU.

virtual time_t nux::NFileManager::GetFileLastModified ( const TCHAR Filename) [pure virtual, inherited]

Implemented in nux::NFileManagerGNU.

virtual bool nux::NFileManager::GetTimeStamp ( const TCHAR Path,
FileTimeStamp Timestamp 
) [pure virtual, inherited]

Implemented in nux::NFileManagerGNU.

virtual void nux::NFileManager::Init ( bool  Startup) [inline, virtual, inherited]

Definition at line 107 of file NFileManagerGeneric.h.

{}
virtual bool nux::NFileManager::IsDirectory ( const TCHAR DirectoryName) [pure virtual, inherited]

Implemented in nux::NFileManagerGNU.

bool nux::NFileManagerGeneric::IsDrive ( const TCHAR Path)

Definition at line 129 of file NFileManagerGeneric.cpp.

  {
    // Does Path refer to a drive letter or UNC path?
    // A UNC is a naming convention that permits you to use a network resource,
    // such as a network server, without formally connecting to the network resource
    // with a mapped drive. A UNC path uses the following syntax:
    //      \<Server><Share>
    // The share is a drive: D:\Folder of ServerA = "\\ServerA\D\Folder"

    if (Stricmp (Path, TEXT ("") ) == 0)
      return 1;
    else if ( (ToUpperCase (Path[0]) != ToLowerCase (Path[0]) ) && (Path[1] == TEXT (':') ) && (Path[2] == 0) ) // look for "a:", "c:", "d:" ...
      return 1;
    else if (Stricmp (Path, TEXT ("\\") ) == 0) // look for "\"
      return 1;
    else if (Stricmp (Path, TEXT ("\\\\") ) == 0) // look for "\\"
      return 1;
    else if (Path[0] == TEXT ('\\') && Path[1] == TEXT ('\\') && !Strchr (Path + 2, TEXT ('\\') ) ) // look for "\\Server"
      return 1;
    else if (Path[0] == TEXT ('\\') && Path[1] == TEXT ('\\') && Strchr (Path + 2, TEXT ('\\') ) && !Strchr (Strchr (Path + 2, TEXT ('\\') ) + 1, TEXT ('\\') ) )
      // look for "\\Server\share"
      return 1;
    else
      return 0;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

virtual bool nux::NFileManager::IsHidden ( const TCHAR Filename) [pure virtual, inherited]

Implemented in nux::NFileManagerGNU.

virtual bool nux::NFileManager::IsReadOnly ( const TCHAR Filename) [pure virtual, inherited]

Implemented in nux::NFileManagerGNU.

virtual void nux::NFileManager::ListFilesInDirectory ( std::vector< NString > &  Result,
const TCHAR DirName 
) [pure virtual, inherited]

Implemented in nux::NFileManagerGNU.

bool nux::NFileManagerGeneric::MakeDirectory ( const TCHAR Path,
bool  CreateCompletePath = false 
) [virtual]

Implements nux::NFileManager.

Reimplemented in nux::NFileManagerGNU.

Definition at line 155 of file NFileManagerGeneric.cpp.

  {
    // Support code for making a directory tree.
    nuxAssert (CreateCompletePath);
    unsigned int SlashCount = 0, CreateCount = 0;

    for (TCHAR Full[256] = TEXT (""), *Ptr = Full; ; *Ptr++ = *Path++)
    {
      if ( (*Path == NUX_BACKSLASH_CHAR) || (*Path == NUX_SLASH_CHAR) || (*Path == 0) )
      {
        if ( (SlashCount++ > 0) && !IsDrive (Full) )
        {
          *Ptr = 0;

          if (MakeDirectory (Full, 0) != NUX_OK)
            return 0;

          CreateCount++;
        }
      }

      if (*Path == 0)
        break;
    }

    return CreateCount != 0;
  }

Here is the call graph for this function:

bool nux::NFileManagerGeneric::Move ( const TCHAR Dest,
const TCHAR Src,
bool  OverWriteExisting = true,
bool  OverWriteReadOnly = false,
NFileTransferMonitor Monitor = NULL 
) [virtual]

Implements nux::NFileManager.

Reimplemented in nux::NFileManagerGNU.

Definition at line 222 of file NFileManagerGeneric.cpp.

  {
    // Move file manually.
    if (Copy (Dest, Src, OverWriteExisting, OverWriteReadOnly, NULL) != COPY_OK)
      return 0;

    Delete (Src, 1);
    return 1;
  }

Here is the call graph for this function:

virtual bool nux::NFileManager::SetDefaultDirectory ( ) [pure virtual, inherited]

Implemented in nux::NFileManagerGNU.


The documentation for this class was generated from the following files: