1 #ifndef DIY_IO_UTILS_HPP
2 #define DIY_IO_UTILS_HPP
16 #include "../constants.h"
27 inline bool is_directory(
const std::string& filename)
30 DWORD attr = GetFileAttributes(filename.c_str());
31 return (attr != INVALID_FILE_ATTRIBUTES) && ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0);
34 return (stat(filename.c_str(), &s) == 0 && S_ISDIR(s.st_mode));
41 inline bool make_directory(
const std::string& filename)
44 return _mkdir(filename.c_str()) == 0;
46 return mkdir(filename.c_str(), 0755) == 0;
54 inline void truncate(
const std::string& filename,
size_t length)
58 _sopen_s(&fd, filename.c_str(), _O_WRONLY | _O_BINARY, _SH_DENYNO, _S_IWRITE);
61 _chsize_s(fd, static_cast<__int64>(length));
65 ::truncate(filename.c_str(),
static_cast<off_t
>(length));
69 inline int mkstemp(std::string& filename)
71 const size_t slen = filename.size();
72 char *s_template =
new char[filename.size() + 1];
73 std::copy_n(filename.c_str(), slen+1, s_template);
77 if (_mktemp_s(s_template, slen+1) == 0)
78 _sopen_s(&handle, s_template, _O_WRONLY | _O_CREAT | _O_BINARY, _SH_DENYNO, _S_IWRITE);
79 #elif defined(__MACH__)
81 handle = ::mkstemp(s_template);
83 handle = mkostemp(s_template, O_WRONLY | O_SYNC);
86 filename = s_template;
90 inline void close(
int fd)
100 inline void sync(
int fd)
109 inline bool remove(
const std::string& filename)
111 return ::remove(filename.c_str()) == 0;