]> git.mxchange.org Git - simgear.git/blobdiff - simgear/misc/sg_dir.cxx
Use wide-string APIs on Windows.
[simgear.git] / simgear / misc / sg_dir.cxx
index 5ffc10d67c29aa500e8f28d2552ee8550259bac9..208a05ede923466f8a00303fc3232c40a1ef8e77 100644 (file)
 //
 // $Id$
 
-#ifdef HAVE_CONFIG_H
-#  include <simgear_config.h>
-#endif
+#include <simgear_config.h>
+#include <simgear/compiler.h>
 
 #include <simgear/misc/sg_dir.hxx>
+#include <simgear/structure/exception.hxx>
 #include <math.h>
 #include <stdlib.h>
 #include <cstdio>
@@ -31,6 +31,7 @@
 #  define WIN32_LEAN_AND_MEAN
 #  include <windows.h>
 #  include <direct.h>
+#  include <Shlwapi.h>
 #else
 #  include <sys/types.h>
 #  include <dirent.h>
@@ -39,6 +40,7 @@
 #  include <errno.h>
 #endif
 
+#include <simgear/misc/strutils.hxx>
 #include <simgear/debug/logstream.hxx>
 #include <boost/foreach.hpp>
 
@@ -65,7 +67,7 @@ Dir::Dir(const SGPath& path) :
 }
 
 Dir::Dir(const Dir& rel, const SGPath& relPath) :
-  _path(rel.file(relPath.str())),
+  _path(rel.file(relPath.utf8Str())),
   _removeOnDestroy(false)
 {
     _path.set_cached(false); // disable caching, so create/remove work
@@ -85,11 +87,16 @@ void Dir::setRemoveOnDestroy()
 
 Dir Dir::current()
 {
-#ifdef _WIN32
-    char* buf = _getcwd(NULL, 0);
+#if defined(SG_WINDOWS)
+    wchar_t* buf = _wgetcwd(NULL, 0);
 #else
-    charbuf = ::getcwd(NULL, 0);
+    char *buf = ::getcwd(NULL, 0);
 #endif
+    if (!buf) {
+        if  (errno == 2) throw sg_exception("The current directory is invalid");
+        else throw sg_exception(strerror(errno));
+    }
+
     SGPath p(buf);
     free(buf);
     return Dir(p);
@@ -108,18 +115,26 @@ Dir Dir::tempDir(const std::string& templ)
     // Mac OS-X / BSD manual says any number of 'X's, but GLibc manual
     // says exactly six, so that's what I'm going with
     p.concat("-XXXXXX");
-    ::snprintf(buf, 1024, "%s", p.c_str());
+    std::string s = p.local8BitStr();
+    ::snprintf(buf, 1024, "%s", s.c_str());
     if (!mkdtemp(buf)) {
         SG_LOG(SG_IO, SG_WARN, "mkdtemp failed:" << strerror(errno));
         return Dir();
     }
     
     return Dir(SGPath(buf));
+#else
+#if defined(SG_WINDOWS)
+       std::wstring wideTemplate = simgear::strutils::convertUtf8ToWString(templ);
+       wchar_t* buf = _wtempnam(0, wideTemplate.c_str());
+       SGPath p(buf);
+       free(buf); // unlike tempnam(), _wtempnam mallocs its result buffer
 #else
     SGPath p(tempnam(0, templ.c_str()));
+#endif
     Dir t(p);
     if (!t.create(0700)) {
-        SG_LOG(SG_IO, SG_WARN, "failed to create temporary directory at " << p.str());
+        SG_LOG(SG_IO, SG_WARN, "failed to create temporary directory at " << p);
     }
     
     return t;
@@ -138,36 +153,37 @@ PathList Dir::children(int types, const std::string& nameFilter) const
     types = TYPE_FILE | TYPE_DIR | NO_DOT_OR_DOTDOT;
   }
   
-#ifdef _WIN32
-  std::string search(_path.str());
+#if defined(SG_WINDOWS)
+  std::wstring search(_path.wstr());
   if (nameFilter.empty()) {
-    search += "\\*"; // everything
+    search += simgear::strutils::convertUtf8ToWString("\\*"); // everything
   } else {
-    search += "\\*" + nameFilter;
+    search += simgear::strutils::convertUtf8ToWString("\\*" + nameFilter);
   }
   
-  WIN32_FIND_DATA fData;
-  HANDLE find = FindFirstFile(search.c_str(), &fData);
+  WIN32_FIND_DATAW fData;
+  HANDLE find = FindFirstFileW(search.c_str(), &fData);
   if (find == INVALID_HANDLE_VALUE) {
          int err = GetLastError();
          if (err != ERROR_FILE_NOT_FOUND) {
                SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: FindFirstFile failed:" << 
-                       _path.str() << " with error:" << err);
+                       _path << " with error:" << err);
          }
     return result;
   }
   
   bool done = false;
