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 //===----------------------------------------------------------------------===//
10 #include "lldb/Interpreter/CommandHistory.h"
11 #include "lldb/Interpreter/Args.h"
14 using namespace lldb_private;
17 CommandHistory::CommandHistory () :
18 m_mutex(Mutex::eMutexTypeRecursive),
22 CommandHistory::~CommandHistory ()
26 CommandHistory::GetSize () const
28 Mutex::Locker locker(m_mutex);
29 return m_history.size();
33 CommandHistory::IsEmpty () const
35 Mutex::Locker locker(m_mutex);
36 return m_history.empty();
40 CommandHistory::FindString (const char* input_str) const
42 Mutex::Locker locker(m_mutex);
45 if (input_str[0] != g_repeat_char)
47 if (input_str[1] == '-')
50 size_t idx = Args::StringToUInt32 (input_str+2, 0, 0, &success);
53 if (idx > m_history.size())
55 idx = m_history.size() - idx;
56 return m_history[idx].c_str();
59 else if (input_str[1] == g_repeat_char)
61 if (m_history.empty())
64 return m_history.back().c_str();
69 uint32_t idx = Args::StringToUInt32 (input_str+1, 0, 0, &success);
72 if (idx >= m_history.size())
74 return m_history[idx].c_str();
79 CommandHistory::GetStringAtIndex (size_t idx) const
81 Mutex::Locker locker(m_mutex);
82 if (idx < m_history.size())
83 return m_history[idx].c_str();
88 CommandHistory::operator [] (size_t idx) const
90 return GetStringAtIndex(idx);
94 CommandHistory::GetRecentmostString () const
96 Mutex::Locker locker(m_mutex);
97 if (m_history.empty())
99 return m_history.back().c_str();
103 CommandHistory::AppendString (const std::string& str,
106 Mutex::Locker locker(m_mutex);
109 if (!m_history.empty())
111 if (str == m_history.back())
115 m_history.push_back(std::string(str));
119 CommandHistory::Clear ()
121 Mutex::Locker locker(m_mutex);
126 CommandHistory::Dump (Stream& stream,
128 size_t stop_idx) const
130 Mutex::Locker locker(m_mutex);
131 stop_idx = std::min(stop_idx, m_history.size() - 1);
132 for (size_t counter = start_idx;
136 const std::string hist_item = m_history[counter];
137 if (!hist_item.empty())
140 stream.Printf ("%4zu: %s\n", counter, hist_item.c_str());