xref: /llvm-project/lldb/source/API/SBFileSpec.cpp (revision d5b440369dbb0d41e6ecd47d6ac7410201e27f17)
1 //===-- SBFileSpec.cpp ------------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <inttypes.h>
10 #include <limits.h>
11 
12 #include "lldb/API/SBFileSpec.h"
13 #include "lldb/API/SBStream.h"
14 #include "lldb/Host/FileSystem.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"
19 
20 #include "llvm/ADT/SmallString.h"
21 
22 using namespace lldb;
23 using namespace lldb_private;
24 
25 SBFileSpec::SBFileSpec() : m_opaque_up(new lldb_private::FileSpec()) {}
26 
27 SBFileSpec::SBFileSpec(const SBFileSpec &rhs)
28     : m_opaque_up(new lldb_private::FileSpec(*rhs.m_opaque_up)) {}
29 
30 SBFileSpec::SBFileSpec(const lldb_private::FileSpec &fspec)
31     : m_opaque_up(new lldb_private::FileSpec(fspec)) {}
32 
33 // Deprecated!!!
34 SBFileSpec::SBFileSpec(const char *path) : m_opaque_up(new FileSpec(path)) {
35   FileSystem::Instance().Resolve(*m_opaque_up);
36 }
37 
38 SBFileSpec::SBFileSpec(const char *path, bool resolve)
39     : m_opaque_up(new FileSpec(path)) {
40   if (resolve)
41     FileSystem::Instance().Resolve(*m_opaque_up);
42 }
43 
44 SBFileSpec::~SBFileSpec() {}
45 
46 const SBFileSpec &SBFileSpec::operator=(const SBFileSpec &rhs) {
47   if (this != &rhs)
48     *m_opaque_up = *rhs.m_opaque_up;
49   return *this;
50 }
51 
52 bool SBFileSpec::IsValid() const { return m_opaque_up->operator bool(); }
53 
54 bool SBFileSpec::Exists() const {
55   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
56 
57   bool result = FileSystem::Instance().Exists(*m_opaque_up);
58 
59   if (log)
60     log->Printf("SBFileSpec(%p)::Exists () => %s",
61                 static_cast<void *>(m_opaque_up.get()),
62                 (result ? "true" : "false"));
63 
64   return result;
65 }
66 
67 bool SBFileSpec::ResolveExecutableLocation() {
68   return FileSystem::Instance().ResolveExecutableLocation(*m_opaque_up);
69 }
70 
71 int SBFileSpec::ResolvePath(const char *src_path, char *dst_path,
72                             size_t dst_len) {
73   llvm::SmallString<64> result(src_path);
74   FileSystem::Instance().Resolve(result);
75   ::snprintf(dst_path, dst_len, "%s", result.c_str());
76   return std::min(dst_len - 1, result.size());
77 }
78 
79 const char *SBFileSpec::GetFilename() const {
80   const char *s = m_opaque_up->GetFilename().AsCString();
81 
82   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
83   if (log) {
84     if (s)
85       log->Printf("SBFileSpec(%p)::GetFilename () => \"%s\"",
86                   static_cast<void *>(m_opaque_up.get()), s);
87     else
88       log->Printf("SBFileSpec(%p)::GetFilename () => NULL",
89                   static_cast<void *>(m_opaque_up.get()));
90   }
91 
92   return s;
93 }
94 
95 const char *SBFileSpec::GetDirectory() const {
96   FileSpec directory{*m_opaque_up};
97   directory.GetFilename().Clear();
98   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
99   if (log) {
100     if (directory)
101       log->Printf("SBFileSpec(%p)::GetDirectory () => \"%s\"",
102                   static_cast<void *>(m_opaque_up.get()),
103                   directory.GetCString());
104     else
105       log->Printf("SBFileSpec(%p)::GetDirectory () => NULL",
106                   static_cast<void *>(m_opaque_up.get()));
107   }
108   return directory.GetCString();
109 }
110 
111 void SBFileSpec::SetFilename(const char *filename) {
112   if (filename && filename[0])
113     m_opaque_up->GetFilename().SetCString(filename);
114   else
115     m_opaque_up->GetFilename().Clear();
116 }
117 
118 void SBFileSpec::SetDirectory(const char *directory) {
119   if (directory && directory[0])
120     m_opaque_up->GetDirectory().SetCString(directory);
121   else
122     m_opaque_up->GetDirectory().Clear();
123 }
124 
125 uint32_t SBFileSpec::GetPath(char *dst_path, size_t dst_len) const {
126   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
127 
128   uint32_t result = m_opaque_up->GetPath(dst_path, dst_len);
129 
130   if (log)
131     log->Printf("SBFileSpec(%p)::GetPath (dst_path=\"%.*s\", dst_len=%" PRIu64
132                 ") => %u",
133                 static_cast<void *>(m_opaque_up.get()), result, dst_path,
134                 static_cast<uint64_t>(dst_len), result);
135 
136   if (result == 0 && dst_path && dst_len > 0)
137     *dst_path = '\0';
138   return result;
139 }
140 
141 const lldb_private::FileSpec *SBFileSpec::operator->() const {
142   return m_opaque_up.get();
143 }
144 
145 const lldb_private::FileSpec *SBFileSpec::get() const {
146   return m_opaque_up.get();
147 }
148 
149 const lldb_private::FileSpec &SBFileSpec::operator*() const {
150   return *m_opaque_up;
151 }
152 
153 const lldb_private::FileSpec &SBFileSpec::ref() const { return *m_opaque_up; }
154 
155 void SBFileSpec::SetFileSpec(const lldb_private::FileSpec &fs) {
156   *m_opaque_up = fs;
157 }
158 
159 bool SBFileSpec::GetDescription(SBStream &description) const {
160   Stream &strm = description.ref();
161   char path[PATH_MAX];
162   if (m_opaque_up->GetPath(path, sizeof(path)))
163     strm.PutCString(path);
164   return true;
165 }
166 
167 void SBFileSpec::AppendPathComponent(const char *fn) {
168   m_opaque_up->AppendPathComponent(fn);
169 }
170