]> git.mxchange.org Git - simgear.git/blobdiff - simgear/misc/sg_path.cxx
Fix #1783: repeated error message on console
[simgear.git] / simgear / misc / sg_path.cxx
index befb41a2e6cad487b0a33207d0464eaa7b1c02b7..8255fa6aab742fdd11c519a64c22b39f8d820d4d 100644 (file)
@@ -56,7 +56,10 @@ static const char sgSearchPathSep = ':';
 #endif
 
 #ifdef _WIN32
-#include <ShlObj.h> // for CSIDL
+#include <ShlObj.h>         // for CSIDL
+// TODO: replace this include file with the official <versionhelpers.h> header
+// included in the Windows 8.1 SDK
+#include "sgversionhelpers.hxx"
 
 static SGPath pathForCSIDL(int csidl, const SGPath& def)
 {
@@ -80,23 +83,47 @@ static SGPath pathForCSIDL(int csidl, const SGPath& def)
 
        return def;
 }
-#elif __APPLE__
-#include <CoreServices/CoreServices.h>
 
-//------------------------------------------------------------------------------
-static SGPath appleSpecialFolder(OSType type, const SGPath& def)
+static SGPath pathForKnownFolder(REFKNOWNFOLDERID folderId, const SGPath& def)
 {
-  FSRef ref;
-  OSErr err = FSFindFolder(kUserDomain, kDesktopFolderType, false, &ref);
-  if( err )
-    return def;
+    typedef HRESULT (WINAPI*PSHGKFP)(REFKNOWNFOLDERID, DWORD, HANDLE, PWSTR*);
 
-  unsigned char path[1024];
-  if( FSRefMakePath(&ref, path, 1024) != noErr )
-    return def;
+    HINSTANCE shellDll = LoadLibrary(TEXT("shell32"));
+    if (shellDll != NULL) {
+        PSHGKFP pSHGetKnownFolderPath = (PSHGKFP) GetProcAddress(shellDll, "SHGetKnownFolderPath");
+        if (pSHGetKnownFolderPath != NULL) {
+            // system call will allocate dynamic memory... which we must release when done
+            wchar_t* localFolder = 0;
+
+            if (pSHGetKnownFolderPath(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;
+            }
+        }
+
+        FreeLibrary(shellDll);
+    }
 
-  return SGPath((const char*) path, def.getPermissionChecker());
+    return def;
 }
+
+#elif __APPLE__
+
+// defined in CocoaHelpers.mm
+SGPath appleSpecialFolder(int dirType, int domainMask, const SGPath& def);
+
 #else
 static SGPath getXDGDir( const std::string& name,
                          const SGPath& def,
@@ -489,14 +516,23 @@ bool SGPath::isFile() const
   return _exists && _isFile;
 }
 
+//------------------------------------------------------------------------------
 #ifdef _WIN32
 #  define sgMkDir(d,m)       _mkdir(d)
 #else
 #  define sgMkDir(d,m)       mkdir(d,m)
 #endif
 
+int SGPath::create_dir(mode_t mode)
+{
+  if( !canWrite() )
+  {
+    SG_LOG( SG_IO,
+            SG_WARN, "Error creating directory for '" << str() << "'"
+                                                    " reason: access denied" );
+    return -3;
+  }
 
-int SGPath::create_dir( mode_t mode ) {
     string_list dirlist = sgPathSplit(dir());
     if ( dirlist.empty() )
         return -1;
@@ -513,37 +549,31 @@ int SGPath::create_dir( mode_t mode ) {
         i = 2;
     }
 #endif
-    struct stat info;
-    int r;
-    for(; ( r = stat( dir.c_str(), &info ) ) == 0 && i < path_elements.size(); i++) {
-        dir.append(path_elements[i]);
-    }
-    if ( r == 0 ) {
-        return 0; // Directory already exists
-    }
-    for(;;)
+  struct stat info;
+  int r;
+  for(; (r = stat(dir.c_str(), &info)) == 0 && i < path_elements.size(); ++i)
+    dir.append(path_elements[i]);
+  if( r == 0 )
+      return 0; // Directory already exists
+
+  for(;;)
+  {
+    if( sgMkDir(dir.c_str(), mode) )
     {
-      if( !dir.canWrite() )
-      {
-        SG_LOG( SG_IO,
-                SG_WARN, "Error creating directory: (" << dir.str() << ")" <<
-                                                  " reason: access denied" );
-        return -3;
-      }
-      else if( sgMkDir(dir.c_str(), mode) )
-      {
-        SG_LOG( SG_IO,
-                SG_ALERT, "Error creating directory: (" << dir.str() << ")" );
-        return -2;
-      }
-
-      if( i >= path_elements.size() )
-        return  0;
-
-      dir.append(path_elements[i++]);
+      SG_LOG( SG_IO,
+              SG_ALERT, "Error creating directory: (" << dir.str() << ")" );
+      return -2;
     }
+    else
+      SG_LOG(SG_IO, SG_DEBUG, "Directory created: " << dir.str());
+
+    if( i >= path_elements.size() )
+      return 0;
 
-    return 0;
+    dir.append(path_elements[i++]);
+  }
+
+  return 0;
 }
 
 string_list sgPathBranchSplit( const string &dirpath ) {
@@ -713,31 +743,46 @@ SGPath SGPath::standardLocation(StandardLocation type, const SGPath& def)
   {
     case HOME:
       return home(def);
+
 #ifdef _WIN32
     case DESKTOP:
-      return pathForCSIDL(CSIDL_DESKTOPDIRECTORY, def);
+        if (IsWindowsVistaOrGreater())
+            return pathForKnownFolder(FOLDERID_Desktop, def);
+
+        return pathForCSIDL(CSIDL_DESKTOPDIRECTORY, def);
+
     case DOWNLOADS:
-      // TODO use KnownFolders
-      // http://msdn.microsoft.com/en-us/library/bb776911%28v=vs.85%29.aspx
-      if( !def.isNull() )
-        return def;
+        if (IsWindowsVistaOrGreater())
+            return pathForKnownFolder(FOLDERID_Downloads, def);
+
+        if (!def.isNull())
+            return def;
+
+        return pathForCSIDL(CSIDL_DESKTOPDIRECTORY, def);
 
-      return pathForCSIDL(CSIDL_DESKTOPDIRECTORY, def);
     case DOCUMENTS:
-      return pathForCSIDL(CSIDL_MYDOCUMENTS, def);
+        if (IsWindowsVistaOrGreater())
+            return pathForKnownFolder(FOLDERID_Documents, def);
+
+        return pathForCSIDL(CSIDL_MYDOCUMENTS, def);
+
     case PICTURES:
-      return pathForCSIDL(CSIDL_MYPICTURES, def);
+        if (IsWindowsVistaOrGreater())
+            return pathForKnownFolder(FOLDERID_Pictures, def);
+
+        return pathForCSIDL(CSIDL_MYPICTURES, def);
+
 #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)
     case DOWNLOADS:
-      if( !def.isNull() )
-        return def;
-      // There is no special downloads folder -> just use the desktop
+      return appleSpecialFolder(15, 1, def);
     case DESKTOP:
-      return appleSpecialFolder(kDesktopFolderType, def);
+      return appleSpecialFolder(12, 1, def);
     case DOCUMENTS:
-      return appleSpecialFolder(kDocumentsFolderType, def);
+      return appleSpecialFolder(9, 1, def);
     case PICTURES:
-      return appleSpecialFolder(kPictureDocumentsFolderType, def);
+      return appleSpecialFolder(19, 1, def);
 #else
     case DESKTOP:
       return getXDGDir("DESKTOP", def, "Desktop");
@@ -765,20 +810,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)
@@ -809,7 +849,7 @@ std::string SGPath::realpath() const
   #endif
     if (!buf)
     {
-        SG_LOG(SG_IO, SG_ALERT, "ERROR: The path '" << path << "' does not exist in the file system.");
+        SG_LOG(SG_IO, SG_WARN, "ERROR: The path '" << path << "' does not exist in the file system.");
         return path;
     }
     std::string p(buf);