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