blob: 027528c46ca71d1e862b9f7b9734ef112d1500a8 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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 Callananaf90cf52014-04-19 03:09:28 +000010#include <inttypes.h> // PRIu64
Stephen Wilson8acdbb82011-04-08 13:36:44 +000011#include <limits.h>
12
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include "lldb/API/SBFileSpec.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000014#include "lldb/API/SBStream.h"
Jonas Devlieghere2c22c802018-11-01 17:09:22 +000015#include "lldb/Host/FileSystem.h"
Zachary Turner8d48cd62017-03-22 17:33:23 +000016#include "lldb/Host/PosixApi.h"
Zachary Turner5713a052017-03-22 18:40:07 +000017#include "lldb/Utility/FileSpec.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000018#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000019#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020
Zachary Turner3f559742014-08-07 17:33:36 +000021#include "llvm/ADT/SmallString.h"
22
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023using namespace lldb;
24using namespace lldb_private;
25
Kate Stoneb9c1b512016-09-06 20:57:50 +000026SBFileSpec::SBFileSpec() : m_opaque_ap(new lldb_private::FileSpec()) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
Kate Stoneb9c1b512016-09-06 20:57:50 +000028SBFileSpec::SBFileSpec(const SBFileSpec &rhs)
29 : m_opaque_ap(new lldb_private::FileSpec(*rhs.m_opaque_ap)) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030
Kate Stoneb9c1b512016-09-06 20:57:50 +000031SBFileSpec::SBFileSpec(const lldb_private::FileSpec &fspec)
32 : m_opaque_ap(new lldb_private::FileSpec(fspec)) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000034// Deprecated!!!
Kate Stoneb9c1b512016-09-06 20:57:50 +000035SBFileSpec::SBFileSpec(const char *path)
36 : m_opaque_ap(new FileSpec(path, true)) {}
37
38SBFileSpec::SBFileSpec(const char *path, bool resolve)
39 : m_opaque_ap(new FileSpec(path, resolve)) {}
40
41SBFileSpec::~SBFileSpec() {}
42
43const SBFileSpec &SBFileSpec::operator=(const SBFileSpec &rhs) {
44 if (this != &rhs)
45 *m_opaque_ap = *rhs.m_opaque_ap;
46 return *this;
Greg Clayton274060b2010-10-20 20:54:39 +000047}
48
Kate Stoneb9c1b512016-09-06 20:57:50 +000049bool SBFileSpec::IsValid() const { return m_opaque_ap->operator bool(); }
50
51bool SBFileSpec::Exists() const {
52 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
53
54 bool result = m_opaque_ap->Exists();
55
56 if (log)
57 log->Printf("SBFileSpec(%p)::Exists () => %s",
58 static_cast<void *>(m_opaque_ap.get()),
59 (result ? "true" : "false"));
60
61 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062}
63
Kate Stoneb9c1b512016-09-06 20:57:50 +000064bool SBFileSpec::ResolveExecutableLocation() {
Jonas Devlieghere2c22c802018-11-01 17:09:22 +000065 return FileSystem::Instance().ResolveExecutableLocation(*m_opaque_ap);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066}
67
Kate Stoneb9c1b512016-09-06 20:57:50 +000068int SBFileSpec::ResolvePath(const char *src_path, char *dst_path,
69 size_t dst_len) {
70 llvm::SmallString<64> result(src_path);
71 lldb_private::FileSpec::Resolve(result);
72 ::snprintf(dst_path, dst_len, "%s", result.c_str());
73 return std::min(dst_len - 1, result.size());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074}
75
Kate Stoneb9c1b512016-09-06 20:57:50 +000076const char *SBFileSpec::GetFilename() const {
77 const char *s = m_opaque_ap->GetFilename().AsCString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
80 if (log) {
81 if (s)
82 log->Printf("SBFileSpec(%p)::GetFilename () => \"%s\"",
83 static_cast<void *>(m_opaque_ap.get()), s);
Greg Claytonfbb76342013-11-20 21:07:01 +000084 else
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 log->Printf("SBFileSpec(%p)::GetFilename () => NULL",
86 static_cast<void *>(m_opaque_ap.get()));
87 }
88
89 return s;
Greg Claytonfbb76342013-11-20 21:07:01 +000090}
91
Kate Stoneb9c1b512016-09-06 20:57:50 +000092const char *SBFileSpec::GetDirectory() const {
93 FileSpec directory{*m_opaque_ap};
94 directory.GetFilename().Clear();
95 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
96 if (log) {
97 if (directory)
98 log->Printf("SBFileSpec(%p)::GetDirectory () => \"%s\"",
99 static_cast<void *>(m_opaque_ap.get()),
100 directory.GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +0000101 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 log->Printf("SBFileSpec(%p)::GetDirectory () => NULL",
103 static_cast<void *>(m_opaque_ap.get()));
104 }
105 return directory.GetCString();
Greg Claytonfbb76342013-11-20 21:07:01 +0000106}
107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108void SBFileSpec::SetFilename(const char *filename) {
109 if (filename && filename[0])
110 m_opaque_ap->GetFilename().SetCString(filename);
111 else
112 m_opaque_ap->GetFilename().Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113}
114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115void SBFileSpec::SetDirectory(const char *directory) {
116 if (directory && directory[0])
117 m_opaque_ap->GetDirectory().SetCString(directory);
118 else
119 m_opaque_ap->GetDirectory().Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120}
121
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122uint32_t SBFileSpec::GetPath(char *dst_path, size_t dst_len) const {
123 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
124
125 uint32_t result = m_opaque_ap->GetPath(dst_path, dst_len);
126
127 if (log)
128 log->Printf("SBFileSpec(%p)::GetPath (dst_path=\"%.*s\", dst_len=%" PRIu64
129 ") => %u",
130 static_cast<void *>(m_opaque_ap.get()), result, dst_path,
131 static_cast<uint64_t>(dst_len), result);
132
133 if (result == 0 && dst_path && dst_len > 0)
134 *dst_path = '\0';
135 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000136}
137
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138const lldb_private::FileSpec *SBFileSpec::operator->() const {
139 return m_opaque_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140}
141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142const lldb_private::FileSpec *SBFileSpec::get() const {
143 return m_opaque_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144}
145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146const lldb_private::FileSpec &SBFileSpec::operator*() const {
147 return *m_opaque_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148}
149
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150const lldb_private::FileSpec &SBFileSpec::ref() const {
151 return *m_opaque_ap.get();
Caroline Ticeceb6b132010-10-26 03:11:13 +0000152}
Jason Molenda878ae012016-02-19 00:05:17 +0000153
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154void SBFileSpec::SetFileSpec(const lldb_private::FileSpec &fs) {
155 *m_opaque_ap = fs;
156}
157
158bool SBFileSpec::GetDescription(SBStream &description) const {
159 Stream &strm = description.ref();
160 char path[PATH_MAX];
161 if (m_opaque_ap->GetPath(path, sizeof(path)))
162 strm.PutCString(path);
163 return true;
164}
165
166void SBFileSpec::AppendPathComponent(const char *fn) {
167 m_opaque_ap->AppendPathComponent(fn);
Jason Molenda878ae012016-02-19 00:05:17 +0000168}