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