Skip to content

Commit e372d80

Browse files
committed
staticDIR to FatFS
1 parent a13d54f commit e372d80

File tree

8 files changed

+42
-50
lines changed

8 files changed

+42
-50
lines changed

src/deluge/gui/ui/browser/browser.cpp

+10-17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "gui/ui/browser/browser.h"
1919
#include "definitions_cxx.hpp"
2020
#include "extern.h"
21+
#include "fatfs.hpp"
2122
#include "gui/context_menu/delete_file.h"
2223
#include "gui/l10n/l10n.h"
2324
#include "gui/ui_timer_manager.h"
@@ -35,6 +36,7 @@
3536
#include "storage/file_item.h"
3637
#include "storage/storage_manager.h"
3738
#include "util/functions.h"
39+
#include "util/try.h"
3840
#include <cstring>
3941
#include <new>
4042

@@ -269,18 +271,8 @@ Error Browser::readFileItemsForFolder(char const* filePrefixHere, bool allowFold
269271
return error;
270272
}
271273

272-
FRESULT result = f_opendir(&staticDIR, currentDir.get());
273-
if (result) {
274-
return fresultToDelugeErrorCode(result);
275-
}
276-
277-
/*
278-
error = fileItems.allocateMemory(FILE_ITEMS_MAX_NUM_ELEMENTS, false);
279-
if (error != Error::NONE) {
280-
f_closedir(&staticDIR);
281-
return error;
282-
}
283-
*/
274+
staticDIR =
275+
D_TRY_CATCH(FatFS::Directory::open(currentDir.get()), error, { return fatfsErrorToDelugeError(error); });
284276

