//
// $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 <errno.h>
#endif
+#include <simgear/misc/strutils.hxx>
#include <simgear/debug/logstream.hxx>
#include <boost/foreach.hpp>
_removeOnDestroy = true;
}
-#include <stdio.h>
Dir Dir::current()
{
-#ifdef _WIN32
- char* buf = _getcwd(NULL, 0);
+#if defined(SG_WINDOWS)
+ wchar_t* buf = _wgetcwd(NULL, 0);
#else
char *buf = ::getcwd(NULL, 0);
#endif
}
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);
types = TYPE_FILE | TYPE_DIR | NO_DOT_OR_DOTDOT;
}
-#ifdef _WIN32
- std::string search(_path.local8BitStr());
+#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) {
}
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;
}
}
continue;
}
- result.push_back(file(fData.cFileName));
+ result.push_back(file(utf8File));
}
FindClose(find);
bool Dir::isEmpty() const
{
- std::string ps = _path.local8BitStr();
-#ifdef _WIN32
- return PathIsDirectoryEmpty( ps.c_str() );
+#if defined(SG_WINDOWS)
+ std::wstring ps = _path.wstr();
+ return PathIsDirectoryEmptyW( ps.c_str() );
#else
+ std::string ps = _path.local8BitStr();
DIR* dp = opendir( ps.c_str() );
if (!dp) return true;
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()) {
}
// finally, create ourselves
+#if defined(SG_WINDOWS)
+ std::wstring ps = _path.wstr();
+ int err = _wmkdir(ps.c_str());
+#else
std::string ps = _path.local8BitStr();
- int err = sgMkDir(ps.c_str(), mode);
+ int err = mkdir(ps.c_str(), mode);
+#endif
if (err) {
SG_LOG(SG_IO, SG_WARN, "directory creation failed: (" << _path << ") " << strerror(errno) );
}
}
} // of recursive deletion
- std::string ps = _path.local8BitStr();
-#ifdef _WIN32
- int err = _rmdir(ps.c_str());
+#if defined(SG_WINDOWS)
+ std::wstring ps = _path.wstr();
+ int err = _wrmdir(ps.c_str());
#else
+ std::string ps = _path.local8BitStr();
int err = rmdir(ps.c_str());
#endif
if (err) {