Back to index

nux  3.0.0
Public Member Functions | Private Member Functions
nux::NFileManagerGNU Class Reference

#include <NFileManagerGNU.h>

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

List of all members.

Public Member Functions

virtual NSerializerCreateFileReader (const TCHAR *Filename, DWORD Flags, LogOutputDevice &Error=GNullDevice)
virtual NSerializerCreateFileWriter (const TCHAR *Filename, DWORD Flags, LogOutputDevice &Error=GNullDevice)
long long FileSize (const TCHAR *Filename)
bool FileExist (const TCHAR *Filename)
int Copy (const TCHAR *DestFile, const TCHAR *SrcFile, bool OverWriteExisting, bool OverWriteReadOnly, NFileTransferMonitor *Monitor)
bool Move (const TCHAR *Dest, const TCHAR *Src, bool OverWriteExisting=true, bool OverWriteReadOnly=false, NFileTransferMonitor *Monitor=NULL)
bool Delete (const TCHAR *Filename, bool OverWriteReadOnly=false)
bool IsReadOnly (const TCHAR *Filename)
bool IsDirectory (const TCHAR *DirectoryName)
bool IsHidden (const TCHAR *Filename)
bool GetFileAttribute (const TCHAR *Filename, bool &isDirectory, bool &isReadOnly, bool &isHidden, long long &Size)
bool MakeDirectory (const TCHAR *Path, bool CreateCompletePath=false)
bool DeleteDirectory (const TCHAR *Path, bool DeleteContentFirst=false)
 Delete directory.
void FindFiles (std::vector< NString > &Result, const TCHAR *Filename, bool Files, bool Directories)
void ListFilesInDirectory (std::vector< NString > &Result, const TCHAR *DirName)
double GetFileAgeSeconds (const TCHAR *Filename)
time_t GetFileLastModified (const TCHAR *Filename)
bool SetDefaultDirectory ()
NString GetCurrentDirectory ()
bool GetTimeStamp (const TCHAR *Filename, FileTimeStamp &Timestamp)
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)

Private Member Functions

 NUX_DECLARE_GLOBAL_OBJECT (NFileManagerGNU, GlobalSingletonInitializer)

Detailed Description

Definition at line 98 of file NFileManagerGNU.h.


Member Function Documentation

int nux::NFileManagerGNU::Copy ( const TCHAR DestFile,
const TCHAR SrcFile,
bool  OverWriteExisting,
bool  OverWriteReadOnly,
NFileTransferMonitor Monitor 
) [virtual]

Reimplemented from nux::NFileManagerGeneric.

Definition at line 523 of file NFileManagerGNU.cpp.

  {
    size_t nmemb;
    //int nmemb;
    FILE *ifp, *ofp;
    char buf[BUFSIZ];

    if (OverWriteExisting)
    {
      if (access (DestFile, F_OK) == 0)
      {
        //OUTLOG((FUNC, TRWRN, "file %s already exists\n", DestFile));
        return false;
      }
      else if (errno != ENOENT)
      {
        //OUTLOG((FUNC, TRERR, "access(%s, F_OK) failed\n", DestFile));
        return false;
      }
    }

    if ( (ifp = fopen (SrcFile, "r") ) == NULL)
    {
      //OUTLOG((FUNC, TRERR, "%s doesn't exist\n", SrcFile));
      return false;
    }

    if ( (ofp = fopen (DestFile, "w+") ) == NULL)
    {
      //OUTLOG((FUNC, TRERR, "can't create %s\n", DestFile));
      fclose (ifp);
      return false;
    }

    while ( (nmemb = fread (buf, 1, sizeof (buf), ifp) ) > 0)
    {
      if (fwrite (buf, 1, nmemb, ofp) != nmemb)
      {
        //OUTLOG((FUNC, TRERR, "fwrite failed\n"));
        fclose (ifp);
        fclose (ofp);
        return false;
      }
    }

    fclose (ifp);
    fclose (ofp);
    return true;
  }
