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) => SBFileSpec(%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=%i) => SBFileSpec(%p)", path,
55                      resolve, 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     bool result = false;
85     if (m_opaque_ap.get())
86         result = m_opaque_ap->Exists();
87 
88     if (log)
89         log->Printf ("SBFileSpec(%p)::Exists () => %s", m_opaque_ap.get(), (result ? "true" : "false"));
90 
91     return result;
92 }
93 
94 bool
95 SBFileSpec::ResolveExecutableLocation ()
96 {
97     if (m_opaque_ap.get())
98         return m_opaque_ap->ResolveExecutableLocation ();
99     return false;
100 }
101 
102 int
103 SBFileSpec::ResolvePath (const char *src_path, char *dst_path, size_t dst_len)
104 {
105     return lldb_private::FileSpec::Resolve (src_path, dst_path, dst_len);
106 }
107 
108 const char *
109 SBFileSpec::GetFilename() const
110 {
111     const char *s = NULL;
112     if (m_opaque_ap.get())
113         s = m_opaque_ap->GetFilename().AsCString();
114 
115     Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
116     if (log)
117         log->Printf ("SBFileSpec(%p)::GetFilename () => \"%s\"", m_opaque_ap.get(), s ? s : "");
118 
119     return s;
120 }
121 
122 const char *
123 SBFileSpec::GetDirectory() const
124 {
125     const char *s = NULL;
126     if (m_opaque_ap.get())
127         s = m_opaque_ap->GetDirectory().AsCString();
128     Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
129     if (log)
130         log->Printf ("SBFileSpec(%p)::GetDirectory () => \"%s\"", m_opaque_ap.get(), s ? s : "");
131     return s;
132 }
133 
134 uint32_t
135 SBFileSpec::GetPath (char *dst_path, size_t dst_len) const
136 {
137     Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
138 
139     uint32_t result;
140     if (m_opaque_ap.get())
141     {
142         result = m_opaque_ap->GetPath (dst_path, dst_len);
143         if (log)
144             log->Printf ("SBFileSpec(%p)::GetPath (dst_path, dst_len) => dst_path='%s', dst_len='%d', "
145                          "result='%d'", m_opaque_ap.get(), dst_path, (uint32_t) dst_len, result);
146         return result;
147     }
148 
149     if (log)
150         log->Printf ("SBFileSpec(%p)::GetPath (dst_path, dst_len) => NULL (0)", m_opaque_ap.get());
151 
152     if (dst_path && dst_len)
153         *dst_path = '\0';
154     return 0;
155 }
156 
157 
158 const lldb_private::FileSpec *
159 SBFileSpec::operator->() const
160 {
161     return m_opaque_ap.get();
162 }
163 
164 const lldb_private::FileSpec *
165 SBFileSpec::get() const
166 {
167     return m_opaque_ap.get();
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::ref() const
179 {
180     return *m_opaque_ap.get();
181 }
182 
183 
184 void
185 SBFileSpec::SetFileSpec (const lldb_private::FileSpec& fs)
186 {
187     if (m_opaque_ap.get())
188         *m_opaque_ap = fs;
189     else
190         m_opaque_ap.reset (new FileSpec (fs));
191 }
192 
193 bool
194 SBFileSpec::GetDescription (SBStream &description) const
195 {
196     if (m_opaque_ap.get())
197     {
198         const char *filename = GetFilename();
199         const char *dir_name = GetDirectory();
200         if (!filename && !dir_name)
201             description.Printf ("No value");
202         else if (!dir_name)
203             description.Printf ("%s", filename);
204         else
205             description.Printf ("%s/%s", dir_name, filename);
206     }
207     else
208         description.Printf ("No value");
209 
210     return true;
211 }
212