]> git.mxchange.org Git - simgear.git/blobdiff - simgear/misc/sg_path.cxx
Update doxgen config and some comments.
[simgear.git] / simgear / misc / sg_path.cxx
index 375daae68c85938be61a65de973d0633f44913e7..3c6e5b6b6693b8ebbc3d3e75bc21c7e3f99126ba 100644 (file)
@@ -55,6 +55,79 @@ static const char sgSearchPathSep = ';';
 static const char sgSearchPathSep = ':';
 #endif
 
+#ifdef _WIN32
+#include <ShlObj.h> // for CSIDL
+
+static SGPath pathForCSIDL(int csidl, const SGPath& def)
+{
+       typedef BOOL (WINAPI*GetSpecialFolderPath)(HWND, LPSTR, int, BOOL);
+       static GetSpecialFolderPath SHGetSpecialFolderPath = NULL;
+
+       // lazy open+resolve of shell32
+       if (!SHGetSpecialFolderPath) {
+               HINSTANCE shellDll = ::LoadLibrary("shell32");
+               SHGetSpecialFolderPath = (GetSpecialFolderPath) GetProcAddress(shellDll, "SHGetSpecialFolderPathA");
+       }
+
+       if (!SHGetSpecialFolderPath){
+               return def;
+       }
+
+       char path[MAX_PATH];
+       if (SHGetSpecialFolderPath(0, path, csidl, false)) {
+               return SGPath(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,
+                         const std::string& fallback )
+{
+  // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
+
+  // $XDG_CONFIG_HOME defines the base directory relative to which user specific
+  // configuration files should be stored. If $XDG_CONFIG_HOME is either not set
+  // or empty, a default equal to $HOME/.config should be used.
+  const SGPath user_dirs = SGPath::fromEnv( "XDG_CONFIG_HOME",
+                                            SGPath::home() / ".config")
+                         / "user-dirs.dirs";
+
+  // Format is XDG_xxx_DIR="$HOME/yyy", where yyy is a shell-escaped
+  // homedir-relative path, or XDG_xxx_DIR="/yyy", where /yyy is an absolute
+  // path. No other format is supported.
+  const std::string XDG_ID = "XDG_" + name + "_DIR=\"";
+
+  std::ifstream user_dirs_file( user_dirs.c_str() );
+  std::string line;
+  while( std::getline(user_dirs_file, line).good() )
+  {
+    if( !starts_with(line, XDG_ID) || *line.rbegin() != '"' )
+      continue;
+
+    // Extract dir from XDG_<name>_DIR="<dir>"
+    line = line.substr(XDG_ID.length(), line.length() - XDG_ID.length() - 1 );
+
+    const std::string HOME = "$HOME";
+    if( starts_with(line, HOME) )
+      return SGPath::home(def)
+           / simgear::strutils::unescape(line.substr(HOME.length()));
+
+    return SGPath(line, def.getPermissionChecker());
+  }
+
+  if( def.isNull() )
+    return SGPath::home(def) / fallback;
+
+  return def;
+}
+#endif
 
 // For windows, replace "\" by "/".
 void
@@ -75,9 +148,9 @@ SGPath::fix()
 
 
 // default constructor
-SGPath::SGPath(PermissonChecker validator)
+SGPath::SGPath(PermissionChecker validator)
     : path(""),
-    _permisson_checker(validator),
+    _permission_checker(validator),
     _cached(false),
     _rwCached(false),
     _cacheEnabled(true)
@@ -86,9 +159,9 @@ SGPath::SGPath(PermissonChecker validator)
 
 
 // create a path based on "path"
-SGPath::SGPath( const std::string& p, PermissonChecker validator )
+SGPath::SGPath( const std::string& p, PermissionChecker validator )
     : path(p),
-    _permisson_checker(validator),
+    _permission_checker(validator),
     _cached(false),
     _rwCached(false),
     _cacheEnabled(true)
@@ -99,9 +172,9 @@ SGPath::SGPath( const std::string& p, PermissonChecker validator )
 // create a path based on "path" and a "subpath"
 SGPath::SGPath( const SGPath& p,
                 const std::string& r,
-                PermissonChecker validator )
+                PermissionChecker validator )
     : path(p.path),
-    _permisson_checker(validator),
+    _permission_checker(validator),
     _cached(false),
     _rwCached(false),
     _cacheEnabled(p._cacheEnabled)
@@ -112,7 +185,7 @@ SGPath::SGPath( const SGPath& p,
 
 SGPath::SGPath(const SGPath& p) :
   path(p.path),
-  _permisson_checker(p._permisson_checker),
+  _permission_checker(p._permission_checker),
   _cached(p._cached),
   _rwCached(p._rwCached),
   _cacheEnabled(p._cacheEnabled),
@@ -128,7 +201,7 @@ SGPath::SGPath(const SGPath& p) :
 SGPath& SGPath::operator=(const SGPath& p)
 {
   path = p.path;
-  _permisson_checker = p._permisson_checker,
+  _permission_checker = p._permission_checker,
   _cached = p._cached;
   _rwCached = p._rwCached;
   _cacheEnabled = p._cacheEnabled;
@@ -155,16 +228,16 @@ void SGPath::set( const string& p ) {
 }
 
 //------------------------------------------------------------------------------
-void SGPath::setPermissonChecker(PermissonChecker validator)
+void SGPath::setPermissionChecker(PermissionChecker validator)
 {
-  _permisson_checker = validator;
+  _permission_checker = validator;
   _rwCached = false;
 }
 
 //------------------------------------------------------------------------------
-SGPath::PermissonChecker SGPath::getPermissonChecker() const
+SGPath::PermissionChecker SGPath::getPermissionChecker() const
 {
-  return _permisson_checker;
+  return _permission_checker;
 }
 
 //------------------------------------------------------------------------------
@@ -306,19 +379,27 @@ string SGPath::complete_lower_extension() const
     }
 }
 
+//------------------------------------------------------------------------------
 void SGPath::validate() const
 {
   if (_cached && _cacheEnabled) {
     return;
   }
 
+  if (path.empty()) {
+         _exists = false;
+         return;
+  }
+
 #ifdef _WIN32
   struct _stat buf ;
-
   bool remove_trailing = false;
-  if ( path.length() > 1 && path[path.length()-1] == '/' )
-      remove_trailing=true;
-  if (_stat (path.substr(0,remove_trailing?path.length()-1:path.length()).c_str(), &buf ) < 0) {
+  string statPath(path);
+  if ((path.length() > 1) && (path.back() == '/')) {
+         statPath.pop_back();
+  }
+      
+  if (_stat(statPath.c_str(), &buf ) < 0) {
     _exists = false;
   } else {
     _exists = true;
@@ -343,14 +424,15 @@ void SGPath::validate() const
   _cached = true;
 }
 
+//------------------------------------------------------------------------------
 void SGPath::checkAccess() const
 {
   if( _rwCached && _cacheEnabled )
     return;
 
-  if( _permisson_checker )
+  if( _permission_checker )
   {
-    Permissions p = _permisson_checker(*this);
+    Permissions p = _permission_checker(*this);
     _canRead = p.read;
     _canWrite = p.write;
   }
@@ -411,7 +493,7 @@ int SGPath::create_dir( mode_t mode ) {
     bool absolute = !path.empty() && path[0] == sgDirPathSep;
 
     unsigned int i = 1;
-    SGPath dir(absolute ? string( 1, sgDirPathSep ) : "", _permisson_checker);
+    SGPath dir(absolute ? string( 1, sgDirPathSep ) : "", _permission_checker);
     dir.concat( path_elements[0] );
 #ifdef _WIN32
     if ( dir.str().find(':') != string::npos && path_elements.size() >= 2 ) {
@@ -432,8 +514,8 @@ int SGPath::create_dir( mode_t mode ) {
       if( !dir.canWrite() )
       {
         SG_LOG( SG_IO,
-                SG_ALERT, "Error creating directory: (" << dir.str() << ")" <<
-                                                   " reason: access denied" );
+                SG_WARN, "Error creating directory: (" << dir.str() << ")" <<
+                                                  " reason: access denied" );
         return -3;
       }
       else if( sgMkDir(dir.c_str(), mode) )
@@ -575,7 +657,7 @@ bool SGPath::operator!=(const SGPath& other) const
 //------------------------------------------------------------------------------
 bool SGPath::rename(const SGPath& newName)
 {
-  if( !newName.canWrite() )
+  if( !canRead() || !canWrite() || !newName.canWrite() )
   {
     SG_LOG( SG_IO, SG_WARN, "rename failed: from " << str() <<
                                             " to " << newName.str() <<
@@ -600,121 +682,105 @@ bool SGPath::rename(const SGPath& newName)
   }
 
   path = newName.path;
+
+  // Do not remove permission checker (could happen for example if just using
+  // a std::string as new name)
+  if( newName._permission_checker )
+    _permission_checker = newName._permission_checker;
+
   _cached = false;
   _rwCached = false;
 
   return true;
 }
 
+//------------------------------------------------------------------------------
+SGPath SGPath::standardLocation(StandardLocation type, const SGPath& def)
+{
+  switch(type)
+  {
+    case HOME:
+      return home(def);
+#ifdef _WIN32
+    case DESKTOP:
+      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;
+
+      return pathForCSIDL(CSIDL_DESKTOPDIRECTORY, def);
+    case DOCUMENTS:
+      return pathForCSIDL(CSIDL_MYDOCUMENTS, 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:
+      return appleSpecialFolder(15, 1, def);
+    case DESKTOP:
+      return appleSpecialFolder(12, 1, def);
+    case DOCUMENTS:
+      return appleSpecialFolder(9, 1, def);
+    case PICTURES:
+      return appleSpecialFolder(19, 1, def);
+#else
+    case DESKTOP:
+      return getXDGDir("DESKTOP", def, "Desktop");
+    case DOWNLOADS:
+      return getXDGDir("DOWNLOADS", def, "Downloads");
+    case DOCUMENTS:
+      return getXDGDir("DOCUMENTS", def, "Documents");
+    case PICTURES:
+      return getXDGDir("PICTURES", def, "Pictures");
+#endif
+    default:
+      SG_LOG( SG_GENERAL,
+              SG_WARN,
+              "SGPath::standardLocation() unhandled type: " << type );
+      return def;
+  }
+}
+
 //------------------------------------------------------------------------------
 SGPath SGPath::fromEnv(const char* name, const SGPath& def)
 {
   const char* val = getenv(name);
   if( val && val[0] )
-    return SGPath(val, def._permisson_checker);
+    return SGPath(val, def._permission_checker);
   return def;
 }
 
 #ifdef _WIN32
 //------------------------------------------------------------------------------
-SGPath SGPath::home()
+SGPath SGPath::home(const SGPath& def)
 {
   // TODO
-  return SGPath();
+  return def;
 }
 #else
 //------------------------------------------------------------------------------
-SGPath SGPath::home()
+SGPath SGPath::home(const SGPath& def)
 {
-  return fromEnv("HOME");
+  return fromEnv("HOME", def);
 }
 #endif
 
-#ifdef _WIN32
-
-#include <ShlObj.h> // for CSIDL
-
 //------------------------------------------------------------------------------
-SGPath SGPath::desktop()
+SGPath SGPath::desktop(const SGPath& def)
 {
-       typedef BOOL (WINAPI*GetSpecialFolderPath)(HWND, LPSTR, int, BOOL);
-       static GetSpecialFolderPath SHGetSpecialFolderPath = NULL;
-
-       // lazy open+resolve of shell32
-       if (!SHGetSpecialFolderPath) {
-               HINSTANCE shellDll = ::LoadLibrary("shell32");
-               SHGetSpecialFolderPath = (GetSpecialFolderPath) GetProcAddress(shellDll, "SHGetSpecialFolderPathA");
-       }
-
-       if (!SHGetSpecialFolderPath){
-               return SGPath();
-       }
-
-       char path[MAX_PATH];
-       if (SHGetSpecialFolderPath(0, path, CSIDL_DESKTOPDIRECTORY, false)) {
-               return SGPath(path);
-       }
-
-       SG_LOG(SG_GENERAL, SG_ALERT, "SGPath::desktop() failed, bad" );
-       return SGPath();
+  return standardLocation(DESKTOP, def);
 }
-#elif __APPLE__
-#include <CoreServices/CoreServices.h>
-
-//------------------------------------------------------------------------------
-SGPath SGPath::desktop()
-{
-  FSRef ref;
-  OSErr err = FSFindFolder(kUserDomain, kDesktopFolderType, false, &ref);
-  if (err) {
-    return SGPath();
-  }
-
-  unsigned char path[1024];
-  if (FSRefMakePath(&ref, path, 1024) != noErr) {
-    return SGPath();
-  }
 
-  return SGPath((const char*) path);
-}
-#else
 //------------------------------------------------------------------------------
-SGPath SGPath::desktop()
+SGPath SGPath::documents(const SGPath& def)
 {
-  // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
-
-  // $XDG_CONFIG_HOME defines the base directory relative to which user specific
-  // configuration files should be stored. If $XDG_CONFIG_HOME is either not set
-  // or empty, a default equal to $HOME/.config should be used.
-  const SGPath user_dirs = fromEnv("XDG_CONFIG_HOME", home() / ".config")
-                         / "user-dirs.dirs";
-
-  // Format is XDG_xxx_DIR="$HOME/yyy", where yyy is a shell-escaped
-  // homedir-relative path, or XDG_xxx_DIR="/yyy", where /yyy is an absolute
-  // path. No other format is supported.
-  const std::string DESKTOP = "XDG_DESKTOP_DIR=\"";
-
-  std::ifstream user_dirs_file( user_dirs.c_str() );
-  std::string line;
-  while( std::getline(user_dirs_file, line).good() )
-  {
-    if( !starts_with(line, DESKTOP) || *line.rbegin() != '"' )
-      continue;
-
-    // Extract dir from XDG_DESKTOP_DIR="<dir>"
-    line = line.substr(DESKTOP.length(), line.length() - DESKTOP.length() - 1 );
-
-    const std::string HOME = "$HOME";
-    if( starts_with(line, HOME) )
-      return home() / simgear::strutils::unescape(line.substr(HOME.length()));
-
-    return SGPath(line);
-  }
-
-  return home() / "Desktop";
+  return standardLocation(DOCUMENTS, def);
 }
-#endif
 
+//------------------------------------------------------------------------------
 std::string SGPath::realpath() const
 {
 #if (defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED <= 1050)