dgamevfs.vfs
-
Declaration
abstract classVFSDir;A directory in the VFS.
Discussion
Provides basic directory information and access to files and subdirectories within the directory.
Directory names in the VFS can contain any characters except /, which is used as directory separator, and the :: sequence, which is used for explicit package lookup (see ).Examples
//Construct the directory (ordinary physical file system directory in this case): VFSDir dir = new FSDir("main", "./user_data/main", Yes.writable); //Print information about the directory: writeln("name: ", dir.name, ", full path: ", dir.path, ", writable: ", dir.writable, ", exists: ", dir.exists); //Access a file. If it does not exist, it will be created when writing: auto file = dir.file("logs/memory.log"); //Access a subdirectory: auto shaders = dir.dir("shaders"); //Create a subdirectory. If the directory exists, nothing happens (no error): auto shaders = dir.dir("does_not_exist").create(); //dirs() and files() methods can be used to get ranges of files and subdirectories: //Print paths of all immediate subdirectories and their files: foreach(subdir; dir.dirs()) { writeln(dir.path, ":"); foreach(file; subdir.files()) { writeln(" ", file.path); } } //Print paths of all subdirectories and their subdirectories, etc. recursively: foreach(subdir; dir.dirs(Yes.deep)) { writeln(dir.path); } //Glob patterns can be used to filter the results: //Print paths of all immediate subdirectories with paths containg "doc": foreach(subdir; dir.dirs(No.deep, "*doc*")) { writeln(dir.path); } //Print paths of all files in the directory and in subdirectories with paths ending with ".txt": foreach(file; dir.files(Yes.deep, "*.txt")) { writeln(file.path); }
-
Declaration
final const pure nothrow @nogc @property @safe stringname();Get the name of this directory.
-
Declaration
final const pure nothrow @property @safe stringpath();Get full path of this directory in the VFS.
-
Declaration
const pure nothrow @nogc @property @safe boolwritable();Is it possible to write to the directory?
-
Declaration
const nothrow @property @safe boolexists();Does the directory exist?
-
Declaration
VFSFilefile(stringpath);Get file with specified path in the directory.
Discussion
The file will be returned even if it does not exist - it will be created when writing into it.
Parameters
stringpathPath of the file to get.
Throws
if the directory does not exist or the file is in a nonexistent subdirectory.
if the path is invalid.Return Value
File with specified path.
-
Declaration
@safe VFSDirdir(stringpath);Get a subdirectory with specified path in the directory.
Discussion
The subdirectory will be returned even if it does not exist - it can be created with the method.
Parameters
stringpathPath of the subdirectory to get.
Throws
if this VFSDir does not exist or the subdirectory is in a nonexistent subdirectory.
if the path is invalid.Return Value
Subdirectory with specified path.
-
Declaration
@safe VFSFilesfiles(Flag!"deep"deep= No.deep, stringglob= null);Get a range of files in the directory.
Parameters
Flag!"deep"deepIf
true, recursively get files in subdirectories. Otherwise only get files directly in this directory.stringglobGlob pattern used to filter the results. If
null(default), all files will be returned. Otherwise only files whose VFS paths within this directory matchglob(case sensitive) will be returned. Some characters of glob patterns have special meanings: For instance, *.txt matches any path ending with the .txt extension.Return Value
Range of the files.
Throws
if the directory does not exist.
See Also
-
Declaration
@safe VFSDirsdirs(Flag!"deep"deep= No.deep, stringglob= null);Get a range of subdirectories.
Parameters
Flag!"deep"deepIf
true, recursively get all subdirectories. Otherwise just get subdirectories of this directory.stringglobGlob pattern used to filter the results. If
null(default), all subdirectories will be returned. Otherwise only subdirectories whose VFS paths within this directory matchglob(case sensitive) will be returned. Some characters of glob patterns have special meanings: For instance, *.txt matches any path ending with the .txt extension.Return Value
Range of the directories.
Throws
if the directory does not exist.
-
Declaration
final @safe voidcreate();Create the directory if it does not exist (otherwise do nothing).
Throws
if the directory could not be created.
-
Declaration
@system voidremove();Remove the directory if it exists (otherwise do nothing).
Discussion
Removes recursively, together with any subdirectories and files.
Warning: This will make any references to subdirectories or files in this directory invalid.
Throws
if the directory could not be removed.
-
Declaration
protected pure nothrow @nogc @safe this(VFSDirparent, stringpathInParent);Constructor to initialize state common for implementations.
Parameters
VFSDirparentParent directory. If
null, this directory has no parent.stringpathInParentPath of the directory within the parent.
-
Declaration
protected static @safe VFSDirsdirsRange(VFSDirs.Itemsdirs);Construct a range from a set of directories.
-
Declaration
protected static @safe VFSFilesfilesRange(VFSFiles.Itemsfiles);Construct a range from a set of files.
-
Declaration
protected const pure nothrow @safe stringcomposePath(const VFSDirchild);Compose path for a child directory. Used e.g. to allow to set children's paths.
-
Declaration
protected @trusted voidcreate_();Implementation of . Caller contract guarantees that the directory is writable.
-
Declaration
protected @safe VFSDircopyWithoutParent();Return a copy of this VFSDir without a parent. Used for mounting.
-
Declaration
protected final @safe VFSDirgetCopyWithoutParent(VFSDirdir);Access for derived classes to call copyWithoutParent() of other instances.
-
Declaration
structVFSRange(T) if (is(T == VFSDir) || is(T == VFSFile));A bidirectional range of VFS items (files or directories).
Examples
//VFSDirs is a VFSRange of directories - VFSFiles of files. VFSDirs dirs; //Get the first directory. auto f = dirs.front; //Get the last directory. auto b = dirs.back; //Remove the first directory from the range (this will not remove the directory itself). dirs.popFront(); //Remove the last directory from the range (this will not remove the directory itself). dirs.popBack(); //Are there no files/directories ? bool empty = r.empty;
-
Declaration
static boolcompare(inout(T)a, inout(T)b);Function used to compare items alphabetically.
-
Declaration
aliasItems= RedBlackTree!(T, compare);Type used to store the items.
-
Declaration
const pure nothrow @nogc @property @safe size_tlength();Get number of items in the range.
-
Declaration
pure nothrow @nogc @safe voidpopFront();Pop the front element from the range.
-
Declaration
pure nothrow @nogc @safe voidpopBack();Pop the back element from the range.
-
Declaration
aliasVFSDirs= VFSRange!(VFSDir).VFSRange;A VFSRange of directories.
-
Declaration
aliasVFSFiles= VFSRange!(VFSFile).VFSRange;A VFSRange of files.
-
Declaration
abstract classVFSFile;A file in the VFS.
Discussion
Provides basic file information and access to I/O.
File names in the VFS can contain any characters except /, which is used as directory separator, and the :: sequence, which is used for explicit package lookup (see ).Examples
VFSDir dir = new FSDir("main", "./user_data/main", Yes.writable); //Get the file from a directory. VFSFile file = dir.file("logs/memory.log"); //Print information about the file (note that we can only get file size of an existing file): writeln("name: ", file.name, ", full path: ", file.path, ", writable: ", file.writable, ", exists: ", file.exists, ", size in bytes: ", file.bytes); //Get access to read from the file: { auto input = file.input; //Simply read the file to a buffer: auto buffer = new ubyte[file.bytes]; input.read(buffer); } //Get access to write to the file: { auto output = file.output; //Simply write a buffer to the file: output.write(cast(const void[])"The answer is 42"); }
-
Declaration
enumMode: int;File mode (used by implementations);
-
Declaration
final const nothrow @property @safe stringname();Get name of the file.
-
Declaration
final const nothrow @property @safe stringpath();Get full path of the file in the VFS.
-
Declaration
const @property @safe ulongbytes();Get file size in bytes.
Throws
if the file does not exist.
-
Declaration
const nothrow @property @safe boolexists();Does the file exist?
-
Declaration
const nothrow @property @safe boolwritable();Is it possible to write to this file?
-
Declaration
const pure nothrow @nogc @property @safe boolopen();Is the file open?
-
Declaration
final @property VFSFileInputinput();Open the file and get reading access.
Return Value
providing input access to the file.
Throws
if the file does not exist or is already open.
-
Declaration
final @property VFSFileOutputoutput(Flag!"append"append= No.append);Open the file and get writing access. Must not already be open.
Return Value
providing output access to the file.
Throws
if the file is not writable or is already open.
-
Declaration
protected nothrow @safe this(VFSDirparent, stringpathInParent);Constructor to initialize state common for implementations.
Parameters
VFSDirparentParent directory. Must not be
null.stringpathInParentPath of the file within the parent.
-
Declaration
protected nothrow @safe this(stringabsolutePath);Constructor to initialize a without a parent.
Parameters
noParentParent directory. Must not be
null.pathInParentPath of the file within the parent.
-
Declaration
protected voidopenRead();Open the file for reading.
-
Declaration
protected voidopenWrite(Flag!"append"append);Open the file for writing/appending.
-
Declaration
protected void[]read(void[]target);Read up to bytes to
targetfrom current file position.Parameters
void[]targetBuffer to read to.
Return Value
Slice of target containing the
readdata. -
Declaration
protected voidwrite(in void[]data);Write bytes to file from current file position.
-
Declaration
protected voidseek(longoffset, Seekorigin);Seek
offsetbytes fromoriginwithin the file. -
Declaration
protected voidclose();Close the file, finalizing any file operations.
-
Declaration
protected static voidopenReadProxy(VFSFilefile);
protected static voidopenWriteProxy(VFSFilefile, Flag!"append"append);
protected static void[]readProxy(VFSFilefile, void[]target);
protected static voidwriteProxy(VFSFilefile, const void[]data);
protected static voidseekProxy(VFSFilefile, longoffset, Seekorigin);
protected static voidcloseProxy(VFSFilefile);Proxies to for derived VFSFiles to call protected members of other VFSFiles.
-
Declaration
enumSeek: int; -
Declaration
structVFSFileInput;Provides basic file input functionality - seeking and reading.
Discussion
uses reference counting so that the file is closed when the last instance of provided by the file is destroyed.
Examples
VFSFile file; //initialized somewhere before auto input = file.input; with(input) { auto buffer = new ubyte[32]; //Read the first 32 bytes from the file: read(buffer); //Read the next 32 bytes: read(buffer); //Read the last 32 bytes in the file: seek(-32, file); read(buffer); }
-
Declaration
void[]read(void[]target);Read at most bytes starting at current file position to
target.Discussion
If the file does not have enough bytes to fill
targetor a reading error is encountered, it reads as much data as possible and returns the part oftargetcontaining the read data.Parameters
void[]targetBuffer to read to.
Return Value
Slice of target containing the
readdata. -
Declaration
voidseek(longoffset, Seekorigin);Set file position to
offsetbytes from specifiedorigin.Parameters
longoffsetNumber of bytes to set file position relative to
origin.SeekoriginPosition to which
offsetis added.Throws
if trying to seek before the beginning or beyond the end of file.
-
Declaration
structVFSFileOutput;Provides basic file output functionality - seeking and writing.
Discussion
uses reference counting so that the file is closed when the last instance of provided by the file is destroyed.
Examples
VFSFile file; //initialized somewhere before auto output = file.output; with(output) { //Write to the file: write(cast(const void[])"The answer is ??"); //Change the last two characters in the file: seek(-2, Seek.End); write(cast(const void[])"42"); }
//Appending: //When appending to the file, every write writes to the end of file //regardless of any calls to seek(), and sets the file position //to end of file. This //(This is to stay in line with the C standard so we can use C functions directly) auto output = file.output(Yes.append); with(output) { //Append to the file: write(cast(const void[])"The answer is ??"); //This will NOT change the last 2 characters: it will append anyway: seek(-2, Seek.End); write(cast(const void[])"42"); }
-
Declaration
voidwrite(const void[]data);Write
datato file starting at current file position.Note: In append mode, any write will write to the end of file regardless of the current file position and file position will be set to the end of file.
(This is to stay in line with the C standard so we can use C I/O functions directly)Parameters
void[]dataData to write to the file.
Throws
on error (e.g. after running out of disk space).
-
Declaration
voidseek(longoffset, Seekorigin);Set file position to
offsetbytes from specifiedorigin.Parameters
longoffsetNumber of bytes to set file position relative to
origin.SeekoriginPosition to which
offsetis added.Throws
if trying to seek before the beginning or behind the end of file, or on a different error.