xref: /src/contrib/llvm-project/lldb/source/API/SBFileSpec.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1cfca06d7SDimitry Andric //===-- SBFileSpec.cpp ----------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste 
9f034231aSEd Maste #include "lldb/API/SBFileSpec.h"
105f29bb8aSDimitry Andric #include "Utils.h"
11f034231aSEd Maste #include "lldb/API/SBStream.h"
1294994d37SDimitry Andric #include "lldb/Host/FileSystem.h"
1374a628f7SDimitry Andric #include "lldb/Host/PosixApi.h"
1474a628f7SDimitry Andric #include "lldb/Utility/FileSpec.h"
156f8fc217SDimitry Andric #include "lldb/Utility/Instrumentation.h"
1674a628f7SDimitry Andric #include "lldb/Utility/Stream.h"
17f034231aSEd Maste 
180cac4ca3SEd Maste #include "llvm/ADT/SmallString.h"
190cac4ca3SEd Maste 
20344a3780SDimitry Andric #include <cinttypes>
21344a3780SDimitry Andric #include <climits>
225f29bb8aSDimitry Andric 
23f034231aSEd Maste using namespace lldb;
24f034231aSEd Maste using namespace lldb_private;
25f034231aSEd Maste 
SBFileSpec()265f29bb8aSDimitry Andric SBFileSpec::SBFileSpec() : m_opaque_up(new lldb_private::FileSpec()) {
276f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this);
285f29bb8aSDimitry Andric }
29f034231aSEd Maste 
SBFileSpec(const SBFileSpec & rhs)306f8fc217SDimitry Andric SBFileSpec::SBFileSpec(const SBFileSpec &rhs) {
316f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
325f29bb8aSDimitry Andric 
335f29bb8aSDimitry Andric   m_opaque_up = clone(rhs.m_opaque_up);
345f29bb8aSDimitry Andric }
35f034231aSEd Maste 
SBFileSpec(const lldb_private::FileSpec & fspec)3614f1b3e8SDimitry Andric SBFileSpec::SBFileSpec(const lldb_private::FileSpec &fspec)
375f29bb8aSDimitry Andric     : m_opaque_up(new lldb_private::FileSpec(fspec)) {}
38f034231aSEd Maste 
390cac4ca3SEd Maste // Deprecated!!!
SBFileSpec(const char * path)405f29bb8aSDimitry Andric SBFileSpec::SBFileSpec(const char *path) : m_opaque_up(new FileSpec(path)) {
416f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, path);
425f29bb8aSDimitry Andric 
435f29bb8aSDimitry Andric   FileSystem::Instance().Resolve(*m_opaque_up);
4494994d37SDimitry Andric }
45f034231aSEd Maste 
SBFileSpec(const char * path,bool resolve)4614f1b3e8SDimitry Andric SBFileSpec::SBFileSpec(const char *path, bool resolve)
475f29bb8aSDimitry Andric     : m_opaque_up(new FileSpec(path)) {
486f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, path, resolve);
495f29bb8aSDimitry Andric 
5094994d37SDimitry Andric   if (resolve)
515f29bb8aSDimitry Andric     FileSystem::Instance().Resolve(*m_opaque_up);
5294994d37SDimitry Andric }
53f034231aSEd Maste 
54cfca06d7SDimitry Andric SBFileSpec::~SBFileSpec() = default;
55f034231aSEd Maste 
operator =(const SBFileSpec & rhs)5614f1b3e8SDimitry Andric const SBFileSpec &SBFileSpec::operator=(const SBFileSpec &rhs) {
576f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
585f29bb8aSDimitry Andric 
59f034231aSEd Maste   if (this != &rhs)
605f29bb8aSDimitry Andric     m_opaque_up = clone(rhs.m_opaque_up);
616f8fc217SDimitry Andric   return *this;
62f034231aSEd Maste }
63f034231aSEd Maste 
operator ==(const SBFileSpec & rhs) const645f29bb8aSDimitry Andric bool SBFileSpec::operator==(const SBFileSpec &rhs) const {
656f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
665f29bb8aSDimitry Andric 
675f29bb8aSDimitry Andric   return ref() == rhs.ref();
685f29bb8aSDimitry Andric }
695f29bb8aSDimitry Andric 
operator !=(const SBFileSpec & rhs) const705f29bb8aSDimitry Andric bool SBFileSpec::operator!=(const SBFileSpec &rhs) const {
716f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
725f29bb8aSDimitry Andric 
735f29bb8aSDimitry Andric   return !(*this == rhs);
745f29bb8aSDimitry Andric }
755f29bb8aSDimitry Andric 
IsValid() const765f29bb8aSDimitry Andric bool SBFileSpec::IsValid() const {
776f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this);
785f29bb8aSDimitry Andric   return this->operator bool();
795f29bb8aSDimitry Andric }
operator bool() const805f29bb8aSDimitry Andric SBFileSpec::operator bool() const {
816f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this);
825f29bb8aSDimitry Andric 
835f29bb8aSDimitry Andric   return m_opaque_up->operator bool();
845f29bb8aSDimitry Andric }
85f034231aSEd Maste 
Exists() const8614f1b3e8SDimitry Andric bool SBFileSpec::Exists() const {
876f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this);
88f034231aSEd Maste 
895f29bb8aSDimitry Andric   return FileSystem::Instance().Exists(*m_opaque_up);
90f034231aSEd Maste }
91f034231aSEd Maste 
ResolveExecutableLocation()9214f1b3e8SDimitry Andric bool SBFileSpec::ResolveExecutableLocation() {
936f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this);
945f29bb8aSDimitry Andric 
955f29bb8aSDimitry Andric   return FileSystem::Instance().ResolveExecutableLocation(*m_opaque_up);
96f034231aSEd Maste }
97f034231aSEd Maste 
ResolvePath(const char * src_path,char * dst_path,size_t dst_len)9814f1b3e8SDimitry Andric int SBFileSpec::ResolvePath(const char *src_path, char *dst_path,
9914f1b3e8SDimitry Andric                             size_t dst_len) {
1006f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(src_path, dst_path, dst_len);
1015f29bb8aSDimitry Andric 
1020cac4ca3SEd Maste   llvm::SmallString<64> result(src_path);
10394994d37SDimitry Andric   FileSystem::Instance().Resolve(result);
1045e95aa85SEd Maste   ::snprintf(dst_path, dst_len, "%s", result.c_str());
1055e95aa85SEd Maste   return std::min(dst_len - 1, result.size());
106f034231aSEd Maste }
107f034231aSEd Maste 
GetFilename() const10814f1b3e8SDimitry Andric const char *SBFileSpec::GetFilename() const {
1096f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this);
110f034231aSEd Maste 
1115f29bb8aSDimitry Andric   return m_opaque_up->GetFilename().AsCString();
112f034231aSEd Maste }
113f034231aSEd Maste 
GetDirectory() const11414f1b3e8SDimitry Andric const char *SBFileSpec::GetDirectory() const {
1156f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1165f29bb8aSDimitry Andric 
1175f29bb8aSDimitry Andric   FileSpec directory{*m_opaque_up};
118e3b55780SDimitry Andric   directory.ClearFilename();
119e3b55780SDimitry Andric   return directory.GetPathAsConstString().GetCString();
120f034231aSEd Maste }
121f034231aSEd Maste 
SetFilename(const char * filename)12214f1b3e8SDimitry Andric void SBFileSpec::SetFilename(const char *filename) {
1236f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, filename);
1245f29bb8aSDimitry Andric 
12586758c71SEd Maste   if (filename && filename[0])
126e3b55780SDimitry Andric     m_opaque_up->SetFilename(filename);
12786758c71SEd Maste   else
128e3b55780SDimitry Andric     m_opaque_up->ClearFilename();
12986758c71SEd Maste }
13086758c71SEd Maste 
SetDirectory(const char * directory)13114f1b3e8SDimitry Andric void SBFileSpec::SetDirectory(const char *directory) {
1326f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, directory);
1335f29bb8aSDimitry Andric 
13486758c71SEd Maste   if (directory && directory[0])
135e3b55780SDimitry Andric     m_opaque_up->SetDirectory(directory);
13686758c71SEd Maste   else
137e3b55780SDimitry Andric     m_opaque_up->ClearDirectory();
13886758c71SEd Maste }
13986758c71SEd Maste 
GetPath(char * dst_path,size_t dst_len) const14014f1b3e8SDimitry Andric uint32_t SBFileSpec::GetPath(char *dst_path, size_t dst_len) const {
1416f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, dst_path, dst_len);
142f034231aSEd Maste 
1435f29bb8aSDimitry Andric   uint32_t result = m_opaque_up->GetPath(dst_path, dst_len);
144f034231aSEd Maste 
145f034231aSEd Maste   if (result == 0 && dst_path && dst_len > 0)
146f034231aSEd Maste     *dst_path = '\0';
147f034231aSEd Maste   return result;
148f034231aSEd Maste }
149f034231aSEd Maste 
operator ->() const15014f1b3e8SDimitry Andric const lldb_private::FileSpec *SBFileSpec::operator->() const {
1515f29bb8aSDimitry Andric   return m_opaque_up.get();
152f034231aSEd Maste }
153f034231aSEd Maste 
get() const15414f1b3e8SDimitry Andric const lldb_private::FileSpec *SBFileSpec::get() const {
1555f29bb8aSDimitry Andric   return m_opaque_up.get();
156f034231aSEd Maste }
157f034231aSEd Maste 
operator *() const15814f1b3e8SDimitry Andric const lldb_private::FileSpec &SBFileSpec::operator*() const {
1595f29bb8aSDimitry Andric   return *m_opaque_up;
160f034231aSEd Maste }
161f034231aSEd Maste 
ref() const1625f29bb8aSDimitry Andric const lldb_private::FileSpec &SBFileSpec::ref() const { return *m_opaque_up; }
163f034231aSEd Maste 
SetFileSpec(const lldb_private::FileSpec & fs)16414f1b3e8SDimitry Andric void SBFileSpec::SetFileSpec(const lldb_private::FileSpec &fs) {
1655f29bb8aSDimitry Andric   *m_opaque_up = fs;
166f034231aSEd Maste }
167f034231aSEd Maste 
GetDescription(SBStream & description) const16814f1b3e8SDimitry Andric bool SBFileSpec::GetDescription(SBStream &description) const {
1696f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, description);
1705f29bb8aSDimitry Andric 
171f034231aSEd Maste   Stream &strm = description.ref();
172f034231aSEd Maste   char path[PATH_MAX];
1735f29bb8aSDimitry Andric   if (m_opaque_up->GetPath(path, sizeof(path)))
174f034231aSEd Maste     strm.PutCString(path);
175f034231aSEd Maste   return true;
176f034231aSEd Maste }
177f3fbd1c0SDimitry Andric 
AppendPathComponent(const char * fn)17814f1b3e8SDimitry Andric void SBFileSpec::AppendPathComponent(const char *fn) {
1796f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, fn);
1805f29bb8aSDimitry Andric 
1815f29bb8aSDimitry Andric   m_opaque_up->AppendPathComponent(fn);
1825f29bb8aSDimitry Andric }
183