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 bool
164 SBCommandReturnObject::Succeeded ()
165 {
166     if (m_opaque_ap.get())
167         return m_opaque_ap->Succeeded();
168     return false;
169 }
170 
171 bool
172 SBCommandReturnObject::HasResult ()
173 {
174     if (m_opaque_ap.get())
175         return m_opaque_ap->HasResult();
176     return false;
177 }
178 
179 void
180 SBCommandReturnObject::AppendMessage (const char *message)
181 {
182     if (m_opaque_ap.get())
183         m_opaque_ap->AppendMessage (message);
184 }
185 
186 CommandReturnObject *
187 SBCommandReturnObject::operator ->() const
188 {
189     return m_opaque_ap.get();
190 }
191 
192 CommandReturnObject *
193 SBCommandReturnObject::get() const
194 {
195     return m_opaque_ap.get();
196 }
197 
198 CommandReturnObject &
199 SBCommandReturnObject::operator *() const
200 {
201     assert(m_opaque_ap.get());
202     return *(m_opaque_ap.get());
203 }
204 
205 
206 CommandReturnObject &
207 SBCommandReturnObject::ref() const
208 {
209     assert(m_opaque_ap.get());
210     return *(m_opaque_ap.get());
211 }
212 
213 
214 void
215 SBCommandReturnObject::SetLLDBObjectPtr (CommandReturnObject *ptr)
216 {
217     if (m_opaque_ap.get())
218         m_opaque_ap.reset (ptr);
219 }
220 
221 bool
222 SBCommandReturnObject::GetDescription (SBStream &description)
223 {
224     if (m_opaque_ap.get())
225     {
226         description.Printf ("Status:  ");
227         lldb::ReturnStatus status = m_opaque_ap->GetStatus();
228         if (status == lldb::eReturnStatusStarted)
229             description.Printf ("Started");
230         else if (status == lldb::eReturnStatusInvalid)
231             description.Printf ("Invalid");
232         else if (m_opaque_ap->Succeeded())
233             description.Printf ("Success");
234         else
235             description.Printf ("Fail");
236 
237         if (GetOutputSize() > 0)
238             description.Printf ("\nOutput Message:\n%s", GetOutput());
239 
240         if (GetErrorSize() > 0)
241             description.Printf ("\nError Message:\n%s", GetError());
242     }
243     else
244         description.Printf ("No value");
245 
246     return true;
247 }
248 
249 void
250 SBCommandReturnObject::SetImmediateOutputFile (FILE *fh)
251 {
252     if (m_opaque_ap.get())
253         m_opaque_ap->SetImmediateOutputFile (fh);
254 }
255 
256 void
257 SBCommandReturnObject::SetImmediateErrorFile (FILE *fh)
258 {
259     if (m_opaque_ap.get())
260         m_opaque_ap->SetImmediateErrorFile (fh);
261 }
262 
263 void
264 SBCommandReturnObject::PutCString(const char* string, int len)
265 {
266     if (m_opaque_ap.get())
267     {
268         m_opaque_ap->AppendMessage(string, len);
269     }
270 }
271 
272 size_t
273 SBCommandReturnObject::Printf(const char* format, ...)
274 {
275     if (m_opaque_ap.get())
276     {
277         va_list args;
278         va_start (args, format);
279         size_t result = m_opaque_ap->GetOutputStream().PrintfVarArg(format, args);
280         va_end (args);
281         return result;
282     }
283     return 0;
284 }
285 
286