Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | |
Sean Callanan | af90cf5 | 2014-04-19 03:09:28 +0000 | [diff] [blame] | 10 | #include <inttypes.h> // PRIu64 |
Stephen Wilson | 8acdbb8 | 2011-04-08 13:36:44 +0000 | [diff] [blame] | 11 | #include <limits.h> |
| 12 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | #include "lldb/API/SBFileSpec.h" |
Caroline Tice | dde9cff | 2010-09-20 05:20:02 +0000 | [diff] [blame] | 14 | #include "lldb/API/SBStream.h" |
Zachary Turner | 8d48cd6 | 2017-03-22 17:33:23 +0000 | [diff] [blame] | 15 | #include "lldb/Host/PosixApi.h" |
Zachary Turner | 5713a05 | 2017-03-22 18:40:07 +0000 | [diff] [blame] | 16 | #include "lldb/Utility/FileSpec.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 17 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 18 | #include "lldb/Utility/Stream.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | |
Zachary Turner | 3f55974 | 2014-08-07 17:33:36 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallString.h" |
| 21 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | using namespace lldb; |
| 23 | using namespace lldb_private; |
| 24 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | SBFileSpec::SBFileSpec() : m_opaque_ap(new lldb_private::FileSpec()) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 27 | SBFileSpec::SBFileSpec(const SBFileSpec &rhs) |
| 28 | : m_opaque_ap(new lldb_private::FileSpec(*rhs.m_opaque_ap)) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 30 | SBFileSpec::SBFileSpec(const lldb_private::FileSpec &fspec) |
| 31 | : m_opaque_ap(new lldb_private::FileSpec(fspec)) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 33 | // Deprecated!!! |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Greg Clayton | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | bool SBFileSpec::ResolveExecutableLocation() { |
| 64 | return m_opaque_ap->ResolveExecutableLocation(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 75 | const char *SBFileSpec::GetFilename() const { |
| 76 | const char *s = m_opaque_ap->GetFilename().AsCString(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 77 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 83 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | log->Printf("SBFileSpec(%p)::GetFilename () => NULL", |
| 85 | static_cast<void *>(m_opaque_ap.get())); |
| 86 | } |
| 87 | |
| 88 | return s; |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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()); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 100 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | log->Printf("SBFileSpec(%p)::GetDirectory () => NULL", |
| 102 | static_cast<void *>(m_opaque_ap.get())); |
| 103 | } |
| 104 | return directory.GetCString(); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 137 | const lldb_private::FileSpec *SBFileSpec::operator->() const { |
| 138 | return m_opaque_ap.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 141 | const lldb_private::FileSpec *SBFileSpec::get() const { |
| 142 | return m_opaque_ap.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 145 | const lldb_private::FileSpec &SBFileSpec::operator*() const { |
| 146 | return *m_opaque_ap.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 149 | const lldb_private::FileSpec &SBFileSpec::ref() const { |
| 150 | return *m_opaque_ap.get(); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 151 | } |
Jason Molenda | 878ae01 | 2016-02-19 00:05:17 +0000 | [diff] [blame] | 152 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
Jason Molenda | 878ae01 | 2016-02-19 00:05:17 +0000 | [diff] [blame] | 167 | } |