124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//===-- StreamString.cpp ----------------------------------------*- C++ -*-===//
224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//
324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//                     The LLVM Compiler Infrastructure
424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//
524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// This file is distributed under the University of Illinois Open Source
624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// License. See LICENSE.TXT for details.
724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//
824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//===----------------------------------------------------------------------===//
924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
1024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Core/StreamString.h"
11a408326b499c3ffdfed2378738598c4ad0cf745fEli Friedman#include <stdio.h>
1224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
1324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerusing namespace lldb;
1424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerusing namespace lldb_private;
1524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
1624943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerStreamString::StreamString () :
1724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    Stream (0, 4, eByteOrderBig)
1824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
1924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
2024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
2124943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerStreamString::StreamString(uint32_t flags, uint32_t addr_size, ByteOrder byte_order) :
2224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    Stream (flags, addr_size, byte_order),
2324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    m_packet ()
2424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
2524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
2624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
2724943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerStreamString::~StreamString()
2824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
2924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
3024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
3124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnervoid
3224943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerStreamString::Flush ()
3324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
3424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    // Nothing to do when flushing a buffer based stream...
3524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
3624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
3736da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Claytonsize_t
3824943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerStreamString::Write (const void *s, size_t length)
3924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
4024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    m_packet.append ((char *)s, length);
4124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    return length;
4224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
4324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
4424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnervoid
4524943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerStreamString::Clear()
4624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
4724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    m_packet.clear();
4824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
4924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
50e23940fece748f8702bb5bc2383af19bb97275ddJohnny Chenbool
51e23940fece748f8702bb5bc2383af19bb97275ddJohnny ChenStreamString::Empty() const
52e23940fece748f8702bb5bc2383af19bb97275ddJohnny Chen{
53e23940fece748f8702bb5bc2383af19bb97275ddJohnny Chen    return GetSize() == 0;
54e23940fece748f8702bb5bc2383af19bb97275ddJohnny Chen}
55e23940fece748f8702bb5bc2383af19bb97275ddJohnny Chen
5624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerconst char *
5724943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerStreamString::GetData () const
5824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
5924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    return m_packet.c_str();
6024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
6124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
6224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnersize_t
6324943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerStreamString::GetSize () const
6424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
6524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    return m_packet.size();
6624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
6724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
6824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerstd::string &
6924943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerStreamString::GetString()
7024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
7124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    return m_packet;
7224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
7324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
7424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerconst std::string &
7524943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerStreamString::GetString() const
7624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
7724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    return m_packet;
7824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
7924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
800fef968c843be422d6facc2e8d54d8471eee88edGreg Claytonvoid
810fef968c843be422d6facc2e8d54d8471eee88edGreg ClaytonStreamString::FillLastLineToColumn (uint32_t column, char fill_char)
820fef968c843be422d6facc2e8d54d8471eee88edGreg Clayton{
830fef968c843be422d6facc2e8d54d8471eee88edGreg Clayton    const size_t length = m_packet.size();
840fef968c843be422d6facc2e8d54d8471eee88edGreg Clayton    size_t last_line_begin_pos = m_packet.find_last_of("\r\n");
850fef968c843be422d6facc2e8d54d8471eee88edGreg Clayton    if (last_line_begin_pos == std::string::npos)
860fef968c843be422d6facc2e8d54d8471eee88edGreg Clayton    {
870fef968c843be422d6facc2e8d54d8471eee88edGreg Clayton        last_line_begin_pos = 0;
880fef968c843be422d6facc2e8d54d8471eee88edGreg Clayton    }
890fef968c843be422d6facc2e8d54d8471eee88edGreg Clayton    else
900fef968c843be422d6facc2e8d54d8471eee88edGreg Clayton    {
910fef968c843be422d6facc2e8d54d8471eee88edGreg Clayton        ++last_line_begin_pos;
920fef968c843be422d6facc2e8d54d8471eee88edGreg Clayton    }
930fef968c843be422d6facc2e8d54d8471eee88edGreg Clayton
940fef968c843be422d6facc2e8d54d8471eee88edGreg Clayton    const size_t line_columns = length - last_line_begin_pos;
950fef968c843be422d6facc2e8d54d8471eee88edGreg Clayton    if (column > line_columns)
960fef968c843be422d6facc2e8d54d8471eee88edGreg Clayton    {
970fef968c843be422d6facc2e8d54d8471eee88edGreg Clayton        m_packet.append(column - line_columns, fill_char);
980fef968c843be422d6facc2e8d54d8471eee88edGreg Clayton    }
990fef968c843be422d6facc2e8d54d8471eee88edGreg Clayton}
1000fef968c843be422d6facc2e8d54d8471eee88edGreg Clayton
101