]> git.mxchange.org Git - simgear.git/commitdiff
Portability: Implemented Known Folders for Windows
authorJames Turner <zakalawe@mac.com>
Tue, 24 Mar 2015 16:08:36 +0000 (11:08 -0500)
committerJames Turner <zakalawe@mac.com>
Tue, 24 Mar 2015 16:08:36 +0000 (11:08 -0500)
Patch from Scott (xDraconian)

simgear/misc/path_test.cxx
simgear/misc/sg_path.cxx

index 94f9eaad296087a327e8669ecee9b157c45b9e9d..61a9fba94ea77100b9d2741c8bf90ea561c8f0af 100644 (file)
@@ -141,8 +141,12 @@ int main(int argc, char* argv[])
     
 // add
     pc.add("/opt/local");
+#ifdef _WIN32
+    COMPARE(pc.str(), std::string("/usr/local/include/;/opt/local"));
+#else
     COMPARE(pc.str(), std::string("/usr/local/include/:/opt/local"));
-    
+#endif
+
 // concat
     SGPath pd = pb;
     pd.concat("-1");
index 7895e68ceca21b12649cf70b878d8700d030f559..1cfe465c06e43cc8244a677d0e060a2d4eaaeb69 100644 (file)
@@ -56,7 +56,8 @@ static const char sgSearchPathSep = ':';
 #endif
 
 #ifdef _WIN32
-#include <ShlObj.h> // for CSIDL
+#include <ShlObj.h>         // for CSIDL
+#include <Knownfolders.h>   // for Standard Folder GUIDs
 
 static SGPath pathForCSIDL(int csidl, const SGPath& def)
 {
@@ -80,6 +81,31 @@ static SGPath pathForCSIDL(int csidl, const SGPath& def)
 
        return def;
 }
+
+static SGPath pathForKnownFolder(REFKNOWNFOLDERID folderId, const SGPath& def)
+{
+    // system call will allocate dynamic memory... which we must release when done
+    wchar_t* localFolder = 0;
+    if (SHGetKnownFolderPath(folderId, KF_FLAG_DEFAULT_PATH, NULL, &localFolder) == S_OK) {
+        // copy into local memory
+        char path[MAX_PATH];
+        size_t len;
+        if (wcstombs_s(&len, path, localFolder, MAX_PATH) != S_OK) {
+            path[0] = '\0';
+            SG_LOG(SG_GENERAL, SG_WARN, "WCS to MBS failed");
+        }
+
+        SGPath folder_path = SGPath(path, def.getPermissionChecker());
+
+        // release dynamic memory
+        CoTaskMemFree(static_cast<void*>(localFolder));
+
+        return folder_path;
+    }
+
+    return def;
+}
+
 #elif __APPLE__
 
 // defined in CocoaHelpers.mm
@@ -705,6 +731,16 @@ SGPath SGPath::standardLocation(StandardLocation type, const SGPath& def)
     case HOME:
       return home(def);
 #ifdef _WIN32
+#   if (WINVER > 0x0501)
+    case DESKTOP:
+        return pathForKnownFolder(FOLDERID_Desktop, def);
+    case DOWNLOADS:
+        return pathForKnownFolder(FOLDERID_Downloads, def);
+    case DOCUMENTS:
+        return pathForKnownFolder(FOLDERID_Documents, def);
+    case PICTURES:
+        return pathForKnownFolder(FOLDERID_Pictures, def);
+#   else
     case DESKTOP:
       return pathForCSIDL(CSIDL_DESKTOPDIRECTORY, def);
     case DOWNLOADS:
@@ -718,6 +754,7 @@ SGPath SGPath::standardLocation(StandardLocation type, const SGPath& def)
       return pathForCSIDL(CSIDL_MYDOCUMENTS, def);
     case PICTURES:
       return pathForCSIDL(CSIDL_MYPICTURES, def);
+#   endif
 #elif __APPLE__
       // since this is C++, we can't include NSPathUtilities.h to access the enum
       // values, so hard-coding them here (they are stable, don't worry)
@@ -756,20 +793,15 @@ SGPath SGPath::fromEnv(const char* name, const SGPath& def)
   return def;
 }
 
-#ifdef _WIN32
 //------------------------------------------------------------------------------
 SGPath SGPath::home(const SGPath& def)
 {
-  // TODO
-  return def;
-}
+#ifdef _WIN32
+    return fromEnv("USERPROFILE", def);
 #else
-//------------------------------------------------------------------------------
-SGPath SGPath::home(const SGPath& def)
-{
-  return fromEnv("HOME", def);
-}
+    return fromEnv("HOME", def);
 #endif
+}
 
 //------------------------------------------------------------------------------
 SGPath SGPath::desktop(const SGPath& def)