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/FileSpec.h"
16 #include "lldb/Core/Log.h"
17 #include "lldb/Core/Stream.h"
19 #include "llvm/ADT/SmallString.h"
22 using namespace lldb_private;
26 SBFileSpec::SBFileSpec () :
27 m_opaque_ap(new lldb_private::FileSpec())
31 SBFileSpec::SBFileSpec (const SBFileSpec &rhs) :
32 m_opaque_ap(new lldb_private::FileSpec(*rhs.m_opaque_ap))
36 SBFileSpec::SBFileSpec (const lldb_private::FileSpec& fspec) :
37 m_opaque_ap(new lldb_private::FileSpec(fspec))
42 SBFileSpec::SBFileSpec (const char *path) :
43 m_opaque_ap(new FileSpec (path, true))
47 SBFileSpec::SBFileSpec (const char *path, bool resolve) :
48 m_opaque_ap(new FileSpec (path, resolve))
52 SBFileSpec::~SBFileSpec ()
57 SBFileSpec::operator = (const SBFileSpec &rhs)
60 *m_opaque_ap = *rhs.m_opaque_ap;
65 SBFileSpec::IsValid() const
67 return m_opaque_ap->operator bool();
71 SBFileSpec::Exists () const
73 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
75 bool result = m_opaque_ap->Exists();
78 log->Printf ("SBFileSpec(%p)::Exists () => %s",
79 static_cast<void*>(m_opaque_ap.get()),
80 (result ? "true" : "false"));
86 SBFileSpec::ResolveExecutableLocation ()
88 return m_opaque_ap->ResolveExecutableLocation ();
92 SBFileSpec::ResolvePath (const char *src_path, char *dst_path, size_t dst_len)
94 llvm::SmallString<64> result(src_path);
95 lldb_private::FileSpec::Resolve (result);
96 ::snprintf(dst_path, dst_len, "%s", result.c_str());
97 return std::min(dst_len-1, result.size());
101 SBFileSpec::GetFilename() const
103 const char *s = m_opaque_ap->GetFilename().AsCString();
105 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
109 log->Printf ("SBFileSpec(%p)::GetFilename () => \"%s\"",
110 static_cast<void*>(m_opaque_ap.get()), s);
112 log->Printf ("SBFileSpec(%p)::GetFilename () => NULL",
113 static_cast<void*>(m_opaque_ap.get()));
120 SBFileSpec::GetDirectory() const
122 FileSpec directory{*m_opaque_ap};
123 directory.GetFilename().Clear();
124 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
128 log->Printf ("SBFileSpec(%p)::GetDirectory () => \"%s\"",
129 static_cast<void*>(m_opaque_ap.get()), directory.GetCString());
131 log->Printf ("SBFileSpec(%p)::GetDirectory () => NULL",
132 static_cast<void*>(m_opaque_ap.get()));
134 return directory.GetCString();
138 SBFileSpec::SetFilename(const char *filename)
140 if (filename && filename[0])
141 m_opaque_ap->GetFilename().SetCString(filename);
143 m_opaque_ap->GetFilename().Clear();
147 SBFileSpec::SetDirectory(const char *directory)
149 if (directory && directory[0])
150 m_opaque_ap->GetDirectory().SetCString(directory);
152 m_opaque_ap->GetDirectory().Clear();
156 SBFileSpec::GetPath (char *dst_path, size_t dst_len) const
158 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
160 uint32_t result = m_opaque_ap->GetPath (dst_path, dst_len);
163 log->Printf ("SBFileSpec(%p)::GetPath (dst_path=\"%.*s\", dst_len=%" PRIu64 ") => %u",
164 static_cast<void*>(m_opaque_ap.get()), result, dst_path,
165 static_cast<uint64_t>(dst_len), result);
167 if (result == 0 && dst_path && dst_len > 0)
173 const lldb_private::FileSpec *
174 SBFileSpec::operator->() const
176 return m_opaque_ap.get();
179 const lldb_private::FileSpec *
180 SBFileSpec::get() const
182 return m_opaque_ap.get();
186 const lldb_private::FileSpec &
187 SBFileSpec::operator*() const
189 return *m_opaque_ap.get();
192 const lldb_private::FileSpec &
193 SBFileSpec::ref() const
195 return *m_opaque_ap.get();
200 SBFileSpec::SetFileSpec (const lldb_private::FileSpec& fs)
206 SBFileSpec::GetDescription (SBStream &description) const
208 Stream &strm = description.ref();
210 if (m_opaque_ap->GetPath(path, sizeof(path)))
211 strm.PutCString (path);