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