1 //===-- SBCommandReturnObject.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/SBCommandReturnObject.h"
11 #include "lldb/API/SBStream.h"
12 
13 #include "lldb/Core/Log.h"
14 #include "lldb/Interpreter/CommandReturnObject.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 SBCommandReturnObject::SBCommandReturnObject () :
20     m_opaque_ap (new CommandReturnObject ())
21 {
22 }
23 
24 SBCommandReturnObject::SBCommandReturnObject (const SBCommandReturnObject &rhs):
25     m_opaque_ap ()
26 {
27     if (rhs.m_opaque_ap.get())
28         m_opaque_ap.reset (new CommandReturnObject (*rhs.m_opaque_ap));
29 }
30 
31 const SBCommandReturnObject &
32 SBCommandReturnObject::operator = (const SBCommandReturnObject &rhs)
33 {
34     if (this != &rhs)
35     {
36         if (rhs.m_opaque_ap.get())
37             m_opaque_ap.reset (new CommandReturnObject (*rhs.m_opaque_ap));
38         else
39             m_opaque_ap.reset();
40     }
41     return *this;
42 }
43 
44 
45 SBCommandReturnObject::~SBCommandReturnObject ()
46 {
47     // m_opaque_ap will automatically delete any pointer it owns
48 }
49 
50 bool
51 SBCommandReturnObject::IsValid() const
52 {
53     return m_opaque_ap.get() != NULL;
54 }
55 
56 
57 const char *
58 SBCommandReturnObject::GetOutput ()
59 {
60     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
61 
62     if (m_opaque_ap.get())
63     {
64         if (log)
65             log->Printf ("SBCommandReturnObject(%p)::GetOutput () => \"%s\"", m_opaque_ap.get(),
66                          m_opaque_ap->GetOutputStream().GetData());
67 
68         return m_opaque_ap->GetOutputStream().GetData();
69     }
70 
71     if (log)
72         log->Printf ("SBCommandReturnObject(%p)::GetOutput () => NULL", m_opaque_ap.get());
73 
74     return NULL;
75 }
76 
77 const char *
78 SBCommandReturnObject::GetError ()
79 {
80     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
81 
82     if (m_opaque_ap.get())
83     {
84         if (log)
85             log->Printf ("SBCommandReturnObject(%p)::GetError () => \"%s\"", m_opaque_ap.get(),
86                          m_opaque_ap->GetErrorStream().GetData());
87 
88         return m_opaque_ap->GetErrorStream().GetData();
89     }
90 
91     if (log)
92         log->Printf ("SBCommandReturnObject(%p)::GetError () => NULL", m_opaque_ap.get());
93 
94     return NULL;
95 }
96 
97 size_t
98 SBCommandReturnObject::GetOutputSize ()
99 {
100     if (m_opaque_ap.get())
101         return m_opaque_ap->GetOutputStream().GetSize();
102     return 0;
103 }
104 
105 size_t
106 SBCommandReturnObject::GetErrorSize ()
107 {
108     if (m_opaque_ap.get())
109         return m_opaque_ap->GetErrorStream().GetSize();
110     return 0;
111 }
112 
113 size_t
114 SBCommandReturnObject::PutOutput (FILE *fh)
115 {
116     if (fh)
117     {
118         size_t num_bytes = GetOutputSize ();
119         if (num_bytes)
120             return ::fprintf (fh, "%s", GetOutput());
121     }
122     return 0;
123 }
124 
125 size_t
126 SBCommandReturnObject::PutError (FILE *fh)
127 {
128     if (fh)
129     {
130         size_t num_bytes = GetErrorSize ();
131         if (num_bytes)
132             return ::fprintf (fh, "%s", GetError());
133     }
134     return 0;
135 }
136 
137 void
138 SBCommandReturnObject::Clear()
139 {
140     if (m_opaque_ap.get())
141         m_opaque_ap->Clear();
142 }
143 
144 lldb::ReturnStatus
145 SBCommandReturnObject::GetStatus()
146 {
147     if (m_opaque_ap.get())
148         return m_opaque_ap->GetStatus();
149     return lldb::eReturnStatusInvalid;
150 }
151 
152 bool
153 SBCommandReturnObject::Succeeded ()
154 {
155     if (m_opaque_ap.get())
156         return m_opaque_ap->Succeeded();
157     return false;
158 }
159 
160 bool
161 SBCommandReturnObject::HasResult ()
162 {
163     if (m_opaque_ap.get())
164         return m_opaque_ap->HasResult();
165     return false;
166 }
167 
168 void
169 SBCommandReturnObject::AppendMessage (const char *message)
170 {
171     if (m_opaque_ap.get())
172         m_opaque_ap->AppendMessage (message);
173 }
174 
175 CommandReturnObject *
176 SBCommandReturnObject::operator ->() const
177 {
178     return m_opaque_ap.get();
179 }
180 
181 CommandReturnObject *
182 SBCommandReturnObject::get() const
183 {
184     return m_opaque_ap.get();
185 }
186 
187 CommandReturnObject &
188 SBCommandReturnObject::operator *() const
189 {
190     assert(m_opaque_ap.get());
191     return *(m_opaque_ap.get());
192 }
193 
194 
195 CommandReturnObject &
196 SBCommandReturnObject::ref() const
197 {
198     assert(m_opaque_ap.get());
199     return *(m_opaque_ap.get());
200 }
201 
202 
203 void
204 SBCommandReturnObject::SetLLDBObjectPtr (CommandReturnObject *ptr)
205 {
206     if (m_opaque_ap.get())
207         m_opaque_ap.reset (ptr);
208 }
209 
210 bool
211 SBCommandReturnObject::GetDescription (SBStream &description)
212 {
213     if (m_opaque_ap.get())
214     {
215         description.Printf ("Status:  ");
216         lldb::ReturnStatus status = m_opaque_ap->GetStatus();
217         if (status == lldb::eReturnStatusStarted)
218             description.Printf ("Started");
219         else if (status == lldb::eReturnStatusInvalid)
220             description.Printf ("Invalid");
221         else if (m_opaque_ap->Succeeded())
222             description.Printf ("Success");
223         else
224             description.Printf ("Fail");
225 
226         if (GetOutputSize() > 0)
227             description.Printf ("\nOutput Message:\n%s", GetOutput());
228 
229         if (GetErrorSize() > 0)
230             description.Printf ("\nError Message:\n%s", GetError());
231     }
232     else
233         description.Printf ("No value");
234 
235     return true;
236 }
237