9 #include <dmlc/logging.h> 15 #pragma comment(lib, "Shlwapi.lib") 21 #include <sys/types.h> 28 inline void HandleSystemError(
const std::string& msg) {
31 DWORD dw = GetLastError();
33 FORMAT_MESSAGE_ALLOCATE_BUFFER |
34 FORMAT_MESSAGE_FROM_SYSTEM |
35 FORMAT_MESSAGE_IGNORE_INSERTS,
38 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
41 const std::string msg_err(static_cast<const char*>(msg_buf));
44 const std::string msg_err(strerror(errno));
46 LOG(FATAL) << msg <<
"\nReason: " << msg_err;
52 namespace filesystem {
56 DWORD ftyp = GetFileAttributesA(dirpath);
57 if (ftyp == INVALID_FILE_ATTRIBUTES) {
59 if (CreateDirectoryA(dirpath, NULL) == 0) {
61 HandleSystemError(std::string(
"CreateDirectoryIfNotExist: " 62 "failed to create new directory ") + dirpath);
65 if (!(ftyp & FILE_ATTRIBUTE_DIRECTORY)) {
66 LOG(FATAL) <<
"CreateDirectoryIfNotExist: " 67 << dirpath <<
" is a file, not a directory";
72 if (stat(dirpath, &sb) != 0) {
74 if (mkdir(dirpath, S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
76 HandleSystemError(std::string(
"CreateDirectoryIfNotExist: " 77 "failed to create new directory ") + dirpath);
80 if (!S_ISDIR(sb.st_mode)) {
81 LOG(FATAL) <<
"CreateDirectoryIfNotExist: " 82 << dirpath <<
" is a file, not a directory";
88 void WriteToFile(
const std::string& filename,
const std::string& content) {
89 std::ofstream of(filename);
93 void WriteToFile(
const std::string& filename,
const std::vector<char>& content) {
94 std::ofstream of(filename, std::ios::out | std::ios::binary);
95 of.write(content.data(), content.size());
void CreateDirectoryIfNotExist(const char *dirpath)
Create a directory with a given name if one doesn't exist already.
Cross-platform wrapper for common filesystem functions.
void WriteToFile(const std::string &filename, const std::string &content)
Write a sequence of strings to a text file, with newline character ( ) inserted between strings...