From 72702130d95477eb84904f22b0024ea30765922b Mon Sep 17 00:00:00 2001 From: Max Horn Date: Fri, 23 Jan 2009 03:06:21 +0000 Subject: DS: Apply an old cleanup patch of mine from last August svn-id: r36011 --- backends/fs/ds/ds-fs.cpp | 93 +++++++++----------------- backends/platform/ds/arm9/source/dsmain.cpp | 1 - backends/platform/ds/arm9/source/zipreader.cpp | 63 ++++++++--------- backends/platform/ds/arm9/source/zipreader.h | 9 +-- 4 files changed, 62 insertions(+), 104 deletions(-) (limited to 'backends') diff --git a/backends/fs/ds/ds-fs.cpp b/backends/fs/ds/ds-fs.cpp index 97a034a998..3ab1c90038 100644 --- a/backends/fs/ds/ds-fs.cpp +++ b/backends/fs/ds/ds-fs.cpp @@ -60,24 +60,21 @@ DSFileSystemNode::DSFileSystemNode() { DSFileSystemNode::DSFileSystemNode(const Common::String& path) { // consolePrintf("--%s ",path.c_str()); - char disp[128]; - char* pathStr = (char *) path.c_str(); - int lastSlash = 3; - for (int r = 0; r < (int) strlen(pathStr) - 1; r++) { + for (int r = 0; r < (int) path.size() - 1; r++) { if (path[r] == '\\') { lastSlash = r; } } - strcpy(disp, pathStr + lastSlash + 1); - - _displayName = Common::String(disp); + _displayName = Common::String(path.c_str() + lastSlash + 1); _path = path; // _isValid = true; // _isDirectory = false; - if (!strncmp(pathStr, "ds:/", 4)) { + const char *pathStr = path.c_str(); + + if (path.hasPrefix("ds:/")) { pathStr += 4; } @@ -103,18 +100,14 @@ DSFileSystemNode::DSFileSystemNode(const Common::String& path) { DSFileSystemNode::DSFileSystemNode(const Common::String& path, bool isDir) { // consolePrintf("--%s ",path.c_str()); - char disp[128]; - char* pathStr = (char *) path.c_str(); int lastSlash = 3; - for (int r = 0; r < (int) strlen(pathStr) - 1; r++) { + for (int r = 0; r < (int) path.size() - 1; r++) { if (path[r] == '\\') { lastSlash = r; } } - strcpy(disp, pathStr + lastSlash + 1); - - _displayName = Common::String(disp); + _displayName = Common::String(path.c_str() + lastSlash + 1); _path = path; _isValid = true; _isDirectory = isDir; @@ -142,14 +135,11 @@ bool DSFileSystemNode::getChildren(AbstractFSList &dirList, ListMode mode, bool //TODO: honor the hidden flag - char temp[128]; - strcpy(temp, _path.c_str()); - -// consolePrintf("This dir: %s\n", temp); +// consolePrintf("This dir: %s\n", _path.c_str()); - if ((temp[0] == 'd') && (temp[1] == 's') && (temp[2] == ':') && (temp[3] == '/')) { - if (strlen(temp) != 4) { - _zipFile->changeDirectory(&temp[4]); + if (_path.hasPrefix("ds:/")) { + if (_path.size() > 4) { + _zipFile->changeDirectory(_path.c_str() + 4); } else { _zipFile->changeToRoot(); @@ -160,7 +150,7 @@ bool DSFileSystemNode::getChildren(AbstractFSList &dirList, ListMode mode, bool */ } } else { - _zipFile->changeDirectory(temp); + _zipFile->changeDirectory(_path.c_str()); } if (_zipFile->restartFile()) { @@ -191,14 +181,14 @@ AbstractFSNode* DSFileSystemNode::getParent() const { char *path = (char *) _path.c_str(); int lastSlash = 4; - for (int r = 4; r < (int) strlen((char *) path); r++) { + for (int r = 4; r < (int) _path.size(); r++) { if (path[r] == '\\') { lastSlash = r; } } p = new DSFileSystemNode(Common::String(path, lastSlash)); - ((DSFileSystemNode *) (p))->_isDirectory = true; + p->_isDirectory = true; } else { p = new DSFileSystemNode(); } @@ -229,79 +219,58 @@ GBAMPFileSystemNode::GBAMPFileSystemNode() { GBAMPFileSystemNode::GBAMPFileSystemNode(const Common::String& path) { // consolePrintf("'%s'",path.c_str()); - char disp[128]; - char* pathStr = (char *) path.c_str(); int lastSlash = 3; - for (int r = 0; r < (int) strlen(pathStr) - 1; r++) { + for (int r = 0; r < (int) path.size() - 1; r++) { if ((path[r] == '\\') || (path[r] == '/')) { lastSlash = r; } } - - strcpy(disp, pathStr + lastSlash + 1); - - char check[128]; - int fileOrDir; - - if (!strcmp(pathStr, "mp:/")) { + + if (path == "mp:/") { // This is the root directory _isDirectory = true; _isValid = false; // Old code returned false here, but I'm not sure why - } else if ((strlen(pathStr) > 4) && (!strncmp(pathStr, "mp:/", 4))) { - // Files which start with mp:/ - - // Clear the filename to 128 bytes, because a libfat bug occationally tries to read in this area. + } else { + char check[128]; memset(check, 0, 128); - strcpy(check, pathStr + 3); - // Remove terminating slash - FileExists fails without this - if (check[strlen(check) - 1] == '/') { - check[strlen(check) - 1] = 0; + if (path.size() > 4 && path.hasPrefix("mp:/")) { + // Files which start with mp:/ + // Clear the filename to 128 bytes, because a libfat bug occasionally tries to read in this area. + strcpy(check, path.c_str() + 3); + } else { + // Clear the filename to 128 bytes, because a libfat bug occationally tries to read in this area. + strcpy(check, path.c_str()); } - fileOrDir = FAT_FileExists(check); - _isDirectory = fileOrDir == FT_DIR; - _isValid = fileOrDir == FT_FILE; - } else { - // Files which don't start with mp:/ (like scummvm.ini in default implementation) - - // Clear the filename to 128 bytes, because a libfat bug occationally tries to read in this area. - memset(check, 0, 128); - strcpy(check, pathStr); - - // Remove terminating slash - FileExists fails on directories without this + // Remove terminating slash - FileExists fails without this if (check[strlen(check) - 1] == '/') { check[strlen(check) - 1] = 0; } - fileOrDir = FAT_FileExists(check); + int fileOrDir = FAT_FileExists(check); _isDirectory = fileOrDir == FT_DIR; _isValid = fileOrDir == FT_FILE; - } // consolePrintf("Path: %s \n", check); - _displayName = Common::String(disp); + _displayName = Common::String(path.c_str() + lastSlash + 1); _path = path; } GBAMPFileSystemNode::GBAMPFileSystemNode(const Common::String& path, bool isDirectory) { //consolePrintf("'%s'",path.c_str()); - char disp[128]; - char* pathStr = (char *) path.c_str(); int lastSlash = 3; - for (int r = 0; r < (int) strlen(pathStr) - 1; r++) { + for (int r = 0; r < (int) path.size() - 1; r++) { if ((path[r] == '\\') || (path[r] == '/')) { lastSlash = r; } } - strcpy(disp, pathStr + lastSlash + 1); - - _displayName = Common::String(disp); + _displayName = Common::String(path.c_str() + lastSlash + 1); _path = path; _isValid = true; _isDirectory = isDirectory; diff --git a/backends/platform/ds/arm9/source/dsmain.cpp b/backends/platform/ds/arm9/source/dsmain.cpp index be02e0cd21..297e2c6037 100644 --- a/backends/platform/ds/arm9/source/dsmain.cpp +++ b/backends/platform/ds/arm9/source/dsmain.cpp @@ -2384,7 +2384,6 @@ void penUpdate() { // if (getKeysHeld() & KEY_L) consolePrintf("%d, %d penX=%d, penY=%d tz=%d\n", IPC->touchXpx, IPC->touchYpx, penX, penY, IPC->touchZ1); bool penDownThisFrame = (IPC->touchZ1 > 0) && (IPC->touchXpx > 0) && (IPC->touchYpx > 0); - bool firstFrame = penDownFrames == 2; static bool moved = false; if ((tapScreenClicks) && (!getKeyboardEnable()) && (getIsDisplayMode8Bit())) { diff --git a/backends/platform/ds/arm9/source/zipreader.cpp b/backends/platform/ds/arm9/source/zipreader.cpp index 7af0718a44..fa9a229348 100644 --- a/backends/platform/ds/arm9/source/zipreader.cpp +++ b/backends/platform/ds/arm9/source/zipreader.cpp @@ -23,10 +23,6 @@ #include "zipreader.h" -#define SWAP_U16(v) (((v & 0xFF) << 8) + (v & 0xFF00)) -#define SWAP_U32(v) ((SWAP_U16((v & 0XFFFF0000) >> 16) + (SWAP_U16(v & 0x0000FFFF) << 16))) - - ZipFile::ZipFile() { // Locate a zip file in cartridge memory space @@ -90,7 +86,8 @@ bool ZipFile::restartFile() { bool ZipFile::currentFileInFolder() { char name[128]; - if (_allFilesVisible) return true; + if (_allFilesVisible) + return true; getFileName(name); @@ -118,7 +115,7 @@ bool ZipFile::currentFileInFolder() { void ZipFile::getFileName(char* name) { strncpy(name, (char *) (_currentFile + 1), _currentFile->nameLength); - for (int r = 0; r < (int) strlen(name); r++) { + for (int r = 0; name[r] != 0; r++) { if (name[r] == '/') name[r] = '\\'; } @@ -136,11 +133,11 @@ bool ZipFile::skipFile() { // Move on to the next file _currentFile = (FileHeader *) ( - ((char *) (_currentFile)) + sizeof(*_currentFile) + _currentFile->nameLength + _currentFile->fileSize + _currentFile->extraLength + getFile() + _currentFile->fileSize ); // Return true if there are more files. Check this by looking for the magic number - valid = (_currentFile->magic[0] == 0x50) && + valid = (_currentFile->magic[0] == 0x50) && (_currentFile->magic[1] == 0x4B) && (_currentFile->magic[2] == 0x03) && (_currentFile->magic[3] == 0x04); @@ -155,16 +152,6 @@ bool ZipFile::skipFile() { -u32 ZipFile::misaligned32(u32* v) { - char* b = (char *) v; - return (b[0] << 24) + (b[1] << 16) + (b[2] << 8) + b[3]; -} - -u16 ZipFile::misaligned16(u16* v) { - char* b = (char *) v; - return (b[0] << 8) + b[1]; -} - int ZipFile::getFileSize() { return _currentFile->fileSize; } @@ -173,22 +160,27 @@ bool ZipFile::isDirectory() { return _currentFile->fileSize == 0; // This is a bit wrong, but seems to work. } -char* ZipFile::getFile() { +char *ZipFile::getFile() { return ((char *) (_currentFile)) + sizeof(*_currentFile) + _currentFile->nameLength + _currentFile->extraLength; } -bool ZipFile::findFile(char* search) { +bool ZipFile::findFile(const char *search) { changeToRoot(); restartFile(); char searchName[128]; strcpy(searchName, search); - for (int r = 0; r < (int) strlen(searchName); r++) { - if (searchName[r] == '/') searchName[r] = '\\'; + char *tmp = searchName; + + // Change slashes to backslashes + for (; *tmp; ++tmp) { + if (*tmp == '/') + *tmp = '\\'; } - if (*(searchName + strlen(searchName) - 1) == '\\') { // Directories have a terminating slash - *(searchName + strlen(searchName) - 1) = '\0'; // which we need to dispose of. + // Remove trailing slashes + if (*(tmp-1) == '\\') { + *(tmp-1) = 0; } @@ -215,19 +207,22 @@ void ZipFile::changeToRoot() { _directory[0] = 0; } -void ZipFile::changeDirectory(char* dir) { +void ZipFile::changeDirectory(const char* dir) { // consolePrintf("Current dir now '%s'\n", dir); - strcpy(_directory, dir); - for (int r = 0; r < (int) strlen(_directory); r++) { - if (_directory[r] == '/') _directory[r] = '\\'; - } + assert(dir && *dir); - if (_directory[strlen(_directory) - 1] == '\\') { - _directory[strlen(_directory) - 1] = '\0'; + // Copy dir to _directory, changing slashes to backslashes + char *dst = _directory; + for (; *dir; ++dir) { + if (*dir == '/') + *dst++ = '\\'; + else + *dst++ = *dir; } -} - -ZipFile::~ZipFile() { + // Remove trailing backslash + if (*(dst-1) == '\\') { + *(dst-1) = 0; + } } diff --git a/backends/platform/ds/arm9/source/zipreader.h b/backends/platform/ds/arm9/source/zipreader.h index cd7244dba5..d1f70942ce 100644 --- a/backends/platform/ds/arm9/source/zipreader.h +++ b/backends/platform/ds/arm9/source/zipreader.h @@ -52,14 +52,13 @@ class ZipFile { public: ZipFile(); - ~ZipFile(); bool isReady(); // These operations set the current file bool restartFile(); bool skipFile(); - bool findFile(char* search); + bool findFile(const char *search); // These return the file's data and information char* getFile(); @@ -68,15 +67,11 @@ public: bool isDirectory(); // These set the current directory - void changeDirectory(char* name); + void changeDirectory(const char* name); void changeToRoot(); void setAllFilesVisible(bool state) { _allFilesVisible = state; } bool currentFileInFolder(); - - u16 misaligned16(u16* v); - u32 misaligned32(u32* v); - }; -- cgit v1.2.3