285277
numFileItemsDeletedAtStart = 0;
286278
numFileItemsDeletedAtEnd = 0;
@@ -304,10 +296,12 @@ Error Browser::readFileItemsForFolder(char const* filePrefixHere, bool allowFold
304296
audioFileManager.loadAnyEnqueuedClusters();
305297
FilePointer thisFilePointer;
306298

307-
result = f_readdir_get_filepointer(&staticDIR, &staticFNO, &thisFilePointer); /* Read a directory item */
299+
std::tie(staticFNO, thisFilePointer) = D_TRY_CATCH(staticDIR.read_and_get_filepointer(), error, {
300+
break; // Break on error
301+
});
308302

309-
if (result != FR_OK || staticFNO.fname[0] == 0) {
310-
break; /* Break on error or end of dir */
303+
if (staticFNO.fname[0] == 0) {
304+
break; /* Break on end of dir */
311305
}
312306
if (staticFNO.fname[0] == '.') {
313307
continue; /* Ignore dot entry */
@@ -387,8 +381,7 @@ Error Browser::readFileItemsForFolder(char const* filePrefixHere, bool allowFold
387381
thisItem->displayName = storedFilenameChars;
388382
}
389383
}
390-
391-
f_closedir(&staticDIR);
384+
staticDIR.close();
392385

393386
if (error != Error::NONE) {
394387
emptyFileItems();

src/deluge/gui/ui/browser/sample_browser.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
*/
1717

1818
#include "definitions_cxx.hpp"
19+
#include "fatfs.hpp"
1920
#include "hid/button.h"
2021
#include "model/sample/sample.h"
22+
#include "util/try.h"
2123
#include <ranges>
2224
#undef __GNU_VISIBLE
2325
#define __GNU_VISIBLE 1 // Makes strcasestr visible. Might already be the reason for the define above
@@ -1191,11 +1193,10 @@ bool SampleBrowser::loadAllSamplesInFolder(bool detectPitch, int32_t* getNumSamp
11911193
previouslyViewedFilename = currentFileItem->filename.get();
11921194
}
11931195

1194-
FRESULT result = f_opendir(&staticDIR, dirToLoad.get());
1195-
if (result != FR_OK) {
1196+
staticDIR = D_TRY_CATCH(FatFS::Directory::open(dirToLoad.get()), error, {
11961197
display->displayError(Error::SD_CARD);
11971198
return false;
1198-
}
1199+
});
11991200

12001201
int32_t numSamples = 0;
12011202

@@ -1240,10 +1241,13 @@ bool SampleBrowser::loadAllSamplesInFolder(bool detectPitch, int32_t* getNumSamp
12401241
audioFileManager.loadAnyEnqueuedClusters();
12411242
FilePointer thisFilePointer;
12421243

1243-
result = f_readdir_get_filepointer(&staticDIR, &staticFNO, &thisFilePointer); /* Read a directory item */
1244+
/* Read a directory item */
1245+
std::tie(staticFNO, thisFilePointer) = D_TRY_CATCH(staticDIR.read_and_get_filepointer(), error, {
1246+
break; // break on error
1247+
});
12441248

1245-
if (result != FR_OK || staticFNO.fname[0] == 0) {
1246-
break; // Break on error or end of dir
1249+
if (staticFNO.fname[0] == 0) {
1250+
break; // Break on end of dir
12471251
}
12481252
if (staticFNO.fname[0] == '.') {
12491253
continue; // Ignore dot entry
@@ -1278,7 +1282,7 @@ bool SampleBrowser::loadAllSamplesInFolder(bool detectPitch, int32_t* getNumSamp
12781282

12791283
if (!maybeNewSample.has_value() || maybeNewSample.value() == nullptr) {
12801284
error = maybeNewSample.error_or(Error::NONE);
1281-
f_closedir(&staticDIR);
1285+
staticDIR.close();
12821286
goto removeReasonsFromSamplesAndGetOut;
12831287
}
12841288

@@ -1301,7 +1305,7 @@ bool SampleBrowser::loadAllSamplesInFolder(bool detectPitch, int32_t* getNumSamp
13011305

13021306
numSamples++;
13031307
}
1304-
f_closedir(&staticDIR);
1308+
staticDIR.close();
13051309

13061310
if (getPrefixAndDirLength) {
13071311
// If just one file, there's no prefix.
@@ -1872,7 +1876,7 @@ bool SampleBrowser::importFolderAsKit() {
18721876
range = source->getOrCreateFirstRange();
18731877
if (!range) {
18741878
getOut:
1875-
f_closedir(&staticDIR);
1879+
staticDIR.close();
18761880
display->displayError(Error::INSUFFICIENT_RAM);
18771881
goto doReturnFalse;
18781882
}

src/deluge/gui/ui/browser/slot_browser.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ Error SlotBrowser::beginSlotSession(bool shouldDrawKeys, bool allowIfNoFolder) {
4343

4444
// But we won't try to open the folder yet, because we don't yet know what it should be.
4545

46-
/*
47-
FRESULT result = f_opendir(&staticDIR, currentDir.get());
48-
if (result != FR_OK && !allowIfNoFolder) return false;
49-
*/
50-
5146
bool success = Browser::opened();
5247
if (!success) {
5348
return Error::UNSPECIFIED;

src/deluge/gui/views/instrument_clip_view.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "gui/views/instrument_clip_view.h"
1919
#include "definitions_cxx.hpp"
2020
#include "extern.h"
21+
#include "fatfs.hpp"
2122
#include "gui/colour/colour.h"
2223
#include "gui/l10n/l10n.h"
2324
#include "gui/menu_item/colour.h"
@@ -93,6 +94,7 @@
9394
#include "util/cfunctions.h"
9495
#include "util/functions.h"
9596
#include "util/lookuptables/lookuptables.h"
97+
#include "util/try.h"
9698
#include <limits>
9799
#include <new>
98100
#include <stdint.h>
@@ -1562,17 +1564,16 @@ ActionResult InstrumentClipView::padAction(int32_t x, int32_t y, int32_t velocit
15621564

15631565
// Open directory of current audio file
15641566
*slashAddress = 0;
1565-
FRESULT result = f_opendir(&staticDIR, path);
1566-
*slashAddress = '/';
1567-
if (result != FR_OK) {
1568-
1567+
staticDIR = D_TRY_CATCH(FatFS::Directory::open(path), error, {
1568+
*slashAddress = '/';
15691569
display->displayError(Error::SD_CARD);
15701570
return ActionResult::DEALT_WITH;
1571-
}
1571+
});
1572+
*slashAddress = '/';
15721573

15731574
// Select random audio file from directory
15741575
int32_t fileCount = 0;
1575-
while (f_readdir(&staticDIR, &staticFNO) == FR_OK && staticFNO.fname[0] != 0) {
1576+
while (f_readdir(&staticDIR.inner(), &staticFNO) == FR_OK && staticFNO.fname[0] != 0) {
15761577
audioFileManager.loadAnyEnqueuedClusters();
15771578
if (staticFNO.fattrib & AM_DIR || !isAudioFilename(staticFNO.fname)) {
15781579
continue;

src/deluge/storage/audio/audio_file_manager.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -235,18 +235,19 @@ Error AudioFileManager::getUnusedAudioRecordingFilePath(String& filePath, String
235235
// recordings will be much smaller
236236
if (highestUsedAudioRecordingNumberNeedsReChecking[folderID]) {
237237

238-
FRESULT result = f_opendir(&staticDIR, audioRecordingFolderNames[folderID]);
239-
if (result == FR_OK) {
238+
auto maybeDIR = staticDIR.open(audioRecordingFolderNames[folderID]);
239+
if (maybeDIR) {
240+
staticDIR = *maybeDIR;
240241

241242
while (true) {
242243
loadAnyEnqueuedClusters();
243-
FRESULT result = f_readdir(&staticDIR, &staticFNO); /* Read a directory item */
244-
if (__builtin_expect(result != FR_OK, 0)) {
245-
return Error::SD_CARD;
246-
}
244+
/* Read a directory item */
245+
staticFNO = D_TRY_CATCH(staticDIR.read(), error, {
246+
return Error::SD_CARD; // error if invalid
247+
});
247248

248-
if (__builtin_expect((*(uint32_t*)staticFNO.altname & 0x00FFFFFF) == 0x00434552, 1)) { // "REC"
249-
if (*(uint32_t*)&staticFNO.altname[8] == 0x5641572E) { // ".WAV"
249+
if ((*(uint32_t*)staticFNO.altname & 0x00FFFFFF) == 0x00434552) [[likely]] { // "REC"
250+
if (*(uint32_t*)&staticFNO.altname[8] == 0x5641572E) { // ".WAV"
250251

251252
int32_t thisSlot = memToUIntOrError(&staticFNO.altname[3], &staticFNO.altname[8]);
252253
if (thisSlot == -1) {

src/deluge/storage/storage_manager.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void routineForSD(void);
5959

6060
FirmwareVersion song_firmware_version = FirmwareVersion::current();
6161
FILINFO staticFNO;
62-
DIR staticDIR;
62+
FatFS::Directory staticDIR;
6363
XMLSerializer smSerializer;
6464
XMLDeserializer smDeserializer;
6565
JsonSerializer smJsonSerializer;

src/deluge/storage/storage_manager.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ Error openInstrumentFile(OutputType outputType, FilePointer* filePointer);
399399

400400
extern FirmwareVersion song_firmware_version;
401401
extern FILINFO staticFNO;
402-
extern DIR staticDIR;
402+
extern FatFS::Directory staticDIR;
403403
extern const bool writeJsonFlag;
404404

405405
inline bool isCardReady() {

src/fatfs/fatfs.hpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,6 @@ using FileInfo = FILINFO;
126126

127127
class Directory {
128128
public:
129-
Directory(Directory &) = default; // Copy constructor
130-
Directory(Directory &&) = default; // Move constructor
131129
~Directory() { f_closedir(&dir_); }
132130

133131
/* Open a directory */
@@ -161,7 +159,7 @@ class Directory {
161159
constexpr DIR &inner() { return dir_; }
162160

163161
private:
164-
Directory() = default;
162+
//Directory() = default;
165163

166164
DIR dir_;
167165

0 commit comments

Comments
 (0)