1 //===-- SBFileSpec.cpp ----------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBFileSpec.h"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/Host/FileSystem.h"
13 #include "lldb/Host/PosixApi.h"
14 #include "lldb/Utility/FileSpec.h"
15 #include "lldb/Utility/Instrumentation.h"
16 #include "lldb/Utility/Stream.h"
18 #include "llvm/ADT/SmallString.h"
24 using namespace lldb_private
;
26 SBFileSpec::SBFileSpec() : m_opaque_up(new lldb_private::FileSpec()) {
27 LLDB_INSTRUMENT_VA(this);
30 SBFileSpec::SBFileSpec(const SBFileSpec
&rhs
) {
31 LLDB_INSTRUMENT_VA(this, rhs
);
33 m_opaque_up
= clone(rhs
.m_opaque_up
);
36 SBFileSpec::SBFileSpec(const lldb_private::FileSpec
&fspec
)
37 : m_opaque_up(new lldb_private::FileSpec(fspec
)) {}
40 SBFileSpec::SBFileSpec(const char *path
) : m_opaque_up(new FileSpec(path
)) {
41 LLDB_INSTRUMENT_VA(this, path
);
43 FileSystem::Instance().Resolve(*m_opaque_up
);
46 SBFileSpec::SBFileSpec(const char *path
, bool resolve
)
47 : m_opaque_up(new FileSpec(path
)) {
48 LLDB_INSTRUMENT_VA(this, path
, resolve
);
51 FileSystem::Instance().Resolve(*m_opaque_up
);
54 SBFileSpec::~SBFileSpec() = default;
56 const SBFileSpec
&SBFileSpec::operator=(const SBFileSpec
&rhs
) {
57 LLDB_INSTRUMENT_VA(this, rhs
);
60 m_opaque_up
= clone(rhs
.m_opaque_up
);
64 bool SBFileSpec::operator==(const SBFileSpec
&rhs
) const {
65 LLDB_INSTRUMENT_VA(this, rhs
);
67 return ref() == rhs
.ref();
70 bool SBFileSpec::operator!=(const SBFileSpec
&rhs
) const {
71 LLDB_INSTRUMENT_VA(this, rhs
);
73 return !(*this == rhs
);
76 bool SBFileSpec::IsValid() const {
77 LLDB_INSTRUMENT_VA(this);
78 return this->operator bool();
80 SBFileSpec::operator bool() const {
81 LLDB_INSTRUMENT_VA(this);
83 return m_opaque_up
->operator bool();
86 bool SBFileSpec::Exists() const {
87 LLDB_INSTRUMENT_VA(this);
89 return FileSystem::Instance().Exists(*m_opaque_up
);
92 bool SBFileSpec::ResolveExecutableLocation() {
93 LLDB_INSTRUMENT_VA(this);
95 return FileSystem::Instance().ResolveExecutableLocation(*m_opaque_up
);
98 int SBFileSpec::ResolvePath(const char *src_path
, char *dst_path
,
100 LLDB_INSTRUMENT_VA(src_path
, dst_path
, dst_len
);
102 llvm::SmallString
<64> result(src_path
);
103 FileSystem::Instance().Resolve(result
);
104 ::snprintf(dst_path
, dst_len
, "%s", result
.c_str());
105 return std::min(dst_len
- 1, result
.size());
108 const char *SBFileSpec::GetFilename() const {
109 LLDB_INSTRUMENT_VA(this);
111 return m_opaque_up
->GetFilename().AsCString();
114 const char *SBFileSpec::GetDirectory() const {
115 LLDB_INSTRUMENT_VA(this);
117 FileSpec directory
{*m_opaque_up
};
118 directory
.ClearFilename();
119 return directory
.GetPathAsConstString().GetCString();
122 void SBFileSpec::SetFilename(const char *filename
) {
123 LLDB_INSTRUMENT_VA(this, filename
);
125 if (filename
&& filename
[0])
126 m_opaque_up
->SetFilename(filename
);
128 m_opaque_up
->ClearFilename();
131 void SBFileSpec::SetDirectory(const char *directory
) {
132 LLDB_INSTRUMENT_VA(this, directory
);
134 if (directory
&& directory
[0])
135 m_opaque_up
->SetDirectory(directory
);
137 m_opaque_up
->ClearDirectory();
140 uint32_t SBFileSpec::GetPath(char *dst_path
, size_t dst_len
) const {
141 LLDB_INSTRUMENT_VA(this, dst_path
, dst_len
);
143 uint32_t result
= m_opaque_up
->GetPath(dst_path
, dst_len
);
145 if (result
== 0 && dst_path
&& dst_len
> 0)
150 const lldb_private::FileSpec
*SBFileSpec::operator->() const {
151 return m_opaque_up
.get();
154 const lldb_private::FileSpec
*SBFileSpec::get() const {
155 return m_opaque_up
.get();
158 const lldb_private::FileSpec
&SBFileSpec::operator*() const {
162 const lldb_private::FileSpec
&SBFileSpec::ref() const { return *m_opaque_up
; }
164 void SBFileSpec::SetFileSpec(const lldb_private::FileSpec
&fs
) {
168 bool SBFileSpec::GetDescription(SBStream
&description
) const {
169 LLDB_INSTRUMENT_VA(this, description
);
171 Stream
&strm
= description
.ref();
173 if (m_opaque_up
->GetPath(path
, sizeof(path
)))
174 strm
.PutCString(path
);
178 void SBFileSpec::AppendPathComponent(const char *fn
) {
179 LLDB_INSTRUMENT_VA(this, fn
);
181 m_opaque_up
->AppendPathComponent(fn
);