@file
FileConduit
.d
Copyright (c) 2004 Kris Bell
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for damages
of any kind arising from the use of this software.
Permission is hereby granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and/or
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment within documentation of
said product would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must
not be misrepresented as being the original software.
3. This notice may not be removed or altered from any distribution
of the source.
4. Derivative works are permitted, but they must carry this notice
in full and credit the original source.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@version Initial version; March 2004
@author Kris
John Reimer
Anders F Bjorklund (Darwin patches)
Chris Sauls (Win95 file support)
- struct
FileStyle
;
- Defines how a file should be opened. You can use the predefined
instances, or create specializations for your own needs.
- enum
Open
;
-
Exists
- must exist
-
Create
- create always
-
Truncate
- must exist
-
Append
- create if necessary
- enum
Share
;
-
Read
- shared reading
-
Write
- shared writing
-
ReadWrite
- both
- enum
Cache
;
-
None
- don't optimize
-
Random
- optimize for random
-
Stream
- optimize for stream
-
WriteThru
- backing-cache flag
- const Bits
ReadExisting
;
- class
FileConduit
: mango.io.DeviceConduit.DeviceConduit, mango.io.model.IConduit.ISeekable;
- Implements a means of reading and writing a generic file. Conduits
are the primary means of accessing external data, and are usually
routed through a Buffer. File conduit extends the generic conduit
by providing file-specific methods to set the file size, seek to a
specific file position, and so on. Also provided is a class for
creating a memory-mapped Buffer upon a file.
Serial input and output is straightforward. In this example we
copy a file directly to the console:
@code
// open a file for reading
FileConduit
from = new
FileConduit
("test.txt");
// stream directly to console
Stdout.conduit.copy (from);
@endcode
And here we copy one file to another:
@code
// open a file for reading
FileConduit
from = new
FileConduit
("test.txt");
// open another for writing
FileConduit
to = new
FileConduit
("copy.txt", FileStyle.WriteTruncate);
// copy file
to.copy (from);
@endcode
FileConduit
can just as easily handle random IO. Here we see how
a Reader and Writer are used to perform simple input and output:
@code
// open a file for reading
FileConduit
fc = new
FileConduit
("random.bin", FileStyle.ReadWriteCreate);
// construct (binary) reader & writer upon this conduit
Reader read = new Reader (fc);
Writer write = new Writer (fc);
int x=10, y=20;
// write some data, and flush output since IO is buffered
write (x) (y) ();
// rewind to file start
fc.seek (0);
// read data back again, but swap destinations
read (y) (x);
assert (y==10);
assert (x==20);
fc.close();
@endcode
FileConduits can also be used directly, without Readers, Writers, or
Buffers. To load a file directly into local-memory one might do this:
@code
// open file for reading
FileConduit
fc = new
FileConduit
("test.txt");
// create an array to house the entire file
char[] content = new char[fc.length];
// read the file content. Return value is the number of bytes read
int bytesRead = fc.read (content);
@endcode
Conversely, one may write directly to a
FileConduit
, like so:
@code
// open file for writing
FileConduit
to = new
FileConduit
("text.txt", FileStyle.WriteTruncate);
// write an array of content to it
int bytesWritten = fc.write (content);
@endcode
See File, FilePath, FileProxy, FileConst, FileScan, and FileSystem for
additional functionality related to file manipulation.
Doxygen has a hard time with D version() statements, so part of this
class is documented within
FileConduit
::VersionWin32 instead.
Compile with -version=Win32SansUnicode to enable Win95 & Win32s file
support.
- this(char[] name, Bits style = ReadExisting);
- Create a FileConduit with the provided path and style.
- this(FileProxy proxy, Bits style = ReadExisting);
- Create a FileConduit from the provided proxy and style.
- this(FilePath path, Bits style = ReadExisting);
- Create a FileConduit with the provided path and style.
- FilePath
getPath
();
- Return the FilePath used by this file.
- ulong
getPosition
();
- Return the current file position.
- ulong
length
();
- Return the total
length
of this file.
- FileConduit
copy
(FilePath source);
- Transfer the content of another file to this one. Returns a
reference to this class on success, or throws an IOException
upon failure.
- protected char[]
getName
();
- Return the name used by this file.
- protected void
open
(Bits style);
- Open a file with the provided style.
Windows-specific code
- protected uint
writer
(void[] src);
- Write a chunk of bytes to the file from the provided
array (typically that belonging to an IBuffer)
Windows-specific code
- void
truncate
();
- Set the file size to be that of the current seek
position. The file must be writable for this to
succeed.
Windows-specific code
- ulong
seek
(ulong offset, SeekAnchor anchor = cast(SeekAnchor)0);
- Set the file
seek
position to the specified offset
from the given anchor.
Windows-specific code
- class
TextFileConduit
: mango.io.FileConduit.FileConduit;
- Open a text-oriented FileConduit
- this(char[] name, Bits style = ReadExisting);
- Create a FileConduit with the provided path and style.
- this(FileProxy proxy, Bits style = ReadExisting);
- Create a FileConduit from the provided proxy and style.
- this(FilePath path, Bits style = ReadExisting);
- Create a FileConduit with the provided path and style.
- bool
isTextual
();
- Returns true if this conduit is text-based
|