summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/common/file_util.cpp42
-rw-r--r--src/common/file_util.h11
-rw-r--r--src/core/file_sys/directory.h4
-rw-r--r--src/core/file_sys/directory_sdmc.cpp43
-rw-r--r--src/core/file_sys/directory_sdmc.h9
5 files changed, 80 insertions, 29 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp
index 9292a1cd6..5fd155222 100644
--- a/src/common/file_util.cpp
+++ b/src/common/file_util.cpp
@@ -780,6 +780,48 @@ size_t ReadFileToString(bool text_file, const char *filename, std::string &str)
780 return file.ReadArray(&str[0], str.size()); 780 return file.ReadArray(&str[0], str.size());
781} 781}
782 782
783/**
784 * Splits the filename into 8.3 format
785 * Loosely implemented following https://en.wikipedia.org/wiki/8.3_filename
786 * @param filename The normal filename to use
787 * @param short_name A 9-char array in which the short name will be written
788 * @param extension A 4-char array in which the extension will be written
789 */
790void SplitFilename83(const std::string& filename, std::array<char, 9>& short_name,
791 std::array<char, 4>& extension) {
792 const std::string forbidden_characters = ".\"/\\[]:;=, ";
793
794 // On a FAT32 partition, 8.3 names are stored as a 11 bytes array, filled with spaces.
795 short_name = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\0'};
796 extension = {' ', ' ', ' ', '\0'};
797
798 std::string::size_type point = filename.rfind('.');
799 if (point == filename.size() - 1)
800 point = filename.rfind('.', point);
801
802 // Get short name.
803 int j = 0;
804 for (char letter : filename.substr(0, point)) {
805 if (forbidden_characters.find(letter, 0) != std::string::npos)
806 continue;
807 if (j == 8) {
808 // TODO(Link Mauve): also do that for filenames containing a space.
809 // TODO(Link Mauve): handle multiple files having the same short name.
810 short_name[6] = '~';
811 short_name[7] = '1';
812 break;
813 }
814 short_name[j++] = toupper(letter);
815 }
816
817 // Get extension.
818 if (point != std::string::npos) {
819 j = 0;
820 for (char letter : filename.substr(point + 1, 3))
821 extension[j++] = toupper(letter);
822 }
823}
824
783IOFile::IOFile() 825IOFile::IOFile()
784 : m_file(NULL), m_good(true) 826 : m_file(NULL), m_good(true)
785{} 827{}
diff --git a/src/common/file_util.h b/src/common/file_util.h
index f9d91972f..288734cad 100644
--- a/src/common/file_util.h
+++ b/src/common/file_util.h
@@ -4,6 +4,7 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <array>
7#include <fstream> 8#include <fstream>
8#include <cstdio> 9#include <cstdio>
9#include <cstring> 10#include <cstring>
@@ -131,6 +132,16 @@ std::string &GetExeDirectory();
131size_t WriteStringToFile(bool text_file, const std::string &str, const char *filename); 132size_t WriteStringToFile(bool text_file, const std::string &str, const char *filename);
132size_t ReadFileToString(bool text_file, const char *filename, std::string &str); 133size_t ReadFileToString(bool text_file, const char *filename, std::string &str);
133 134
135/**
136 * Splits the filename into 8.3 format
137 * Loosely implemented following https://en.wikipedia.org/wiki/8.3_filename
138 * @param filename The normal filename to use
139 * @param short_name A 9-char array in which the short name will be written
140 * @param extension A 4-char array in which the extension will be written
141 */
142void SplitFilename83(const std::string& filename, std::array<char, 9>& short_name,
143 std::array<char, 4>& extension);
144
134// simple wrapper for cstdlib file functions to 145// simple wrapper for cstdlib file functions to
135// hopefully will make error checking easier 146// hopefully will make error checking easier
136// and make forgetting an fclose() harder 147// and make forgetting an fclose() harder
diff --git a/src/core/file_sys/directory.h b/src/core/file_sys/directory.h
index 9f3546b05..e10431337 100644
--- a/src/core/file_sys/directory.h
+++ b/src/core/file_sys/directory.h
@@ -19,9 +19,9 @@ namespace FileSys {
19const size_t FILENAME_LENGTH = 0x20C / 2; 19const size_t FILENAME_LENGTH = 0x20C / 2;
20struct Entry { 20struct Entry {
21 char16_t filename[FILENAME_LENGTH]; // Entry name (UTF-16, null-terminated) 21 char16_t filename[FILENAME_LENGTH]; // Entry name (UTF-16, null-terminated)
22 char short_name[9]; // 8.3 file name ('longfilename' -> 'LONGFI~1', null-terminated) 22 std::array<char, 9> short_name; // 8.3 file name ('longfilename' -> 'LONGFI~1', null-terminated)
23 char unknown1; // unknown (observed values: 0x0A, 0x70, 0xFD) 23 char unknown1; // unknown (observed values: 0x0A, 0x70, 0xFD)
24 char extension[4]; // 8.3 file extension (set to spaces for directories, null-terminated) 24 std::array<char, 4> extension; // 8.3 file extension (set to spaces for directories, null-terminated)
25 char unknown2; // unknown (always 0x01) 25 char unknown2; // unknown (always 0x01)
26 char unknown3; // unknown (0x00 or 0x08) 26 char unknown3; // unknown (0x00 or 0x08)
27 char is_directory; // directory flag 27 char is_directory; // directory flag
diff --git a/src/core/file_sys/directory_sdmc.cpp b/src/core/file_sys/directory_sdmc.cpp
index 11e867857..36951564d 100644
--- a/src/core/file_sys/directory_sdmc.cpp
+++ b/src/core/file_sys/directory_sdmc.cpp
@@ -20,8 +20,8 @@ Directory_SDMC::Directory_SDMC(const Archive_SDMC* archive, const std::string& p
20 // the root directory we set while opening the archive. 20 // the root directory we set while opening the archive.
21 // For example, opening /../../usr/bin can give the emulated program your installed programs. 21 // For example, opening /../../usr/bin can give the emulated program your installed programs.
22 std::string absolute_path = archive->GetMountPoint() + path; 22 std::string absolute_path = archive->GetMountPoint() + path;
23 entry_count = FileUtil::ScanDirectoryTree(absolute_path, entry); 23 FileUtil::ScanDirectoryTree(absolute_path, directory);
24 current_entry = 0; 24 children_iterator = directory.children.begin();
25} 25}
26 26
27Directory_SDMC::~Directory_SDMC() { 27Directory_SDMC::~Directory_SDMC() {
@@ -35,44 +35,39 @@ Directory_SDMC::~Directory_SDMC() {
35 * @return Number of entries listed 35 * @return Number of entries listed
36 */ 36 */
37u32 Directory_SDMC::Read(const u32 count, Entry* entries) { 37u32 Directory_SDMC::Read(const u32 count, Entry* entries) {
38 u32 i; 38 u32 entries_read = 0;
39 for (i = 0; i < count && current_entry < entry_count; ++i) { 39
40 FileUtil::FSTEntry file = entry.children[current_entry]; 40 while (entries_read < count && children_iterator != directory.children.cend()) {
41 std::string filename = file.virtualName; 41 const FileUtil::FSTEntry& file = *children_iterator;
42 WARN_LOG(FILESYS, "File %s: size=%d dir=%d", filename.c_str(), file.size, file.isDirectory); 42 const std::string& filename = file.virtualName;
43 Entry& entry = entries[entries_read];
43 44
44 Entry* entry = &entries[i]; 45 WARN_LOG(FILESYS, "File %s: size=%d dir=%d", filename.c_str(), file.size, file.isDirectory);
45 46
46 // TODO(Link Mauve): use a proper conversion to UTF-16. 47 // TODO(Link Mauve): use a proper conversion to UTF-16.
47 for (int j = 0; j < FILENAME_LENGTH; ++j) { 48 for (int j = 0; j < FILENAME_LENGTH; ++j) {
48 entry->filename[j] = filename[j]; 49 entry.filename[j] = filename[j];
49 if (!filename[j]) 50 if (!filename[j])
50 break; 51 break;
51 } 52 }
52 53
53 // Split the filename into 8.3 format. 54 FileUtil::SplitFilename83(filename, entry.short_name, entry.extension);
54 // TODO(Link Mauve): move that to common, I guess, and make it more robust to long filenames.
55 std::string::size_type n = filename.rfind('.');
56 if (n == std::string::npos) {
57 strncpy(entry->short_name, filename.c_str(), 8);
58 memset(entry->extension, '\0', 3);
59 } else {
60 strncpy(entry->short_name, filename.substr(0, n).c_str(), 8);
61 strncpy(entry->extension, filename.substr(n + 1).c_str(), 8);
62 }
63 55
64 entry->is_directory = file.isDirectory; 56 entry.is_directory = file.isDirectory;
65 entry->file_size = file.size; 57 entry.is_hidden = (filename[0] == '.');
58 entry.is_read_only = 0;
59 entry.file_size = file.size;
66 60
67 // We emulate a SD card where the archive bit has never been cleared, as it would be on 61 // We emulate a SD card where the archive bit has never been cleared, as it would be on
68 // most user SD cards. 62 // most user SD cards.
69 // Some homebrews (blargSNES for instance) are known to mistakenly use the archive bit as a 63 // Some homebrews (blargSNES for instance) are known to mistakenly use the archive bit as a
70 // file bit. 64 // file bit.
71 entry->is_archive = !file.isDirectory; 65 entry.is_archive = !file.isDirectory;
72 66
73 ++current_entry; 67 ++entries_read;
68 ++children_iterator;
74 } 69 }
75 return i; 70 return entries_read;
76} 71}
77 72
78/** 73/**
diff --git a/src/core/file_sys/directory_sdmc.h b/src/core/file_sys/directory_sdmc.h
index 0bc6c9eff..cb8d32fda 100644
--- a/src/core/file_sys/directory_sdmc.h
+++ b/src/core/file_sys/directory_sdmc.h
@@ -37,9 +37,12 @@ public:
37 bool Close() const override; 37 bool Close() const override;
38 38
39private: 39private:
40 u32 entry_count; 40 u32 total_entries_in_directory;
41 u32 current_entry; 41 FileUtil::FSTEntry directory;
42 FileUtil::FSTEntry entry; 42
43 // We need to remember the last entry we returned, so a subsequent call to Read will continue
44 // from the next one. This iterator will always point to the next unread entry.
45 std::vector<FileUtil::FSTEntry>::iterator children_iterator;
43}; 46};
44 47
45} // namespace FileSys 48} // namespace FileSys