CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
fileobj.cxh
1#include <cx/fs/fs.h>
2
3/// @addtogroup fs_file
4/// @{
5
6/// An open file
7///
8/// Every open file is a File, whichever layer opened it: fsOpen() hands back one backed by an
9/// OS file handle, and vfsOpen() one backed by a VFS provider. Code that only reads and writes
10/// can take a File* and work with either.
11///
12/// A File is reference counted. fileClose() closes the file and releases your reference;
13/// objRelease() only releases the reference, and closes the file if it was the last one.
14///
15/// A file handle is single-owner: one thread at a time, like a C FILE*. Two threads may use two
16/// handles on the same file, but they must not share one handle.
17abstract class File {
18 // Flushes pending writes and closes the underlying handle without releasing the File.
19 // Implemented by each kind of File; callers use fileClose() instead. Further reads and
20 // writes fail, and closing an already closed file must be harmless. Returns true if
21 // successful, false if flushing or closing failed or if self was NULL.
22 [abstract] [nullself false] bool closeHandle();
23
24 /// Reads data from the file
25 ///
26 /// Reads up to sz bytes from the current position into buf and advances the position by the
27 /// number of bytes actually read. Reading less than requested is not an error; it means the
28 /// end of the file was reached.
29 ///
30 /// @param buf Buffer to receive the data (must be at least sz bytes)
31 /// @param sz Maximum number of bytes to read
32 /// @param bytesread Receives the number of bytes read (0 at end of file)
33 /// @return true if the read succeeded, false on an I/O error (bytesread is set to 0)
34 [abstract] [nullself false] bool read([sal _Out_writes_bytes_to_(sz, *bytesread)] void *buf, size_t sz,
35 [out] [sal _Deref_out_range_(0, sz)] size_t *bytesread);
36
37 /// Writes data to the file
38 ///
39 /// Writes sz bytes from buf at the current position and advances the position by the number
40 /// of bytes written. Most of the time everything is written or the call fails, but a short
41 /// write is possible on some filesystems, so check byteswritten when it matters.
42 ///
43 /// @param buf Buffer containing the data to write
44 /// @param sz Number of bytes to write
45 /// @param byteswritten Receives the number of bytes written, or NULL
46 /// @return true if the write succeeded, false on an I/O error
47 [abstract] [nullself false] bool write([sal _In_reads_bytes_(sz)] const void *buf, size_t sz,
48 [out] [opt] [sal _Deref_out_range_(0, sz)] size_t *byteswritten);
49
50 /// Gets the current position in the file
51 ///
52 /// @return Current position in bytes from the start of the file, -1 on error
53 [abstract] [nullself -1] int64 tell();
54
55 /// Changes the current position in the file
56 ///
57 /// @param off Offset in bytes, which may be negative for FS_Cur and FS_End
58 /// @param seektype Where to measure from: FS_Set (start), FS_Cur (current), FS_End (end)
59 /// @return New position in bytes from the start of the file, -1 on error
60 [abstract] [nullself -1] int64 seek(int64 off, FSSeekType seektype);
61
62 /// Flushes buffered writes to storage
63 ///
64 /// Forces buffered data out to the storage device. This is slow, so only do it when
65 /// durability matters right now; closing the file flushes it anyway.
66 ///
67 /// @return true if successful, false on error
68 [abstract] [nullself false] bool flush();
69
70 /// Writes the contents of a string to the file
71 ///
72 /// The string is written as-is, with no line ending added.
73 ///
74 /// @param str String to write
75 /// @param byteswritten Receives the number of bytes written, or NULL
76 /// @return true if the write succeeded, false on an I/O error
77 [nullself false] unbound bool writeString(strref str, [out] [opt] size_t *byteswritten);
78}
79
80/// @}