NSerializer * nux::NFileManagerGNU::CreateFileReader ( const TCHAR Filename,
DWORD  Flags,
LogOutputDevice Error = GNullDevice 
) [virtual]

Implements nux::NFileManager.

Definition at line 382 of file NFileManagerGNU.cpp.

  {
    int FileDesc = open (TCHAR_TO_ANSI (Filename), O_RDONLY);

    if (FileDesc == -1)
    {
      nuxDebugMsg (TEXT ("[NFileManagerGNU::CreateFileReader] Can't create file reade for: %s"), Filename);

      if (Flags & NSerializer::OutputErrorIfFail)
      {
        nuxError (TEXT ("[NFileManagerGNU::CreateFileReader] Can't open file: %s"), Filename);
      }

      return NULL;
    }

    struct stat sb;

    if (fstat (FileDesc, &sb) != 0)
    {
      int ret = close (FileDesc);

      if (ret == -1)
      {
        Error.LogFunction (NUX_MSG_SEVERITY_NONE, TEXT ("[NFileManagerGNU::CreateFileReader] Error while closing file descriptor: %s"), Filename);
      }

      nuxDebugMsg (TEXT ("[NFileManagerGNU::CreateFileReader] Can't get file descriptor: %s"), Filename);
      Error.LogFunction (NUX_MSG_SEVERITY_NONE, TEXT ("[NFileManagerGNU::CreateFileReader] Can't get file descriptor: %s"), Filename);
      return NULL;
    }

    return new NGNUSerialFileReader (FileDesc, Error, sb.st_size);
  }
NSerializer * nux::NFileManagerGNU::CreateFileWriter ( const TCHAR Filename,
DWORD  Flags,
LogOutputDevice Error = GNullDevice 
) [virtual]

Implements nux::NFileManager.

