|
JASSv2
|
File based I/O methods including whole file and partial files. More...
#include <file.h>
Classes | |
| class | file_read_only |
| A read_only file object, the memory was probably allocated with mmap() and needs deallocating accordingly. More... | |
Public Member Functions | |
| file ()=delete | |
| Constructor. | |
| file (FILE *fp) | |
| Constructor with a C FILE * object. More... | |
| file (const char *filename, const char *mode) | |
| Constructor used for opening files. More... | |
| file (const std::string &filename, const std::string &mode) | |
| Constructor used for opening files. More... | |
| ~file () | |
| Destructor. | |
| size_t | setvbuf (size_t size) |
| change the size of the internal buffer (does not flush() first) More... | |
| void | flush (void) |
| Flush the internal buffers to disk (called automatically on close). | |
| size_t | read (void *data, size_t size) |
| Read bytes number of bytes from the give file into the buffer. More... | |
| void | read (std::vector< uint8_t > &buffer) |
| Read buffer.size() bytes from the give file into the buffer. If at end of file then this method will resize buffer to the number of bytes read from the file. More... | |
| size_t | write (const void *data, size_t size) |
| Write bytes number of bytes to the give file at the current cursor position. More... | |
| size_t | write (const std::string &buffer) |
| Write bytes number of bytes to the give file at the current cursor position. More... | |
| size_t | size (void) const |
| Return the length of the file as it currently stands. More... | |
| size_t | tell (void) |
| Return the byte offset of the file pointer in the current file. More... | |
| void | seek (size_t offset) |
| Seek to the given offset in the file. More... | |
Static Public Member Functions | |
| static size_t | read_entire_file (const std::string &filename, std::string &into) |
| Read the contents of file filename into the std::string into. More... | |
| static size_t | read_entire_file (const std::string &filename, file_read_only &into) |
| Read the contents of file filename into the std::string into. More... | |
| static bool | write_entire_file (const std::string &filename, const std::string &buffer) |
| Write the contents of buffer to the file specified in filenane. More... | |
| static void | buffer_to_list (std::vector< uint8_t *> &line_list, std::string &buffer) |
| Turn a single std::string into a vector of uint8_t * (i.e. "C" strings). More... | |
| static bool | is_directory (const std::string &filename) |
| Determines whether the given file system object is a directoy or not. More... | |
| static std::string | mkstemp (std::string prefix) |
| Generate a temporary filename containing the given prefix. More... | |
| static void | unittest (void) |
| Unit test this class. | |
Protected Attributes | |
| FILE * | fp |
| The underlying representation is a FILE * from C (as they appear to be fast). | |
| size_t | file_position |
| The ftell() position in the file. | |
| size_t | buffer_size |
| Size of the internal file buffering. | |
| size_t | buffer_used |
| How much of the internal file buffer is being used. | |
| std::unique_ptr< uint8_t []> | buffer |
| Internal file buffer. | |
| size_t | bytes_written |
| Number of bytes written to this file. | |
| size_t | bytes_read |
| Number of bytes read from this file. | |
File based I/O methods including whole file and partial files.
This class exists in order to abstract file I/O which has, in the past, been different on different platforms. For example, 64-bit file I/O under Windows (using Win32) is awkward because the Win32 functions do not take 64-bit parameters.
This file class is based on Resource Allocation Is Initialisation (RAII). That is, the file is opened when the object is constructed and closed when the object is destroyed.
|
inline |
Constructor with a C FILE * object.
| fp | [in] The FILE * object this object should use. This class takes ownership and closes the file on destruction. |
|
inline |
Constructor used for opening files.
| filename | [in] the name of the file. |
| mode | [in] The file open mode. See C's fopen() for details on possible modes. |
|
inline |
Constructor used for opening files.
| filename | [in] the name of the file. |
| mode | [in] The file open mode. See C's fopen() for details on possible modes. |
|
static |
Turn a single std::string into a vector of uint8_t * (i.e. "C" strings).
Note that these pointers are in-place. That is, they point into buffer so any change to the uint8_t or to buffer effect each other. This method removes blank lines from buffer and changes buffer by inserting '\0' characters at the end of each line.
| line_list | [out] The vector to write into |
| buffer | [in, out] the string to decompose |
|
static |
Determines whether the given file system object is a directoy or not.
| filename | [in] The path to the file system object to check. |
|
static |
Generate a temporary filename containing the given prefix.
This method is a wrapper for mkstemp on Linux / MacOS and _mktemp on windows. These methods are renounds for having the problem that the filename may not be unique once it has been generated and therefore opening a file with this name may fail. However, there are times when this doesn't matter (such as unit tests).
| prefix | [in] The prefix to the unique filenane, |
|
inline |
Read bytes number of bytes from the give file into the buffer.
| data | [out] Buffer large enough to hold bytes number of bytes of data which are written into the memory pointed to by buffer. |
| size | [in] The number of bytes of data to read. |
|
inline |
Read buffer.size() bytes from the give file into the buffer. If at end of file then this method will resize buffer to the number of bytes read from the file.
| buffer | [in, out] Read buffer.size() bytes into buffer, calling buffer.resize() on failure. |
|
static |
Read the contents of file filename into the std::string into.
Because into is a string it is naturally '\0' terminated by the C++ std::string class.
| filename | [in] The path of the file to read. |
| into | [out] The std::string to write into. This string will be re-sized to the size of the file. |
|
inlinestatic |
Read the contents of file filename into the std::string into.
Because into is a string it is naturally '\0' terminated by the C++ std::string class.
| filename | [in] The path of the file to read. |
| into | [out] The std::string to write into. This string will be re-sized to the size of the file. |
|
inline |
Seek to the given offset in the file.
Throws std::out_of_range in the unlikely event of an error.
| offset | [in] The location to seek to. |
|
inline |
change the size of the internal buffer (does not flush() first)
The C standard states "This function should be called once the stream has been associated with an open file, but before any input or output operation is performed with it", and this is true of this function too.
| size | [in] The new size of the buffer. |
| size_t JASS::file::size | ( | void | ) | const |
Return the length of the file as it currently stands.
|
inline |
Return the byte offset of the file pointer in the current file.
|
inline |
Write bytes number of bytes to the give file at the current cursor position.
| data | [in] the byte sequence to write. |
| size | [in] The number of bytes of data to write. |
|
inline |
Write bytes number of bytes to the give file at the current cursor position.
| buffer | [in] the byte sequence to write. |
|
static |
Write the contents of buffer to the file specified in filenane.
If the file does not exist it is created. If it does already exist it is overwritten.
| filename | [in] The path of the file to write to. |
| buffer | [in] The data to write to the file. |
1.8.13