Add support for finding file position, or positionning from and of file, as this is available in other OSes

This commit is contained in:
Vincent Barrilliot 2021-11-12 12:23:55 +00:00
parent 1990e0761d
commit 47478a73af
2 changed files with 17 additions and 10 deletions

View file

@ -38,6 +38,7 @@
#define CDEV_SEEK_ABSOLUTE 0 #define CDEV_SEEK_ABSOLUTE 0
#define CDEV_SEEK_RELATIVE 1 #define CDEV_SEEK_RELATIVE 1
#define CDEV_SEEK_END 2
/* /*
* Structure defining a channel * Structure defining a channel

View file

@ -544,23 +544,29 @@ short fchan_flush(t_channel * chan) {
} }
/** /**
* attempt to move the "cursor" position in the channel * Attempt to move the "cursor" position in the channel
*/ */
short fchan_seek(t_channel * chan, long position, short base) { short fchan_seek(t_channel * chan, long position, short base) {
FIL * file; FIL * file;
FRESULT result; FSIZE_t new_position;
int total_written;
file = fchan_to_file(chan); file = fchan_to_file(chan);
if (file) { if (file) {
if (base == CDEV_SEEK_ABSOLUTE) { switch (base) {
result = f_lseek(file, position); case CDEV_SEEK_ABSOLUTE:
return fatfs_to_foenix(result); new_position = position;
} else if (base == CDEV_SEEK_RELATIVE) { break;
long current = f_tell(file); case CDEV_SEEK_RELATIVE:
result = f_lseek(file, current + position); new_position = f_tell(file) + position;
return fatfs_to_foenix(result); break;
case CDEV_SEEK_END:
new_position = f_size(file) - position;
break;
default:
return ERR_GENERAL;
} }
return fatfs_to_foenix(f_lseek(file, position));
} }
return ERR_BADCHANNEL; return ERR_BADCHANNEL;