Browse Source

misc. pointer formatting

vanilla_fb_2.0.x
Scott Lahteine 4 years ago
committed by Scott Lahteine
parent
commit
ba5644376c
  1. 44
      Marlin/src/sd/SdBaseFile.cpp

44
Marlin/src/sd/SdBaseFile.cpp

@ -187,12 +187,11 @@ bool SdBaseFile::createContiguous(SdBaseFile* dirFile, const char *path, uint32_
* \return true for success, false for failure. * \return true for success, false for failure.
*/ */
bool SdBaseFile::dirEntry(dir_t *dir) { bool SdBaseFile::dirEntry(dir_t *dir) {
dir_t *p;
// make sure fields on SD are correct // make sure fields on SD are correct
if (!sync()) return false; if (!sync()) return false;
// read entry // read entry
p = cacheDirEntry(SdVolume::CACHE_FOR_READ); dir_t *p = cacheDirEntry(SdVolume::CACHE_FOR_READ);
if (!p) return false; if (!p) return false;
// copy to caller's struct // copy to caller's struct
@ -458,10 +457,6 @@ bool SdBaseFile::mkdir(SdBaseFile* parent, const char *path, bool pFlag) {
bool SdBaseFile::mkdir(SdBaseFile *parent, const uint8_t dname[11]) { bool SdBaseFile::mkdir(SdBaseFile *parent, const uint8_t dname[11]) {
if (ENABLED(SDCARD_READONLY)) return false; if (ENABLED(SDCARD_READONLY)) return false;
uint32_t block;
dir_t d;
dir_t *p;
if (!parent->isDir()) return false; if (!parent->isDir()) return false;
// create a normal file // create a normal file
@ -478,19 +473,20 @@ bool SdBaseFile::mkdir(SdBaseFile* parent, const uint8_t dname[11]) {
if (!sync()) return false; if (!sync()) return false;
// cache entry - should already be in cache due to sync() call // cache entry - should already be in cache due to sync() call
p = cacheDirEntry(SdVolume::CACHE_FOR_WRITE); dir_t *p = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
if (!p) return false; if (!p) return false;
// change directory entry attribute // change directory entry attribute
p->attributes = DIR_ATT_DIRECTORY; p->attributes = DIR_ATT_DIRECTORY;
// make entry for '.' // make entry for '.'
dir_t d;
memcpy(&d, p, sizeof(d)); memcpy(&d, p, sizeof(d));
d.name[0] = '.'; d.name[0] = '.';
LOOP_S_L_N(i, 1, 11) d.name[i] = ' '; LOOP_S_L_N(i, 1, 11) d.name[i] = ' ';
// cache block for '.' and '..' // cache block for '.' and '..'
block = vol_->clusterStartBlock(firstCluster_); uint32_t block = vol_->clusterStartBlock(firstCluster_);
if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_WRITE)) return false; if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_WRITE)) return false;
// copy '.' to block // copy '.' to block
@ -697,8 +693,6 @@ bool SdBaseFile::open(SdBaseFile* dirFile, const uint8_t dname[11], uint8_t ofla
* \return true for success or false for failure. * \return true for success or false for failure.
*/ */
bool SdBaseFile::open(SdBaseFile *dirFile, uint16_t index, uint8_t oflag) { bool SdBaseFile::open(SdBaseFile *dirFile, uint16_t index, uint8_t oflag) {
dir_t *p;
vol_ = dirFile->vol_; vol_ = dirFile->vol_;
// error if already open // error if already open
@ -711,7 +705,7 @@ bool SdBaseFile::open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag) {
if (!dirFile->seekSet(32 * index)) return false; if (!dirFile->seekSet(32 * index)) return false;
// read entry into cache // read entry into cache
p = dirFile->readDirCache(); dir_t *p = dirFile->readDirCache();
if (!p) return false; if (!p) return false;
// error if empty slot or '.' or '..' // error if empty slot or '.' or '..'
@ -785,9 +779,6 @@ bool SdBaseFile::openCachedEntry(uint8_t dirIndex, uint8_t oflag) {
* \return true for success or false for failure. * \return true for success or false for failure.
*/ */
bool SdBaseFile::openNext(SdBaseFile *dirFile, uint8_t oflag) { bool SdBaseFile::openNext(SdBaseFile *dirFile, uint8_t oflag) {
dir_t *p;
uint8_t index;
if (!dirFile) return false; if (!dirFile) return false;
// error if already open // error if already open
@ -796,10 +787,10 @@ bool SdBaseFile::openNext(SdBaseFile* dirFile, uint8_t oflag) {
vol_ = dirFile->vol_; vol_ = dirFile->vol_;
while (1) { while (1) {
index = 0xF & (dirFile->curPosition_ >> 5); uint8_t index = 0xF & (dirFile->curPosition_ >> 5);
// read entry into cache // read entry into cache
p = dirFile->readDirCache(); dir_t *p = dirFile->readDirCache();
if (!p) return false; if (!p) return false;
// done if last entry // done if last entry
@ -827,7 +818,6 @@ bool SdBaseFile::openNext(SdBaseFile* dirFile, uint8_t oflag) {
*/ */
bool SdBaseFile::openParent(SdBaseFile *dir) { bool SdBaseFile::openParent(SdBaseFile *dir) {
dir_t entry; dir_t entry;
dir_t *p;
SdBaseFile file; SdBaseFile file;
uint32_t c; uint32_t c;
uint32_t cluster; uint32_t cluster;
@ -850,7 +840,7 @@ bool SdBaseFile::openParent(SdBaseFile* dir) {
// first block of parent dir // first block of parent dir
if (!vol_->cacheRawBlock(lbn, SdVolume::CACHE_FOR_READ)) return false; if (!vol_->cacheRawBlock(lbn, SdVolume::CACHE_FOR_READ)) return false;
p = &vol_->cacheBuffer_.dir[1]; dir_t *p = &vol_->cacheBuffer_.dir[1];
// verify name for '../..' // verify name for '../..'
if (p->name[0] != '.' || p->name[1] != '.') return false; if (p->name[0] != '.' || p->name[1] != '.') return false;
// '..' is pointer to first cluster of parent. open '../..' to find parent // '..' is pointer to first cluster of parent. open '../..' to find parent
@ -1136,7 +1126,7 @@ int8_t SdBaseFile::readDir(dir_t *dir, char *longFilename) {
// Reset n to the start of the long name // Reset n to the start of the long name
n = 0; n = 0;
for (uint16_t idx = 0; idx < (LONG_FILENAME_LENGTH) / 2; idx += 2) { // idx is fixed since FAT LFN always contains UTF-16LE encoding for (uint16_t idx = 0; idx < (LONG_FILENAME_LENGTH) / 2; idx += 2) { // idx is fixed since FAT LFN always contains UTF-16LE encoding
uint16_t utf16_ch = longFilename[idx] | (longFilename[idx + 1] << 8); const uint16_t utf16_ch = longFilename[idx] | (longFilename[idx + 1] << 8);
if (0xD800 == (utf16_ch & 0xF800)) // Surrogate pair - encode as '_' if (0xD800 == (utf16_ch & 0xF800)) // Surrogate pair - encode as '_'
longFilename[n++] = '_'; longFilename[n++] = '_';
else if (0 == (utf16_ch & 0xFF80)) // Encode as 1-byte UTF-8 char else if (0 == (utf16_ch & 0xFF80)) // Encode as 1-byte UTF-8 char
@ -1199,12 +1189,11 @@ dir_t* SdBaseFile::readDirCache() {
bool SdBaseFile::remove() { bool SdBaseFile::remove() {
if (ENABLED(SDCARD_READONLY)) return false; if (ENABLED(SDCARD_READONLY)) return false;
dir_t *d;
// free any clusters - will fail if read-only or directory // free any clusters - will fail if read-only or directory
if (!truncate(0)) return false; if (!truncate(0)) return false;
// cache directory entry // cache directory entry
d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE); dir_t *d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
if (!d) return false; if (!d) return false;
// mark entry deleted // mark entry deleted
@ -1255,10 +1244,7 @@ bool SdBaseFile::remove(SdBaseFile* dirFile, const char *path) {
bool SdBaseFile::rename(SdBaseFile *dirFile, const char *newPath) { bool SdBaseFile::rename(SdBaseFile *dirFile, const char *newPath) {
if (ENABLED(SDCARD_READONLY)) return false; if (ENABLED(SDCARD_READONLY)) return false;
dir_t entry;
uint32_t dirCluster = 0; uint32_t dirCluster = 0;
SdBaseFile file;
dir_t *d;
// must be an open file or subdirectory // must be an open file or subdirectory
if (!(isFile() || isSubDir())) return false; if (!(isFile() || isSubDir())) return false;
@ -1268,16 +1254,18 @@ bool SdBaseFile::rename(SdBaseFile* dirFile, const char *newPath) {
// sync() and cache directory entry // sync() and cache directory entry
sync(); sync();
d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE); dir_t *d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
if (!d) return false; if (!d) return false;
// save directory entry // save directory entry
dir_t entry;
memcpy(&entry, d, sizeof(entry)); memcpy(&entry, d, sizeof(entry));
// mark entry deleted // mark entry deleted
d->name[0] = DIR_NAME_DELETED; d->name[0] = DIR_NAME_DELETED;
// make directory entry for new path // make directory entry for new path
SdBaseFile file;
if (isFile()) { if (isFile()) {
if (!file.open(dirFile, newPath, O_CREAT | O_EXCL | O_WRITE)) { if (!file.open(dirFile, newPath, O_CREAT | O_EXCL | O_WRITE)) {
goto restore; goto restore;
@ -1537,7 +1525,6 @@ bool SdBaseFile::sync() {
* \return true for success, false for failure. * \return true for success, false for failure.
*/ */
bool SdBaseFile::timestamp(SdBaseFile *file) { bool SdBaseFile::timestamp(SdBaseFile *file) {
dir_t *d;
dir_t dir; dir_t dir;
// get timestamps // get timestamps
@ -1546,7 +1533,7 @@ bool SdBaseFile::timestamp(SdBaseFile* file) {
// update directory fields // update directory fields
if (!sync()) return false; if (!sync()) return false;
d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE); dir_t *d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
if (!d) return false; if (!d) return false;
// copy timestamps // copy timestamps
@ -1599,7 +1586,6 @@ bool SdBaseFile::timestamp(uint8_t flags, uint16_t year, uint8_t month,
if (ENABLED(SDCARD_READONLY)) return false; if (ENABLED(SDCARD_READONLY)) return false;
uint16_t dirDate, dirTime; uint16_t dirDate, dirTime;
dir_t *d;
if (!isOpen() if (!isOpen()
|| year < 1980 || year < 1980
@ -1616,7 +1602,7 @@ bool SdBaseFile::timestamp(uint8_t flags, uint16_t year, uint8_t month,
// update directory entry // update directory entry
if (!sync()) return false; if (!sync()) return false;
d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE); dir_t *d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
if (!d) return false; if (!d) return false;
dirDate = FAT_DATE(year, month, day); dirDate = FAT_DATE(year, month, day);

Loading…
Cancel
Save