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