1 //===-- SBStream.cpp ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/API/SBStream.h"
12 #include "lldb/Core/Error.h"
13 #include "lldb/Core/Stream.h"
14 #include "lldb/Core/StreamFile.h"
15 #include "lldb/Core/StreamString.h"
18 using namespace lldb_private;
20 SBStream::SBStream () :
21 m_opaque_ap (new StreamString()),
26 SBStream::~SBStream ()
31 SBStream::IsValid() const
33 return (m_opaque_ap.get() != NULL);
36 // If this stream is not redirected to a file, it will maintain a local
37 // cache for the stream data which can be accessed using this accessor.
41 if (m_is_file || m_opaque_ap.get() == NULL)
44 return static_cast<StreamString *>(m_opaque_ap.get())->GetData();
47 // If this stream is not redirected to a file, it will maintain a local
48 // cache for the stream output whose length can be accessed using this
53 if (m_is_file || m_opaque_ap.get() == NULL)
56 return static_cast<StreamString *>(m_opaque_ap.get())->GetSize();
60 SBStream::Printf (const char *format, ...)
65 va_start (args, format);
66 ref().PrintfVarArg (format, args);
71 SBStream::RedirectToFile (const char *path, bool append)
73 std::string local_data;
74 if (m_opaque_ap.get())
76 // See if we have any locally backed data. If so, copy it so we can then
77 // redirect it to the file so we don't lose the data
79 local_data.swap(static_cast<StreamString *>(m_opaque_ap.get())->GetString());
81 StreamFile *stream_file = new StreamFile;
82 uint32_t open_options = File::eOpenOptionWrite | File::eOpenOptionCanCreate;
84 open_options |= File::eOpenOptionAppend;
85 stream_file->GetFile().Open (path, open_options, File::ePermissionsDefault);
87 m_opaque_ap.reset (stream_file);
89 if (m_opaque_ap.get())
93 // If we had any data locally in our StreamString, then pass that along to
94 // the to new file we are redirecting to.
95 if (!local_data.empty())
96 m_opaque_ap->Write (&local_data[0], local_data.size());
103 SBStream::RedirectToFileHandle (FILE *fh, bool transfer_fh_ownership)
105 std::string local_data;
106 if (m_opaque_ap.get())
108 // See if we have any locally backed data. If so, copy it so we can then
109 // redirect it to the file so we don't lose the data
111 local_data.swap(static_cast<StreamString *>(m_opaque_ap.get())->GetString());
113 m_opaque_ap.reset (new StreamFile (fh, transfer_fh_ownership));
115 if (m_opaque_ap.get())
119 // If we had any data locally in our StreamString, then pass that along to
120 // the to new file we are redirecting to.
121 if (!local_data.empty())
122 m_opaque_ap->Write (&local_data[0], local_data.size());
129 SBStream::RedirectToFileDescriptor (int fd, bool transfer_fh_ownership)
131 std::string local_data;
132 if (m_opaque_ap.get())
134 // See if we have any locally backed data. If so, copy it so we can then
135 // redirect it to the file so we don't lose the data
137 local_data.swap(static_cast<StreamString *>(m_opaque_ap.get())->GetString());
140 m_opaque_ap.reset (new StreamFile (::fdopen (fd, "w"), transfer_fh_ownership));
141 if (m_opaque_ap.get())
145 // If we had any data locally in our StreamString, then pass that along to
146 // the to new file we are redirecting to.
147 if (!local_data.empty())
148 m_opaque_ap->Write (&local_data[0], local_data.size());
155 lldb_private::Stream *
156 SBStream::operator->()
158 return m_opaque_ap.get();
161 lldb_private::Stream *
164 return m_opaque_ap.get();
167 lldb_private::Stream &
170 if (m_opaque_ap.get() == NULL)
171 m_opaque_ap.reset (new StreamString());
172 return *m_opaque_ap.get();
178 if (m_opaque_ap.get())
180 // See if we have any locally backed data. If so, copy it so we can then
181 // redirect it to the file so we don't lose the data
185 static_cast<StreamString *>(m_opaque_ap.get())->GetString().clear();