ADTF File Library
Loading...
Searching...
No Matches
file.h
Go to the documentation of this file.
1
16
17#ifndef FILE_CLASS_EXT_HEADER
18#define FILE_CLASS_EXT_HEADER
19
20#include <stdint.h>
21#include <a_util/filesystem.h>
22#include <a_util/datetime.h>
23#include <a_util/memory.h>
24#include <system_error>
25
31namespace utils5ext
32{
33
37using FilePos = int64_t;
42
47#ifdef _WIN32
48 typedef void* FileHandle;
49#else
50 typedef int FileHandle;
51#endif
52
56namespace exceptions {
60 class EndOfFile : public std::runtime_error
61 {
62 public:
66 EndOfFile() : runtime_error("end of file")
67 {
68 }
69
73 EndOfFile(const std::string& what) : runtime_error(what)
74 {
75 }
76 };
77
82 class ErrorFileAccess : public std::system_error {
83 public:
88 ErrorFileAccess(const std::string& what);
94 ErrorFileAccess(int system_error_code, const std::string& what);
95 };
96
102 {
103 public:
109 ErrorFileAlreadyExists(int system_error_code, const std::string& what)
110 : ErrorFileAccess(system_error_code, what)
111 {
112 }
113 };
114}
115
120class File
121{
122 public:
124 typedef enum
125 {
129 om_read = 0x0001,
130
133 om_write = 0x0002,
134
136 om_append = 0x0004,
137
141
144
149
154
157
161
164
167 om_text_mode = 0x2000,
168
171
174
175 } OpenMode;
176
178 typedef enum
179 {
182
185
188 } FilePosRef;
189
190 protected:
192 uint8_t* _read_cache;
201
202 public:
204 File() noexcept;
205
207 virtual ~File();
208
223 void open(const a_util::filesystem::Path& filename, uint32_t mode);
224
228 void close();
229
234 void attach(File& file);
235
241 void detach();
242
254 void setReadCache(size_t read_cache_size);
255
270 size_t read(void* buffer, size_t buffer_size);
271
283 void readAll(void* buffer, size_t buffer_size);
284
295 size_t skip(size_t number_of_bytes);
296
314 size_t write(const void* buffer, size_t buffer_size);
315
329 void writeAll(const void* buffer, size_t buffer_size);
330
337 void write(const std::string& string);
338
345 void readLine(std::string& string);
346
354 void writeLine(const std::string& string);
355
362
369
381
386 bool isEof();
387
393 bool isValid() const;
394
403 void truncate(FilePos size);
404
405 protected:
410
416 void allocReadCache(size_t cache_size);
417
422
431 void* internalMalloc(size_t size, bool use_segment_size=false);
432
439 void internalFree(void* memory, bool use_segment_size=false);
440};
441
454a_util::datetime::DateTime getTimeAccess(const a_util::filesystem::Path filename);
455
468a_util::datetime::DateTime getTimeCreation(const a_util::filesystem::Path filename);
469
482a_util::datetime::DateTime getTimeChange(const a_util::filesystem::Path filename);
483
488size_t getDefaultSectorSize() noexcept;
489
498void* allocPageAlignedMemory(size_t size, size_t page_size);
499
504void freePageAlignedMemory(void* memory);
505
512size_t getSectorSizeFor(const a_util::filesystem::Path& filename) noexcept;
513
519inline void memZero(void* data, size_t bytes)
520{
521 a_util::memory::set(data, bytes, 0x00, bytes);
522}
523
530void fileRename(const a_util::filesystem::Path& from, const a_util::filesystem::Path& to);
531
532} // namespace utils5ext
533
534#endif // _FILE_CLASS_EXT_HEADER_
bool _system_cache_disabled
System cache disabled.
Definition file.h:198
FileHandle _file
File handle.
Definition file.h:191
File() noexcept
Constructor.
bool isValid() const
size_t _file_cache_offset
File cache offset.
Definition file.h:194
FilePosRef
File position reference.
Definition file.h:179
@ fp_current
offsets are measured from the current file position
Definition file.h:184
@ fp_end
offsets are measured from the end of the file
Definition file.h:187
@ fp_begin
offsets are measured from the beginning of the file
Definition file.h:181
void freeReadCache()
bool _read_cache_enabled
Read cache enabled.
Definition file.h:196
void writeLine(const std::string &string)
FileSize getSize() const
FilePos setFilePos(FilePos offset, FilePosRef move_mode)
void readLine(std::string &string)
void attach(File &file)
bool _reference
File is reference.
Definition file.h:197
FilePos getFilePos() const
void readAll(void *buffer, size_t buffer_size)
void allocReadCache(size_t cache_size)
size_t skip(size_t number_of_bytes)
FilePos _sector_bytes_to_skip
Sector bytes that will be skipped.
Definition file.h:200
int _sector_size
Sector size.
Definition file.h:199
uint8_t * _read_cache
File read cache.
Definition file.h:192
size_t _file_cache_size
File cache size.
Definition file.h:195
size_t _file_cache_usage
File cache usage.
Definition file.h:193
size_t read(void *buffer, size_t buffer_size)
size_t write(const void *buffer, size_t buffer_size)
void setReadCache(size_t read_cache_size)
void open(const a_util::filesystem::Path &filename, uint32_t mode)
void truncate(FilePos size)
void * internalMalloc(size_t size, bool use_segment_size=false)
OpenMode
Several flags to open files. Can be combined with bitwise or.
Definition file.h:125
@ om_temporary_file
Definition file.h:160
@ om_no_overwrite
No overwrite if file already exists.
Definition file.h:143
@ om_text_mode
Definition file.h:167
@ om_shared_read
Definition file.h:148
@ om_read_write
Definition file.h:140
@ om_write
Definition file.h:133
@ om_short_lived
Advices the OS not to flush any data to the disk. Just supported on POSIX platforms.
Definition file.h:163
@ om_sequential_access
Optimized for sequential access, use in addition to flags above.
Definition file.h:156
@ om_shared_write
Definition file.h:153
@ om_read
Definition file.h:129
@ om_write_through
Immediately flush file data to disk. Windows only.
Definition file.h:170
@ om_append
Append data to existing file. Sets seeking position to the end of the file stream.
Definition file.h:136
@ om_disable_file_system_cache
Disable all file system caches. Windows only.
Definition file.h:173
void internalFree(void *memory, bool use_segment_size=false)
void writeAll(const void *buffer, size_t buffer_size)
EndOfFile()
default CTOR
Definition file.h:66
EndOfFile(const std::string &what)
CTOR with description.
Definition file.h:73
ErrorFileAccess(int system_error_code, const std::string &what)
CTOR for file access errors.
ErrorFileAccess(const std::string &what)
CTOR for file access errors with no error code!
ErrorFileAlreadyExists(int system_error_code, const std::string &what)
CTOR for file access errors.
Definition file.h:109
namespace to collect special exceptions for file operations.
Definition file.h:56
Definition file.h:32
int FileHandle
The type FileHandle is used internally only.
Definition file.h:50
void * allocPageAlignedMemory(size_t size, size_t page_size)
allocates a memory buffer for the size of size (in bytes), page aligned in size to page_size
a_util::datetime::DateTime getTimeAccess(const a_util::filesystem::Path filename)
This function returns the last access time of the file.
int64_t FilePos
Type for a file position.
Definition file.h:37
a_util::datetime::DateTime getTimeChange(const a_util::filesystem::Path filename)
This function returns the last change (write) time of the file.
void memZero(void *data, size_t bytes)
Helper function to set a memory buffer to zero.
Definition file.h:519
size_t getSectorSizeFor(const a_util::filesystem::Path &filename) noexcept
Get the sector size of a file within windows file system.
void freePageAlignedMemory(void *memory)
frees the memory buffer allocated with utils5ext::allocPageAlignedMemory
void fileRename(const a_util::filesystem::Path &from, const a_util::filesystem::Path &to)
Renames or moves a file.
size_t getDefaultSectorSize() noexcept
Get the Default Sector Size.
FilePos FileSize
Type for the file size.
Definition file.h:41
a_util::datetime::DateTime getTimeCreation(const a_util::filesystem::Path filename)
This function returns the creation time of the file.