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