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 SBCommandReturnObject::SBCommandReturnObject (CommandReturnObject *ptr) :
32     m_opaque_ap (ptr)
33 {
34 }
35 
36 CommandReturnObject *
37 SBCommandReturnObject::Release ()
38 {
39     return m_opaque_ap.release();
40 }
41 
42 const SBCommandReturnObject &
43 SBCommandReturnObject::operator = (const SBCommandReturnObject &rhs)
44 {
45     if (this != &rhs)
46     {
47         if (rhs.m_opaque_ap.get())
48             m_opaque_ap.reset (new CommandReturnObject (*rhs.m_opaque_ap));
49         else
50             m_opaque_ap.reset();
51     }
52     return *this;
53 }
54 
55 
56 SBCommandReturnObject::~SBCommandReturnObject ()
57 {
58     // m_opaque_ap will automatically delete any pointer it owns
59 }
60 
61 bool
62 SBCommandReturnObject::IsValid() const
63 {
64     return m_opaque_ap.get() != NULL;
65 }
66 
67 
68 const char *
69 SBCommandReturnObject::GetOutput ()
70 {
71     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
72 
73     if (m_opaque_ap.get())
74     {
75         if (log)
76             log->Printf ("SBCommandReturnObject(%p)::GetOutput () => \"%s\"", m_opaque_ap.get(),
77                          m_opaque_ap->GetOutputData());
78 
79         return m_opaque_ap->GetOutputData();
80     }
81 
82     if (log)
83         log->Printf ("SBCommandReturnObject(%p)::GetOutput () => NULL", m_opaque_ap.get());
84 
85     return NULL;
86 }
87 
88 const char *
89 SBCommandReturnObject::GetError ()
90 {
91     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
92 
93     if (m_opaque_ap.get())
94     {
95         if (log)
96             log->Printf ("SBCommandReturnObject(%p)::GetError () => \"%s\"", m_opaque_ap.get(),
97                          m_opaque_ap->GetErrorData());
98 
99         return m_opaque_ap->GetErrorData();
100     }
101 
102     if (log)
103         log->Printf ("SBCommandReturnObject(%p)::GetError () => NULL", m_opaque_ap.get());
104 
105     return NULL;
106 }
107 
108 size_t
109 SBCommandReturnObject::GetOutputSize ()
110 {
111     if (m_opaque_ap.get())
112         return strlen (m_opaque_ap->GetOutputData());
113     return 0;
114 }
115 
116 size_t
117 SBCommandReturnObject::GetErrorSize ()
118 {
119     if (m_opaque_ap.get())
120         return strlen(m_opaque_ap->GetErrorData());
121     return 0;
122 }
123 
124 size_t
125 SBCommandReturnObject::PutOutput (FILE *fh)
126 {
127     if (fh)
128     {
129         size_t num_bytes = GetOutputSize ();
130         if (num_bytes)
131             return ::fprintf (fh, "%s", GetOutput());
132     }
133     return 0;
134 }
135 
136 size_t
137 SBCommandReturnObject::PutError (FILE *fh)
138 {
139     if (fh)
140     {
141         size_t num_bytes = GetErrorSize ();
142         if (num_bytes)
143             return ::fprintf (fh, "%s", GetError());
144     }
145     return 0;
146 }
147 
148 void
149 SBCommandReturnObject::Clear()
150 {
151     if (m_opaque_ap.get())
152         m_opaque_ap->Clear();
153 }
154 
155 lldb::ReturnStatus
156 SBCommandReturnObject::GetStatus()
157 {
158     if (m_opaque_ap.get())
159         return m_opaque_ap->GetStatus();
160     return lldb::eReturnStatusInvalid;
161 }
162 
163 void
164 SBCommandReturnObject::SetStatus(lldb::ReturnStatus status)
165 {
166     if (m_opaque_ap.get())
167          m_opaque_ap->SetStatus(status);
168 }
169 
170 bool
171 SBCommandReturnObject::Succeeded ()
172 {
173     if (m_opaque_ap.get())
174         return m_opaque_ap->Succeeded();
175     return false;
176 }
177 
178 bool
179 SBCommandReturnObject::HasResult ()
180 {
181     if (m_opaque_ap.get())
182         return m_opaque_ap->HasResult();
183     return false;
184 }
185 
186 void
187 SBCommandReturnObject::AppendMessage (const char *message)
188 {
189     if (m_opaque_ap.get())
190         m_opaque_ap->AppendMessage (message);
191 }
192 
193 CommandReturnObject *
194 SBCommandReturnObject::operator ->() const
195 {
196     return m_opaque_ap.get();
197 }
198 
199 CommandReturnObject *
200 SBCommandReturnObject::get() const
201 {
202     return m_opaque_ap.get();
203 }
204 
205 CommandReturnObject &
206 SBCommandReturnObject::operator *() const
207 {
208     assert(m_opaque_ap.get());
209     return *(m_opaque_ap.get());
210 }
211 
212 
213 CommandReturnObject &
214 SBCommandReturnObject::ref() const
215 {
216     assert(m_opaque_ap.get());
217     return *(m_opaque_ap.get());
218 }
219 
220 
221 void
222 SBCommandReturnObject::SetLLDBObjectPtr (CommandReturnObject *ptr)
223 {
224     if (m_opaque_ap.get())
225         m_opaque_ap.reset (ptr);
226 }
227 
228 bool
229 SBCommandReturnObject::GetDescription (SBStream &description)
230 {
231     Stream &strm = description.ref();
232 
233     if (m_opaque_ap.get())
234     {
235         description.Printf ("Status:  ");
236         lldb::ReturnStatus status = m_opaque_ap->GetStatus();
237         if (status == lldb::eReturnStatusStarted)
238             strm.PutCString ("Started");
239         else if (status == lldb::eReturnStatusInvalid)
240             strm.PutCString ("Invalid");
241         else if (m_opaque_ap->Succeeded())
242             strm.PutCString ("Success");
243         else
244             strm.PutCString ("Fail");
245 
246         if (GetOutputSize() > 0)
247             strm.Printf ("\nOutput Message:\n%s", GetOutput());
248 
249         if (GetErrorSize() > 0)
250             strm.Printf ("\nError Message:\n%s", GetError());
251     }
252     else
253         strm.PutCString ("No value");
254 
255     return true;
256 }
257 
258 void
259 SBCommandReturnObject::SetImmediateOutputFile (FILE *fh)
260 {
261     if (m_opaque_ap.get())
262         m_opaque_ap->SetImmediateOutputFile (fh);
263 }
264 
265 void
266 SBCommandReturnObject::SetImmediateErrorFile (FILE *fh)
267 {
268     if (m_opaque_ap.get())
269         m_opaque_ap->SetImmediateErrorFile (fh);
270 }
271 
272 void
273 SBCommandReturnObject::PutCString(const char* string, int len)
274 {
275     if (m_opaque_ap.get())
276     {
277         m_opaque_ap->AppendMessage(string, len);
278     }
279 }
280 
281 size_t
282 SBCommandReturnObject::Printf(const char* format, ...)
283 {
284     if (m_opaque_ap.get())
285     {
286         va_list args;
287         va_start (args, format);
288         size_t result = m_opaque_ap->GetOutputStream().PrintfVarArg(format, args);
289         va_end (args);
290         return result;
291     }
292     return 0;
293 }
294 
295