Add optional lpSecurityAttributes parameter to CreateDirectory call to make compilati...
[supertux.git] / src / util / file_system.cpp
index a89b1b4..6082f9d 100644 (file)
 #include "util/log.hpp"
 
 #include <sstream>
+#include <stdexcept>
+#include <sys/stat.h>
+#include <sys/types.h>
 #include <vector>
 
+#ifdef _WIN32
+#  include <shlwapi.h>
+#else
+#  include <unistd.h>
+#endif
+
 namespace FileSystem {
 
+bool exists(const std::string& path)
+{
+#ifdef _WIN32
+  DWORD dwAttrib = GetFileAttributes(path.c_str());
+
+  return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
+          !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
+#else
+  return !access(path.c_str(), F_OK);
+#endif
+}
+
+bool is_directory(const std::string& path)
+{
+  struct stat info;
+
+  if (stat(path.c_str(), &info ) != 0)
+  {
+    // access error
+    return false;
+  }
+  else if (info.st_mode & S_IFDIR)
+  {
+    return true;
+  }
+  else
+  {
+    return false;
+  }
+}
+
+void mkdir(const std::string& directory)
+{
+#ifdef _WIN32
+  if (!CreateDirectory(directory.c_str(), NULL))
+  {
+    throw std::runtime_error("failed to create directory: "  + directory);
+  }
+#else
+  if (::mkdir(directory.c_str(), 0777) != 0)
+  {
+    throw std::runtime_error("failed to create directory: "  + directory);
+  }
+#endif
+}
+
 std::string dirname(const std::string& filename)
 {
   std::string::size_type p = filename.find_last_of('/');
@@ -103,6 +158,22 @@ std::string normalize(const std::string& filename)
   return result.str();
 }
 
+std::string join(const std::string& lhs, const std::string& rhs)
+{
+  if (lhs.empty())
+  {
+    return rhs;
+  }
+  else if (lhs.back() == '/')
+  {
+    return lhs + rhs;
+  }
+  else
+  {
+    return lhs + "/" + rhs;
+  }
+}
+
 } // namespace FileSystem
 
 /* EOF */