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 <limits.h>
11 
12 #include "lldb/API/SBFileSpec.h"
13 #include "lldb/API/SBStream.h"
14 #include "lldb/Host/FileSpec.h"
15 #include "lldb/Core/Log.h"
16 #include "lldb/Core/Stream.h"
17 
18 using namespace lldb;
19 using namespace lldb_private;
20 
21 
22 
23 SBFileSpec::SBFileSpec () :
24     m_opaque_ap()
25 {
26 }
27 
28 SBFileSpec::SBFileSpec (const SBFileSpec &rhs) :
29     m_opaque_ap()
30 {
31     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
32 
33     if (rhs.m_opaque_ap.get())
34         m_opaque_ap.reset (new FileSpec (rhs.get()));
35 
36     if (log)
37     {
38         SBStream sstr;
39         GetDescription (sstr);
40         log->Printf ("SBFileSpec::SBFileSpec (const SBFileSpec rhs.ap=%p) => SBFileSpec(%p): %s",
41                      rhs.m_opaque_ap.get(), m_opaque_ap.get(), sstr.GetData());
42     }
43 }
44 
45 // Deprected!!!
46 SBFileSpec::SBFileSpec (const char *path) :
47     m_opaque_ap(new FileSpec (path, true))
48 {
49 }
50 
51 SBFileSpec::SBFileSpec (const char *path, bool resolve) :
52     m_opaque_ap(new FileSpec (path, resolve))
53 {
54     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
55 
56     if (log)
57         log->Printf ("SBFileSpec::SBFileSpec (path=\"%s\", resolve=%i) => SBFileSpec(%p)", path,
58                      resolve, m_opaque_ap.get());
59 }
60 
61 SBFileSpec::~SBFileSpec ()
62 {
63 }
64 
65 const SBFileSpec &
66 SBFileSpec::operator = (const SBFileSpec &rhs)
67 {
68     if (this != &rhs)
69     {
70         if (rhs.IsValid())
71             m_opaque_ap.reset (new lldb_private::FileSpec(*rhs.m_opaque_ap.get()));
72     }
73     return *this;
74 }
75 
76 bool
77 SBFileSpec::IsValid() const
78 {
79     return m_opaque_ap.get() != NULL;
80 }
81 
82 bool
83 SBFileSpec::Exists () const
84 {
85     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
86 
87     bool result = false;
88     if (m_opaque_ap.get())
89         result = m_opaque_ap->Exists();
90 
91     if (log)
92         log->Printf ("SBFileSpec(%p)::Exists () => %s", m_opaque_ap.get(), (result ? "true" : "false"));
93 
94     return result;
95 }
96 
97 bool
98 SBFileSpec::ResolveExecutableLocation ()
99 {
100     if (m_opaque_ap.get())
101         return m_opaque_ap->ResolveExecutableLocation ();
102     return false;
103 }
104 
105 int
106 SBFileSpec::ResolvePath (const char *src_path, char *dst_path, size_t dst_len)
107 {
108     return lldb_private::FileSpec::Resolve (src_path, dst_path, dst_len);
109 }
110 
111 const char *
112 SBFileSpec::GetFilename() const
113 {
114     const char *s = NULL;
115     if (m_opaque_ap.get())
116         s = m_opaque_ap->GetFilename().AsCString();
117 
118     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
119     if (log)
120     {
121         if (s)
122             log->Printf ("SBFileSpec(%p)::GetFilename () => \"%s\"", m_opaque_ap.get(), s);
123         else
124             log->Printf ("SBFileSpec(%p)::GetFilename () => NULL", m_opaque_ap.get());
125     }
126 
127     return s;
128 }
129 
130 const char *
131 SBFileSpec::GetDirectory() const
132 {
133     const char *s = NULL;
134     if (m_opaque_ap.get())
135         s = m_opaque_ap->GetDirectory().AsCString();
136     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
137     if (log)
138     {
139         if (s)
140             log->Printf ("SBFileSpec(%p)::GetDirectory () => \"%s\"", m_opaque_ap.get(), s);
141         else
142             log->Printf ("SBFileSpec(%p)::GetDirectory () => NULL", m_opaque_ap.get());
143     }
144     return s;
145 }
146 
147 uint32_t
148 SBFileSpec::GetPath (char *dst_path, size_t dst_len) const
149 {
150     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
151 
152     uint32_t result = 0;
153     if (m_opaque_ap.get())
154         result = m_opaque_ap->GetPath (dst_path, dst_len);
155 
156     if (log)
157         log->Printf ("SBFileSpec(%p)::GetPath (dst_path=\"%.*s\", dst_len=%zu) => %u",
158                      m_opaque_ap.get(), result, dst_path, dst_len, result);
159 
160     if (result == 0 && dst_path && dst_len > 0)
161         *dst_path = '\0';
162     return result;
163 }
164 
165 
166 const lldb_private::FileSpec *
167 SBFileSpec::operator->() const
168 {
169     return m_opaque_ap.get();
170 }
171 
172 const lldb_private::FileSpec *
173 SBFileSpec::get() const
174 {
175     return m_opaque_ap.get();
176 }
177 
178 
179 const lldb_private::FileSpec &
180 SBFileSpec::operator*() const
181 {
182     return *m_opaque_ap.get();
183 }
184 
185 const lldb_private::FileSpec &
186 SBFileSpec::ref() const
187 {
188     return *m_opaque_ap.get();
189 }
190 
191 
192 void
193 SBFileSpec::SetFileSpec (const lldb_private::FileSpec& fs)
194 {
195     if (m_opaque_ap.get())
196         *m_opaque_ap = fs;
197     else
198         m_opaque_ap.reset (new FileSpec (fs));
199 }
200 
201 bool
202 SBFileSpec::GetDescription (SBStream &description) const
203 {
204     Stream &strm = description.ref();
205     if (m_opaque_ap.get())
206     {
207         char path[PATH_MAX];
208         if (m_opaque_ap->GetPath(path, sizeof(path)))
209             strm.PutCString (path);
210     }
211     else
212         strm.PutCString ("No value");
213 
214     return true;
215 }
216