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