summaryrefslogtreecommitdiffstats
path: root/llvm/lib/System/Unix/Path.cpp
diff options
context:
space:
mode:
authorReid Spencer <rspencer@reidspencer.com>2004-09-11 04:55:08 +0000
committerReid Spencer <rspencer@reidspencer.com>2004-09-11 04:55:08 +0000
commitae9bbda50a199db082054b0d53db88cbca63ce1f (patch)
tree01f0168ddf8e2ea99bc967d9265d9df5362a2427 /llvm/lib/System/Unix/Path.cpp
parent2d20d765d899cb3532d9b3374d87a088189e9acb (diff)
downloadbcm5719-llvm-ae9bbda50a199db082054b0d53db88cbca63ce1f.tar.gz
bcm5719-llvm-ae9bbda50a199db082054b0d53db88cbca63ce1f.zip
Implemented support for detecting file types by magic number, stripping
path and suffix to leave basename, and getting the DLL suffix. llvm-svn: 16289
Diffstat (limited to 'llvm/lib/System/Unix/Path.cpp')
-rw-r--r--llvm/lib/System/Unix/Path.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/llvm/lib/System/Unix/Path.cpp b/llvm/lib/System/Unix/Path.cpp
index 49b2bf4afe1..23b938b9019 100644
--- a/llvm/lib/System/Unix/Path.cpp
+++ b/llvm/lib/System/Unix/Path.cpp
@@ -20,6 +20,7 @@
#include "Unix.h"
#include <sys/stat.h>
#include <fcntl.h>
+#include <fstream>
namespace llvm {
using namespace sys;
@@ -78,6 +79,53 @@ Path::GetUserHomeDirectory() {
}
bool
+Path::is_file() const {
+ return (is_valid() && path[path.length()-1] != '/');
+}
+
+bool
+Path::is_directory() const {
+ return (is_valid() && path[path.length()-1] == '/');
+}
+
+std::string
+Path::get_basename() const {
+ // Find the last slash
+ size_t slash = path.rfind('/');
+ if (slash == std::string::npos)
+ slash = 0;
+ else
+ slash++;
+
+ return path.substr(slash, path.rfind('.'));
+}
+
+bool Path::has_magic_number(const std::string &Magic) const {
+ size_t len = Magic.size();
+ char buf[ 1 + len];
+ std::ifstream f(path.c_str());
+ f.read(buf, len);
+ buf[len] = '\0';
+ return Magic == buf;
+}
+
+bool
+Path::is_bytecode_file() const {
+ if (readable()) {
+ return has_magic_number("llvm");
+ }
+ return false;
+}
+
+bool
+Path::is_archive() const {
+ if (readable()) {
+ return has_magic_number("!<arch>\012");
+ }
+ return false;
+}
+
+bool
Path::exists() const {
return 0 == access(path.c_str(), F_OK );
}
OpenPOWER on IntegriCloud