1ac7ddfbfSEd Maste //===-- SBFileSpec.cpp ------------------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste 
10*b5893f02SDimitry Andric #include <inttypes.h>
11ac7ddfbfSEd Maste #include <limits.h>
12ac7ddfbfSEd Maste 
13ac7ddfbfSEd Maste #include "lldb/API/SBFileSpec.h"
14ac7ddfbfSEd Maste #include "lldb/API/SBStream.h"
15*b5893f02SDimitry Andric #include "lldb/Host/FileSystem.h"
16f678e45dSDimitry Andric #include "lldb/Host/PosixApi.h"
17f678e45dSDimitry Andric #include "lldb/Utility/FileSpec.h"
18f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
19f678e45dSDimitry Andric #include "lldb/Utility/Stream.h"
20ac7ddfbfSEd Maste 
210127ef0fSEd Maste #include "llvm/ADT/SmallString.h"
220127ef0fSEd Maste 
23ac7ddfbfSEd Maste using namespace lldb;
24ac7ddfbfSEd Maste using namespace lldb_private;
25ac7ddfbfSEd Maste 
SBFileSpec()26435933ddSDimitry Andric SBFileSpec::SBFileSpec() : m_opaque_ap(new lldb_private::FileSpec()) {}
27ac7ddfbfSEd Maste 
SBFileSpec(const SBFileSpec & rhs)28435933ddSDimitry Andric SBFileSpec::SBFileSpec(const SBFileSpec &rhs)
29435933ddSDimitry Andric     : m_opaque_ap(new lldb_private::FileSpec(*rhs.m_opaque_ap)) {}
30ac7ddfbfSEd Maste 
SBFileSpec(const lldb_private::FileSpec & fspec)31435933ddSDimitry Andric SBFileSpec::SBFileSpec(const lldb_private::FileSpec &fspec)
32435933ddSDimitry Andric     : m_opaque_ap(new lldb_private::FileSpec(fspec)) {}
33ac7ddfbfSEd Maste 
340127ef0fSEd Maste // Deprecated!!!
SBFileSpec(const char * path)35*b5893f02SDimitry Andric SBFileSpec::SBFileSpec(const char *path) : m_opaque_ap(new FileSpec(path)) {
36*b5893f02SDimitry Andric   FileSystem::Instance().Resolve(*m_opaque_ap);
37*b5893f02SDimitry Andric }
38ac7ddfbfSEd Maste 
SBFileSpec(const char * path,bool resolve)39435933ddSDimitry Andric SBFileSpec::SBFileSpec(const char *path, bool resolve)
40*b5893f02SDimitry Andric     : m_opaque_ap(new FileSpec(path)) {
41*b5893f02SDimitry Andric   if (resolve)
42*b5893f02SDimitry Andric     FileSystem::Instance().Resolve(*m_opaque_ap);
43*b5893f02SDimitry Andric }
44ac7ddfbfSEd Maste 
~SBFileSpec()45435933ddSDimitry Andric SBFileSpec::~SBFileSpec() {}
46ac7ddfbfSEd Maste 
operator =(const SBFileSpec & rhs)47435933ddSDimitry Andric const SBFileSpec &SBFileSpec::operator=(const SBFileSpec &rhs) {
48ac7ddfbfSEd Maste   if (this != &rhs)
49ac7ddfbfSEd Maste     *m_opaque_ap = *rhs.m_opaque_ap;
50ac7ddfbfSEd Maste   return *this;
51ac7ddfbfSEd Maste }
52ac7ddfbfSEd Maste 
IsValid() const53435933ddSDimitry Andric bool SBFileSpec::IsValid() const { return m_opaque_ap->operator bool(); }
54ac7ddfbfSEd Maste 
Exists() const55435933ddSDimitry Andric bool SBFileSpec::Exists() const {
56ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
57ac7ddfbfSEd Maste 
58*b5893f02SDimitry Andric   bool result = FileSystem::Instance().Exists(*m_opaque_ap);
59ac7ddfbfSEd Maste 
60ac7ddfbfSEd Maste   if (log)
610127ef0fSEd Maste     log->Printf("SBFileSpec(%p)::Exists () => %s",
620127ef0fSEd Maste                 static_cast<void *>(m_opaque_ap.get()),
630127ef0fSEd Maste                 (result ? "true" : "false"));
64ac7ddfbfSEd Maste 
65ac7ddfbfSEd Maste   return result;
66ac7ddfbfSEd Maste }
67ac7ddfbfSEd Maste 
ResolveExecutableLocation()68435933ddSDimitry Andric bool SBFileSpec::ResolveExecutableLocation() {
69*b5893f02SDimitry Andric   return FileSystem::Instance().ResolveExecutableLocation(*m_opaque_ap);
70ac7ddfbfSEd Maste }
71ac7ddfbfSEd Maste 
ResolvePath(const char * src_path,char * dst_path,size_t dst_len)72435933ddSDimitry Andric int SBFileSpec::ResolvePath(const char *src_path, char *dst_path,
73435933ddSDimitry Andric                             size_t dst_len) {
740127ef0fSEd Maste   llvm::SmallString<64> result(src_path);
75*b5893f02SDimitry Andric   FileSystem::Instance().Resolve(result);
761c3bbb01SEd Maste   ::snprintf(dst_path, dst_len, "%s", result.c_str());
771c3bbb01SEd Maste   return std::min(dst_len - 1, result.size());
78ac7ddfbfSEd Maste }
79ac7ddfbfSEd Maste 
GetFilename() const80435933ddSDimitry Andric const char *SBFileSpec::GetFilename() const {
81ac7ddfbfSEd Maste   const char *s = m_opaque_ap->GetFilename().AsCString();
82ac7ddfbfSEd Maste 
83ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
84435933ddSDimitry Andric   if (log) {
85ac7ddfbfSEd Maste     if (s)
860127ef0fSEd Maste       log->Printf("SBFileSpec(%p)::GetFilename () => \"%s\"",
870127ef0fSEd Maste                   static_cast<void *>(m_opaque_ap.get()), s);
88ac7ddfbfSEd Maste     else
890127ef0fSEd Maste       log->Printf("SBFileSpec(%p)::GetFilename () => NULL",
900127ef0fSEd Maste                   static_cast<void *>(m_opaque_ap.get()));
91ac7ddfbfSEd Maste   }
92ac7ddfbfSEd Maste 
93ac7ddfbfSEd Maste   return s;
94ac7ddfbfSEd Maste }
95ac7ddfbfSEd Maste 
GetDirectory() const96435933ddSDimitry Andric const char *SBFileSpec::GetDirectory() const {
971c3bbb01SEd Maste   FileSpec directory{*m_opaque_ap};
981c3bbb01SEd Maste   directory.GetFilename().Clear();
99ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
100435933ddSDimitry Andric   if (log) {
1011c3bbb01SEd Maste     if (directory)
1020127ef0fSEd Maste       log->Printf("SBFileSpec(%p)::GetDirectory () => \"%s\"",
103435933ddSDimitry Andric                   static_cast<void *>(m_opaque_ap.get()),
104435933ddSDimitry Andric                   directory.GetCString());
105ac7ddfbfSEd Maste     else
1060127ef0fSEd Maste       log->Printf("SBFileSpec(%p)::GetDirectory () => NULL",
1070127ef0fSEd Maste                   static_cast<void *>(m_opaque_ap.get()));
108ac7ddfbfSEd Maste   }
1091c3bbb01SEd Maste   return directory.GetCString();
110ac7ddfbfSEd Maste }
111ac7ddfbfSEd Maste 
SetFilename(const char * filename)112435933ddSDimitry Andric void SBFileSpec::SetFilename(const char *filename) {
113b952cd58SEd Maste   if (filename && filename[0])
114b952cd58SEd Maste     m_opaque_ap->GetFilename().SetCString(filename);
115b952cd58SEd Maste   else
116b952cd58SEd Maste     m_opaque_ap->GetFilename().Clear();
117b952cd58SEd Maste }
118b952cd58SEd Maste 
SetDirectory(const char * directory)119435933ddSDimitry Andric void SBFileSpec::SetDirectory(const char *directory) {
120b952cd58SEd Maste   if (directory && directory[0])
121b952cd58SEd Maste     m_opaque_ap->GetDirectory().SetCString(directory);
122b952cd58SEd Maste   else
123b952cd58SEd Maste     m_opaque_ap->GetDirectory().Clear();
124b952cd58SEd Maste }
125b952cd58SEd Maste 
GetPath(char * dst_path,size_t dst_len) const126435933ddSDimitry Andric uint32_t SBFileSpec::GetPath(char *dst_path, size_t dst_len) const {
127ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
128ac7ddfbfSEd Maste 
129ac7ddfbfSEd Maste   uint32_t result = m_opaque_ap->GetPath(dst_path, dst_len);
130ac7ddfbfSEd Maste 
131ac7ddfbfSEd Maste   if (log)
132435933ddSDimitry Andric     log->Printf("SBFileSpec(%p)::GetPath (dst_path=\"%.*s\", dst_len=%" PRIu64
133435933ddSDimitry Andric                 ") => %u",
1340127ef0fSEd Maste                 static_cast<void *>(m_opaque_ap.get()), result, dst_path,
1350127ef0fSEd Maste                 static_cast<uint64_t>(dst_len), result);
136ac7ddfbfSEd Maste 
137ac7ddfbfSEd Maste   if (result == 0 && dst_path && dst_len > 0)
138ac7ddfbfSEd Maste     *dst_path = '\0';
139ac7ddfbfSEd Maste   return result;
140ac7ddfbfSEd Maste }
141ac7ddfbfSEd Maste 
operator ->() const142435933ddSDimitry Andric const lldb_private::FileSpec *SBFileSpec::operator->() const {
143ac7ddfbfSEd Maste   return m_opaque_ap.get();
144ac7ddfbfSEd Maste }
145ac7ddfbfSEd Maste 
get() const146435933ddSDimitry Andric const lldb_private::FileSpec *SBFileSpec::get() const {
147ac7ddfbfSEd Maste   return m_opaque_ap.get();
148ac7ddfbfSEd Maste }
149ac7ddfbfSEd Maste 
operator *() const150435933ddSDimitry Andric const lldb_private::FileSpec &SBFileSpec::operator*() const {
151*b5893f02SDimitry Andric   return *m_opaque_ap;
152ac7ddfbfSEd Maste }
153ac7ddfbfSEd Maste 
ref() const154*b5893f02SDimitry Andric const lldb_private::FileSpec &SBFileSpec::ref() const { return *m_opaque_ap; }
155ac7ddfbfSEd Maste 
SetFileSpec(const lldb_private::FileSpec & fs)156435933ddSDimitry Andric void SBFileSpec::SetFileSpec(const lldb_private::FileSpec &fs) {
157ac7ddfbfSEd Maste   *m_opaque_ap = fs;
158ac7ddfbfSEd Maste }
159ac7ddfbfSEd Maste 
GetDescription(SBStream & description) const160435933ddSDimitry Andric bool SBFileSpec::GetDescription(SBStream &description) const {
161ac7ddfbfSEd Maste   Stream &strm = description.ref();
162ac7ddfbfSEd Maste   char path[PATH_MAX];
163ac7ddfbfSEd Maste   if (m_opaque_ap->GetPath(path, sizeof(path)))
164ac7ddfbfSEd Maste     strm.PutCString(path);
165ac7ddfbfSEd Maste   return true;
166ac7ddfbfSEd Maste }
1674bb0738eSEd Maste 
AppendPathComponent(const char * fn)168435933ddSDimitry Andric void SBFileSpec::AppendPathComponent(const char *fn) {
1694bb0738eSEd Maste   m_opaque_ap->AppendPathComponent(fn);
1704bb0738eSEd Maste }
171