Encapsulated management of a memory-mapped file.
this(const char*
filename);
Openfilename and map it in memory. IfDatum isconst, 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.
If
Datum is mutable, opens for read/write (creates file if it does not exist) and fails fatally on any error.
Due to quirks in
mmap, if the file is empty,
handle is valid but
data is
null. This state is valid and accounted for.
Parameters:char*filename | the name of the file to be mapped in memory |
Frees resources associated with this mapping. However, it does not deallocate the name. Reinitializesthis as a fresh object that can be reused.
Deletes the underlying file and frees all resources associated. Reinitializesthis as a fresh object that can be reused.
This function does not abort if the file cannot be deleted, but does print a message onstderr and returnsfalse to the caller. The underlying rationale is to give the caller the option to continue execution if deleting the file is not important.
Returns:true iff the file was successfully deleted. If the file was not deleted, prints a message tostderr and returnsfalse.
pure nothrow @nogc bool
active() const;
Queries whetherthis is currently associated with a file.
Returns:true iff there is an active mapping.
pure nothrow @nogc @safe size_t
length() const;
Queries the length of the file associated with this mapping. If not active, returns 0.
Returns:the length of the file, or 0 if no file associated.
pure nothrow @nogc @safe auto
opSlice();
Get a slice to the contents of the entire file.
Returns:the contents of the file. If not active, returns thenull slice.
pure void
resize(size_t
size);
Resizes the file and mapping to the specifiedsize.
Parameters:size_tsize | new length requested |
bool
moveToFile(const char*
filename);
Unconditionally and destructively moves the underlying file tofilename. If the operation succeeds, returns true. Upon failure, prints a message tostderr and returnsfalse. In all cases it closes the underlying file.
Parameters:char*filename | zero-terminated name of the file to move to. |
Returns:true iff the operation was successful.