Prusa MINI Firmware overview
SdBaseFile Class Reference

Base class for SdFile with Print and C++ streams. More...

#include <SdBaseFile.h>

Inheritance diagram for SdBaseFile:
Collaboration diagram for SdBaseFile:

Public Member Functions

 SdBaseFile ()
 
 SdBaseFile (const char *path, uint8_t oflag)
 
 ~SdBaseFile ()
 
void getpos (filepos_t *pos)
 
void setpos (filepos_t *pos)
 
bool close ()
 
bool contiguousRange (uint32_t *bgnBlock, uint32_t *endBlock)
 
bool createContiguous (SdBaseFile *dirFile, const char *path, uint32_t size)
 
uint32_t curCluster () const
 
uint32_t curPosition () const
 
bool dirEntry (dir_t *dir)
 
bool exists (const char *name)
 
int16_t fgets (char *str, int16_t num, char *delim=0)
 
uint32_t fileSize () const
 
uint32_t firstCluster () const
 
bool isDir () const
 
bool isFile () const
 
bool isOpen () const
 
bool isSubDir () const
 
bool isRoot () const
 
bool getDosName (char *const name)
 
void ls (uint8_t flags=0, uint8_t indent=0)
 
bool mkdir (SdBaseFile *dir, const char *path, bool pFlag=true)
 
bool open (SdBaseFile *dirFile, uint16_t index, uint8_t oflag)
 
bool open (SdBaseFile *dirFile, const char *path, uint8_t oflag)
 
bool open (const char *path, uint8_t oflag=O_READ)
 
bool openNext (SdBaseFile *dirFile, uint8_t oflag)
 
bool openRoot (SdVolume *vol)
 
int peek ()
 
bool printName ()
 
int16_t read ()
 
int16_t read (void *buf, uint16_t nbyte)
 
int8_t readDir (dir_t *dir, char *longFilename)
 
bool remove ()
 
void rewind ()
 
bool rename (SdBaseFile *dirFile, const char *newPath)
 
bool rmdir ()
 
bool rmRfStar ()
 
bool seekCur (const int32_t offset)
 
bool seekEnd (const int32_t offset=0)
 
bool seekSet (const uint32_t pos)
 
bool sync ()
 
bool timestamp (SdBaseFile *file)
 
