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     Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
112 
113     if (m_opaque_ap.get())
114     {
115         if (log)
116             log->Printf ("SBFileSpec(%p)::GetFilename () => %s", m_opaque_ap.get(),
117                          m_opaque_ap->GetFilename().AsCString());
118 
119         return m_opaque_ap->GetFilename().AsCString();
120     }
121 
122     if (log)
123         log->Printf ("SBFileSpec(%p)::GetFilename () => NULL", m_opaque_ap.get());
124 
125     return NULL;
126 }
127 
128 const char *
129 SBFileSpec::GetDirectory() const
130 {
131     if (m_opaque_ap.get())
132         return m_opaque_ap->GetDirectory().AsCString();
133     return NULL;
134 }
135 
136 uint32_t
137 SBFileSpec::GetPath (char *dst_path, size_t dst_len) const
138 {
139     Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
140 
141     uint32_t result;
142     if (m_opaque_ap.get())
143     {
144         result = m_opaque_ap->GetPath (dst_path, dst_len);
145         if (log)
146             log->Printf ("SBFileSpec(%p)::GetPath (dst_path, dst_len) => dst_path='%s', dst_len='%d', "
147                          "result='%d'", m_opaque_ap.get(), dst_path, (uint32_t) dst_len, result);
148         return result;
149     }
150 
151     if (log)
152         log->Printf ("SBFileSpec(%p)::GetPath (dst_path, dst_len) => NULL (0)", m_opaque_ap.get());
153 
154     if (dst_path && dst_len)
155         *dst_path = '\0';
156     return 0;
157 }
158 
159 
160 const lldb_private::FileSpec *
161 SBFileSpec::operator->() const
162 {
163     return m_opaque_ap.get();
164 }
165 
166 const lldb_private::FileSpec *
167 SBFileSpec::get() const
168 {
169     return m_opaque_ap.get();
170 }
171 
172 
173 const lldb_private::FileSpec &
174 SBFileSpec::operator*() const
175 {
176     return *m_opaque_ap.get();
177 }
178 
179 const lldb_private::FileSpec &
180 SBFileSpec::ref() const
181 {
182     return *m_opaque_ap.get();
183 }
184 
185 
186 void
187 SBFileSpec::SetFileSpec (const lldb_private::FileSpec& fs)
188 {
189     if (m_opaque_ap.get())
190         *m_opaque_ap = fs;
191     else
192         m_opaque_ap.reset (new FileSpec (fs));
193 }
194 
195 bool
196 SBFileSpec::GetDescription (SBStream &description) const
197 {
198     if (m_opaque_ap.get())
199     {
200         const char *filename = GetFilename();
201         const char *dir_name = GetDirectory();
202         if (!filename && !dir_name)
203             description.Printf ("No value");
204         else if (!dir_name)
205             description.Printf ("%s", filename);
206         else
207             description.Printf ("%s/%s", dir_name, filename);
208     }
209     else
210         description.Printf ("No value");
211 
212     return true;
213 }
214