69 if (stream->eof == 1) {
71 Serial.println(
"EOF");
77 Serial.println(
"NOT EOF");
96 return (stream) ? !(stream->f.seek(*pos)) : 1;
104 *pos = (stream) ? stream->f.position() : 0;
116 if ((strcmp(mode,
"r") == 0) || (strcmp(mode,
"rb") == 0)) {
120 if (!SD.exists(filename)) {
124 operation = FILE_READ;
126 else if ((strcmp(mode,
"w") == 0) || (strcmp(mode,
"wb") == 0)) {
132 if (SD.exists(filename)) {
136 operation = FILE_WRITE;
139 else if (strstr(mode,
"r+") != NULL) {
140 if (!SD.exists(filename)) {
144 operation = FILE_WRITE;
148 else if (strstr(mode,
"w+") != NULL) {
149 if (SD.exists(filename)) {
153 operation = FILE_WRITE;
155 else if (strcmp(mode,
"a+") == 0) {
156 operation = FILE_WRITE;
164 (file)->
f = SD.open(filename, operation);
185 int num_bytes = stream->f.read((
char *) ptr, size * nmemb);
188 Serial.print(
"Bytes read : ");
189 Serial.println(num_bytes);
192 if (num_bytes < size * nmemb) {
194 Serial.println(
"End of file");
199 return num_bytes / size;
208 if (NULL == stream) {
212 unsigned long cur_pos = stream->f.position();
213 unsigned long cur_end = stream->f.size();
224 if (offset > cur_end) {
225 if (!stream->f.seek(cur_end)) {
229 unsigned long bytes_to_pad = offset - cur_end;
231 size_t num_written =
sd_fwrite(&payload,
sizeof(payload), bytes_to_pad, stream);
233 if (num_written != bytes_to_pad) {
241 return stream->f.seek(offset) ? 0 : -1;
246 if (offset + cur_pos > cur_end) {
247 if (!stream->f.seek(cur_end)) {
251 unsigned long bytes_to_pad = (offset + cur_pos) - cur_end;
253 size_t num_written =
sd_fwrite(&payload,
sizeof(payload), bytes_to_pad, stream);
255 if (num_written != bytes_to_pad) {
263 if (offset - cur_pos < 0) {
267 return stream->f.seek(cur_pos + offset) ? 0 : -1;
273 if (!stream->f.seek(cur_end)) {
277 unsigned long bytes_to_pad = offset;
279 size_t num_written =
sd_fwrite(&payload,
sizeof(payload), bytes_to_pad, stream);
281 if (num_written != bytes_to_pad) {
289 return stream->f.seek(cur_end + offset) ? 0 : -1;
302 long int pos = (stream) ? stream->f.position() : -1;
305 Serial.print(
"cur pos: ");
319 size_t bytes_written = 0;
320 size_t total_count = 0;
323 for (idx = 0; idx < nmemb; idx++) {
324 bytes_written = stream->f.write((uint8_t *) ptr, size);
326 if (bytes_written != size) {
333 if (total_count != nmemb) {
345 return SD.remove(filename) ? 0 : 1;
360 return SD.begin(csPin);
374 return (
int) (SD.exists(filepath));
380 File root = SD.open(
"/");
383 File entry = root.openNextFile();
391 bool is_ok = SD.remove(entry.name());
long int sd_ftell(SD_FILE *stream)
size_t sd_fread(void *ptr, size_t size, size_t nmemb, SD_FILE *stream)
int sd_fgetpos(SD_FILE *stream, ion_fpos_t *pos)
A structure that translates a file object to a C-compatible struct.
int sd_fclose(SD_FILE *stream)
size_t sd_fwrite(void *ptr, size_t size, size_t nmemb, SD_FILE *stream)
int sd_fseek(SD_FILE *stream, long int offset, int whence)
int SD_File_Begin(uint8_t csPin)
int SD_File_Exists(char *filepath)
int sd_feof(SD_FILE *stream)
This code contains definitions for stdio.h file functions for Arduino flash libraries.
int sd_fflush(SD_FILE *stream)
void sd_rewind(SD_FILE *stream)
int sd_remove(char *filename)
SD_FILE * sd_fopen(char *filename, char *mode)
int sd_fsetpos(SD_FILE *stream, ion_fpos_t *pos)
long ion_fpos_t
A file position type.
char ion_boolean_t
A boolean type.