From 5808c1d66711c56a0b6765626d4b7d7a2110fc06 Mon Sep 17 00:00:00 2001 From: zeromus Date: Sun, 20 Jun 2010 21:12:06 +0000 Subject: [PATCH] win32: lets try this to fix path configuration --- desmume/src/path.h | 104 +++++++++++++++++++++++++++++++++-- desmume/src/windows/main.cpp | 5 +- 2 files changed, 101 insertions(+), 8 deletions(-) diff --git a/desmume/src/path.h b/desmume/src/path.h index ffd57cf57..3004aefb7 100644 --- a/desmume/src/path.h +++ b/desmume/src/path.h @@ -1,4 +1,9 @@ #include +#include + +#ifdef _MSC_VER +#define mkdir _mkdir +#endif #if defined(_WINDOWS) && !defined(WXPORT) #include "resource.h" @@ -8,6 +13,78 @@ #include "time.h" #include "utils/xstring.h" +//----------------------------------- +//This is taken from mono Path.cs +static const char InvalidPathChars[] = { + '\x22', '\x3C', '\x3E', '\x7C', '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', + '\x08', '\x09', '\x0A', '\x0B', '\x0C', '\x0D', '\x0E', '\x0F', '\x10', '\x11', '\x12', + '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1A', '\x1B', '\x1C', '\x1D', + '\x1E', '\x1F' +}; + +//but it is sort of windows-specific. Does it work in linux? Maybe we'll have to make it smarter +static const char VolumeSeparatorChar = ':'; +static const char DirectorySeparatorChar = '\\'; +static const char AltDirectorySeparatorChar = '/'; +static bool dirEqualsVolume = (DirectorySeparatorChar == VolumeSeparatorChar); + +class Path +{ +public: + static bool IsPathRooted (const std::string& path) + { + if (path.size() == 0) + return false; + + + if (path.find_first_of(InvalidPathChars) != -1) + { + //yuck. + //throw new ArgumentException ("Illegal characters in path."); + } + + char c = path [0]; + return (c == DirectorySeparatorChar || + c == AltDirectorySeparatorChar || + (!dirEqualsVolume && path.size() > 1 && path [1] == VolumeSeparatorChar)); + } +}; +//----------------------------------- +static void FCEUD_MakePathDirs(const char *fname) +{ + char path[MAX_PATH]; + const char* div = fname; + + do + { + const char* fptr = strchr(div, '\\'); + + if(!fptr) + { + fptr = strchr(div, '/'); + } + + if(!fptr) + { + break; + } + + int off = fptr - fname; + strncpy(path, fname, off); + path[off] = '\0'; + mkdir(path); + + div = fptr + 1; + + while(div[0] == '\\' || div[0] == '/') + { + div++; + } + + } while(1); +} +//------------------------------ + class PathInfo { public: @@ -103,7 +180,12 @@ public: void GetDefaultPath(char *pathToDefault, const char *key, int maxCount) { +#ifdef _WINDOWS + std::string temp = (std::string)".\\" + pathToDefault; + strncpy(pathToDefault, temp.c_str(), maxCount); +#else strncpy(pathToDefault, pathToModule, maxCount); +#endif } void ReadKey(char *pathToRead, const char *key) @@ -184,16 +266,25 @@ public: if(action == GET) { - strncpy(buffer, pathToCopy, MAX_PATH); - int len = strlen(buffer)-1; + std::string temp = pathToCopy; + int len = (int)temp.size()-1; #ifdef WIN32 - if(buffer[len] != '\\') - strcat(buffer, "\\"); + if(temp[len] != '\\') + temp += "\\"; #else - if(buffer[len] != '/') - strcat(buffer, "/"); + if(temp[len] != '/') + temp += "/"; #endif + + if(!Path::IsPathRooted(temp)) + { + temp = (std::string)pathToModule + temp; + } + strncpy(buffer, temp.c_str(), MAX_PATH); + #ifdef _WINDOWS + FCEUD_MakePathDirs(buffer); + #endif } else if(action == SET) { @@ -366,3 +457,4 @@ public: }; extern PathInfo path; + diff --git a/desmume/src/windows/main.cpp b/desmume/src/windows/main.cpp index 9d947fde2..e525b63e0 100644 --- a/desmume/src/windows/main.cpp +++ b/desmume/src/windows/main.cpp @@ -2406,7 +2406,7 @@ static void RefreshMicSettings() DWORD wmTimerRes; int _main() { - //7zup initialization + //7zip initialization InitDecoder(); #ifdef HAVE_WX @@ -2457,6 +2457,8 @@ int _main() GetINIPath(); + path.ReadPathSettings(); + CommonSettings.cheatsDisable = GetPrivateProfileBool("General", "cheatsDisable", false, IniName); addon_type = GetPrivateProfileInt("GBAslot", "type", NDS_ADDON_NONE, IniName); @@ -3943,7 +3945,6 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM case WM_CREATE: { - path.ReadPathSettings(); pausedByMinimize = FALSE; UpdateScreenRects();