1ac7ddfbfSEd Maste //===-- SBStream.cpp ----------------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste // The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste
10ac7ddfbfSEd Maste #include "lldb/API/SBStream.h"
11ac7ddfbfSEd Maste
12ac7ddfbfSEd Maste #include "lldb/Core/StreamFile.h"
13*b5893f02SDimitry Andric #include "lldb/Host/FileSystem.h"
145517e702SDimitry Andric #include "lldb/Utility/Status.h"
15f678e45dSDimitry Andric #include "lldb/Utility/Stream.h"
16f678e45dSDimitry Andric #include "lldb/Utility/StreamString.h"
17ac7ddfbfSEd Maste
18ac7ddfbfSEd Maste using namespace lldb;
19ac7ddfbfSEd Maste using namespace lldb_private;
20ac7ddfbfSEd Maste
SBStream()21435933ddSDimitry Andric SBStream::SBStream() : m_opaque_ap(new StreamString()), m_is_file(false) {}
22ac7ddfbfSEd Maste
SBStream(SBStream && rhs)23435933ddSDimitry Andric SBStream::SBStream(SBStream &&rhs)
24435933ddSDimitry Andric : m_opaque_ap(std::move(rhs.m_opaque_ap)), m_is_file(rhs.m_is_file) {}
259f2f44ceSEd Maste
~SBStream()26435933ddSDimitry Andric SBStream::~SBStream() {}
279f2f44ceSEd Maste
IsValid() const28*b5893f02SDimitry Andric bool SBStream::IsValid() const { return (m_opaque_ap != NULL); }
29ac7ddfbfSEd Maste
304ba319b5SDimitry Andric // If this stream is not redirected to a file, it will maintain a local cache
314ba319b5SDimitry Andric // for the stream data which can be accessed using this accessor.
GetData()32435933ddSDimitry Andric const char *SBStream::GetData() {
33*b5893f02SDimitry Andric if (m_is_file || m_opaque_ap == NULL)
34ac7ddfbfSEd Maste return NULL;
35ac7ddfbfSEd Maste
36ac7ddfbfSEd Maste return static_cast<StreamString *>(m_opaque_ap.get())->GetData();
37ac7ddfbfSEd Maste }
38ac7ddfbfSEd Maste
394ba319b5SDimitry Andric // If this stream is not redirected to a file, it will maintain a local cache
404ba319b5SDimitry Andric // for the stream output whose length can be accessed using this accessor.
GetSize()41435933ddSDimitry Andric size_t SBStream::GetSize() {
42*b5893f02SDimitry Andric if (m_is_file || m_opaque_ap == NULL)
43ac7ddfbfSEd Maste return 0;
44ac7ddfbfSEd Maste
45ac7ddfbfSEd Maste return static_cast<StreamString *>(m_opaque_ap.get())->GetSize();
46ac7ddfbfSEd Maste }
47ac7ddfbfSEd Maste
Printf(const char * format,...)48435933ddSDimitry Andric void SBStream::Printf(const char *format, ...) {
49ac7ddfbfSEd Maste if (!format)
50ac7ddfbfSEd Maste return;
51ac7ddfbfSEd Maste va_list args;
52ac7ddfbfSEd Maste va_start(args, format);
53ac7ddfbfSEd Maste ref().PrintfVarArg(format, args);
54ac7ddfbfSEd Maste va_end(args);
55ac7ddfbfSEd Maste }
56ac7ddfbfSEd Maste
RedirectToFile(const char * path,bool append)57435933ddSDimitry Andric void SBStream::RedirectToFile(const char *path, bool append) {
581c3bbb01SEd Maste if (path == nullptr)
591c3bbb01SEd Maste return;
601c3bbb01SEd Maste
61ac7ddfbfSEd Maste std::string local_data;
62*b5893f02SDimitry Andric if (m_opaque_ap) {
63ac7ddfbfSEd Maste // See if we have any locally backed data. If so, copy it so we can then
64ac7ddfbfSEd Maste // redirect it to the file so we don't lose the data
65ac7ddfbfSEd Maste if (!m_is_file)
66435933ddSDimitry Andric local_data = static_cast<StreamString *>(m_opaque_ap.get())->GetString();
67ac7ddfbfSEd Maste }
68ac7ddfbfSEd Maste StreamFile *stream_file = new StreamFile;
69ac7ddfbfSEd Maste uint32_t open_options = File::eOpenOptionWrite | File::eOpenOptionCanCreate;
70ac7ddfbfSEd Maste if (append)
71ac7ddfbfSEd Maste open_options |= File::eOpenOptionAppend;
720127ef0fSEd Maste else
730127ef0fSEd Maste open_options |= File::eOpenOptionTruncate;
74ac7ddfbfSEd Maste
75*b5893f02SDimitry Andric FileSystem::Instance().Open(stream_file->GetFile(), FileSpec(path),
76*b5893f02SDimitry Andric open_options);
77ac7ddfbfSEd Maste m_opaque_ap.reset(stream_file);
78ac7ddfbfSEd Maste
79*b5893f02SDimitry Andric if (m_opaque_ap) {
80ac7ddfbfSEd Maste m_is_file = true;
81ac7ddfbfSEd Maste
82ac7ddfbfSEd Maste // If we had any data locally in our StreamString, then pass that along to
83ac7ddfbfSEd Maste // the to new file we are redirecting to.
84ac7ddfbfSEd Maste if (!local_data.empty())
85ac7ddfbfSEd Maste m_opaque_ap->Write(&local_data[0], local_data.size());
86435933ddSDimitry Andric } else
87ac7ddfbfSEd Maste m_is_file = false;
88ac7ddfbfSEd Maste }
89ac7ddfbfSEd Maste
RedirectToFileHandle(FILE * fh,bool transfer_fh_ownership)90435933ddSDimitry Andric void SBStream::RedirectToFileHandle(FILE *fh, bool transfer_fh_ownership) {
911c3bbb01SEd Maste if (fh == nullptr)
921c3bbb01SEd Maste return;
931c3bbb01SEd Maste
94ac7ddfbfSEd Maste std::string local_data;
95*b5893f02SDimitry Andric if (m_opaque_ap) {
96ac7ddfbfSEd Maste // See if we have any locally backed data. If so, copy it so we can then
97ac7ddfbfSEd Maste // redirect it to the file so we don't lose the data
98ac7ddfbfSEd Maste if (!m_is_file)
99435933ddSDimitry Andric local_data = static_cast<StreamString *>(m_opaque_ap.get())->GetString();
100ac7ddfbfSEd Maste }
101ac7ddfbfSEd Maste m_opaque_ap.reset(new StreamFile(fh, transfer_fh_ownership));
102ac7ddfbfSEd Maste
103*b5893f02SDimitry Andric if (m_opaque_ap) {
104ac7ddfbfSEd Maste m_is_file = true;
105ac7ddfbfSEd Maste
106ac7ddfbfSEd Maste // If we had any data locally in our StreamString, then pass that along to
107ac7ddfbfSEd Maste // the to new file we are redirecting to.
108ac7ddfbfSEd Maste if (!local_data.empty())
109ac7ddfbfSEd Maste m_opaque_ap->Write(&local_data[0], local_data.size());
110435933ddSDimitry Andric } else
111ac7ddfbfSEd Maste m_is_file = false;
112ac7ddfbfSEd Maste }
113ac7ddfbfSEd Maste
RedirectToFileDescriptor(int fd,bool transfer_fh_ownership)114435933ddSDimitry Andric void SBStream::RedirectToFileDescriptor(int fd, bool transfer_fh_ownership) {
115ac7ddfbfSEd Maste std::string local_data;
116*b5893f02SDimitry Andric if (m_opaque_ap) {
117ac7ddfbfSEd Maste // See if we have any locally backed data. If so, copy it so we can then
118ac7ddfbfSEd Maste // redirect it to the file so we don't lose the data
119ac7ddfbfSEd Maste if (!m_is_file)
120435933ddSDimitry Andric local_data = static_cast<StreamString *>(m_opaque_ap.get())->GetString();
121ac7ddfbfSEd Maste }
122ac7ddfbfSEd Maste
123ac7ddfbfSEd Maste m_opaque_ap.reset(new StreamFile(::fdopen(fd, "w"), transfer_fh_ownership));
124*b5893f02SDimitry Andric if (m_opaque_ap) {
125ac7ddfbfSEd Maste m_is_file = true;
126ac7ddfbfSEd Maste
127ac7ddfbfSEd Maste // If we had any data locally in our StreamString, then pass that along to
128ac7ddfbfSEd Maste // the to new file we are redirecting to.
129ac7ddfbfSEd Maste if (!local_data.empty())
130ac7ddfbfSEd Maste m_opaque_ap->Write(&local_data[0], local_data.size());
131435933ddSDimitry Andric } else
132ac7ddfbfSEd Maste m_is_file = false;
133ac7ddfbfSEd Maste }
134ac7ddfbfSEd Maste
operator ->()135435933ddSDimitry Andric lldb_private::Stream *SBStream::operator->() { return m_opaque_ap.get(); }
136ac7ddfbfSEd Maste
get()137435933ddSDimitry Andric lldb_private::Stream *SBStream::get() { return m_opaque_ap.get(); }
138ac7ddfbfSEd Maste
ref()139435933ddSDimitry Andric lldb_private::Stream &SBStream::ref() {
140*b5893f02SDimitry Andric if (m_opaque_ap == NULL)
141ac7ddfbfSEd Maste m_opaque_ap.reset(new StreamString());
142*b5893f02SDimitry Andric return *m_opaque_ap;
143ac7ddfbfSEd Maste }
144ac7ddfbfSEd Maste
Clear()145435933ddSDimitry Andric void SBStream::Clear() {
146*b5893f02SDimitry Andric if (m_opaque_ap) {
147ac7ddfbfSEd Maste // See if we have any locally backed data. If so, copy it so we can then
148ac7ddfbfSEd Maste // redirect it to the file so we don't lose the data
149ac7ddfbfSEd Maste if (m_is_file)
150ac7ddfbfSEd Maste m_opaque_ap.reset();
151ac7ddfbfSEd Maste else
152435933ddSDimitry Andric static_cast<StreamString *>(m_opaque_ap.get())->Clear();
153ac7ddfbfSEd Maste }
154ac7ddfbfSEd Maste }
155