Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4 feature request #9

Merged
merged 11 commits into from
Oct 3, 2023
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ CFILES :=
CPPFILES := stb_image_wrapper.cpp 3dsmain.cpp 3dsmenu.cpp 3dsopt.cpp \
3dsgpu.cpp 3dssound.cpp 3dsui.cpp 3dsexit.cpp \
3dsconfig.cpp 3dsfiles.cpp 3dsinput.cpp 3dsmatrix.cpp \
3dsimpl.cpp 3dsimpl_tilecache.cpp 3dsimpl_gpu.cpp 3dssettings.cpp \
3dsimpl.cpp 3dsimpl_tilecache.cpp 3dsimpl_gpu.cpp 3dsthemes.cpp 3dssettings.cpp \
gpulib.cpp \
Snes9x/bsx.cpp Snes9x/fxinst.cpp Snes9x/fxemu.cpp Snes9x/fxdbg.cpp Snes9x/c4.cpp Snes9x/c4emu.cpp \
Snes9x/soundux.cpp Snes9x/spc700.cpp Snes9x/apu.cpp Snes9x/cpuexec.cpp Snes9x/sa1cpu.cpp Snes9x/hwregisters.cpp \
Expand Down
65 changes: 46 additions & 19 deletions source/3dsfiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,27 @@ void file3dsSetRomNameMappings(const char* file) {
}


// will return empty string if its root directory
std::string file3dsGetCurrentDirName() {
std::string path = std::string(currentDir);
std::string dirName = "";
// Find the position of the last slash
size_t lastSlashPos = path.rfind('/');

// Check if a slash was found
if (lastSlashPos != std::string::npos) {
// Find the position of the second-to-last slash
size_t secondLastSlashPos = path.rfind('/', lastSlashPos - 1);

if (secondLastSlashPos != std::string::npos) {
// Extract the substring between the two last slashes
dirName = path.substr(secondLastSlashPos + 1, lastSlashPos - secondLastSlashPos - 1);
}
}

return dirName;
}

//----------------------------------------------------------------------
// Gets the current directory.
//----------------------------------------------------------------------
Expand Down Expand Up @@ -244,7 +265,7 @@ bool IsFileExists(const char * filename) {
//----------------------------------------------------------------------
void file3dsGoToChildDirectory(const char* childDir)
{
strncat(currentDir, &childDir[2], _MAX_PATH);
strncat(currentDir, &childDir[0], _MAX_PATH);
strncat(currentDir, "/", _MAX_PATH);
}

Expand Down Expand Up @@ -294,11 +315,15 @@ StoredFile file3dsAddFileBufferToMemory(const std::string& id, const std::string
//----------------------------------------------------------------------
// Fetch all file names with any of the given extensions
//----------------------------------------------------------------------
void file3dsGetFiles(std::vector<DirectoryEntry>& files, const std::vector<std::string>& extensions)
bool file3dsGetFiles(std::vector<DirectoryEntry>& files, const std::vector<std::string>& extensions, const char* startDir)
{
files.clear();
currentDirRomCount = 0;

if (startDir != NULL && startDir[0] != 0) {
strcpy(currentDir, startDir);
}

if (currentDir[0] == '/')
{
char tempDir[_MAX_PATH];
Expand All @@ -308,36 +333,36 @@ void file3dsGetFiles(std::vector<DirectoryEntry>& files, const std::vector<std::

struct dirent* dir;
DIR* d = opendir(currentDir);
if (d == nullptr) {
return false;
}

if (file3dsCountDirectoryDepth(currentDir) > 1)
{
// Insert the parent directory.
files.emplace_back(".. (Up to Parent Directory)"s, FileEntryType::ParentDirectory);
files.emplace_back(std::string(PARENT_DIRECTORY_LABEL), FileEntryType::ParentDirectory);
}

if (d)
while ((dir = readdir(d)) != NULL)
{
while ((dir = readdir(d)) != NULL)
if (dir->d_name[0] == '.')
continue;
if (dir->d_type == DT_DIR)
{
if (dir->d_name[0] == '.')
continue;
if (dir->d_type == DT_DIR)
{
files.emplace_back(std::string("\x01 ") + std::string(dir->d_name), FileEntryType::ChildDirectory);
}
if (dir->d_type == DT_REG)
files.emplace_back(std::string(dir->d_name), FileEntryType::ChildDirectory);
}
if (dir->d_type == DT_REG)
{
if (file3dsIsValidFilename(dir->d_name, extensions))
{
if (file3dsIsValidFilename(dir->d_name, extensions))
{
files.emplace_back(std::string(dir->d_name), FileEntryType::File);
currentDirRomCount++;
}
files.emplace_back(std::string(dir->d_name), FileEntryType::File);
currentDirRomCount++;
}
}

closedir(d);
}

closedir(d);

std::sort(files.begin(), files.end(), [](const DirectoryEntry& a, const DirectoryEntry& b) {
// lowercase sorting of filenames (e.g. "NHL 96" comes after "New Horizons")
std::string filenameA = a.Filename;
Expand All @@ -347,6 +372,8 @@ void file3dsGetFiles(std::vector<DirectoryEntry>& files, const std::vector<std::

return std::tie(a.Type, filenameA) < std::tie(b.Type, filenameB);
});

return true;
}

bool file3dsIsValidFilename(const char* filename, const std::vector<std::string>& extensions) {
Expand Down
5 changes: 4 additions & 1 deletion source/3dsfiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

enum class FileEntryType { ParentDirectory, ChildDirectory, File };

#define PARENT_DIRECTORY_LABEL " ... Parent Directory"

struct DirectoryEntry {
std::string Filename;
FileEntryType Type;
Expand Down Expand Up @@ -71,7 +73,7 @@ void file3dsGoToChildDirectory(const char* childDir);
//----------------------------------------------------------------------
// Fetch all file names with any of the given extensions
//----------------------------------------------------------------------
void file3dsGetFiles(std::vector<DirectoryEntry>& files, const std::vector<std::string>& extensions);
bool file3dsGetFiles(std::vector<DirectoryEntry>& files, const std::vector<std::string>& extensions, const char* startDir);
bool file3dsSetThumbnailSubDirectories(const char* type);
bool file3dsthumbnailsAvailable(const char* type);
void file3dsSetRomNameMappings(const char* file);
Expand All @@ -82,6 +84,7 @@ bool file3dsIsValidFilename(const char* filename, const std::vector<std::string>
StoredFile file3dsAddFileBufferToMemory(const std::string& id, const std::string& filename);


std::string file3dsGetCurrentDirName();
std::string file3dsGetFileBasename(const char* filename, bool ext);
std::string file3dsGetTrimmedFileBasename(const char* filename, bool ext);
std::string file3dsGetAssociatedFilename(const char* filename, const char* ext, const char* targetDir, bool trimmed = false);
Expand Down
Loading