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