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