SBCommandReturnObject.cpp revision bb04be12f8762500c1c308b0d1b93fe8f69c667f
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
16using namespace lldb;
17using namespace lldb_private;
18
19SBCommandReturnObject::SBCommandReturnObject () :
20    m_opaque_ap (new CommandReturnObject ())
21{
22}
23
24SBCommandReturnObject::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
31SBCommandReturnObject::SBCommandReturnObject (CommandReturnObject *ptr) :
32    m_opaque_ap (ptr)
33{
34}
35
36CommandReturnObject *
37SBCommandReturnObject::Release ()
38{
39    return m_opaque_ap.release();
40}
41
42const SBCommandReturnObject &
43SBCommandReturnObject::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
56SBCommandReturnObject::~SBCommandReturnObject ()
57{
58    // m_opaque_ap will automatically delete any pointer it owns
59}
60
61bool
62SBCommandReturnObject::IsValid() const
63{
64    return m_opaque_ap.get() != NULL;
65}
66
67
68const char *
69SBCommandReturnObject::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
88const char *
89SBCommandReturnObject::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
108size_t
109SBCommandReturnObject::GetOutputSize ()
110{
111    if (m_opaque_ap.get())
112        return strlen (m_opaque_ap->GetOutputData());
113    return 0;
114}
115
116size_t
117SBCommandReturnObject::GetErrorSize ()
118{
119    if (m_opaque_ap.get())
120        return strlen(m_opaque_ap->GetErrorData());
121    return 0;
122}
123
124size_t
125SBCommandReturnObject::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
136size_t
137SBCommandReturnObject::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
148void
149SBCommandReturnObject::Clear()
150{
151    if (m_opaque_ap.get())
152        m_opaque_ap->Clear();
153}
154
155lldb::ReturnStatus
156SBCommandReturnObject::GetStatus()
157{
158    if (m_opaque_ap.get())
159        return m_opaque_ap->GetStatus();
160    return lldb::eReturnStatusInvalid;
161}
162
163void
164SBCommandReturnObject::SetStatus(lldb::ReturnStatus status)
165{
166    if (m_opaque_ap.get())
167         m_opaque_ap->SetStatus(status);
168}
169
170bool
171SBCommandReturnObject::Succeeded ()
172{
173    if (m_opaque_ap.get())
174        return m_opaque_ap->Succeeded();
175    return false;
176}
177
178bool
179SBCommandReturnObject::HasResult ()
180{
181    if (m_opaque_ap.get())
182        return m_opaque_ap->HasResult();
183    return false;
184}
185
186void
187SBCommandReturnObject::AppendMessage (const char *message)
188{
189    if (m_opaque_ap.get())
190        m_opaque_ap->AppendMessage (message);
191}
192
193void
194SBCommandReturnObject::AppendWarning (const char *message)
195{
196    if (m_opaque_ap.get())
197        m_opaque_ap->AppendWarning (message);
198}
199
200CommandReturnObject *
201SBCommandReturnObject::operator ->() const
202{
203    return m_opaque_ap.get();
204}
205
206CommandReturnObject *
207SBCommandReturnObject::get() const
208{
209    return m_opaque_ap.get();
210}
211
212CommandReturnObject &
213SBCommandReturnObject::operator *() const
214{
215    assert(m_opaque_ap.get());
216    return *(m_opaque_ap.get());
217}
218
219
220CommandReturnObject &
221SBCommandReturnObject::ref() const
222{
223    assert(m_opaque_ap.get());
224    return *(m_opaque_ap.get());
225}
226
227
228void
229SBCommandReturnObject::SetLLDBObjectPtr (CommandReturnObject *ptr)
230{
231    if (m_opaque_ap.get())
232        m_opaque_ap.reset (ptr);
233}
234
235bool
236SBCommandReturnObject::GetDescription (SBStream &description)
237{
238    Stream &strm = description.ref();
239
240    if (m_opaque_ap.get())
241    {
242        description.Printf ("Status:  ");
243        lldb::ReturnStatus status = m_opaque_ap->GetStatus();
244        if (status == lldb::eReturnStatusStarted)
245            strm.PutCString ("Started");
246        else if (status == lldb::eReturnStatusInvalid)
247            strm.PutCString ("Invalid");
248        else if (m_opaque_ap->Succeeded())
249            strm.PutCString ("Success");
250        else
251            strm.PutCString ("Fail");
252
253        if (GetOutputSize() > 0)
254            strm.Printf ("\nOutput Message:\n%s", GetOutput());
255
256        if (GetErrorSize() > 0)
257            strm.Printf ("\nError Message:\n%s", GetError());
258    }
259    else
260        strm.PutCString ("No value");
261
262    return true;
263}
264
265void
266SBCommandReturnObject::SetImmediateOutputFile (FILE *fh)
267{
268    if (m_opaque_ap.get())
269        m_opaque_ap->SetImmediateOutputFile (fh);
270}
271
272void
273SBCommandReturnObject::SetImmediateErrorFile (FILE *fh)
274{
275    if (m_opaque_ap.get())
276        m_opaque_ap->SetImmediateErrorFile (fh);
277}
278
279void
280SBCommandReturnObject::PutCString(const char* string, int len)
281{
282    if (m_opaque_ap.get())
283    {
284        if (len == 0)
285        {
286            return;
287        }
288        else if (len > 0)
289        {
290            std::string buffer(string, len);
291            m_opaque_ap->AppendMessage(buffer.c_str());
292        }
293        else
294            m_opaque_ap->AppendMessage(string);
295    }
296}
297
298const char *
299SBCommandReturnObject::GetOutput (bool only_if_no_immediate)
300{
301    if (!m_opaque_ap.get())
302        return NULL;
303    if (only_if_no_immediate == false || m_opaque_ap->GetImmediateOutputStream().get() == NULL)
304        return GetOutput();
305    return NULL;
306}
307
308const char *
309SBCommandReturnObject::GetError (bool only_if_no_immediate)
310{
311    if (!m_opaque_ap.get())
312        return NULL;
313    if (only_if_no_immediate == false || m_opaque_ap->GetImmediateErrorStream().get() == NULL)
314        return GetError();
315    return NULL;
316}
317
318size_t
319SBCommandReturnObject::Printf(const char* format, ...)
320{
321    if (m_opaque_ap.get())
322    {
323        va_list args;
324        va_start (args, format);
325        size_t result = m_opaque_ap->GetOutputStream().PrintfVarArg(format, args);
326        va_end (args);
327        return result;
328    }
329    return 0;
330}
331
332