|
| 1 | +#include "SD_CI.h" |
| 2 | + |
| 3 | +#ifdef MOCK_PINS_COUNT |
| 4 | + |
| 5 | +#include <cassert> |
| 6 | + |
| 7 | +file_ci::file_ci(String name) { |
| 8 | + this->name = name; |
| 9 | + this->contents = static_cast<uint8_t *>(malloc(0)); |
| 10 | + this->size = 0; |
| 11 | +} |
| 12 | + |
| 13 | +file_ci::~file_ci() { free(this->contents); } |
| 14 | + |
| 15 | +/* |
| 16 | +#define O_RDONLY 0x00 ///< Open for reading only. |
| 17 | +#define O_WRONLY 0x01 ///< Open for writing only. |
| 18 | +#define O_RDWR 0x02 ///< Open for reading and writing. |
| 19 | +#define O_AT_END 0x04 ///< Open at EOF. |
| 20 | +#define O_APPEND 0x08 ///< Set append mode. |
| 21 | +#define O_TRUNC 0x20 ///< Truncate this->file to zero length. |
| 22 | +*/ |
| 23 | +File_CI::File_CI(file_ci *file, oflag_t oflag) { |
| 24 | + this->file = file; |
| 25 | + this->oflag = oflag; |
| 26 | + if (oflag & O_TRUNC) { |
| 27 | + if (this->file->size) { |
| 28 | + free(this->file->contents); |
| 29 | + this->file->contents = static_cast<uint8_t *>(malloc(0)); |
| 30 | + this->file->size = 0; |
| 31 | + } |
| 32 | + this->_position = 0; |
| 33 | + } else if (oflag & O_AT_END) { |
| 34 | + this->_position = this->size(); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +uint32_t File_CI::available32() const { |
| 39 | + return this->size() - this->position(); |
| 40 | +} |
| 41 | + |
| 42 | +bool File_CI::close() { |
| 43 | + bool flag = this->file != nullptr; |
| 44 | + this->file = nullptr; |
| 45 | + return flag; |
| 46 | +} |
| 47 | + |
| 48 | +void File_CI::flush() { assert(this->file != nullptr); } |
| 49 | + |
| 50 | +size_t File_CI::getName(char *name, size_t size) { |
| 51 | + assert(this->file != nullptr); |
| 52 | + strncpy(name, this->file->name.c_str(), size); |
| 53 | + return min(size, this->file->name.length()); |
| 54 | +} |
| 55 | + |
| 56 | +const char *File_CI::name() const { |
| 57 | + assert(this->file != nullptr); |
| 58 | + return this->file->name.c_str(); |
| 59 | +} |
| 60 | + |
| 61 | +uint32_t File_CI::position() const { |
| 62 | + assert(this->file != nullptr); |
| 63 | + return this->_position; |
| 64 | +} |
| 65 | + |
| 66 | +int File_CI::read() { |
| 67 | + assert(this->file != nullptr); |
| 68 | + if (this->oflag == O_WRONLY) { |
| 69 | + return -1; |
| 70 | + } |
| 71 | + if (this->available32()) { |
| 72 | + uint8_t *ptr = |
| 73 | + static_cast<uint8_t *>(this->file->contents + this->_position); |
| 74 | + int value = *ptr; |
| 75 | + ++(this->_position); |
| 76 | + return value; |
| 77 | + } else { |
| 78 | + return -1; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +int File_CI::read(void *buf, size_t count) { |
| 83 | + assert(this->file != nullptr); |
| 84 | + if (this->oflag == O_WRONLY) { |
| 85 | + return -1; |
| 86 | + } |
| 87 | + int size = min(count, this->available32()); |
| 88 | + memcpy(buf, this->file->contents + this->_position, size); |
| 89 | + this->_position += size; |
| 90 | + return size; |
| 91 | +} |
| 92 | + |
| 93 | +void File_CI::rewind() { |
| 94 | + assert(this->file != nullptr); |
| 95 | + this->_position = 0; |
| 96 | +} |
| 97 | + |
| 98 | +bool File_CI::seek(uint32_t pos) { |
| 99 | + assert(this->file != nullptr); |
| 100 | + this->_position = pos; |
| 101 | + return true; |
| 102 | +} |
| 103 | + |
| 104 | +bool File_CI::seekEnd(int32_t offset) { |
| 105 | + assert(this->file != nullptr); |
| 106 | + this->seek(this->size() + offset); |
| 107 | + return true; |
| 108 | +} |
| 109 | + |
| 110 | +uint32_t File_CI::size() const { |
| 111 | + assert(this->file != nullptr); |
| 112 | + return this->file->size; |
| 113 | +} |
| 114 | + |
| 115 | +bool File_CI::truncate() { |
| 116 | + assert(this->file != nullptr); |
| 117 | + uint8_t *ptr = static_cast<uint8_t *>(malloc(this->_position)); |
| 118 | + if (!ptr) { |
| 119 | + return false; |
| 120 | + } |
| 121 | + memcpy(ptr, this->file->contents, this->_position); |
| 122 | + free(this->file->contents); |
| 123 | + this->file->contents = ptr; |
| 124 | + this->file->size = this->_position; |
| 125 | + return true; |
| 126 | +} |
| 127 | + |
| 128 | +bool File_CI::truncate(uint32_t length) { |
| 129 | + assert(this->file != nullptr); |
| 130 | + this->_position = length; |
| 131 | + return this->truncate(); |
| 132 | +} |
| 133 | + |
| 134 | +size_t File_CI::write(const char *str) { |
| 135 | + assert(this->file != nullptr); |
| 136 | + if (this->oflag == O_RDONLY) { |
| 137 | + return -1; |
| 138 | + } |
| 139 | + return this->write(static_cast<const void *>(str), strlen(str)); |
| 140 | +} |
| 141 | + |
| 142 | +size_t File_CI::write(const void *buf, size_t count) { |
| 143 | + assert(this->file != nullptr); |
| 144 | + if (this->oflag == O_RDONLY) { |
| 145 | + return -1; |
| 146 | + } |
| 147 | + if (this->available32() < count) { |
| 148 | + uint8_t *ptr = static_cast<uint8_t *>(malloc(this->_position + count)); |
| 149 | + if (!ptr) { |
| 150 | + return -1; |
| 151 | + } |
| 152 | + memcpy(ptr, this->file->contents, this->_position); |
| 153 | + free(this->file->contents); |
| 154 | + this->file->contents = ptr; |
| 155 | + this->file->size = this->_position + count; |
| 156 | + } |
| 157 | + memcpy(this->file->contents + this->_position, buf, count); |
| 158 | + this->_position += count; |
| 159 | + return count; |
| 160 | +} |
| 161 | + |
| 162 | +File_CI::operator bool() { return this->file != nullptr; } |
| 163 | + |
| 164 | +#endif |
0 commit comments