#include "common.hpp" #include "config.hpp" #include "fileBrowse.hpp" #include "gfx.hpp" #include "gui.hpp" #include "screenCommon.hpp" #include "structs.hpp" #include <3ds.h> #include #include #include #include #include #include #include #include #include int file_count = 0; extern bool continueNdsScan; extern uint selectedFile; extern int keyRepeatDelay; extern bool dirChanged; std::vector dirContents; extern bool touching(touchPosition touch, Structs::ButtonPos button); extern touchPosition touch; std::vector buttonPositions = { {295, 0, 25, 25}, // Arrow Up. {295, 215, 25, 25}, // Arrow Down. {15, 220, 50, 15}, // Open. {80, 220, 50, 15}, // Select. {145, 220, 50, 15}, // Refresh. {210, 220, 50, 15}, // Back. {0, 0, 25, 25}, // ViewMode Change. }; off_t getFileSize(const char *fileName) { FILE* fp = fopen(fileName, "rb"); off_t fsize = 0; if (fp) { fseek(fp, 0, SEEK_END); fsize = ftell(fp); // Get source file's size fseek(fp, 0, SEEK_SET); } fclose(fp); return fsize; } bool nameEndsWith(const std::string& name, const std::vector extensionList) { if(name.substr(0, 2) == "._") return false; if(name.size() == 0) return false; if(extensionList.size() == 0) return true; for(int i = 0; i <(int)extensionList.size(); i++) { const std::string ext = extensionList.at(i); if(strcasecmp(name.c_str() + name.size() - ext.size(), ext.c_str()) == 0) return true; } return false; } bool dirEntryPredicate(const DirEntry& lhs, const DirEntry& rhs) { if(!lhs.isDirectory && rhs.isDirectory) { return false; } if(lhs.isDirectory && !rhs.isDirectory) { return true; } return strcasecmp(lhs.name.c_str(), rhs.name.c_str()) < 0; } void getDirectoryContents(std::vector& dirContents, const std::vector extensionList) { struct stat st; dirContents.clear(); DIR *pdir = opendir("."); if(pdir == NULL) { Msg::DisplayMsg("Unable to open the directory."); for(int i=0;i<120;i++) gspWaitForVBlank(); } else { while(true) { DirEntry dirEntry; struct dirent* pent = readdir(pdir); if(pent == NULL) break; stat(pent->d_name, &st); dirEntry.name = pent->d_name; dirEntry.isDirectory = (st.st_mode & S_IFDIR) ? true : false; if(dirEntry.name.compare(".") != 0 && (dirEntry.isDirectory || nameEndsWith(dirEntry.name, extensionList))) { dirContents.push_back(dirEntry); } } closedir(pdir); } sort(dirContents.begin(), dirContents.end(), dirEntryPredicate); } void getDirectoryContents(std::vector& dirContents) { getDirectoryContents(dirContents, {}); } std::vector getContents(const std::string &name, const std::vector &extensionList) { std::vector dirContents; DIR* pdir = opendir(name.c_str()); struct dirent *pent; while ((pent = readdir(pdir)) != NULL) { if(nameEndsWith(pent->d_name, extensionList)) dirContents.push_back(pent->d_name); } closedir(pdir); return dirContents; } // Directory exist? bool returnIfExist(const std::string &path, const std::vector &extensionList) { dirContents.clear(); chdir(path.c_str()); std::vector dirContentsTemp; getDirectoryContents(dirContentsTemp, extensionList); for(uint i=0;i Path, 2 -> File. std::string selectFilePath(std::string selectText, std::string initialPath, const std::vector &extensionList, int selectionMode) { uint selectedFile = 0; std::string selectedPath = ""; int keyRepeatDelay = 4; bool dirChanged = true; bool fastMode = false; uint screenPos = 0; uint screenPosList = 0; std::vector dirContents; std::string dirs; // Initial dir change. dirContents.clear(); chdir(initialPath.c_str()); std::vector dirContentsTemp; getDirectoryContents(dirContentsTemp, extensionList); for(uint i=0;i dirContentsTemp; getDirectoryContents(dirContentsTemp, extensionList); for(uint i=0;i 0) selectedFile--; } if (hidKeysDown() & KEY_TOUCH && touching(touch, buttonPositions[1])) { if (selectedFile < dirContents.size()-1) selectedFile++; } if (hidKeysHeld() & KEY_UP) { if (selectedFile > 0 && !keyRepeatDelay) { selectedFile--; if (fastMode == true) { keyRepeatDelay = 3; } else if (fastMode == false){ keyRepeatDelay = 6; } } } if (hidKeysHeld() & KEY_DOWN && !keyRepeatDelay) { if (selectedFile < dirContents.size()-1) { selectedFile++; if (fastMode == true) { keyRepeatDelay = 3; } else if (fastMode == false){ keyRepeatDelay = 6; } } } if ((hidKeysDown() & KEY_B) || (hidKeysDown() & KEY_TOUCH && touching(touch, buttonPositions[5]))) { char path[PATH_MAX]; getcwd(path, PATH_MAX); if(strcmp(path, "sdmc:/") == 0 || strcmp(path, "/") == 0) { return ""; } else { chdir(".."); selectedFile = 0; dirChanged = true; } } if ((hidKeysDown() & KEY_X) || (hidKeysDown() & KEY_TOUCH && touching(touch, buttonPositions[3]))) { char path[PATH_MAX]; getcwd(path, PATH_MAX); selectedPath = path; if (selectionMode == 2) { selectedPath += dirContents[selectedFile].name; } return selectedPath; } if (hidKeysDown() & KEY_R) { fastMode = true; } if (hidKeysDown() & KEY_L) { fastMode = false; } // Switch ViewMode. if ((hidKeysDown() & KEY_Y) || (hidKeysDown() & KEY_TOUCH && touching(touch, buttonPositions[6]))) { if (Config::viewMode == 0) { Config::viewMode = 1; } else { Config::viewMode = 0; } } if (Config::viewMode == 0) { if(selectedFile < screenPos) { screenPos = selectedFile; } else if (selectedFile > screenPos + ENTRIES_PER_SCREEN - 1) { screenPos = selectedFile - ENTRIES_PER_SCREEN + 1; } } else if (Config::viewMode == 1) { if(selectedFile < screenPosList) { screenPosList = selectedFile; } else if (selectedFile > screenPosList + ENTRIES_PER_LIST - 1) { screenPosList = selectedFile - ENTRIES_PER_LIST + 1; } } } } #define copyBufSize 0x8000 u32 copyBuf[copyBufSize]; void dirCopy(DirEntry* entry, int i, const char *destinationPath, const char *sourcePath) { std::vector dirContents; dirContents.clear(); if(entry->isDirectory) chdir((sourcePath + ("/" + entry->name)).c_str()); getDirectoryContents(dirContents); if(((int)dirContents.size()) == 1) mkdir((destinationPath + ("/" + entry->name)).c_str(), 0777); if(((int)dirContents.size()) != 1) fcopy((sourcePath + ("/" + entry->name)).c_str(), (destinationPath + ("/" + entry->name)).c_str()); } int fcopy(const char *sourcePath, const char *destinationPath) { DIR *isDir = opendir(sourcePath); if(isDir != NULL) { closedir(isDir); // Source path is a directory chdir(sourcePath); std::vector dirContents; getDirectoryContents(dirContents); DirEntry* entry = &dirContents.at(1); mkdir(destinationPath, 0777); for(int i = 1; i < ((int)dirContents.size()); i++) { chdir(sourcePath); entry = &dirContents.at(i); dirCopy(entry, i, destinationPath, sourcePath); } chdir(destinationPath); chdir(".."); return 1; } else { closedir(isDir); // Source path is a file FILE* sourceFile = fopen(sourcePath, "rb"); off_t fsize = 0; if(sourceFile) { fseek(sourceFile, 0, SEEK_END); fsize = ftell(sourceFile); // Get source file's size fseek(sourceFile, 0, SEEK_SET); } else { fclose(sourceFile); return -1; } FILE* destinationFile = fopen(destinationPath, "wb"); //if(destinationFile) { fseek(destinationFile, 0, SEEK_SET); /*} else { fclose(sourceFile); fclose(destinationFile); return -1; }*/ off_t offset = 0; int numr; while(1) { scanKeys(); if(keysHeld() & KEY_B) { // Cancel copying fclose(sourceFile); fclose(destinationFile); return -1; break; } printf("\x1b[16;0H"); printf("Progress:\n"); printf("%i/%i Bytes ", (int)offset, (int)fsize); // Copy file to destination path numr = fread(copyBuf, 2, copyBufSize, sourceFile); fwrite(copyBuf, 2, numr, destinationFile); offset += copyBufSize; if(offset > fsize) { fclose(sourceFile); fclose(destinationFile); printf("\x1b[17;0H"); printf("%i/%i Bytes ", (int)fsize, (int)fsize); for(int i = 0; i < 30; i++) gspWaitForVBlank(); return 1; break; } } return -1; } }