forked from Mirrorlandia_minetest/minetest
Add Windows-specific CreateTempFile() implementation
Once again MSVC is the only compiler not supporting basic POSIX functionality.
This commit is contained in:
parent
b480a3e9fd
commit
4feb799b7e
@ -24,7 +24,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <unistd.h>
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
@ -38,6 +37,7 @@ namespace fs
|
|||||||
#define _WIN32_WINNT 0x0501
|
#define _WIN32_WINNT 0x0501
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <shlwapi.h>
|
#include <shlwapi.h>
|
||||||
|
#include <io.h>
|
||||||
|
|
||||||
std::vector<DirListNode> GetDirListing(const std::string &pathstring)
|
std::vector<DirListNode> GetDirListing(const std::string &pathstring)
|
||||||
{
|
{
|
||||||
@ -178,13 +178,27 @@ std::string TempPath()
|
|||||||
errorstream<<"GetTempPath failed, error = "<<GetLastError()<<std::endl;
|
errorstream<<"GetTempPath failed, error = "<<GetLastError()<<std::endl;
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
std::vector<char> buf(bufsize);
|
std::string buf;
|
||||||
|
buf.resize(bufsize);
|
||||||
DWORD len = GetTempPath(bufsize, &buf[0]);
|
DWORD len = GetTempPath(bufsize, &buf[0]);
|
||||||
if(len == 0 || len > bufsize){
|
if(len == 0 || len > bufsize){
|
||||||
errorstream<<"GetTempPath failed, error = "<<GetLastError()<<std::endl;
|
errorstream<<"GetTempPath failed, error = "<<GetLastError()<<std::endl;
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
return std::string(buf.begin(), buf.begin() + len);
|
buf.resize(len);
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string CreateTempFile()
|
||||||
|
{
|
||||||
|
std::string path = TempPath() + DIR_DELIM "MT_XXXXXX";
|
||||||
|
_mktemp_s(&path[0], path.size() + 1); // modifies path
|
||||||
|
HANDLE file = CreateFile(path.c_str(), GENERIC_WRITE, 0, nullptr,
|
||||||
|
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||||
|
if (file == INVALID_HANDLE_VALUE)
|
||||||
|
return "";
|
||||||
|
CloseHandle(file);
|
||||||
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else // POSIX
|
#else // POSIX
|
||||||
@ -366,6 +380,16 @@ std::string TempPath()
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string CreateTempFile()
|
||||||
|
{
|
||||||
|
std::string path = TempPath() + DIR_DELIM "MT_XXXXXX";
|
||||||
|
int fd = mkstemp(&path[0]); // modifies path
|
||||||
|
if (fd == -1)
|
||||||
|
return "";
|
||||||
|
close(fd);
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir)
|
void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir)
|
||||||
@ -813,15 +837,5 @@ bool Rename(const std::string &from, const std::string &to)
|
|||||||
return rename(from.c_str(), to.c_str()) == 0;
|
return rename(from.c_str(), to.c_str()) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string CreateTempFile()
|
|
||||||
{
|
|
||||||
std::string path = TempPath() + DIR_DELIM "MT_XXXXXX";
|
|
||||||
int fd = mkstemp(&path[0]); // modifies path
|
|
||||||
if (fd == -1)
|
|
||||||
return "";
|
|
||||||
close(fd);
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace fs
|
} // namespace fs
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user