blob: bbe64b446accfaf5fc4d7ee2220b7893933d8e0a [file] [log] [blame]
Enrico Granata7594f142013-06-17 22:51:50 +00001//===-- CommandHistory.cpp --------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Vince Harron5275aaa2015-01-15 20:08:35 +000010#include <inttypes.h>
11
Enrico Granata7594f142013-06-17 22:51:50 +000012#include "lldb/Interpreter/CommandHistory.h"
Vince Harron5275aaa2015-01-15 20:08:35 +000013#include "lldb/Host/StringConvert.h"
Enrico Granata7594f142013-06-17 22:51:50 +000014
15using namespace lldb;
16using namespace lldb_private;
17
18
19CommandHistory::CommandHistory () :
20 m_mutex(Mutex::eMutexTypeRecursive),
21 m_history()
22{}
23
24CommandHistory::~CommandHistory ()
25{}
26
27size_t
28CommandHistory::GetSize () const
29{
30 Mutex::Locker locker(m_mutex);
31 return m_history.size();
32}
33
34bool
35CommandHistory::IsEmpty () const
36{
37 Mutex::Locker locker(m_mutex);
38 return m_history.empty();
39}
40
41const char*
42CommandHistory::FindString (const char* input_str) const
43{
44 Mutex::Locker locker(m_mutex);
45 if (!input_str)
Ed Masted78c9572014-04-20 00:31:37 +000046 return nullptr;
Enrico Granata7594f142013-06-17 22:51:50 +000047 if (input_str[0] != g_repeat_char)
Ed Masted78c9572014-04-20 00:31:37 +000048 return nullptr;
Enrico Granata7594f142013-06-17 22:51:50 +000049 if (input_str[1] == '-')
50 {
51 bool success;
Vince Harron5275aaa2015-01-15 20:08:35 +000052 size_t idx = StringConvert::ToUInt32 (input_str+2, 0, 0, &success);
Enrico Granata7594f142013-06-17 22:51:50 +000053 if (!success)
Ed Masted78c9572014-04-20 00:31:37 +000054 return nullptr;
Enrico Granata7594f142013-06-17 22:51:50 +000055 if (idx > m_history.size())
Ed Masted78c9572014-04-20 00:31:37 +000056 return nullptr;
Enrico Granata7594f142013-06-17 22:51:50 +000057 idx = m_history.size() - idx;
58 return m_history[idx].c_str();
59
60 }
61 else if (input_str[1] == g_repeat_char)
62 {
63 if (m_history.empty())
Ed Masted78c9572014-04-20 00:31:37 +000064 return nullptr;
Enrico Granata7594f142013-06-17 22:51:50 +000065 else
66 return m_history.back().c_str();
67 }
68 else
69 {
70 bool success;
Vince Harron5275aaa2015-01-15 20:08:35 +000071 uint32_t idx = StringConvert::ToUInt32 (input_str+1, 0, 0, &success);
Enrico Granata7594f142013-06-17 22:51:50 +000072 if (!success)
Ed Masted78c9572014-04-20 00:31:37 +000073 return nullptr;
Enrico Granata7594f142013-06-17 22:51:50 +000074 if (idx >= m_history.size())
Ed Masted78c9572014-04-20 00:31:37 +000075 return nullptr;
Enrico Granata7594f142013-06-17 22:51:50 +000076 return m_history[idx].c_str();
77 }
78}
79
80const char*
81CommandHistory::GetStringAtIndex (size_t idx) const
82{
83 Mutex::Locker locker(m_mutex);
84 if (idx < m_history.size())
85 return m_history[idx].c_str();
Ed Masted78c9572014-04-20 00:31:37 +000086 return nullptr;
Enrico Granata7594f142013-06-17 22:51:50 +000087}
88
89const char*
90CommandHistory::operator [] (size_t idx) const
91{
92 return GetStringAtIndex(idx);
93}
94
95const char*
96CommandHistory::GetRecentmostString () const
97{
98 Mutex::Locker locker(m_mutex);
99 if (m_history.empty())
Ed Masted78c9572014-04-20 00:31:37 +0000100 return nullptr;
Enrico Granata7594f142013-06-17 22:51:50 +0000101 return m_history.back().c_str();
102}
103
104void
105CommandHistory::AppendString (const std::string& str,
106 bool reject_if_dupe)
107{
108 Mutex::Locker locker(m_mutex);
109 if (reject_if_dupe)
110 {
111 if (!m_history.empty())
112 {
113 if (str == m_history.back())
114 return;
115 }
116 }
117 m_history.push_back(std::string(str));
118}
119
120void
121CommandHistory::Clear ()
122{
123 Mutex::Locker locker(m_mutex);
124 m_history.clear();
125}
126
127void
128CommandHistory::Dump (Stream& stream,
129 size_t start_idx,
130 size_t stop_idx) const
131{
132 Mutex::Locker locker(m_mutex);
133 stop_idx = std::min(stop_idx, m_history.size() - 1);
134 for (size_t counter = start_idx;
135 counter <= stop_idx;
136 counter++)
137 {
138 const std::string hist_item = m_history[counter];
139 if (!hist_item.empty())
140 {
141 stream.Indent();
Greg Clayton6fea17e2014-03-03 19:15:20 +0000142 stream.Printf("%4" PRIu64 ": %s\n", (uint64_t)counter, hist_item.c_str());
Enrico Granata7594f142013-06-17 22:51:50 +0000143 }
144 }
145}