From: Reid Spencer Date: Sat, 11 Sep 2004 04:55:08 +0000 (+0000) Subject: Implemented support for detecting file types by magic number, stripping X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=1b554b4583044050109f8b70929d6777ec073188;p=oota-llvm.git Implemented support for detecting file types by magic number, stripping path and suffix to leave basename, and getting the DLL suffix. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16289 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/System/Unix/Path.cpp b/lib/System/Unix/Path.cpp index 49b2bf4afe1..23b938b9019 100644 --- a/lib/System/Unix/Path.cpp +++ b/lib/System/Unix/Path.cpp @@ -20,6 +20,7 @@ #include "Unix.h" #include #include +#include namespace llvm { using namespace sys; @@ -77,6 +78,53 @@ Path::GetUserHomeDirectory() { return GetRootDirectory(); } +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("!\012"); + } + return false; +} + bool Path::exists() const { return 0 == access(path.c_str(), F_OK ); diff --git a/lib/System/Unix/Path.inc b/lib/System/Unix/Path.inc index 49b2bf4afe1..23b938b9019 100644 --- a/lib/System/Unix/Path.inc +++ b/lib/System/Unix/Path.inc @@ -20,6 +20,7 @@ #include "Unix.h" #include #include +#include namespace llvm { using namespace sys; @@ -77,6 +78,53 @@ Path::GetUserHomeDirectory() { return GetRootDirectory(); } +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("!\012"); + } + return false; +} + bool Path::exists() const { return 0 == access(path.c_str(), F_OK );