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