Definition at line 417 of file NFileManagerGNU.cpp.

  {
    if (FileExist (Filename) && (Flags & NSerializer::OverWriteReadOnly) )
    {
      int ret = chmod (TCHAR_TO_ANSI (Filename), S_IRUSR | S_IWUSR);

      if (ret == -1)
      {
        Error.LogFunction (NUX_MSG_SEVERITY_NONE, TEXT ("[NFileManagerGNU::CreateFileWriter] Can't change file mode") );
      }
    }

    DWORD  ModeFlags  = 0;

    if ( (Flags & NSerializer::Read) && (Flags & NSerializer::Write) )
    {
      ModeFlags |= O_RDWR;
    }
    else if (Flags & NSerializer::Read)
    {
      ModeFlags |= O_RDONLY;
    }
    else if (Flags & NSerializer::Write)
    {
      ModeFlags |= O_WRONLY;
    }

    ModeFlags |= (Flags & NSerializer::Append) ? O_APPEND : O_TRUNC;
    ModeFlags |= (Flags & NSerializer::NoOverWrite) ? (O_CREAT | O_EXCL) /*fail if the file already exist*/ : O_CREAT /*create the file if it does not exist*/;

    int FileDesc = open (TCHAR_TO_ANSI (Filename), ModeFlags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

    if (FileDesc == -1)
    {
      if (Flags & NSerializer::OutputErrorIfFail)
      {
        nuxError (TEXT ("[NFileManagerGNU::CreateFileWriter] Failed to create file %s."), Filename);
      }

      return NULL;
    }

    long long Pos = 0;

    if (Flags & NSerializer::Append)
    {
      Pos = lseek (FileDesc, Pos, SEEK_END);

      if (Pos == -1)
      {
        Error.LogFunction (NUX_MSG_SEVERITY_NONE, TEXT ("[NGNUSerialFileReader::Seek] Seek to %i has failed."), Pos);
      }
    }

    struct stat sb;

    if (fstat (FileDesc, &sb) != 0)
    {
      int ret = close (FileDesc);

      if (ret == -1)
      {
        Error.LogFunction (NUX_MSG_SEVERITY_NONE, TEXT ("[NFileManagerGNU::CreateFileWriter] Error while closing file") );
      }

      Error.LogFunction (NUX_MSG_SEVERITY_NONE, TEXT ("[NFileManagerGNU::CreateFileWriter] Can't create file reader.") );
      return NULL;
    }

    // The third param is never used.
    return new NGNUSerialFileWriter (FileDesc, Error, 0);
  }

Here is the call graph for this function:

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

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:

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

Implements nux::NFileManager.

Definition at line 577 of file NFileManagerGNU.cpp.

  {
    if (OverWriteReadOnly)
    {
      chmod (TCHAR_TO_ANSI (Filename), S_IRUSR | S_IWUSR);
    }

    if (unlink (TCHAR_TO_ANSI (Filename) ) != 0)
    {
      nuxDebugMsg (TEXT ("[NFileManagerGNU::Delete] Error deleting file '%s'."), Filename);
      return false;
    }

    return true;
  }
bool nux::NFileManagerGNU::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.

Reimplemented from nux::NFileManagerGeneric.

Definition at line 692 of file NFileManagerGNU.cpp.

  {
//     if(DeleteContentFirst)
//     {
//         return NFileManagerGeneric::DeleteDirectory(Path, DeleteContentFirst);
//     }
//     if((::RemoveDirectory(Path) == 0) && (::GetLastError() != ERROR_FILE_NOT_FOUND))
//     {
//         nuxDebugMsg(TEXT("[NFileManagerWindows::DeleteDirectory] Error deleting directory '%s' (GetLastError: %d)"), Path, ::GetLastError());
//         return false;
//     }
    return true;
  }
bool nux::NFileManagerGNU::FileExist ( const TCHAR Filename) [virtual]

Implements nux::NFileManager.

Definition at line 511 of file NFileManagerGNU.cpp.

  {
    struct stat sb;

    if (stat (TCHAR_TO_ANSI (Filename), &sb) != 0)
    {
      return false;
    }

    return true;
  }

Here is the caller graph for this function:

long long nux::NFileManagerGNU::FileSize ( const TCHAR Filename) [virtual]
Returns:
Size of the File. Return -1 if an error occurs.

Implements nux::NFileManager.

Definition at line 492 of file NFileManagerGNU.cpp.

  {
    struct stat sb;

    if (stat (TCHAR_TO_ANSI (Filename), &sb) != 0)
    {
      nuxDebugMsg (TEXT ("[NFileManagerGNU::FileSize] Can't get file size") );
      return 0;
    }

    if (sb.st_mode & S_IFDIR)
    {
      // This is a directory
      return 0;
    }

    return sb.st_size;
  }
void nux::NFileManagerGNU::FindFiles ( std::vector< NString > &  Result,
const TCHAR Filename,
bool  Files,
bool  Directories 
) [inline, virtual]

Implements nux::NFileManager.

Definition at line 130 of file NFileManagerGNU.h.

{};

Implements nux::NFileManager.

Definition at line 144 of file NFileManagerGNU.h.

    {
      return NString();
    };
double nux::NFileManagerGNU::GetFileAgeSeconds ( const TCHAR Filename) [inline, virtual]

Implements nux::NFileManager.

Definition at line 132 of file NFileManagerGNU.h.

    {
      return 0;
    };
bool nux::NFileManagerGNU::GetFileAttribute ( const TCHAR Filename,
bool &  isDirectory,
bool &  isReadOnly,
bool &  isHidden,
long long &  Size 
) [virtual]
Returns:
TRUE is the file exist.

Implements nux::NFileManager.

Definition at line 637 of file NFileManagerGNU.cpp.

  {
    isDirectory = false;
    isReadOnly = false;
    isHidden = false;
    Size = 0;

    struct stat sb;

    if (stat (TCHAR_TO_ANSI (Filename), &sb) != 0)
    {
      return false;
    }

    if (sb.st_mode & S_IFDIR)
    {
      isDirectory = true;
    }

    if ( (sb.st_mode & S_IRUSR) && ! (sb.st_mode & S_IWUSR) )
    {
      isReadOnly = true;
    }

    Size = sb.st_mode;
    return true;
  }
time_t nux::NFileManagerGNU::GetFileLastModified ( const TCHAR Filename) [inline, virtual]

Implements nux::NFileManager.

Definition at line 136 of file NFileManagerGNU.h.

    {
      return 0;
    };
bool nux::NFileManagerGNU::GetTimeStamp ( const TCHAR Filename,
FileTimeStamp Timestamp 
) [inline, virtual]

Implements nux::NFileManager.

Definition at line 148 of file NFileManagerGNU.h.

    {
      return false;
    };
virtual void nux::NFileManager::Init ( bool  Startup) [inline, virtual, inherited]

Definition at line 107 of file NFileManagerGeneric.h.

{}
bool nux::NFileManagerGNU::IsDirectory ( const TCHAR DirectoryName) [virtual]

Implements nux::NFileManager.

Definition at line 611 of file NFileManagerGNU.cpp.

  {
    struct stat sb;

    if (stat (TCHAR_TO_ANSI (DirectoryName), &sb) != 0)
    {
      nuxDebugMsg (TEXT ("[NFileManagerGNU::IsDirectory] Error reading file status '%s'."), DirectoryName);
      return false;
    }

    if (sb.st_mode & S_IFDIR)
    {
      return true;
    }

    return false;
  }
bool nux::NFileManagerGeneric::IsDrive ( const TCHAR Path) [inherited]

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:

bool nux::NFileManagerGNU::IsHidden ( const TCHAR Filename) [virtual]

Implements nux::NFileManager.

Definition at line 629 of file NFileManagerGNU.cpp.

  {
    return false;
  }
bool nux::NFileManagerGNU::IsReadOnly ( const TCHAR Filename) [virtual]

Implements nux::NFileManager.

Definition at line 593 of file NFileManagerGNU.cpp.

  {
    struct stat sb;

    if (stat (TCHAR_TO_ANSI (Filename), &sb) != 0)
    {
      nuxDebugMsg (TEXT ("[NFileManagerGNU::IsReadOnly] Error reading file status '%s'."), Filename);
      return false;
    }

    if ( (sb.st_mode & S_IRUSR) && ! (sb.st_mode & S_IWUSR) )
    {
      return true;
    }

    return false;
  }
void nux::NFileManagerGNU::ListFilesInDirectory ( std::vector< NString > &  Result,
const TCHAR DirName 
) [inline, virtual]

Implements nux::NFileManager.

Definition at line 131 of file NFileManagerGNU.h.

{};
bool nux::NFileManagerGNU::MakeDirectory ( const TCHAR Path,
bool  CreateCompletePath = false 
) [virtual]

Reimplemented from nux::NFileManagerGeneric.

Definition at line 671 of file NFileManagerGNU.cpp.

  {
    if (CreateCompletePath)
    {
      return NFileManagerGeneric::MakeDirectory (Path, CreateCompletePath);
    }

    mkdir (TCHAR_TO_ANSI (Path),
           S_IRUSR | S_IWUSR | S_IXUSR |
           S_IRGRP | S_IWGRP | S_IXGRP |
           S_IROTH | S_IWOTH | S_IXOTH);

    // EEXIST = -2147418092 = 0x80010014
//     if((errno != 0) && (errno != EEXIST))
//     {
//         nuxDebugMsg(TEXT("[NFileManagerGNU::MakeDirectory] Error creating directory '%s'."), Path);
//         return NUX_ERROR;
//     }
    return NUX_OK;
  }
bool nux::NFileManagerGNU::Move ( const TCHAR Dest,
const TCHAR Src,
bool  OverWriteExisting = true,
bool  OverWriteReadOnly = false,
NFileTransferMonitor Monitor = NULL 
) [virtual]

Reimplemented from nux::NFileManagerGeneric.

Definition at line 665 of file NFileManagerGNU.cpp.

  {
    nuxAssert (0);
    return false;
  }
bool nux::NFileManagerGNU::SetDefaultDirectory ( ) [inline, virtual]

Implements nux::NFileManager.

Definition at line 140 of file NFileManagerGNU.h.

    {
      return false;
    };

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