dmd.root.file
Read a file from disk and store it in memory.
License
Source: root/file.d
Documentation: https://dlang.org/phobos/dmd_root_file.html
-
Declaration
structFileMapping(Datum);Encapsulated management of a memory-mapped file.
Parameters
Datumthe mapped data type: Use a POD of size 1 for read/write mapping and a
constversion thereof for read-only mapping. Other primitive types should work, but have not been yet tested.-
Declaration
this(const char*filename);Open
and map it in memory. IffilenameDatumisconst, opens for read-only and maps the content in memory; no error is issued if the file does not exist. This makes it easy to treat a non-existing file as empty.Discussion
If
Datumis mutable, opens for read/write (creates file if it does not exist) and fails fatally on any error.
Due to quirks inmmap, if the file is empty,handleis valid butdataisnull. This state is valid and accounted for.Parameters
char*filenamethe name of the file to be mapped in memory
-
Declaration
const pure nothrow @nogc @safe const(char)*filename();Returns the zero-terminated file name associated with the mapping. Can be saved beyond the lifetime of
this. -
Declaration
voidclose();Frees resources associated with this mapping. However, it does not deallocate the name. Reinitializes
thisas a fresh object that can be reused. -
Declaration
booldiscard();Deletes the underlying file and frees all resources associated. Reinitializes
thisas a fresh object that can be reused.Discussion
This function does not abort if the file cannot be deleted, but does print a message on
stderrand returnsfalseto the caller. The underlying rationale is to give the caller the option to continue execution if deleting the file is not important.Return Value
trueiff the file was successfully deleted. If the file was not deleted, prints a message tostderrand returnsfalse. -
Declaration
const pure nothrow @nogc boolactive();Queries whether
thisis currently associated with a file.Return Value
trueiff there is anactivemapping. -
Declaration
const pure nothrow @nogc @safe size_tlength();Queries the
lengthof the file associated with this mapping. If not active, returns 0.Return Value
the
lengthof the file, or 0 if no file associated. -
Declaration
pure nothrow @nogc @safe autoopSlice();Get a slice to the contents of the entire file.
Return Value
the contents of the file. If not active, returns the
nullslice. -
Declaration
pure voidresize(size_tsize);Resizes the file and mapping to the specified
.sizeParameters
size_tsizenew length requested
-
Declaration
boolmoveToFile(const char*filename);Unconditionally and destructively moves the underlying file to
. If the operation succeds, returnsfilenametrue. Upon failure, prints a message tostderrand returnsfalse.Parameters
char*filenamezero-terminated name of the file to move to.
Return Value
trueiff the operation was successful.
-
-
Declaration
structFileBuffer;Owns a (rmem-managed) file buffer.
-
Declaration
pure nothrow @nogc @safe ubyte[]extractSlice();Transfers ownership of the buffer to the caller.
-
-
Declaration
structFile;-
Declaration
structReadResult;-
Declaration
pure nothrow @nogc @safe ubyte[]extractSlice();Transfers ownership of the buffer to the caller.
-
Declaration
pure nothrow @nogc ubyte[]extractDataZ();ditto Include the
null-terminator at the end of the buffer in the returned array.
-
-
Declaration
static nothrow ReadResultread(const(char)*name);
static nothrow ReadResultread(const(char)[]name);Read the full content of a file.
-
Declaration
static nothrow boolwrite(const(char)*name, const void[]data);
static nothrow boolwrite(const(char)[]name, const void[]data);
static nothrow boolwrite(const(char)*name, const(void)*data, size_tsize);Write a file, returning
trueon success. -
Declaration
static nothrow voidremove(const(char)*name);Delete a file.
-
Declaration
static nothrow boolupdate(const(char)*namez, const void[]data);
static nothrow boolupdate(const(char)[]name, const void[]data);
static nothrow boolupdate(const(char)*name, const(void)*data, size_tsize);Update file
Discussion
If the file exists and is identical to what is to be written, merely
updatethe timestamp on the file. Otherwise, write the file.
The idea is writes are much slower than reads, and build systems often wind up generating identical files.Parameters
const(char)[]namenameof file toupdatevoid[]dataupdated contents of file
Return Value
trueon success -
Declaration
static nothrow booltouch(const char*namez);Touch a file to current date
-
Declaration
static nothrow ulongsize(const char*namez);
static nothrow ulongsize(intfd);Size of a file in bytes.
Parameters
char*nameznull-terminated filenameReturn Value
ulong.maxon any error, the length otherwise.
-