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 | |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame^] | 10 | #include <inttypes.h> |
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" |
Jonas Devlieghere | 2c22c80 | 2018-11-01 17:09:22 +0000 | [diff] [blame] | 15 | #include "lldb/Host/FileSystem.h" |
Zachary Turner | 8d48cd6 | 2017-03-22 17:33:23 +0000 | [diff] [blame] | 16 | #include "lldb/Host/PosixApi.h" |
Zachary Turner | 5713a05 | 2017-03-22 18:40:07 +0000 | [diff] [blame] | 17 | #include "lldb/Utility/FileSpec.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 18 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 19 | #include "lldb/Utility/Stream.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | |
Zachary Turner | 3f55974 | 2014-08-07 17:33:36 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
| 22 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | using namespace lldb; |
| 24 | using namespace lldb_private; |
| 25 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 26 | SBFileSpec::SBFileSpec() : m_opaque_ap(new lldb_private::FileSpec()) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 28 | SBFileSpec::SBFileSpec(const SBFileSpec &rhs) |
| 29 | : m_opaque_ap(new lldb_private::FileSpec(*rhs.m_opaque_ap)) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 31 | SBFileSpec::SBFileSpec(const lldb_private::FileSpec &fspec) |
| 32 | : m_opaque_ap(new lldb_private::FileSpec(fspec)) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 34 | // Deprecated!!! |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 35 | SBFileSpec::SBFileSpec(const char *path) : m_opaque_ap(new FileSpec(path)) { |
| 36 | FileSystem::Instance().Resolve(*m_opaque_ap); |
| 37 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 38 | |
| 39 | SBFileSpec::SBFileSpec(const char *path, bool resolve) |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 40 | : m_opaque_ap(new FileSpec(path)) { |
| 41 | if (resolve) |
| 42 | FileSystem::Instance().Resolve(*m_opaque_ap); |
| 43 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 44 | |
| 45 | SBFileSpec::~SBFileSpec() {} |
| 46 | |
| 47 | const SBFileSpec &SBFileSpec::operator=(const SBFileSpec &rhs) { |
| 48 | if (this != &rhs) |
| 49 | *m_opaque_ap = *rhs.m_opaque_ap; |
| 50 | return *this; |
Greg Clayton | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | bool SBFileSpec::IsValid() const { return m_opaque_ap->operator bool(); } |
| 54 | |
| 55 | bool SBFileSpec::Exists() const { |
| 56 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 57 | |
Jonas Devlieghere | dbd7fab | 2018-11-01 17:09:25 +0000 | [diff] [blame] | 58 | bool result = FileSystem::Instance().Exists(*m_opaque_ap); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | |
| 60 | if (log) |
| 61 | log->Printf("SBFileSpec(%p)::Exists () => %s", |
| 62 | static_cast<void *>(m_opaque_ap.get()), |
| 63 | (result ? "true" : "false")); |
| 64 | |
| 65 | return result; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 68 | bool SBFileSpec::ResolveExecutableLocation() { |
Jonas Devlieghere | 2c22c80 | 2018-11-01 17:09:22 +0000 | [diff] [blame] | 69 | return FileSystem::Instance().ResolveExecutableLocation(*m_opaque_ap); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 72 | int SBFileSpec::ResolvePath(const char *src_path, char *dst_path, |
| 73 | size_t dst_len) { |
| 74 | llvm::SmallString<64> result(src_path); |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 75 | FileSystem::Instance().Resolve(result); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | ::snprintf(dst_path, dst_len, "%s", result.c_str()); |
| 77 | return std::min(dst_len - 1, result.size()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 80 | const char *SBFileSpec::GetFilename() const { |
| 81 | const char *s = m_opaque_ap->GetFilename().AsCString(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 82 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 83 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 84 | if (log) { |
| 85 | if (s) |
| 86 | log->Printf("SBFileSpec(%p)::GetFilename () => \"%s\"", |
| 87 | static_cast<void *>(m_opaque_ap.get()), s); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 88 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 89 | log->Printf("SBFileSpec(%p)::GetFilename () => NULL", |
| 90 | static_cast<void *>(m_opaque_ap.get())); |
| 91 | } |
| 92 | |
| 93 | return s; |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | const char *SBFileSpec::GetDirectory() const { |
| 97 | FileSpec directory{*m_opaque_ap}; |
| 98 | directory.GetFilename().Clear(); |
| 99 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 100 | if (log) { |
| 101 | if (directory) |
| 102 | log->Printf("SBFileSpec(%p)::GetDirectory () => \"%s\"", |
| 103 | static_cast<void *>(m_opaque_ap.get()), |
| 104 | directory.GetCString()); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 105 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 106 | log->Printf("SBFileSpec(%p)::GetDirectory () => NULL", |
| 107 | static_cast<void *>(m_opaque_ap.get())); |
| 108 | } |
| 109 | return directory.GetCString(); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | void SBFileSpec::SetFilename(const char *filename) { |
| 113 | if (filename && filename[0]) |
| 114 | m_opaque_ap->GetFilename().SetCString(filename); |
| 115 | else |
| 116 | m_opaque_ap->GetFilename().Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | void SBFileSpec::SetDirectory(const char *directory) { |
| 120 | if (directory && directory[0]) |
| 121 | m_opaque_ap->GetDirectory().SetCString(directory); |
| 122 | else |
| 123 | m_opaque_ap->GetDirectory().Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 126 | uint32_t SBFileSpec::GetPath(char *dst_path, size_t dst_len) const { |
| 127 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 128 | |
| 129 | uint32_t result = m_opaque_ap->GetPath(dst_path, dst_len); |
| 130 | |
| 131 | if (log) |
| 132 | log->Printf("SBFileSpec(%p)::GetPath (dst_path=\"%.*s\", dst_len=%" PRIu64 |
| 133 | ") => %u", |
| 134 | static_cast<void *>(m_opaque_ap.get()), result, dst_path, |
| 135 | static_cast<uint64_t>(dst_len), result); |
| 136 | |
| 137 | if (result == 0 && dst_path && dst_len > 0) |
| 138 | *dst_path = '\0'; |
| 139 | return result; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 142 | const lldb_private::FileSpec *SBFileSpec::operator->() const { |
| 143 | return m_opaque_ap.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 146 | const lldb_private::FileSpec *SBFileSpec::get() const { |
| 147 | return m_opaque_ap.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 150 | const lldb_private::FileSpec &SBFileSpec::operator*() const { |
| 151 | return *m_opaque_ap.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 154 | const lldb_private::FileSpec &SBFileSpec::ref() const { |
| 155 | return *m_opaque_ap.get(); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 156 | } |
Jason Molenda | 878ae01 | 2016-02-19 00:05:17 +0000 | [diff] [blame] | 157 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 158 | void SBFileSpec::SetFileSpec(const lldb_private::FileSpec &fs) { |
| 159 | *m_opaque_ap = fs; |
| 160 | } |
| 161 | |
| 162 | bool SBFileSpec::GetDescription(SBStream &description) const { |
| 163 | Stream &strm = description.ref(); |
| 164 | char path[PATH_MAX]; |
| 165 | if (m_opaque_ap->GetPath(path, sizeof(path))) |
| 166 | strm.PutCString(path); |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | void SBFileSpec::AppendPathComponent(const char *fn) { |
| 171 | m_opaque_ap->AppendPathComponent(fn); |
Jason Molenda | 878ae01 | 2016-02-19 00:05:17 +0000 | [diff] [blame] | 172 | } |