]> git.mxchange.org Git - simgear.git/blobdiff - simgear/misc/sg_path.cxx
canvas::Layout: support for contents margins.
[simgear.git] / simgear / misc / sg_path.cxx
index befb41a2e6cad487b0a33207d0464eaa7b1c02b7..7895e68ceca21b12649cf70b878d8700d030f559 100644 (file)
@@ -81,22 +81,10 @@ static SGPath pathForCSIDL(int csidl, const SGPath& def)
        return def;
 }
 #elif __APPLE__
-#include <CoreServices/CoreServices.h>
 
-//------------------------------------------------------------------------------
-static SGPath appleSpecialFolder(OSType type, const SGPath& def)
-{
-  FSRef ref;
-  OSErr err = FSFindFolder(kUserDomain, kDesktopFolderType, false, &ref);
-  if( err )
-    return def;
+// defined in CocoaHelpers.mm
+SGPath appleSpecialFolder(int dirType, int domainMask, const SGPath& def);
 
-  unsigned char path[1024];
-  if( FSRefMakePath(&ref, path, 1024) != noErr )
-    return def;
-
-  return SGPath((const char*) path, def.getPermissionChecker());
-}
 #else
 static SGPath getXDGDir( const std::string& name,
                          const SGPath& def,
@@ -489,14 +477,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 +510,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 ) {
@@ -728,16 +719,16 @@ SGPath SGPath::standardLocation(StandardLocation type, const SGPath& def)
     case PICTURES:
       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");