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;
86 open_options |= File::eOpenOptionTruncate;
87 stream_file->GetFile().Open (path, open_options, lldb::eFilePermissionsFileDefault);
89 m_opaque_ap.reset (stream_file);
91 if (m_opaque_ap.get())
95 // If we had any data locally in our StreamString, then pass that along to
96 // the to new file we are redirecting to.
97 if (!local_data.empty())
98 m_opaque_ap->Write (&local_data[0], local_data.size());
105 SBStream::RedirectToFileHandle (FILE *fh, bool transfer_fh_ownership)
107 std::string local_data;
108 if (m_opaque_ap.get())
110 // See if we have any locally backed data. If so, copy it so we can then
111 // redirect it to the file so we don't lose the data
113 local_data.swap(static_cast<StreamString *>(m_opaque_ap.get())->GetString());
115 m_opaque_ap.reset (new StreamFile (fh, transfer_fh_ownership));
117 if (m_opaque_ap.get())
121 // If we had any data locally in our StreamString, then pass that along to
122 // the to new file we are redirecting to.
123 if (!local_data.empty())
124 m_opaque_ap->Write (&local_data[0], local_data.size());
131 SBStream::RedirectToFileDescriptor (int fd, bool transfer_fh_ownership)
133 std::string local_data;
134 if (m_opaque_ap.get())
136 // See if we have any locally backed data. If so, copy it so we can then
137 // redirect it to the file so we don't lose the data
139 local_data.swap(static_cast<StreamString *>(m_opaque_ap.get())->GetString());
142 m_opaque_ap.reset (new StreamFile (::fdopen (fd, "w"), transfer_fh_ownership));
143 if (m_opaque_ap.get())
147 // If we had any data locally in our StreamString, then pass that along to
148 // the to new file we are redirecting to.
149 if (!local_data.empty())
150 m_opaque_ap->Write (&local_data[0], local_data.size());
157 lldb_private::Stream *
158 SBStream::operator->()
160 return m_opaque_ap.get();
163 lldb_private::Stream *
166 return m_opaque_ap.get();
169 lldb_private::Stream &
172 if (m_opaque_ap.get() == NULL)
173 m_opaque_ap.reset (new StreamString());
174 return *m_opaque_ap.get();
180 if (m_opaque_ap.get())
182 // See if we have any locally backed data. If so, copy it so we can then
183 // redirect it to the file so we don't lose the data
187 static_cast<StreamString *>(m_opaque_ap.get())->GetString().clear();