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/SBError.h"
12#include "lldb/API/SBStream.h"
13
14#include "lldb/Core/Error.h"
15#include "lldb/Core/Log.h"
16#include "lldb/Interpreter/CommandReturnObject.h"
17
18using namespace lldb;
19using namespace lldb_private;
20
21SBCommandReturnObject::SBCommandReturnObject () :
22    m_opaque_ap (new CommandReturnObject ())
23{
24}
25
26SBCommandReturnObject::SBCommandReturnObject (const SBCommandReturnObject &rhs):
27    m_opaque_ap ()
28{
29    if (rhs.m_opaque_ap.get())
30        m_opaque_ap.reset (new CommandReturnObject (*rhs.m_opaque_ap));
31}
32
33SBCommandReturnObject::SBCommandReturnObject (CommandReturnObject *ptr) :
34    m_opaque_ap (ptr)
35{
36}
37
38CommandReturnObject *
39SBCommandReturnObject::Release ()
40{
41    return m_opaque_ap.release();
42}
43
44const SBCommandReturnObject &
45SBCommandReturnObject::operator = (const SBCommandReturnObject &rhs)
46{
47    if (this != &rhs)
48    {
49        if (rhs.m_opaque_ap.get())
50            m_opaque_ap.reset (new CommandReturnObject (*rhs.m_opaque_ap));
51        else
52            m_opaque_ap.reset();
53    }
54    return *this;
55}
56
57
58SBCommandReturnObject::~SBCommandReturnObject ()
59{
60    // m_opaque_ap will automatically delete any pointer it owns
61}
62
63bool
64SBCommandReturnObject::IsValid() const
65{
66    return m_opaque_ap.get() != NULL;
67}
68
69
70const char *
71SBCommandReturnObject::GetOutput ()
72{
73    Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
74
75    if (m_opaque_ap.get())
76    {
77        if (log)
78            log->Printf ("SBCommandReturnObject(%p)::GetOutput () => \"%s\"", m_opaque_ap.get(),
79                         m_opaque_ap->GetOutputData());
80
81        return m_opaque_ap->GetOutputData();
82    }
83
84    if (log)
85        log->Printf ("SBCommandReturnObject(%p)::GetOutput () => NULL", m_opaque_ap.get());
86
87    return NULL;
88}
89
90const char *
91SBCommandReturnObject::GetError ()
92{
93    Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
94
95    if (m_opaque_ap.get())
96    {
97        if (log)
98            log->Printf ("SBCommandReturnObject(%p)::GetError () => \"%s\"", m_opaque_ap.get(),
99                         m_opaque_ap->GetErrorData());
100
101        return m_opaque_ap->GetErrorData();
102    }
103
104    if (log)
105        log->Printf ("SBCommandReturnObject(%p)::GetError () => NULL", m_opaque_ap.get());
106
107    return NULL;
108}
109
110size_t
111SBCommandReturnObject::GetOutputSize ()
112{
113    if (m_opaque_ap.get())
114        return strlen (m_opaque_ap->GetOutputData());
115    return 0;
116}
117
118size_t
119SBCommandReturnObject::GetErrorSize ()
120{
121    if (m_opaque_ap.get())
122        return strlen(m_opaque_ap->GetErrorData());
123    return 0;
124}
125
126size_t
127SBCommandReturnObject::PutOutput (FILE *fh)
128{
129    if (fh)
130    {
131        size_t num_bytes = GetOutputSize ();
132        if (num_bytes)
133            return ::fprintf (fh, "%s", GetOutput());
134    }
135    return 0;
136}
137
138size_t
139SBCommandReturnObject::PutError (FILE *fh)
140{
141    if (fh)
142    {
143        size_t num_bytes = GetErrorSize ();
144        if (num_bytes)
145            return ::fprintf (fh, "%s", GetError());
146    }
147    return 0;
148}
149
150void
151SBCommandReturnObject::Clear()
152{
153    if (m_opaque_ap.get())
154        m_opaque_ap->Clear();
155}
156
157lldb::ReturnStatus
158SBCommandReturnObject::GetStatus()
159{
160    if (m_opaque_ap.get())
161        return m_opaque_ap->GetStatus();
162    return lldb::eReturnStatusInvalid;
163}
164
165void
166SBCommandReturnObject::SetStatus(lldb::ReturnStatus status)
167{
168    if (m_opaque_ap.get())
169         m_opaque_ap->SetStatus(status);
170}
171
172bool
173SBCommandReturnObject::Succeeded ()
174{
175    if (m_opaque_ap.get())
176        return m_opaque_ap->Succeeded();
177    return false;
178}
179
180bool
181SBCommandReturnObject::HasResult ()
182{
183    if (m_opaque_ap.get())
184        return m_opaque_ap->HasResult();
185    return false;
186}
187
188void
189SBCommandReturnObject::AppendMessage (const char *message)
190{
191    if (m_opaque_ap.get())
192        m_opaque_ap->AppendMessage (message);
193}
194
195void
196SBCommandReturnObject::AppendWarning (const char *message)
197{
198    if (m_opaque_ap.get())
199        m_opaque_ap->AppendWarning (message);
200}
201
202CommandReturnObject *
203SBCommandReturnObject::operator ->() const
204{
205    return m_opaque_ap.get();
206}
207
208CommandReturnObject *
209SBCommandReturnObject::get() const
210{
211    return m_opaque_ap.get();
212}
213
214CommandReturnObject &
215SBCommandReturnObject::operator *() const
216{
217    assert(m_opaque_ap.get());
218    return *(m_opaque_ap.get());
219}
220
221
222CommandReturnObject &
223SBCommandReturnObject::ref() const
224{
225    assert(m_opaque_ap.get());
226    return *(m_opaque_ap.get());
227}
228
229
230void
231SBCommandReturnObject::SetLLDBObjectPtr (CommandReturnObject *ptr)
232{
233    if (m_opaque_ap.get())
234        m_opaque_ap.reset (ptr);
235}
236
237bool
238SBCommandReturnObject::GetDescription (SBStream &description)
239{
240    Stream &strm = description.ref();
241
242    if (m_opaque_ap.get())
243    {
244        description.Printf ("Status:  ");
245        lldb::ReturnStatus status = m_opaque_ap->GetStatus();
246        if (status == lldb::eReturnStatusStarted)
247            strm.PutCString ("Started");
248        else if (status == lldb::eReturnStatusInvalid)
249            strm.PutCString ("Invalid");
250        else if (m_opaque_ap->Succeeded())
251            strm.PutCString ("Success");
252        else
253            strm.PutCString ("Fail");
254
255        if (GetOutputSize() > 0)
256            strm.Printf ("\nOutput Message:\n%s", GetOutput());
257
258        if (GetErrorSize() > 0)
259            strm.Printf ("\nError Message:\n%s", GetError());
260    }
261    else
262        strm.PutCString ("No value");
263
264    return true;
265}
266
267void
268SBCommandReturnObject::SetImmediateOutputFile (FILE *fh)
269{
270    if (m_opaque_ap.get())
271        m_opaque_ap->SetImmediateOutputFile (fh);
272}
273
274void
275SBCommandReturnObject::SetImmediateErrorFile (FILE *fh)
276{
277    if (m_opaque_ap.get())
278        m_opaque_ap->SetImmediateErrorFile (fh);
279}
280
281void
282SBCommandReturnObject::PutCString(const char* string, int len)
283{
284    if (m_opaque_ap.get())
285    {
286        if (len == 0 || string == NULL || *string == 0)
287        {
288            return;
289        }
290        else if (len > 0)
291        {
292            std::string buffer(string, len);
293            m_opaque_ap->AppendMessage(buffer.c_str());
294        }
295        else
296            m_opaque_ap->AppendMessage(string);
297    }
298}
299
300const char *
301SBCommandReturnObject::GetOutput (bool only_if_no_immediate)
302{
303    if (!m_opaque_ap.get())
304        return NULL;
305    if (only_if_no_immediate == false || m_opaque_ap->GetImmediateOutputStream().get() == NULL)
306        return GetOutput();
307    return NULL;
308}
309
310const char *
311SBCommandReturnObject::GetError (bool only_if_no_immediate)
312{
313    if (!m_opaque_ap.get())
314        return NULL;
315    if (only_if_no_immediate == false || m_opaque_ap->GetImmediateErrorStream().get() == NULL)
316        return GetError();
317    return NULL;
318}
319
320size_t
321SBCommandReturnObject::Printf(const char* format, ...)
322{
323    if (m_opaque_ap.get())
324    {
325        va_list args;
326        va_start (args, format);
327        size_t result = m_opaque_ap->GetOutputStream().PrintfVarArg(format, args);
328        va_end (args);
329        return result;
330    }
331    return 0;
332}
333
334void
335SBCommandReturnObject::SetError (lldb::SBError &error, const char *fallback_error_cstr)
336{
337    if (m_opaque_ap.get())
338    {
339        if (error.IsValid())
340            m_opaque_ap->SetError(error.ref(), fallback_error_cstr);
341        else if (fallback_error_cstr)
342            m_opaque_ap->SetError(Error(), fallback_error_cstr);
343    }
344}
345
346void
347SBCommandReturnObject::SetError (const char *error_cstr)
348{
349    if (m_opaque_ap.get() && error_cstr)
350        m_opaque_ap->SetError(error_cstr);
351}
352
353