Directory entries are now sorted before being sent.

This commit is contained in:
Stephen White 2018-05-30 20:09:15 +10:00
parent c2300db00a
commit 59057a8d44

View file

@ -28,11 +28,13 @@
#include "ff.h" #include "ff.h"
#include "DiskImage.h" #include "DiskImage.h"
#include "Petscii.h" #include "Petscii.h"
#include "FileBrowser.h"
#include <string.h> #include <string.h>
#include <strings.h> #include <strings.h>
#include <stdio.h> #include <stdio.h>
#include <ctype.h> #include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
#include <algorithm>
#define CBM_NAME_LENGTH 16 #define CBM_NAME_LENGTH 16
#define CBM_NAME_LENGTH_MINUS_D64 CBM_NAME_LENGTH-4 #define CBM_NAME_LENGTH_MINUS_D64 CBM_NAME_LENGTH-4
@ -1641,41 +1643,67 @@ void IEC_Commands::AddDirectoryEntry(Channel& channel, const char* name, u16 blo
channel.cursor += dirEntryLength; channel.cursor += dirEntryLength;
} }
struct greater
{
bool operator()(const FileBrowser::BrowsableList::Entry& lhs, const FileBrowser::BrowsableList::Entry& rhs) const
{
if (strcasecmp(lhs.filImage.fname, "..") == 0)
return true;
else if (strcasecmp(rhs.filImage.fname, "..") == 0)
return false;
else if (((lhs.filImage.fattrib & AM_DIR) && (rhs.filImage.fattrib & AM_DIR)) || (!(lhs.filImage.fattrib & AM_DIR) && !(rhs.filImage.fattrib & AM_DIR)))
return strcasecmp(lhs.filImage.fname, rhs.filImage.fname) < 0;
else if ((lhs.filImage.fattrib & AM_DIR) && !(rhs.filImage.fattrib & AM_DIR))
return true;
else
return false;
}
};
void IEC_Commands::LoadDirectory() void IEC_Commands::LoadDirectory()
{ {
DIR dir; DIR dir;
FILINFO filInfo;
char* ext; char* ext;
FRESULT res;
Channel& channel = channels[0]; Channel& channel = channels[0];
memcpy(channel.buffer, DirectoryHeader, sizeof(DirectoryHeader)); memcpy(channel.buffer, DirectoryHeader, sizeof(DirectoryHeader));
channel.cursor = sizeof(DirectoryHeader); channel.cursor = sizeof(DirectoryHeader);
FRESULT res;
FileBrowser::BrowsableList::Entry entry;
std::vector<FileBrowser::BrowsableList::Entry> entries;
res = f_opendir(&dir, "."); res = f_opendir(&dir, ".");
if (res == FR_OK) if (res == FR_OK)
{ {
do do
{ {
res = f_readdir(&dir, &filInfo); res = f_readdir(&dir, &entry.filImage);
ext = strrchr(filInfo.fname, '.'); ext = strrchr(entry.filImage.fname, '.');
if (res == FR_OK && entry.filImage.fname[0] != 0 && !(ext && strcasecmp(ext, ".png") == 0))
entries.push_back(entry);
} while (res == FR_OK && entry.filImage.fname[0] != 0);
f_closedir(&dir);
if (res == FR_OK && filInfo.fname[0] != 0 && !(ext && strcasecmp(ext, ".png") == 0)) std::sort(entries.begin(), entries.end(), greater());
}
for (u32 i = 0; i < entries.size(); ++i)
{ {
const char* fileName = filInfo.fname; FILINFO* filInfo = &entries[i].filImage;
//DEBUG_LOG("%s", fileName); const char* fileName = filInfo->fname;
if (!channel.CanFit(DIRECTORY_ENTRY_SIZE)) if (!channel.CanFit(DIRECTORY_ENTRY_SIZE))
SendBuffer(channel, false); SendBuffer(channel, false);
if (filInfo.fattrib & AM_DIR) AddDirectoryEntry(channel, fileName, 0, 6); if (filInfo->fattrib & AM_DIR) AddDirectoryEntry(channel, fileName, 0, 6);
else AddDirectoryEntry(channel, fileName, filInfo.fsize / 256 + 1, 2); else AddDirectoryEntry(channel, fileName, filInfo->fsize / 256 + 1, 2);
}
}
while (res == FR_OK && filInfo.fname[0] != 0);
f_closedir(&dir);
} }
SendBuffer(channel, false); SendBuffer(channel, false);
memcpy(channel.buffer, DirectoryBlocksFree, sizeof(DirectoryBlocksFree)); memcpy(channel.buffer, DirectoryBlocksFree, sizeof(DirectoryBlocksFree));