-  for (bool done = false; !done; done = (FindNextFile(find, &fData) == 0)) {
+  for (bool done = false; !done; done = (FindNextFileW(find, &fData) == 0)) {
     if (fData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
       if (!(types & INCLUDE_HIDDEN)) {
         continue;
       }
     }
     
+       std::string utf8File = simgear::strutils::convertWStringToUtf8(fData.cFileName);
     if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
          if (types & NO_DOT_OR_DOTDOT) {
-               if (!strcmp(fData.cFileName,".") || !strcmp(fData.cFileName,"..")) {
+               if ((utf8File == ".") || (utf8File == "..")) {
                  continue;
                }
          }
@@ -183,14 +199,15 @@ PathList Dir::children(int types, const std::string& nameFilter) const
        continue;
     }
 
-    result.push_back(file(fData.cFileName));
+    result.push_back(file(utf8File));
   }
 
   FindClose(find);
 #else
-  DIR* dp = opendir(_path.c_str());
+    std::string ps = _path.local8BitStr();
+  DIR* dp = opendir(ps.c_str());
   if (!dp) {
-    SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: opendir failed:" << _path.str());
+    SG_LOG(SG_GENERAL, SG_WARN, "Dir::children: opendir failed:" << _path);
     return result;
   }
   
@@ -262,51 +279,21 @@ PathList Dir::children(int types, const std::string& nameFilter) const
 
 bool Dir::isEmpty() const
 {
-  bool empty= true;
-#ifdef _WIN32 
-  WIN32_FIND_DATA fData;
-  HANDLE find = FindFirstFile("\\*", &fData);
-  if (find == INVALID_HANDLE_VALUE) {
-    return true;
-  }
-  
-// since an empty dir will still have . and .. children, we need
-// watch for those - anything else means the dir is really non-empty
-  bool done = false;
-  for (; !done; done = (FindNextFile(find, &fData) == 0)) {
-    if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
-                 if (!strcmp(fData.cFileName,".") || !strcmp(fData.cFileName,"..")) {
-                   continue;
-                 }
-         }
-    
-    empty = false;
-    break;
-  }
-    
-  FindClose(find);
+#if defined(SG_WINDOWS)
+  std::wstring ps = _path.wstr();
+  return PathIsDirectoryEmptyW( ps.c_str() );
 #else
-  DIR* dp = opendir(_path.c_str());
-  if (!dp) {
-    return true;
-  }
-    
-  while (true) {
-    struct dirent* entry = readdir(dp);
-    if (!entry) {
-      break; // done iteration
-    }
-    
-    if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
-      continue;
-    }
-      
-    empty = false;
-    break;
-  }
+  std::string ps = _path.local8BitStr();
+  DIR* dp = opendir( ps.c_str() );
+  if (!dp) return true;
+
+  int n = 0;
+  dirent* d;
+  while( (d = readdir(dp)) !=NULL && (n < 4) ) n++;
   closedir(dp);
+
+  return (n == 2); // '.' and '..' always exist
 #endif
-  return empty;
 }
 
 bool Dir::exists() const
@@ -322,12 +309,6 @@ SGPath Dir::file(const std::string& name) const
   return childPath;  
 }
 
-#ifdef _WIN32
-#  define sgMkDir(d,m)       _mkdir(d)
-#else
-#  define sgMkDir(d,m)       mkdir(d,m)
-#endif
-
 bool Dir::create(mode_t mode)
 {
     if (exists()) {
@@ -344,9 +325,15 @@ bool Dir::create(mode_t mode)
     }
     
 // finally, create ourselves
-    int err = sgMkDir(_path.c_str(), mode);
+#if defined(SG_WINDOWS)
+       std::wstring ps = _path.wstr();
+       int err = _wmkdir(ps.c_str());
+#else
+    std::string ps = _path.local8BitStr();
+    int err = mkdir(ps.c_str(), mode);
+#endif
     if (err) {
-        SG_LOG(SG_IO, SG_WARN,  "directory creation failed: (" << _path.str() << ") " << strerror(errno) );
+        SG_LOG(SG_IO, SG_WARN,  "directory creation failed: (" << _path << ") " << strerror(errno) );
     }
     
     return (err == 0);
@@ -382,17 +369,20 @@ bool Dir::remove(bool recursive)
     
     if (recursive) {
         if (!removeChildren()) {
+            SG_LOG(SG_IO, SG_WARN, "Dir at:" << _path << " failed to remove children");
             return false;
         }
     } // of recursive deletion
-    
-#ifdef _WIN32
-    int err = _rmdir(_path.c_str());
+
+#if defined(SG_WINDOWS)
+       std::wstring ps = _path.wstr();
+    int err = _wrmdir(ps.c_str());
 #else
-    int err = rmdir(_path.c_str());
+       std::string ps = _path.local8BitStr();
+    int err = rmdir(ps.c_str());
 #endif
     if (err) {
-        SG_LOG(SG_IO, SG_WARN, "rmdir failed:" << _path.str() << ":" << strerror(errno));
+        SG_LOG(SG_IO, SG_WARN, "rmdir failed:" << _path << ":" << strerror(errno));
     }
     return (err == 0);
 }