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