blob: 011b88225ef9c2079c4d3f0386d93f4943f0b2d7 [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"
Zachary Turner8d48cd62017-03-22 17:33:23 +000015#include "lldb/Host/PosixApi.h"
Zachary Turner5713a052017-03-22 18:40:07 +000016#include "lldb/Utility/FileSpec.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000017#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000018#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019
Zachary Turner3f559742014-08-07 17:33:36 +000020#include "llvm/ADT/SmallString.h"
21
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022using namespace lldb;
23using namespace lldb_private;
24
Kate Stoneb9c1b512016-09-06 20:57:50 +000025SBFileSpec::SBFileSpec() : m_opaque_ap(new lldb_private::FileSpec()) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Kate Stoneb9c1b512016-09-06 20:57:50 +000027SBFileSpec::SBFileSpec(const SBFileSpec &rhs)
28 : m_opaque_ap(new lldb_private::FileSpec(*rhs.m_opaque_ap)) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
Kate Stoneb9c1b512016-09-06 20:57:50 +000030SBFileSpec::SBFileSpec(const lldb_private::FileSpec &fspec)
31 : m_opaque_ap(new lldb_private::FileSpec(fspec)) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000033// Deprecated!!!
Kate Stoneb9c1b512016-09-06 20:57:50 +000034SBFileSpec::SBFileSpec(const char *path)
35 : m_opaque_ap(new FileSpec(path, true)) {}
36
37SBFileSpec::SBFileSpec(const char *path, bool resolve)
38 : m_opaque_ap(new FileSpec(path, resolve)) {}
39
40SBFileSpec::~SBFileSpec() {}
41
42const SBFileSpec &SBFileSpec::operator=(const SBFileSpec &rhs) {
43 if (this != &rhs)
44 *m_opaque_ap = *rhs.m_opaque_ap;
45 return *this;
Greg Clayton274060b2010-10-20 20:54:39 +000046}
47
Kate Stoneb9c1b512016-09-06 20:57:50 +000048bool SBFileSpec::IsValid() const { return m_opaque_ap->operator bool(); }
49
50bool 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 Lattner30fdc8d2010-06-08 16:52:24 +000061}
62
Kate Stoneb9c1b512016-09-06 20:57:50 +000063bool SBFileSpec::ResolveExecutableLocation() {
64 return m_opaque_ap->ResolveExecutableLocation();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065}
66
Kate Stoneb9c1b512016-09-06 20:57:50 +000067int 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 Lattner30fdc8d2010-06-08 16:52:24 +000073}
74
Kate Stoneb9c1b512016-09-06 20:57:50 +000075const char *SBFileSpec::GetFilename() const {
76 const char *s = m_opaque_ap->GetFilename().AsCString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 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 Claytonfbb76342013-11-20 21:07:01 +000083 else
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 log->Printf("SBFileSpec(%p)::GetFilename () => NULL",
85 static_cast<void *>(m_opaque_ap.get()));
86 }
87
88 return s;
Greg Claytonfbb76342013-11-20 21:07:01 +000089}
90
Kate Stoneb9c1b512016-09-06 20:57:50 +000091const 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 Claytonfbb76342013-11-20 21:07:01 +0000100 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101 log->Printf("SBFileSpec(%p)::GetDirectory () => NULL",
102 static_cast<void *>(m_opaque_ap.get()));
103 }
104 return directory.GetCString();
Greg Claytonfbb76342013-11-20 21:07:01 +0000105}
106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107void 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 Lattner30fdc8d2010-06-08 16:52:24 +0000112}
113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114void 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 Lattner30fdc8d2010-06-08 16:52:24 +0000119}
120
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121uint32_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 Lattner30fdc8d2010-06-08 16:52:24 +0000135}
136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137const lldb_private::FileSpec *SBFileSpec::operator->() const {
138 return m_opaque_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139}
140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141const lldb_private::FileSpec *SBFileSpec::get() const {
142 return m_opaque_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143}
144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145const lldb_private::FileSpec &SBFileSpec::operator*() const {
146 return *m_opaque_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147}
148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149const lldb_private::FileSpec &SBFileSpec::ref() const {
150 return *m_opaque_ap.get();
Caroline Ticeceb6b132010-10-26 03:11:13 +0000151}
Jason Molenda878ae012016-02-19 00:05:17 +0000152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153void SBFileSpec::SetFileSpec(const lldb_private::FileSpec &fs) {
154 *m_opaque_ap = fs;
155}
156
157bool 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
165void SBFileSpec::AppendPathComponent(const char *fn) {
166 m_opaque_ap->AppendPathComponent(fn);
Jason Molenda878ae012016-02-19 00:05:17 +0000167}