|
| File () |
|
| File (const File &)=delete |
|
| File (const String &path, FileModes mode) |
|
| File (File &&x) |
|
virtual | ~File () |
|
virtual bool | CanRead () const |
|
virtual bool | CanWrite () const |
|
virtual void | Close () |
|
virtual void | Create (const String &path) |
|
virtual void | CreateForWriting (const String &path) |
|
const String & | FileName () const |
|
const String & | FilePath () const |
|
void | Flush () |
|
bool | IsEOF () const |
|
bool | IsOpen () const |
|
FileModes | Mode () const |
|
virtual void | Open (const String &path, FileModes mode=FileMode::Read|FileMode::Open) |
|
virtual void | OpenForReading (const String &path) |
|
virtual void | OpenForReadWrite (const String &path) |
|
void | OpenOrCreate (const String &path) |
|
File & | operator= (const File &)=delete |
|
File & | operator= (File &&x) |
|
void | OutText (const char *i, const char *j) |
|
void | OutText (const char *s) |
|
void | OutText (const char16_type *i, const char16_type *j) |
|
void | OutText (const char16_type *s) |
|
void | OutText (const IsoString &s) |
|
void | OutText (const String &s) |
|
void | OutTextLn () |
|
void | OutTextLn (const char *i, const char *j) |
|
void | OutTextLn (const char *s) |
|
void | OutTextLn (const char16_type *i, const char16_type *j) |
|
void | OutTextLn (const char16_type *s) |
|
void | OutTextLn (const IsoString &s) |
|
void | OutTextLn (const String &s) |
|
virtual fpos_type | Position () const |
|
void | Read (bool &b) |
|
void | Read (IsoString &s) |
|
void | Read (String &s) |
|
template<typename T > |
void | Read (T &x) |
|
virtual void | Read (void *buffer, fsize_type len) |
|
template<typename T > |
void | ReadArray (T *a, size_type n) |
|
template<typename T > |
void | ReadI32 (T &x) |
|
template<typename T > |
void | ReadI64 (T &x) |
|
template<typename T > |
void | ReadUI32 (T &x) |
|
template<typename T > |
void | ReadUI64 (T &x) |
|
virtual void | Resize (fsize_type length) |
|
void | Rewind () |
|
fpos_type | Seek (fpos_type dist, seek_mode mode=SeekMode::FromCurrent) |
|
fpos_type | SeekEnd () |
|
virtual void | SetPosition (fpos_type pos) |
|
virtual fsize_type | Size () const |
|
void | Write (const bool &b) |
|
void | Write (const char *i, const char *j) |
|
void | Write (const char *s) |
|
void | Write (const char16_type *i, const char16_type *j) |
|
void | Write (const char16_type *s) |
|
void | Write (const IsoString &s) |
|
void | Write (const String &s) |
|
template<typename T > |
void | Write (const T &x) |
|
virtual void | Write (const void *buffer, fsize_type len) |
|
template<typename T > |
void | WriteArray (const T *a, size_type n) |
|
template<typename T > |
void | WriteI32 (const T &x) |
|
template<typename T > |
void | WriteI64 (const T &x) |
|
template<typename T > |
void | WriteUI32 (const T &x) |
|
template<typename T > |
void | WriteUI64 (const T &x) |
|
|
static String | AppendToName (const String &filePath, const String &postfix) |
|
static String | ChangeExtension (const String &filePath, const String &ext) |
|
static void | CopyFile (const String &targetFilePath, const String &sourceFilePath, File::Progress *progress=nullptr) |
|
static void | CopyLink (const String &targetLinkPath, const String &sourceLinkPath) |
|
static void | CopyTimesAndPermissions (const String &targetPath, const String &sourcePath) |
|
static void | CreateDirectory (const String &dirPath, bool createIntermediateDirectories=true) |
|
static File | CreateFile (const String &path) |
|
static File | CreateFileForWriting (const String &path) |
|
static bool | DirectoryExists (const String &dirPath) |
|
static UniqueFileChecks | EnsureNewUniqueDirectory (String &dirPath) |
|
static UniqueFileChecks | EnsureNewUniqueFile (String &filePath, bool canOverwrite=false) |
|
static bool | Exists (const String &filePath) |
|
static String | ExtractCompleteSuffix (const String &path) |
|
static String | ExtractDirectory (const String &path) |
|
static String | ExtractDrive (const String &path) |
|
static String | ExtractExtension (const String &path) |
|
static String | ExtractName (const String &path) |
|
static String | ExtractNameAndExtension (const String &path) |
|
static String | ExtractNameAndSuffix (const String &path) |
|
static String | ExtractSuffix (const String &path) |
|
static IsoString | FileURI (const String &path) |
|
static size_type | FindCompleteSuffix (const String &path) |
|
static size_type | FindDrive (const String &path) |
|
static size_type | FindExtension (const String &path) |
|
static size_type | FindName (const String &path) |
|
static size_type | FindSuffix (const String &path) |
|
static String | FullPath (const String &path) |
|
static uint64 | GetAvailableSpace (const String &dirPath, uint64 *totalBytes=nullptr, uint64 *freeBytes=nullptr) |
|
static String | HomeDirectory () |
|
static bool | IsReadOnly (const String &filePath) |
|
static void | Move (const String &filePath, const String &newFilePath) |
|
static void | MoveFile (const String &targetFilePath, const String &sourceFilePath, File::Progress *progress=nullptr) |
|
static File | OpenFileForReading (const String &path) |
|
static File | OpenFileForReadWrite (const String &path) |
|
static File | OpenOrCreateFile (const String &path) |
|
static String | PrependToName (const String &filePath, const String &prefix) |
|
template<typename E > |
static void | Read (Flags< E > &f) |
|
static ByteArray | ReadFile (const String &filePath) |
|
static IsoStringList | ReadLines (const String &filePath, ReadTextOptions options=ReadTextOptions()) |
|
static IsoString | ReadTextFile (const String &filePath) |
|
template<typename E > |
static void | ReadUI32 (Flags< E > &f) |
|
static void | Remove (const String &filePath) |
|
static void | RemoveDirectory (const String &dirPath) |
|
static bool | SameDevice (const String &path1, const String &path2) |
|
static bool | SameFile (const String &path1, const String &path2) |
|
static size_type | ScanLines (const String &filePath, bool(*callback)(char *, void *), void *data=nullptr, ReadTextOptions options=ReadTextOptions()) |
|
static void | SetPermissions (const String &targetPath, FileAttributes permissions) |
|
static void | SetReadOnly (const String &filePath, bool rdOnly=true) |
|
static void | SetReadWrite (const String &filePath) |
|
static String | SizeAsString (fsize_type sizeInBytes, int precision=3, bool alsoShowBytes=false) |
|
static String | SystemCacheDirectory () |
|
static String | SystemTempDirectory () |
|
static String | UniqueFileName (const String &directory=String(), int n=12, const String &prefix=String(), const String &postfix=String()) |
|
static String | UnixPathToWindows (const String &path) |
|
static String | WindowsPathToUnix (const String &path) |
|
template<typename E > |
static void | Write (Flags< E > f) |
|
static void | WriteFile (const String &filePath, const ByteArray &contents) |
|
static void | WriteFile (const String &filePath, const ByteArray &contents, size_type start, size_type size) |
|
static void | WriteFile (const String &filePath, const void *data, size_type size) |
|
static void | WriteTextFile (const String &filePath, const IsoString &text) |
|
template<typename E > |
static void | WriteUI32 (Flags< E > f) |
|
TODO: Write a detailed description for File.
Definition at line 343 of file File.h.
Changes the current file extension in filePath to a new extension ext. If it is not an empty string, the specified extension must include the initial dot separator.
If the specified new extension ext is an empty string, this function returns filePath with its extension (if any) removed.
Returns the resulting path after changing the file extension to the specified value.
static void pcl::File::CopyFile |
( |
const String & |
targetFilePath, |
|
|
const String & |
sourceFilePath, |
|
|
File::Progress * |
progress = nullptr |
|
) |
| |
|
static |
Copies a single file.
- Parameters
-
targetFilePath | Path to the target file, which must not exist. If the specified file exists, an Error exception will be thrown. |
sourceFilePath | Path to the source file. This file must exist and must be different from the specified target file. |
progress | Pointer to an instance of (a derived class of) File::Progress. If a non-null pointer is specified, the object will be used to provide progress information and the possibility of aborting the file copy operation. The default value is nullptr . |
The file copy operation is implemented as an iterative block copying routine. This means that huge files can be copied with minimal consumption of memory resources and no specific file size limit.
Along with the file contents, file access and modification times, as well as file access permissions, will also be copied by this function.
This function does not follow symbolic links. It does not copy the file pointed to by a symbolic link, but the link itself. This is only relevant to platforms that support symbolic links (i.e., all of them but Windows).
If a progress object is specified, it receives successive calls to File::Progress::Add() with increments in bytes. If the progress object aborts the file copy operation, this member function throws a ProcessAborted exception.
Ensures existence of a directory on the local filesystem, guaranteeing that a new, empty directory will be created in case a file or directory at the specified path already existed when the function was called.
- Parameters
-
[in,out] | dirPath | Path to the directory that must exist with guaranteed uniqueness. The value of the variable referenced by this parameter can be modified on output; see the description below for further details. |
After calling this function, a directory at the path specified by the output value of the dirPath variable is guaranteed to exist, either because such directory already existed when the function was called, or because a new empty directory has been created otherwise. Already existing directories or files are never touched or modified in any way by this function (beyond the fact that they are accessed to verify their existence).
Returns a UniqueFileChecks structure where the exists member indicates whether the specified dirPath corresponds to an already existing directory upon calling this function, and the overwrite member is always false, reflecting the fact that a directory cannot be overwritten.
If the specified directory already exists, the string referenced by dirPath will be altered to provide a unique directory name on the same parent directory. In such case the path is made unique by appending the literal representation of an unsigned integer, preceded by an underscore character, to the file name component. For example, if this function is called as follows when the specified directory already exists:
String dirPath = "/path/to/foobar";
static UniqueFileChecks EnsureNewUniqueDirectory(String &dirPath)
then the dirPath
variable could have the value "/path/to/foobar_1" and a new, empty directory would have been created at that path. Successive calls would set dirPath
equal to "/path/to/foobar_2", "/path/to/foobar_3", and so on, creating the corresponding new directories.
The string referenced by the specified dirPath variable can optionally end with a directory separator character, which will be preserved in case the string is modified.
- Note
- This static member function is thread-safe. It will provide coherent results when invoked from multiple threads running concurrently.
Ensures existence of a file on the local filesystem, guaranteeing that a newly created file cannot replace or overwrite an already existing file, or clash with an existing directory.
- Parameters
-
[in,out] | filePath | Path to the file that must exist with guaranteed uniqueness. The value of the variable referenced by this parameter can be modified on output; see the canOverwrite parameter and the description below for further details. |
| canOverwrite | If this parameter is false and the specified filePath corresponds to an already existing file, the string referenced by filePath will be replaced on output with a new path on the same directory where the file name has been modified to guarantee that no file existed with the same name upon calling this function. If this parameter is true the filePath string won't be modified in any way. |
After calling this function, a file at the path specified by the output value of the filePath variable is guaranteed to exist, either because such file already existed when the function was called and the canOverwrite parameter was true, or because a new empty file has been created otherwise. Already existing files are never touched or modified in any way by this function (beyond the fact that they are accessed to verify their existence).
Returns a UniqueFileChecks structure where the exists member indicates whether the specified filePath corresponds to an already existing file upon calling this function, and the overwrite member is true iff an existing file can be overwritten because the specified canOverwrite parameter is true.
When the filePath string is altered to provide a unique file name, the file path is made unique by appending the literal representation of an unsigned integer, preceded by an underscore character, to the file name component. For example, if this function is called as follows when the specified file already exists:
String filePath = "/path/to/foo.bar";
static UniqueFileChecks EnsureNewUniqueFile(String &filePath, bool canOverwrite=false)
then the filePath
variable could have the value "/path/to/foo_1.bar", and a new, empty file would have been created at that path. Successive calls would set filePath
equal to "/path/to/foo_2.bar", "/path/to/foo_3.bar", and so on, creating the corresponding new files.
- Note
- This static member function is thread-safe. It will provide coherent results when invoked from multiple threads running concurrently.
static String pcl::File::ExtractCompleteSuffix |
( |
const String & |
path | ) |
|
|
static |
Extracts the complete file suffix from the specified path.
Returns the extracted complete suffix, or an empty string if no file suffix was found. The returned string includes the initial dot separator.
The complete suffix is the ending substring of the file identifier in the specified path, starting with the first occurrence of a dot character. For example, in 'foo.tar.gz' the complete suffix is '.tar.gz'.
- See also
- ExtractSuffix()
static String pcl::File::ExtractDirectory |
( |
const String & |
path | ) |
|
|
static |
Extracts a directory specification from the specified path.
Returns the extracted directory specification, or an empty string if no directory was found. On Windows, the drive element of the path is not included.
Examples:
In '/foo.tar.gz' the directory is '/'. In '/foo/bar.tar.gz' the directory is '/foo'. In 'foo/bar.tar.gz' the directory is 'foo'. In 'C:/Foo/Bar.txt' the directory is '/Foo'.
static String pcl::File::ExtractDrive |
( |
const String & |
path | ) |
|
|
static |
Extracts a Windows drive specification from the specified path.
Returns the extracted drive specification as a new string, or an empty string if no drive was found.
This function only makes sense on Windows. On UNIX and Linux operating systems, this function always returns an empty string.
Returns a valid URI with the "file" scheme corresponding to the specified local path.
The returned URI is valid as specified by RFC 1738 and RFC 8089. The following characters will always be percent-encoded when present in the specified path:
- Non-printable US-ASCII characters in the ranges [00-1F] and [7F-FF].
- Unsafe characters: ' ', '<', '>', '#', '"', '', '{', '}', '|', '\', '^', '~', '[', ']', '‘’
- Reserved characters: ';', '?', ':', '@', '=', '&'.
The returned URI will include the specified path transformed to a full absolute local path with UNIX directory separators, encoded in UTF-8, percent-encoded as necessary, and prefixed with the "file://" scheme.
On Windows, a drive letter will always be present, prefixed with a root directory specifier, and the colon separator will be used; for example:
file:///c:/path/to/local%20file.txt
References:
https://tools.ietf.org/html/rfc1738
https://tools.ietf.org/html/rfc8089
Finds a file extension in the specified path.
Returns the character index position of the extension found, or String::notFound.
The file extension, or file suffix, is the rightmost substring of path starting with the last occurrence of a dot character. For example, in 'foo.tar.gz' the file extension is '.gz'.
- See also
- FindCompleteSuffix()
static String pcl::File::HomeDirectory |
( |
| ) |
|
|
static |
Returns the home directory of the current user.
On FreeBSD, Linux and macOS, this function returns the value of the HOME environment variable if it is defined, nonempty, and is a valid path to an existing directory. In the extremely weird case this does not happen, the system root directory '/' is returned as a last resort.
On Windows, this function returns the root directory of the current user's profile. This is normally the value of the USERPROFILE environment variable. In case this fails, the HOMEDRIVE, HOMEPATH and HOME variables are checked for validity. If everything fails, the SYSTEMDRIVE variable is checked and ultimately "C:/" is returned.
static void pcl::File::MoveFile |
( |
const String & |
targetFilePath, |
|
|
const String & |
sourceFilePath, |
|
|
File::Progress * |
progress = nullptr |
|
) |
| |
|
static |
Moves a single file.
- Parameters
-
targetFilePath | Path to the target file, which must not exist. If the specified file exists, an Error exception will be thrown. |
sourceFilePath | Path to the source file. This file must exist and must be different from the specified target file. |
progress | Pointer to an instance of (a derived class of) File::Progress. If a non-null pointer is specified, the object will be used to provide progress information and the possibility of aborting the file copy operation. The default value is nullptr . |
For file move operations within the same physical device, this function implements a simple rename operation. In these cases, the progress object (if specified) receives a single call to File::Progress::Add() with the total file size in bytes.
When a file is moved to a different device, this member function first copies the source file to the target location using a routine equivalent to Copy(). When the entire file contents have been transferred, the source file is removed.
Along with the file contents, file access and modification times, as well as file access permissions, will be preserved by this function.
This function does not follow symbolic links. It does not move the file pointed to by a symbolic link, but the link itself. This is only relevant to platforms that support symbolic links (i.e., all of them but Windows).
If a progress object is specified and it aborts the file move operation, this member function throws a ProcessAborted exception. In such case the source file is guaranteed to remain intact.
Reads all existing text lines from a file as 8-bit strings (e.g., ISO 8859-1 or UTF-8 formats) and returns them as a string list.
- Parameters
-
filePath | Path to the source file from which text lines will be read. |
options | Optional set of flags (ORed combination) that can be specified to control the way text lines are extracted and stored. Available flags are defined in the ReadTextOption namespace. |
Returns a string list (IsoStringList) with all existing lines in the specified file. If the file is empty, an empty string list is returned.
This routine automatically detects and processes line ending sequences characteristic of all supported platforms: UNIX (LF = 0x0A), Windows (CR+LF = 0x0D+0x0A) and legacy Mac (CR = 0x0D).
static bool pcl::File::SameDevice |
( |
const String & |
path1, |
|
|
const String & |
path2 |
|
) |
| |
|
static |
Returns true iff two files or directories are mounted on the same physical device.
This is relevant to some critical file operations. For example, moving a file on the same device is a simple and fast rename operation. However, moving a file across devices involves copying the entire file contents.
This function does not follow symbolic links. It does not consider mounted devices for files pointed to by symbolic links, but for the links themselves. This is only relevant to platforms that support symbolic links (all of them but Windows).
static bool pcl::File::SameFile |
( |
const String & |
path1, |
|
|
const String & |
path2 |
|
) |
| |
|
static |
Returns true iff two file path specifications refer to the same file on the system.
On UNIX and Linux platforms, this function compares the device and inode numbers reported by the st_dev and st_ino members of the stat structure, respectively. This pair of numbers uniquely identifies every file on the system, irrespective of file names.
On Windows platforms, this function verifies the devices that support both files and, if they are the same, performs a case-insensitive comparison of the canonical absolute versions of the specified paths.
This function does not follow symbolic links. It does not consider mounted devices for files pointed to by symbolic links, but for the links themselves. This is only relevant to platforms that support symbolic links (all of them but Windows).
Iterates a function through all existing text lines in a file.
- Parameters
-
filePath | Path to the source file from which text lines will be read. This file is assumed to store text as a sequence of 8-bit characters, e.g. ASCII, ISO 8859-1 or UTF-8. |
callback | Callback function. This function will be invoked once for each text line. The function will receive two arguments: a char* with the starting address of an 8-bit null terminated string, and a void* which is the specified data argument. The function will return true if the process can continue for the next text line; false to stop the iteration. |
data | A void* that will be passed to the callback function as its second argument on each call. The default value is nullptr . |
options | Optional set of flags (ORed combination) that can be specified to control the way text lines are explored and passed to the callback function. Available flags are defined in the ReadTextOption namespace. |
Returns the number of text lines that have been scanned, i.e. the number of times the callback function has been invoked and has returned true.
This routine avoids all the work associated with creating and storing new IsoString objects. When IsoString instances are not necessary to acquire the data being read, this routine can be much faster than ReadLines(), especially for vey large files.
This routine automatically detects and processes line ending sequences characteristic of all supported platforms: UNIX (LF = 0x0A), Windows (CR+LF = 0x0D+0x0A) and legacy Mac (CR = 0x0D).
Moves the file pointer after the end of the file. Returns the size of this file, that is, the absolute position of the last byte plus one.
This is a convenience member function, equivalent to Seek( 0, SeekMode::FromEnd )
Definition at line 861 of file File.h.
Changes the access permissions of a file or directory.
- Parameters
-
targetPath | Path to the existing file or directory whose permissions will be modified. |
permissions | An ORed combination of file permission flags. See the FilePermission namespace for contants. |
On UNIX and Linux platforms (FreeBSD, GNU Linux, Mac OS X), the read, write and execution permissions can be specified separately for the owner user, for the owner group, and for the rest of users. On Windows, only generic read and write permissions can be set, and there are no execution permissions.
static String pcl::File::SystemCacheDirectory |
( |
| ) |
|
|
static |
Returns the system cache storage directory.
On FreeBSD, Linux and Windows, this function is equivalent to SystemTempDirectory().
On macOS, this function returns the user's local cache folder, namely "~/Library/Caches" if it exists (it must), or SystemTempDirectory() otherwise.
static String pcl::File::SystemTempDirectory |
( |
| ) |
|
|
static |
Returns the system temporary storage directory.
On FreeBSD, Linux and macOS, this function returns the value of the TMPDIR environment variable if it is defined, nonempty, and is a valid path to an existing directory. Otherwise this function returns "/tmp".
On Windows, this function returns the directory designated for storage of temporary files. Normally this is the value of the TMP or TEMP environment variable. If no valid directory can be retrieved this way (which is extremely weird), this function attempts to return the user's profile directory, as reported by the USERPROFILE variable. If that also fails, "C:/tmp" is returned if it exists, or the current working directory as a last resort.
Returns a nonexistent, randomly generated file name.
- Parameters
-
directory | Name of an existing directory where the generated file name is guaranteed to be unique. If empty or not specified, the current directory of the calling process will be used. |
n | Number of random characters in the generated file name. Must be at least five characters. The default value is twelve characters. |
prefix | An optional prefix that will be prepended to the generated file name. |
postfix | An optional postfix that will be appended to the generated file name. |
This function returns the full absolute path of a nonexistent file name generated by concatenation of n random characters. For compatibility with case-insensitive file systems (FAT, NTFS), only uppercase characters and decimal digits are used in the generated file name.
The returned file name is guaranteed to be unique on the specified directory (or on the current directory of the calling process if no directory is specified) just after this function returns. Note that since a high-quality random number generator is used to select file name characters, there is no practical chance for a race condition by calling this function from several threads concurrently.