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;
19 CommandHistory::CommandHistory () :
20 m_mutex(Mutex::eMutexTypeRecursive),
24 CommandHistory::~CommandHistory ()
28 CommandHistory::GetSize () const
30 Mutex::Locker locker(m_mutex);
31 return m_history.size();
35 CommandHistory::IsEmpty () const
37 Mutex::Locker locker(m_mutex);
38 return m_history.empty();
42 CommandHistory::FindString (const char* input_str) const
44 Mutex::Locker locker(m_mutex);
47 if (input_str[0] != g_repeat_char)
49 if (input_str[1] == '-')
52 size_t idx = StringConvert::ToUInt32 (input_str+2, 0, 0, &success);
55 if (idx > m_history.size())
57 idx = m_history.size() - idx;
58 return m_history[idx].c_str();
61 else if (input_str[1] == g_repeat_char)
63 if (m_history.empty())
66 return m_history.back().c_str();
71 uint32_t idx = StringConvert::ToUInt32 (input_str+1, 0, 0, &success);
74 if (idx >= m_history.size())
76 return m_history[idx].c_str();
81 CommandHistory::GetStringAtIndex (size_t idx) const
83 Mutex::Locker locker(m_mutex);
84 if (idx < m_history.size())
85 return m_history[idx].c_str();
90 CommandHistory::operator [] (size_t idx) const
92 return GetStringAtIndex(idx);
96 CommandHistory::GetRecentmostString () const
98 Mutex::Locker locker(m_mutex);
99 if (m_history.empty())
101 return m_history.back().c_str();
105 CommandHistory::AppendString (const std::string& str,
108 Mutex::Locker locker(m_mutex);
111 if (!m_history.empty())
113 if (str == m_history.back())
117 m_history.push_back(std::string(str));
121 CommandHistory::Clear ()
123 Mutex::Locker locker(m_mutex);
128 CommandHistory::Dump (Stream& stream,
130 size_t stop_idx) const
132 Mutex::Locker locker(m_mutex);
133 stop_idx = std::min(stop_idx + 1, m_history.size());
134 for (size_t counter = start_idx;
138 const std::string hist_item = m_history[counter];
139 if (!hist_item.empty())
142 stream.Printf("%4" PRIu64 ": %s\n", (uint64_t)counter, hist_item.c_str());