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 
14 using namespace lldb;
15 using namespace lldb_private;
16 
17 
18 
19 SBFileSpec::SBFileSpec () :
20     m_opaque_ap()
21 {
22 }
23 
24 SBFileSpec::SBFileSpec (const SBFileSpec &rhs) :
25     m_opaque_ap()
26 {
27     if (rhs.m_opaque_ap.get())
28         m_opaque_ap.reset (new FileSpec (rhs.get()));
29 }
30 
31 SBFileSpec::SBFileSpec (const char *path) :
32     m_opaque_ap(new FileSpec (path))
33 {
34 }
35 
36 SBFileSpec::~SBFileSpec ()
37 {
38 }
39 
40 const SBFileSpec &
41 SBFileSpec::operator = (const SBFileSpec &rhs)
42 {
43     if (this != &rhs)
44     {
45         if (rhs.IsValid())
46             m_opaque_ap.reset (new lldb_private::FileSpec(*rhs.m_opaque_ap.get()));
47     }
48     return *this;
49 }
50 
51 bool
52 SBFileSpec::IsValid() const
53 {
54     return m_opaque_ap.get() != NULL;
55 }
56 
57 bool
58 SBFileSpec::Exists () const
59 {
60     if (m_opaque_ap.get())
61         return m_opaque_ap->Exists();
62     return false;
63 }
64 
65 bool
66 SBFileSpec::ResolveExecutableLocation ()
67 {
68     if (m_opaque_ap.get())
69         return m_opaque_ap->ResolveExecutableLocation ();
70     return false;
71 }
72 
73 int
74 SBFileSpec::ResolvePath (const char *src_path, char *dst_path, size_t dst_len)
75 {
76     return lldb_private::FileSpec::Resolve (src_path, dst_path, dst_len);
77 }
78 
79 const char *
80 SBFileSpec::GetFilename() const
81 {
82     if (m_opaque_ap.get())
83         return m_opaque_ap->GetFilename().AsCString();
84     return NULL;
85 }
86 
87 const char *
88 SBFileSpec::GetDirectory() const
89 {
90     if (m_opaque_ap.get())
91         return m_opaque_ap->GetDirectory().AsCString();
92     return NULL;
93 }
94 
95 uint32_t
96 SBFileSpec::GetPath (char *dst_path, size_t dst_len) const
97 {
98     if (m_opaque_ap.get())
99         return m_opaque_ap->GetPath (dst_path, dst_len);
100 
101     if (dst_path && dst_len)
102         *dst_path = '\0';
103     return 0;
104 }
105 
106 
107 const lldb_private::FileSpec *
108 SBFileSpec::operator->() const
109 {
110     return m_opaque_ap.get();
111 }
112 
113 const lldb_private::FileSpec *
114 SBFileSpec::get() const
115 {
116     return m_opaque_ap.get();
117 }
118 
119 
120 const lldb_private::FileSpec &
121 SBFileSpec::operator*() const
122 {
123     return *m_opaque_ap.get();
124 }
125 
126 const lldb_private::FileSpec &
127 SBFileSpec::ref() const
128 {
129     return *m_opaque_ap.get();
130 }
131 
132 
133 void
134 SBFileSpec::SetFileSpec (const lldb_private::FileSpec& fs)
135 {
136     if (m_opaque_ap.get())
137         *m_opaque_ap = fs;
138     else
139         m_opaque_ap.reset (new FileSpec (fs));
140 }
141 
142 bool
143 SBFileSpec::GetDescription (SBStream &description)
144 {
145     if (m_opaque_ap.get())
146     {
147         const char *filename = GetFilename();
148         const char *dir_name = GetDirectory();
149         if (!filename && !dir_name)
150             description.Printf ("No value");
151         else if (!dir_name)
152             description.Printf ("%s", filename);
153         else
154             description.Printf ("%s/%s", dir_name, filename);
155     }
156     else
157         description.Printf ("No value");
158 
159     return true;
160 }
161 
162 PyObject *
163 SBFileSpec::__repr__ ()
164 {
165     SBStream description;
166     GetDescription (description);
167     return PyString_FromString (description.GetData());
168 }
169