1 //===-- SBFileSpec.cpp ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include <inttypes.h> // PRIu64
13 #include "lldb/API/SBFileSpec.h"
14 #include "lldb/API/SBStream.h"
15 #include "lldb/Core/Log.h"
16 #include "lldb/Core/Stream.h"
17 #include "lldb/Host/FileSpec.h"
19 #include "llvm/ADT/SmallString.h"
22 using namespace lldb_private;
24 SBFileSpec::SBFileSpec() : m_opaque_ap(new lldb_private::FileSpec()) {}
26 SBFileSpec::SBFileSpec(const SBFileSpec &rhs)
27 : m_opaque_ap(new lldb_private::FileSpec(*rhs.m_opaque_ap)) {}
29 SBFileSpec::SBFileSpec(const lldb_private::FileSpec &fspec)
30 : m_opaque_ap(new lldb_private::FileSpec(fspec)) {}
33 SBFileSpec::SBFileSpec(const char *path)
34 : m_opaque_ap(new FileSpec(path, true)) {}
36 SBFileSpec::SBFileSpec(const char *path, bool resolve)
37 : m_opaque_ap(new FileSpec(path, resolve)) {}
39 SBFileSpec::~SBFileSpec() {}
41 const SBFileSpec &SBFileSpec::operator=(const SBFileSpec &rhs) {
43 *m_opaque_ap = *rhs.m_opaque_ap;
47 bool SBFileSpec::IsValid() const { return m_opaque_ap->operator bool(); }
49 bool SBFileSpec::Exists() const {
50 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
52 bool result = m_opaque_ap->Exists();
55 log->Printf("SBFileSpec(%p)::Exists () => %s",
56 static_cast<void *>(m_opaque_ap.get()),
57 (result ? "true" : "false"));
62 bool SBFileSpec::ResolveExecutableLocation() {
63 return m_opaque_ap->ResolveExecutableLocation();
66 int SBFileSpec::ResolvePath(const char *src_path, char *dst_path,
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());
74 const char *SBFileSpec::GetFilename() const {
75 const char *s = m_opaque_ap->GetFilename().AsCString();
77 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
80 log->Printf("SBFileSpec(%p)::GetFilename () => \"%s\"",
81 static_cast<void *>(m_opaque_ap.get()), s);
83 log->Printf("SBFileSpec(%p)::GetFilename () => NULL",
84 static_cast<void *>(m_opaque_ap.get()));
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));
96 log->Printf("SBFileSpec(%p)::GetDirectory () => \"%s\"",
97 static_cast<void *>(m_opaque_ap.get()),
98 directory.GetCString());
100 log->Printf("SBFileSpec(%p)::GetDirectory () => NULL",
101 static_cast<void *>(m_opaque_ap.get()));
103 return directory.GetCString();
106 void SBFileSpec::SetFilename(const char *filename) {
107 if (filename && filename[0])
108 m_opaque_ap->GetFilename().SetCString(filename);
110 m_opaque_ap->GetFilename().Clear();
113 void SBFileSpec::SetDirectory(const char *directory) {
114 if (directory && directory[0])
115 m_opaque_ap->GetDirectory().SetCString(directory);
117 m_opaque_ap->GetDirectory().Clear();
120 uint32_t SBFileSpec::GetPath(char *dst_path, size_t dst_len) const {
121 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
123 uint32_t result = m_opaque_ap->GetPath(dst_path, dst_len);
126 log->Printf("SBFileSpec(%p)::GetPath (dst_path=\"%.*s\", dst_len=%" PRIu64
128 static_cast<void *>(m_opaque_ap.get()), result, dst_path,
129 static_cast<uint64_t>(dst_len), result);
131 if (result == 0 && dst_path && dst_len > 0)
136 const lldb_private::FileSpec *SBFileSpec::operator->() const {
137 return m_opaque_ap.get();
140 const lldb_private::FileSpec *SBFileSpec::get() const {
141 return m_opaque_ap.get();
144 const lldb_private::FileSpec &SBFileSpec::operator*() const {
145 return *m_opaque_ap.get();
148 const lldb_private::FileSpec &SBFileSpec::ref() const {
149 return *m_opaque_ap.get();
152 void SBFileSpec::SetFileSpec(const lldb_private::FileSpec &fs) {
156 bool SBFileSpec::GetDescription(SBStream &description) const {
157 Stream &strm = description.ref();
159 if (m_opaque_ap->GetPath(path, sizeof(path)))
160 strm.PutCString(path);
164 void SBFileSpec::AppendPathComponent(const char *fn) {
165 m_opaque_ap->AppendPathComponent(fn);