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