[llvm-commits] CVS: llvm/lib/System/Unix/Path.cpp

Reid Spencer reid at x10sys.com
Fri Sep 10 23:55:19 CDT 2004



Changes in directory llvm/lib/System/Unix:

Path.cpp updated: 1.4 -> 1.5
---
Log message:

Implemented support for detecting file types by magic number, stripping
path and suffix to leave basename, and getting the DLL suffix.


---
Diffs of the changes:  (+48 -0)

Index: llvm/lib/System/Unix/Path.cpp
diff -u llvm/lib/System/Unix/Path.cpp:1.4 llvm/lib/System/Unix/Path.cpp:1.5
--- llvm/lib/System/Unix/Path.cpp:1.4	Wed Sep  1 17:55:35 2004
+++ llvm/lib/System/Unix/Path.cpp	Fri Sep 10 23:55:08 2004
@@ -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 @@
 }
 
 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 );
 }






More information about the llvm-commits mailing list