diff --git a/arm9/source/fileOperations.cpp b/arm9/source/fileOperations.cpp index 06891cb..4d2239c 100644 --- a/arm9/source/fileOperations.cpp +++ b/arm9/source/fileOperations.cpp @@ -1,5 +1,12 @@ +#include "fileOperations.h" #include #include +#include +#include + +#include "file_browse.h" + +using namespace std; #define copyBufSize 0x8000 @@ -26,61 +33,91 @@ off_t getFileSize(const char *fileName) return fsize; } +void dirCopy(DirEntry* entry, int i, const char *destinationPath, const char *sourcePath) { + 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) { - 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); + DIR *isDir = opendir (sourcePath); + + if (isDir != NULL) { + chdir(sourcePath); + 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 { - fclose(sourceFile); - return -1; - } + 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 + FILE* destinationFile = fopen(destinationPath, "wb"); + //if (destinationFile) { + fseek(destinationFile, 0, SEEK_SET); + /*} else { fclose(sourceFile); fclose(destinationFile); return -1; - break; + }*/ + + 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 < 60; i++) swiWaitForVBlank(); + + 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 < 60; i++) swiWaitForVBlank(); - - return 1; - break; - } + return -1; } - - return -1; + closedir(isDir); } diff --git a/arm9/source/fileOperations.h b/arm9/source/fileOperations.h index f3d1abf..a5e8aae 100644 --- a/arm9/source/fileOperations.h +++ b/arm9/source/fileOperations.h @@ -1,3 +1,5 @@ +#include + #ifndef FILE_COPY #define FILE_COPY diff --git a/arm9/source/file_browse.cpp b/arm9/source/file_browse.cpp index 3ff8e88..87ec1a6 100644 --- a/arm9/source/file_browse.cpp +++ b/arm9/source/file_browse.cpp @@ -49,13 +49,6 @@ using namespace std; static char path[PATH_MAX]; -struct DirEntry { - string name; - off_t size; - bool isDirectory; - bool isApp; -} ; - bool nameEndsWith (const string& name) { if (name.size() == 0) return false; @@ -594,7 +587,7 @@ string browseForFile (void) { getDirectoryContents (dirContents); } } - } else if ((strcmp(entry->name.c_str(), "..") != 0) && !entry->isDirectory) { + } else if (strcmp(entry->name.c_str(), "..") != 0) { snprintf(clipboard, sizeof(clipboard), "%s%s", path, entry->name.c_str()); snprintf(clipboardFilename, sizeof(clipboardFilename), "%s", entry->name.c_str()); clipboardOn = true; diff --git a/arm9/source/file_browse.h b/arm9/source/file_browse.h index 58993a7..f8bd661 100644 --- a/arm9/source/file_browse.h +++ b/arm9/source/file_browse.h @@ -25,7 +25,17 @@ #include #include +using namespace std; + +struct DirEntry { + string name; + off_t size; + bool isDirectory; + bool isApp; +} ; + std::string browseForFile (void); +void getDirectoryContents (vector& dirContents);