Common: Add a helper function to generate a 8.3 filename from a long one.

Core: Fix the SDMC Directory implementation to make blargSnes work.
This commit is contained in:
Emmanuel Gil Peyrot 2014-09-29 08:34:37 +00:00
parent e4377b72a0
commit 094684f0b9
4 changed files with 49 additions and 14 deletions

View file

@ -780,6 +780,44 @@ bool ReadFileToString(bool text_file, const char *filename, std::string &str)
return file.ReadArray(&str[0], str.size()); return file.ReadArray(&str[0], str.size());
} }
// Splits the filename into 8.3 format
// Implemented following http://support2.microsoft.com/kb/142982
void SplitFilename83(const std::string& filename, char *short_name, char *extension)
{
int j = 0;
const std::string forbidden_characters = ".\"/\\[]:;=, ";
memset(short_name, ' ', 8);
memset(extension, ' ', 3);
short_name[8] = '\0';
extension[3] = '\0';
std::string::size_type point = filename.rfind('.');
if (point == filename.size() - 1)
point = filename.rfind('.', point);
// Get short name.
for (char letter: filename.substr(0, point)) {
if (-1 != forbidden_characters.find(letter, 0))
continue;
if (j == 8) {
// TODO(Link Mauve): also do that for filenames containing a space.
// TODO(Link Mauve): handle multiple files having the same short name.
short_name[6] = '~';
short_name[7] = '1';
break;
}
short_name[j++] = toupper(letter);
}
// Get extension.
if (point != std::string::npos) {
j = 0;
for (char letter: filename.substr(point + 1, 3))
extension[j++] = toupper(letter);
}
}
IOFile::IOFile() IOFile::IOFile()
: m_file(NULL), m_good(true) : m_file(NULL), m_good(true)
{} {}

View file

@ -131,6 +131,9 @@ std::string &GetExeDirectory();
bool WriteStringToFile(bool text_file, const std::string &str, const char *filename); bool WriteStringToFile(bool text_file, const std::string &str, const char *filename);
bool ReadFileToString(bool text_file, const char *filename, std::string &str); bool ReadFileToString(bool text_file, const char *filename, std::string &str);
// Splits the filename into 8.3 format
void SplitFilename83(const std::string& filename, char *short_name, char *extension);
// simple wrapper for cstdlib file functions to // simple wrapper for cstdlib file functions to
// hopefully will make error checking easier // hopefully will make error checking easier
// and make forgetting an fclose() harder // and make forgetting an fclose() harder

View file

@ -20,7 +20,8 @@ Directory_SDMC::Directory_SDMC(const Archive_SDMC* archive, const std::string& p
// the root directory we set while opening the archive. // the root directory we set while opening the archive.
// For example, opening /../../usr/bin can give the emulated program your installed programs. // For example, opening /../../usr/bin can give the emulated program your installed programs.
std::string absolute_path = archive->GetMountPoint() + path; std::string absolute_path = archive->GetMountPoint() + path;
entry_count = FileUtil::ScanDirectoryTree(absolute_path, entry); FileUtil::ScanDirectoryTree(absolute_path, directory);
entry_count = directory.children.size();
current_entry = 0; current_entry = 0;
} }
@ -37,8 +38,8 @@ Directory_SDMC::~Directory_SDMC() {
u32 Directory_SDMC::Read(const u32 count, Entry* entries) { u32 Directory_SDMC::Read(const u32 count, Entry* entries) {
u32 i; u32 i;
for (i = 0; i < count && current_entry < entry_count; ++i) { for (i = 0; i < count && current_entry < entry_count; ++i) {
FileUtil::FSTEntry file = entry.children[current_entry]; FileUtil::FSTEntry& file = directory.children[current_entry];
std::string filename = file.virtualName; std::string& filename = file.virtualName;
WARN_LOG(FILESYS, "File %s: size=%d dir=%d", filename.c_str(), file.size, file.isDirectory); WARN_LOG(FILESYS, "File %s: size=%d dir=%d", filename.c_str(), file.size, file.isDirectory);
Entry* entry = &entries[i]; Entry* entry = &entries[i];
@ -50,18 +51,11 @@ u32 Directory_SDMC::Read(const u32 count, Entry* entries) {
break; break;
} }
// Split the filename into 8.3 format. FileUtil::SplitFilename83(filename, entry->short_name, entry->extension);
// TODO(Link Mauve): move that to common, I guess, and make it more robust to long filenames.
std::string::size_type n = filename.rfind('.');
if (n == std::string::npos) {
strncpy(entry->short_name, filename.c_str(), 8);
memset(entry->extension, '\0', 3);
} else {
strncpy(entry->short_name, filename.substr(0, n).c_str(), 8);
strncpy(entry->extension, filename.substr(n + 1).c_str(), 8);
}
entry->is_directory = file.isDirectory; entry->is_directory = file.isDirectory;
entry->is_hidden = filename[0] == '.';
entry->is_read_only = 0;
entry->file_size = file.size; entry->file_size = file.size;
// We emulate a SD card where the archive bit has never been cleared, as it would be on // We emulate a SD card where the archive bit has never been cleared, as it would be on

View file

@ -39,7 +39,7 @@ public:
private: private:
u32 entry_count; u32 entry_count;
u32 current_entry; u32 current_entry;
FileUtil::FSTEntry entry; FileUtil::FSTEntry directory;
}; };
} // namespace FileSys } // namespace FileSys