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/Host/PosixApi.h"
16 #include "lldb/Utility/FileSpec.h"
17 #include "lldb/Utility/Log.h"
18 #include "lldb/Utility/Stream.h"
20 #include "llvm/ADT/SmallString.h"
23 using namespace lldb_private;
25 SBFileSpec::SBFileSpec() : m_opaque_ap(new lldb_private::FileSpec()) {}
27 SBFileSpec::SBFileSpec(const SBFileSpec &rhs)
28 : m_opaque_ap(new lldb_private::FileSpec(*rhs.m_opaque_ap)) {}
30 SBFileSpec::SBFileSpec(const lldb_private::FileSpec &fspec)
31 : m_opaque_ap(new lldb_private::FileSpec(fspec)) {}
34 SBFileSpec::SBFileSpec(const char *path)
35 : m_opaque_ap(new FileSpec(path, true)) {}
37 SBFileSpec::SBFileSpec(const char *path, bool resolve)
38 : m_opaque_ap(new FileSpec(path, resolve)) {}
40 SBFileSpec::~SBFileSpec() {}
42 const SBFileSpec &SBFileSpec::operator=(const SBFileSpec &rhs) {
44 *m_opaque_ap = *rhs.m_opaque_ap;
48 bool SBFileSpec::IsValid() const { return m_opaque_ap->operator bool(); }
50 bool SBFileSpec::Exists() const {
51 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
53 bool result = m_opaque_ap->Exists();
56 log->Printf("SBFileSpec(%p)::Exists () => %s",
57 static_cast<void *>(m_opaque_ap.get()),
58 (result ? "true" : "false"));
63 bool SBFileSpec::ResolveExecutableLocation() {
64 return m_opaque_ap->ResolveExecutableLocation();
67 int SBFileSpec::ResolvePath(const char *src_path, char *dst_path,
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());
75 const char *SBFileSpec::GetFilename() const {
76 const char *s = m_opaque_ap->GetFilename().AsCString();
78 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
81 log->Printf("SBFileSpec(%p)::GetFilename () => \"%s\"",
82 static_cast<void *>(m_opaque_ap.get()), s);
84 log->Printf("SBFileSpec(%p)::GetFilename () => NULL",
85 static_cast<void *>(m_opaque_ap.get()));
91 const char *SBFileSpec::GetDirectory() const {
92 FileSpec directory{*m_opaque_ap};
93 directory.GetFilename().Clear();
94 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
97 log->Printf("SBFileSpec(%p)::GetDirectory () => \"%s\"",
98 static_cast<void *>(m_opaque_ap.get()),
99 directory.GetCString());
101 log->Printf("SBFileSpec(%p)::GetDirectory () => NULL",
102 static_cast<void *>(m_opaque_ap.get()));
104 return directory.GetCString();
107 void SBFileSpec::SetFilename(const char *filename) {
108 if (filename && filename[0])
109 m_opaque_ap->GetFilename().SetCString(filename);
111 m_opaque_ap->GetFilename().Clear();
114 void SBFileSpec::SetDirectory(const char *directory) {
115 if (directory && directory[0])
116 m_opaque_ap->GetDirectory().SetCString(directory);
118 m_opaque_ap->GetDirectory().Clear();
121 uint32_t SBFileSpec::GetPath(char *dst_path, size_t dst_len) const {
122 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
124 uint32_t result = m_opaque_ap->GetPath(dst_path, dst_len);
127 log->Printf("SBFileSpec(%p)::GetPath (dst_path=\"%.*s\", dst_len=%" PRIu64
129 static_cast<void *>(m_opaque_ap.get()), result, dst_path,
130 static_cast<uint64_t>(dst_len), result);
132 if (result == 0 && dst_path && dst_len > 0)
137 const lldb_private::FileSpec *SBFileSpec::operator->() const {
138 return m_opaque_ap.get();
141 const lldb_private::FileSpec *SBFileSpec::get() const {
142 return m_opaque_ap.get();
145 const lldb_private::FileSpec &SBFileSpec::operator*() const {
146 return *m_opaque_ap.get();
149 const lldb_private::FileSpec &SBFileSpec::ref() const {
150 return *m_opaque_ap.get();
153 void SBFileSpec::SetFileSpec(const lldb_private::FileSpec &fs) {
157 bool SBFileSpec::GetDescription(SBStream &description) const {
158 Stream &strm = description.ref();
160 if (m_opaque_ap->GetPath(path, sizeof(path)))
161 strm.PutCString(path);
165 void SBFileSpec::AppendPathComponent(const char *fn) {
166 m_opaque_ap->AppendPathComponent(fn);