1 //===-- SBFileSpec.cpp ------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include <inttypes.h> // PRIu64 11 #include <limits.h> 12 13 #include "lldb/API/SBFileSpec.h" 14 #include "lldb/API/SBStream.h" 15 #include "lldb/Host/PosixApi.h" 16 #include "lldb/Utility/FileSpec.h" 17 #include "lldb/Utility/Log.h" 18 #include "lldb/Utility/Stream.h" 19 20 #include "llvm/ADT/SmallString.h" 21 22 using namespace lldb; 23 using namespace lldb_private; 24 25 SBFileSpec::SBFileSpec() : m_opaque_ap(new lldb_private::FileSpec()) {} 26 27 SBFileSpec::SBFileSpec(const SBFileSpec &rhs) 28 : m_opaque_ap(new lldb_private::FileSpec(*rhs.m_opaque_ap)) {} 29 30 SBFileSpec::SBFileSpec(const lldb_private::FileSpec &fspec) 31 : m_opaque_ap(new lldb_private::FileSpec(fspec)) {} 32 33 // Deprecated!!! 34 SBFileSpec::SBFileSpec(const char *path) 35 : m_opaque_ap(new FileSpec(path, true)) {} 36 37 SBFileSpec::SBFileSpec(const char *path, bool resolve) 38 : m_opaque_ap(new FileSpec(path, resolve)) {} 39 40 SBFileSpec::~SBFileSpec() {} 41 42 const SBFileSpec &SBFileSpec::operator=(const SBFileSpec &rhs) { 43 if (this != &rhs) 44 *m_opaque_ap = *rhs.m_opaque_ap; 45 return *this; 46 } 47 48 bool SBFileSpec::IsValid() const { return m_opaque_ap->operator bool(); } 49 50 bool SBFileSpec::Exists() const { 51 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 52 53 bool result = m_opaque_ap->Exists(); 54 55 if (log) 56 log->Printf("SBFileSpec(%p)::Exists () => %s", 57 static_cast<void *>(m_opaque_ap.get()), 58 (result ? "true" : "false")); 59 60 return result; 61 } 62 63 bool SBFileSpec::ResolveExecutableLocation() { 64 return m_opaque_ap->ResolveExecutableLocation(); 65 } 66 67 int SBFileSpec::ResolvePath(const char *src_path, char *dst_path, 68 size_t dst_len) { 69 llvm::SmallString<64> result(src_path); 70 lldb_private::FileSpec::Resolve(result); 71 ::snprintf(dst_path, dst_len, "%s", result.c_str()); 72 return std::min(dst_len - 1, result.size()); 73 } 74 75 const char *SBFileSpec::GetFilename() const { 76 const char *s = m_opaque_ap->GetFilename().AsCString(); 77 78 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 79 if (log) { 80 if (s) 81 log->Printf("SBFileSpec(%p)::GetFilename () => \"%s\"", 82 static_cast<void *>(m_opaque_ap.get()), s); 83 else 84 log->Printf("SBFileSpec(%p)::GetFilename () => NULL", 85 static_cast<void *>(m_opaque_ap.get())); 86 } 87 88 return s; 89 } 90 91 const char *SBFileSpec::GetDirectory() const { 92 FileSpec directory{*m_opaque_ap}; 93 directory.GetFilename().Clear(); 94 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 95 if (log) { 96 if (directory) 97 log->Printf("SBFileSpec(%p)::GetDirectory () => \"%s\"", 98 static_cast<void *>(m_opaque_ap.get()), 99 directory.GetCString()); 100 else 101 log->Printf("SBFileSpec(%p)::GetDirectory () => NULL", 102 static_cast<void *>(m_opaque_ap.get())); 103 } 104 return directory.GetCString(); 105 } 106 107 void SBFileSpec::SetFilename(const char *filename) { 108 if (filename && filename[0]) 109 m_opaque_ap->GetFilename().SetCString(filename); 110 else 111 m_opaque_ap->GetFilename().Clear(); 112 } 113 114 void SBFileSpec::SetDirectory(const char *directory) { 115 if (directory && directory[0]) 116 m_opaque_ap->GetDirectory().SetCString(directory); 117 else 118 m_opaque_ap->GetDirectory().Clear(); 119 } 120 121 uint32_t SBFileSpec::GetPath(char *dst_path, size_t dst_len) const { 122 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 123 124 uint32_t result = m_opaque_ap->GetPath(dst_path, dst_len); 125 126 if (log) 127 log->Printf("SBFileSpec(%p)::GetPath (dst_path=\"%.*s\", dst_len=%" PRIu64 128 ") => %u", 129 static_cast<void *>(m_opaque_ap.get()), result, dst_path, 130 static_cast<uint64_t>(dst_len), result); 131 132 if (result == 0 && dst_path && dst_len > 0) 133 *dst_path = '\0'; 134 return result; 135 } 136 137 const lldb_private::FileSpec *SBFileSpec::operator->() const { 138 return m_opaque_ap.get(); 139 } 140 141 const lldb_private::FileSpec *SBFileSpec::get() const { 142 return m_opaque_ap.get(); 143 } 144 145 const lldb_private::FileSpec &SBFileSpec::operator*() const { 146 return *m_opaque_ap.get(); 147 } 148 149 const lldb_private::FileSpec &SBFileSpec::ref() const { 150 return *m_opaque_ap.get(); 151 } 152 153 void SBFileSpec::SetFileSpec(const lldb_private::FileSpec &fs) { 154 *m_opaque_ap = fs; 155 } 156 157 bool SBFileSpec::GetDescription(SBStream &description) const { 158 Stream &strm = description.ref(); 159 char path[PATH_MAX]; 160 if (m_opaque_ap->GetPath(path, sizeof(path))) 161 strm.PutCString(path); 162 return true; 163 } 164 165 void SBFileSpec::AppendPathComponent(const char *fn) { 166 m_opaque_ap->AppendPathComponent(fn); 167 } 168