bool timestamp (uint8_t flag, uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
 
uint8_t type () const
 
bool truncate (uint32_t size)
 
SdVolumevolume () const
 
int16_t write (const void *buf, uint16_t nbyte)
 

Static Public Member Functions

static SdBaseFilecwd ()
 
static void dateTimeCallback (void(*dateTime)(uint16_t *date, uint16_t *time))
 
static void dateTimeCallbackCancel ()
 
static void dirName (const dir_t &dir, char *name)
 
static void printFatDate (uint16_t fatDate)
 
static void printFatTime (uint16_t fatTime)
 
static bool remove (SdBaseFile *dirFile, const char *path)
 

Public Attributes

bool writeError
 

Friends

class SdFat
 

Detailed Description

Base class for SdFile with Print and C++ streams.

Constructor & Destructor Documentation

◆ SdBaseFile() [1/2]

SdBaseFile::SdBaseFile ( )
168 : writeError(false), type_(FAT_FILE_TYPE_CLOSED) {}

◆ SdBaseFile() [2/2]

SdBaseFile::SdBaseFile ( const char *  path,
uint8_t  oflag 
)

◆ ~SdBaseFile()

SdBaseFile::~SdBaseFile ( )
170 { if (isOpen()) close(); }
Here is the call graph for this function:

Member Function Documentation

◆ getpos()

void SdBaseFile::getpos ( filepos_t pos)

get position for streams

Parameters
[out]posstruct to receive position

◆ setpos()

void SdBaseFile::setpos ( filepos_t pos)

set position for streams

Parameters
[out]posstruct with value for new position

◆ close()

bool SdBaseFile::close ( )
535  {
536  Slot *slot;
537  bool success = false;
538 
539  if (!isOpen())
540  return false;
541  switch (type_) {
543  slot = _slot_of(this);
544  success = f_close(&slot->file) == FR_OK;
545  Slot::free(slot);
546  _slot_of(this) = nullptr;
547  break;
551  slot = _slot_of(this);
552  success = f_closedir(&slot->directory) == FR_OK;
553  Slot::free(slot);
554  _slot_of(this) = nullptr;
555  break;
556  default:
557  return false;
558  }
559 
560  type_ = FAT_FILE_TYPE_CLOSED;
561  return success;
562 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ contiguousRange()

bool SdBaseFile::contiguousRange ( uint32_t *  bgnBlock,
uint32_t *  endBlock 
)

◆ createContiguous()

bool SdBaseFile::createContiguous ( SdBaseFile dirFile,
const char *  path,
uint32_t  size 
)

◆ curCluster()

uint32_t SdBaseFile::curCluster ( ) const
Returns
The current cluster number for a file or directory.
200 { return curCluster_; }

◆ curPosition()

uint32_t SdBaseFile::curPosition ( ) const
Returns
The current position for a file or directory.
205 { return curPosition_; }

◆ cwd()

static SdBaseFile* SdBaseFile::cwd ( )
static
Returns
Current working directory
210 { return cwd_; }

◆ dateTimeCallback()

static void SdBaseFile::dateTimeCallback ( void(*)(uint16_t *date, uint16_t *time)  dateTime)
static

Set the date/time callback function

Parameters
[in]dateTimeThe user's call back function. The callback function is of the form:
void dateTime(uint16_t* date, uint16_t* time) {
uint16_t year;
uint8_t month, day, hour, minute, second;
// User gets date and time from GPS or real-time clock here
// return date using FAT_DATE macro to format fields
*date = FAT_DATE(year, month, day);
// return time using FAT_TIME macro to format fields
*time = FAT_TIME(hour, minute, second);
}

Sets the function that is called when a file is created or when a file's directory entry is modified by sync(). All timestamps, access, creation, and modify, are set when a file is created. sync() maintains the last access date and last modify date/time.

See the timestamp() function.

241  {
242  dateTime_ = dateTime;
243  }

◆ dateTimeCallbackCancel()

static void SdBaseFile::dateTimeCallbackCancel ( )
static

Cancel the date/time callback function.

248 { dateTime_ = 0; }

◆ dirEntry()

bool SdBaseFile::dirEntry ( dir_t dir)

◆ dirName()

static void SdBaseFile::dirName ( const dir_t dir,
char *  name 
)
static

◆ exists()

bool SdBaseFile::exists ( const char *  name)

◆ fgets()

int16_t SdBaseFile::fgets ( char *  str,
int16_t  num,
char *  delim = 0 
)

◆ fileSize()

uint32_t SdBaseFile::fileSize ( ) const
Returns
The total number of bytes in a file or directory.
257 { return fileSize_; }

◆ firstCluster()

uint32_t SdBaseFile::firstCluster ( ) const
Returns
The first cluster number for a file or directory.
262 { return firstCluster_; }

◆ isDir()

bool SdBaseFile::isDir ( ) const
Returns
True if this is a directory else false.
267 { return type_ >= FAT_FILE_TYPE_MIN_DIR; }
Here is the caller graph for this function:

◆ isFile()

bool SdBaseFile::isFile ( ) const
Returns
True if this is a normal file else false.
272 { return type_ == FAT_FILE_TYPE_NORMAL; }

◆ isOpen()

bool SdBaseFile::isOpen ( ) const
Returns
True if this is an open file/directory else false.
277 { return type_ != FAT_FILE_TYPE_CLOSED; }
Here is the caller graph for this function:

◆ isSubDir()

bool SdBaseFile::isSubDir ( ) const
Returns
True if this is a subdirectory else false.
282 { return type_ == FAT_FILE_TYPE_SUBDIR; }

◆ isRoot()

bool SdBaseFile::isRoot ( ) const
Returns
True if this is the root directory.
287 { return type_ == FAT_FILE_TYPE_ROOT_FIXED || type_ == FAT_FILE_TYPE_ROOT32; }
Here is the caller graph for this function:

◆ getDosName()

bool SdBaseFile::getDosName ( char *const  name)
440  {
441  if (!isOpen())
442  return false;
443  if (isRoot()) {
444  name[0] = '/';
445  name[1] = '\0';
446  return true;
447  }
448  if (isDir()) {
449  DIR *pd = &_slot_of(this)->directory;
450  fname_from_8_3(name, (char *)pd->fn);
451  return true;
452  } else {
453  // TODO: fix
454  bsod("get dos name of a file");
455  }
456  return false;
457 }
Here is the call graph for this function:

◆ ls()

void SdBaseFile::ls ( uint8_t  flags = 0,
uint8_t  indent = 0 
)

◆ mkdir()

bool SdBaseFile::mkdir ( SdBaseFile dir,
const char *  path,
bool  pFlag = true 
)

◆ open() [1/3]

bool SdBaseFile::open ( SdBaseFile dirFile,
uint16_t  index,
uint8_t  oflag 
)

◆ open() [2/3]

bool SdBaseFile::open ( SdBaseFile dirFile,
const char *  path,
uint8_t  oflag 
)
564  {
565  if (!dir || isOpen())
566  return false;
567 
568  // prepare full path
569  char fullpath[256] = { 0 };
570  if (dir->isRoot()) {
571  strcat(fullpath, "/");
572  strcat(fullpath, path);
573  } else {
574  auto dirp = &(_slot_of(dir)->directory);
575  f_getdirpath(dirp, fullpath, sizeof(fullpath));
576  strcat(fullpath, "/");
577  strcat(fullpath, path);
578  }
579 
580  // get file info
581  FILINFO nfo;
582  if (f_stat(fullpath, &nfo) != FR_OK)
583  return false;
584 
585  if (nfo.fattrib & AM_DIR) {
586  auto slot = Slot::allocate(Slot::Directory);
587  if (!slot)
588  return false;
589 
590  if (f_opendir(&slot->directory, path) == FR_OK) {
591  _slot_of(this) = slot;
592  type_ = FAT_FILE_TYPE_SUBDIR;
593  curPosition_ = 0;
594  fileSize_ = f_size(&slot->directory);
595  return true;
596  } else {
597  Slot::free(slot);
598  return false;
599  }
600  } else {
601  auto slot = Slot::allocate(Slot::File);
602  if (!slot)
603  return false;
604 
605  BYTE mode = 0;
606  if (oflag & O_READ)
607  mode |= FA_READ;
608  if (oflag & O_WRITE)
609  mode |= FA_WRITE;
610 
611  if (f_open(&slot->file, fullpath, mode) == FR_OK) {
612  _slot_of(this) = slot;
613  type_ = FAT_FILE_TYPE_NORMAL;
614  curPosition_ = 0;
615  fileSize_ = f_size(&slot->file);
616  return true;
617  } else {
618  Slot::free(slot);
619  return false;
620  }
621  }
622  return true;
623 }
Here is the call graph for this function:

◆ open() [3/3]

bool SdBaseFile::open ( const char *  path,
uint8_t  oflag = O_READ 
)

◆ openNext()

bool SdBaseFile::openNext ( SdBaseFile dirFile,
uint8_t  oflag 
)

◆ openRoot()

bool SdBaseFile::openRoot ( SdVolume vol)

◆ peek()

int SdBaseFile::peek ( )

◆ printFatDate()

static void SdBaseFile::printFatDate ( uint16_t  fatDate)
static

◆ printFatTime()

static void SdBaseFile::printFatTime ( uint16_t  fatTime)
static

◆ printName()

bool SdBaseFile::printName ( )

◆ read() [1/2]

int16_t SdBaseFile::read ( void  )
523  {
524  if (type_ != FAT_FILE_TYPE_NORMAL)
525  return -1;
526 
527  uint8_t b;
528  UINT c;
529  if (f_read(&_slot_of(this)->file, &b, 1, &c) != FR_OK)
530  return -1;
531  curPosition_++;
532  return b;
533 }
Here is the call graph for this function:

◆ read() [2/2]

int16_t SdBaseFile::read ( void buf,
uint16_t  nbyte 
)
518  {
519  DBG("NOT IMPLEMENTED: read(...)");
520  return 0;
521 }

◆ readDir()

int8_t SdBaseFile::readDir ( dir_t dir,
char *  longFilename 
)
459  {
460  // TODO: cleanup (copied from previous implementation)
461  FRESULT res;
462  FILINFO nfo;
463  char *str;
464 #if _USE_LFN != 0
465  char *altname = nfo.altname;
466  char *fname = nfo.fname;
467 #else
468  char *altname = nfo.fname;
469  // char* fname = 0;
470 #endif
471  if (longFilename)
472  longFilename[0] = '\0';
473  while (1) {
474  res = f_readdir(&_slot_of(this)->directory, &nfo);
475  if (res != FR_OK)
476  return -1;
477  memset(dir->name, ' ', 11);
478  str = strchr(altname, '.');
479  if (str) {
480  strncpy(((char *)dir->name) + 0, altname, str - altname);
481  strncpy(((char *)dir->name) + 8, str + 1, 3);
482  } else {
483  strncpy(((char *)dir->name) + 0, altname, 8);
484  }
485  if (dir->name[0] == DIR_NAME_FREE)
486  return 0;
487  if (dir->name[0] == DIR_NAME_DELETED || dir->name[0] == '.') {
488  // if (longFilename) longFilename[0] = '\0'; //
489  // Invalidate erased file long name, if any
490  continue;
491  }
492 #if _USE_LFN != 0
493  if (longFilename)
494  strncpy(longFilename, fname, _MAX_LFN);
495 #endif
496  dir->attributes = nfo.fattrib;
497  dir->lastWriteDate = nfo.fdate;
498  dir->lastWriteTime = nfo.ftime;
499  dir->fileSize = nfo.fsize;
500  return sizeof(dir_t);
501  }
502  return 0;
503 }
Here is the call graph for this function:

◆ remove() [1/2]

static bool SdBaseFile::remove ( SdBaseFile dirFile,
const char *  path 
)
static

◆ remove() [2/2]

bool SdBaseFile::remove ( )

◆ rewind()

void SdBaseFile::rewind ( )

Set the file's current position to zero.

311 { seekSet(0); }
Here is the call graph for this function:

◆ rename()

bool SdBaseFile::rename ( SdBaseFile dirFile,
const char *  newPath 
)

◆ rmdir()

bool SdBaseFile::rmdir ( )

◆ rmRfStar()

bool SdBaseFile::rmRfStar ( )

◆ seekCur()

bool SdBaseFile::seekCur ( const int32_t  offset)

Set the files position to current position + pos. See seekSet().

Parameters
[in]offsetThe new position in bytes from the current position.
Returns
true for success or false for failure.
321 { return seekSet(curPosition_ + offset); }
Here is the call graph for this function:

◆ seekEnd()

bool SdBaseFile::seekEnd ( const int32_t  offset = 0)

Set the files position to end-of-file + offset. See seekSet().

Parameters
[in]offsetThe new position in bytes from end-of-file.
Returns
true for success or false for failure.
328 { return seekSet(fileSize_ + offset); }
Here is the call graph for this function:

◆ seekSet()

bool SdBaseFile::seekSet ( const uint32_t  pos)
505  {
506  if ((pos == 0) && ((type_ == FAT_FILE_TYPE_ROOT_FIXED) || (type_ == FAT_FILE_TYPE_ROOT32) || (type_ == FAT_FILE_TYPE_SUBDIR))) {
507  return f_rewinddir(&_slot_of(this)->directory) == FR_OK;
508  }
509  if (type_ != FAT_FILE_TYPE_NORMAL)
510  return false;
511  FSIZE_t szf = pos;
512  if (f_lseek(&_slot_of(this)->file, szf) != FR_OK)
513  return false;
514  curPosition_ = pos;
515  return true;
516 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sync()

bool SdBaseFile::sync ( )

◆ timestamp() [1/2]

bool SdBaseFile::timestamp ( SdBaseFile file)

◆ timestamp() [2/2]

bool SdBaseFile::timestamp ( uint8_t  flag,
uint16_t  year,
uint8_t  month,
uint8_t  day,
uint8_t  hour,
uint8_t  minute,
uint8_t  second 
)

◆ type()

uint8_t SdBaseFile::type ( ) const

Type of file. Use isFile() or isDir() instead of type() if possible.

Returns
The file or directory type.
340 { return type_; }

◆ truncate()

bool SdBaseFile::truncate ( uint32_t  size)

◆ volume()

SdVolume* SdBaseFile::volume ( ) const
Returns
SdVolume that contains this file.
346 { return vol_; }

◆ write()

int16_t SdBaseFile::write ( const void buf,
uint16_t  nbyte 
)

Friends And Related Function Documentation

◆ SdFat

friend class SdFat
friend

Member Data Documentation

◆ writeError

bool SdBaseFile::writeError

writeError is set to true if an error occurs during a write(). Set writeError to false before calling print() and/or write() and check for true after calls to print() and/or write().

SdBaseFile::seekSet
bool seekSet(const uint32_t pos)
Definition: Marlin_CardReader.cpp:505
FAT_FILE_TYPE_NORMAL
const uint8_t FAT_FILE_TYPE_NORMAL
Definition: SdBaseFile.h:81
FAT_FILE_TYPE_SUBDIR
const uint8_t FAT_FILE_TYPE_SUBDIR
Definition: SdBaseFile.h:84
FAT_FILE_TYPE_ROOT_FIXED
const uint8_t FAT_FILE_TYPE_ROOT_FIXED
Definition: SdBaseFile.h:82
directoryEntry::lastWriteTime
uint16_t lastWriteTime
Definition: SdFatStructs.h:522
fname_from_8_3
static void fname_from_8_3(char *fn, char *fn83)
Definition: Marlin_CardReader.cpp:418
f_stat
FRESULT f_stat(const TCHAR *path, FILINFO *fno)
Definition: ff.c:4412
FA_WRITE
#define FA_WRITE
Definition: ff.h:327
FILINFO::fattrib
BYTE fattrib
Definition: ff.h:201
pd
#define pd
Definition: screen_filebrowser.cpp:39
f_open
FRESULT f_open(FIL *fp, const TCHAR *path, BYTE mode)
Definition: ff.c:3296
Slot::file
FIL file
Definition: Marlin_CardReader.cpp:41
SdBaseFile::close
bool close()
Definition: Marlin_CardReader.cpp:535
FILINFO::fdate
WORD fdate
Definition: ff.h:199
DIR_NAME_DELETED
const uint8_t DIR_NAME_DELETED
Definition: SdFatStructs.h:561
FAT_TIME
static uint16_t FAT_TIME(uint8_t hour, uint8_t minute, uint8_t second)
Definition: SdBaseFile.h:129
f_rewinddir
#define f_rewinddir(dp)
Definition: ff.h:282
f_getdirpath
FRESULT f_getdirpath(DIR *pd, TCHAR *buff, UINT len)
Slot::directory
DIR directory
Definition: Marlin_CardReader.cpp:42
FSIZE_t
DWORD FSIZE_t
Definition: ff.h:78
_slot_of
#define _slot_of(x)
Definition: Marlin_CardReader.cpp:83
O_WRITE
const uint8_t O_WRITE
Definition: SdBaseFile.h:55
SdBaseFile::isOpen
bool isOpen() const
Definition: SdBaseFile.h:277
Slot
Definition: Marlin_CardReader.cpp:31
FA_READ
#define FA_READ
Definition: ff.h:326
dir_t
struct directoryEntry dir_t
Definition: SdFatStructs.h:557
f_size
#define f_size(fp)
Definition: ff.h:280
FILINFO::ftime
WORD ftime
Definition: ff.h:200
directoryEntry::lastWriteDate
uint16_t lastWriteDate
Definition: SdFatStructs.h:523
f_opendir
FRESULT f_opendir(DIR *dp, const TCHAR *path)
Definition: ff.c:4221
directoryEntry::name
uint8_t name[11]
Definition: SdFatStructs.h:486
DBG
#define DBG
Definition: Marlin_CardReader.cpp:9
_MAX_LFN
#define _MAX_LFN
Definition: ffconf.h:150
FAT_DATE
static uint16_t FAT_DATE(uint16_t year, uint8_t month, uint8_t day)
Definition: SdBaseFile.h:95
f_lseek
FRESULT f_lseek(FIL *fp, FSIZE_t ofs)
Definition: ff.c:4060
Slot::free
static void free(Slot *slot)
Definition: Marlin_CardReader.cpp:62
directoryEntry::attributes
uint8_t attributes
Definition: SdFatStructs.h:494
FAT_FILE_TYPE_MIN_DIR
const uint8_t FAT_FILE_TYPE_MIN_DIR
Definition: SdBaseFile.h:85
uint8_t
const uint8_t[]
Definition: 404_html.c:3
FILINFO::fname
TCHAR fname[13]
Definition: ff.h:206
FAT_FILE_TYPE_CLOSED
const uint8_t FAT_FILE_TYPE_CLOSED
Definition: SdBaseFile.h:80
FRESULT
FRESULT
Definition: ff.h:214
AM_DIR
#define AM_DIR
Definition: ff.h:354
SdBaseFile::writeError
bool writeError
Definition: SdBaseFile.h:177
FR_OK
Definition: ff.h:215
DIR
Definition: ff.h:178
bsod
#define bsod(fmt,...)
Definition: bsod.h:20
Slot::File
Definition: Marlin_CardReader.cpp:34
BYTE
unsigned char BYTE
Definition: onboard_sd.h:13
directoryEntry::fileSize
uint32_t fileSize
Definition: SdFatStructs.h:525
f_closedir
FRESULT f_closedir(DIR *dp)
Definition: ff.c:4291
SdBaseFile::isRoot
bool isRoot() const
Definition: SdBaseFile.h:287
mode
png_structrp int mode
Definition: png.h:1139
O_READ
const uint8_t O_READ
Definition: SdBaseFile.h:53
createSpeedLookupTable.b
list b
Definition: createSpeedLookupTable.py:30
Slot::allocate
static Slot * allocate(Type type)
Definition: Marlin_CardReader.cpp:51
FILINFO
Definition: ff.h:197
UINT
unsigned int UINT
Definition: onboard_sd.h:16
DIR_NAME_FREE
const uint8_t DIR_NAME_FREE
Definition: SdFatStructs.h:562
f_readdir
FRESULT f_readdir(DIR *dp, FILINFO *fno)
Definition: ff.c:4324
f_read
FRESULT f_read(FIL *fp, void *buff, UINT btr, UINT *br)
Definition: ff.c:3499
f_close
FRESULT f_close(FIL *fp)
Definition: ff.c:3801
SdBaseFile::isDir
bool isDir() const
Definition: SdBaseFile.h:267
Slot::Directory
Definition: Marlin_CardReader.cpp:35
FILINFO::fsize
FSIZE_t fsize
Definition: ff.h:198
FAT_FILE_TYPE_ROOT32
const uint8_t FAT_FILE_TYPE_ROOT32
Definition: SdBaseFile.h:83