1 //===-- CommandHistory.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 //===----------------------------------------------------------------------===//
12 #include "lldb/Interpreter/CommandHistory.h"
13 #include "lldb/Host/StringConvert.h"
16 using namespace lldb_private;
18 CommandHistory::CommandHistory() : m_mutex(), m_history()
21 CommandHistory::~CommandHistory ()
25 CommandHistory::GetSize () const
27 std::lock_guard<std::recursive_mutex> guard(m_mutex);
28 return m_history.size();
32 CommandHistory::IsEmpty () const
34 std::lock_guard<std::recursive_mutex> guard(m_mutex);
35 return m_history.empty();
39 CommandHistory::FindString (const char* input_str) const
41 std::lock_guard<std::recursive_mutex> guard(m_mutex);
44 if (input_str[0] != g_repeat_char)
46 if (input_str[1] == '-')
49 size_t idx = StringConvert::ToUInt32 (input_str+2, 0, 0, &success);
52 if (idx > m_history.size())
54 idx = m_history.size() - idx;
55 return m_history[idx].c_str();
58 else if (input_str[1] == g_repeat_char)
60 if (m_history.empty())
63 return m_history.back().c_str();
68 uint32_t idx = StringConvert::ToUInt32 (input_str+1, 0, 0, &success);
71 if (idx >= m_history.size())
73 return m_history[idx].c_str();
78 CommandHistory::GetStringAtIndex (size_t idx) const
80 std::lock_guard<std::recursive_mutex> guard(m_mutex);
81 if (idx < m_history.size())
82 return m_history[idx].c_str();
87 CommandHistory::operator [] (size_t idx) const
89 return GetStringAtIndex(idx);
93 CommandHistory::GetRecentmostString () const
95 std::lock_guard<std::recursive_mutex> guard(m_mutex);
96 if (m_history.empty())
98 return m_history.back().c_str();
102 CommandHistory::AppendString (const std::string& str,
105 std::lock_guard<std::recursive_mutex> guard(m_mutex);
108 if (!m_history.empty())
110 if (str == m_history.back())
114 m_history.push_back(std::string(str));
118 CommandHistory::Clear ()
120 std::lock_guard<std::recursive_mutex> guard(m_mutex);
125 CommandHistory::Dump (Stream& stream,
127 size_t stop_idx) const
129 std::lock_guard<std::recursive_mutex> guard(m_mutex);
130 stop_idx = std::min(stop_idx + 1, m_history.size());
131 for (size_t counter = start_idx;
135 const std::string hist_item = m_history[counter];
136 if (!hist_item.empty())
139 stream.Printf("%4" PRIu64 ": %s\n", (uint64_t)counter, hist_item.c_str());