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