FileInfo Class Reference

#include <Pt/System/FileInfo.h>

Provides information about a node in the file-system. More...

Public Types

enum  Type {
  Invalid = 0,
  Directory = 1,
  File = 2
}
 File-node type. More...
 

Public Member Functions

 FileInfo ()
 Default constructor.
 
 FileInfo (const Path &path)
 Constructs from the path.
 
 FileInfo (const Pt::String &path)
 Constructs from the path.
 
 FileInfo (const char *path)
 Constructs from the path.
 
 ~FileInfo ()
 Destructor.
 
void clear ()
 Clears the state.
 
const Pathpath () const
 Returns the full path of node in the file-system. More...
 
Pt::uint64_t size () const
 Returns the file size.
 
Type type () const
 Returns the file type.
 

Static Public Member Functions

static void createDirectories (const Path &path)
 Creates a new directory.
 
static void createDirectory (const Path &path)
 Creates a new directory.
 
static void createFile (const Path &path)
 Creates a new file.
 
static bool exists (const Path &path)
 Returns true if a file or directory exists at the path.
 
static DateTime lastModified (const Path &path)
 Returns the time when last modified.
 
static void move (const Path &path, const Path &to)
 Moves a file or directory.
 
static void remove (const Path &path)
 Removes a file or directory.
 
static void removeAll (const Pt::System::Path &path)
 Removes all content in a directory.
 
static void resize (const Path &path, Pt::uint64_t n)
 Resizes a file.
 
static Pt::uint64_t size (const Path &path)
 Returns the size of the file in bytes.
 
static Type type (const Path &path)
 Returns the type of file at the path.
 

Related Functions

bool operator!= (const FileInfo &a, const FileInfo &b)
 Compare two FileInfo objects.
 
bool operator< (const FileInfo &a, const FileInfo &b)
 Compare two FileInfo objects.
 
bool operator== (const FileInfo &a, const FileInfo &b)
 Compare two FileInfo objects.
 

Detailed Description

The Pt::System::FileInfo class provides operations to query information about files and directories in the file system and to add, remove and modify them. FileInfo objects can be created with a path, are assignable, comparable and can be used as keys for e.g. std::map. The path needs not to refer to existing items in the file system, when a FileInfo object is constructed. It can be checked whether a file exists and what type of file it is, as shown in the following example:

Pt::System::Path path("/tmp/logout.txt");
if( fi.type() == Pt::System::FileInfo::File )
{
std::cout << fi.path().toLocal() << ": " << fi.size() << " bytes.\n";
}
else
{
std::cout << fi.path().toLocal() << " is invalid.\n";
}

Most operations are available as non-member functions, so it is not neccessary to create temporary FileInfo objects. Only the paths to files or directories are required to perform file system operations. The next example illustrates some of the non-member functions for file operations:

try
{
Pt::System::Path tmp1("/tmp/tmpfile1");
Pt::System::Path tmp2("/tmp/tmpfile2");
// create a temporary file
// move it to a new location
// remove the file
}
catch(const Pt::System::AccessFailed& e)
{
std::cerr << "file error: " << e.resource() << std::endl;
}

The code shown above creates a file, moves it to a new location and finally deletes it. If an operation fails, for example because the file could not be created, an exception of type AccessFailed is thrown. This is also the case for all other operations such as size(), createFile(), createDirectory(), resize() and remove(). The exception reports the name of the resource that could not be accessed.

Member Enumeration Documentation

enum Type
Enumerator
Invalid 

Invalid file type.

Directory 

Directory.

File 

Regular file.

Member Function Documentation

const Path& path ( ) const

This method may return a relative path, or a fully qualified one depending on how this object was constructed.