]> git.mxchange.org Git - simgear.git/blobdiff - simgear/misc/sg_path.cxx
HTTP: Rename urlretrieve/urlload to save/load.
[simgear.git] / simgear / misc / sg_path.cxx
index 9267cedf6a1c6971cc76d28ca1bb8d415579f290..cc71a9af1e256e18f729310c23cf62ac16377cc3 100644 (file)
@@ -144,7 +144,7 @@ void SGPath::set_cached(bool cached)
 
 // append another piece to the existing path
 void SGPath::append( const string& p ) {
-    if ( path.size() == 0 ) {
+    if ( path.empty() ) {
         path = p;
     } else {
     if ( p[0] != sgDirPathSep ) {
@@ -173,7 +173,7 @@ void SGPath::add( const string& p ) {
 // concatenate a string to the end of the path without inserting a
 // path separator
 void SGPath::concat( const string& p ) {
-    if ( path.size() == 0 ) {
+    if ( path.empty() ) {
         path = p;
     } else {
         path += p;
@@ -378,7 +378,7 @@ int SGPath::create_dir( mode_t mode ) {
 string_list sgPathBranchSplit( const string &dirpath ) {
     string_list path_elements;
     string element, path = dirpath;
-    while ( path.size() ) {
+    while ( ! path.empty() ) {
         size_t p = path.find( sgDirPathSep );
         if ( p != string::npos ) {
             element = path.substr( 0, p );
@@ -387,7 +387,7 @@ string_list sgPathBranchSplit( const string &dirpath ) {
             element = path;
             path = "";
         }
-        if ( element.size() )
+        if ( ! element.empty() )
             path_elements.push_back( element );
     }
     return path_elements;
@@ -461,7 +461,8 @@ bool SGPath::remove()
     int err = ::unlink(c_str());
     if (err) {
         SG_LOG(SG_IO, SG_WARN,  "file remove failed: (" << str() << ") " << strerror(errno));
-    }
+       }
+       _cached = false; // stat again if required
     return (err == 0);
 }
 
@@ -483,6 +484,14 @@ bool SGPath::operator!=(const SGPath& other) const
 
 bool SGPath::rename(const SGPath& newName)
 {
+#ifdef SG_WINDOWS
+       if (newName.exists()) {
+               SGPath r(newName);
+               if (!r.remove()) {
+                       return false;
+               }
+       }
+#endif
     if (::rename(c_str(), newName.c_str()) != 0) {
         SG_LOG(SG_IO, SG_WARN, "renamed failed: from " << str() << " to " << newName.str()
             << " reason: " << strerror(errno));
@@ -519,11 +528,32 @@ SGPath SGPath::home()
 #endif
 
 #ifdef _WIN32
+
+#include <ShlObj.h> // for CSIDL
+
 //------------------------------------------------------------------------------
 SGPath SGPath::desktop()
 {
-  // TODO
-  return SGPath();
+       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();
 }
 #elif __APPLE__
 #include <CoreServices/CoreServices.h>