Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 1 | //===-- 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 Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 10 | #include <inttypes.h> |
| 11 | |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 12 | #include "lldb/Interpreter/CommandHistory.h" |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 13 | #include "lldb/Host/StringConvert.h" |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 14 | |
| 15 | using namespace lldb; |
| 16 | using namespace lldb_private; |
| 17 | |
| 18 | |
| 19 | CommandHistory::CommandHistory () : |
| 20 | m_mutex(Mutex::eMutexTypeRecursive), |
| 21 | m_history() |
| 22 | {} |
| 23 | |
| 24 | CommandHistory::~CommandHistory () |
| 25 | {} |
| 26 | |
| 27 | size_t |
| 28 | CommandHistory::GetSize () const |
| 29 | { |
| 30 | Mutex::Locker locker(m_mutex); |
| 31 | return m_history.size(); |
| 32 | } |
| 33 | |
| 34 | bool |
| 35 | CommandHistory::IsEmpty () const |
| 36 | { |
| 37 | Mutex::Locker locker(m_mutex); |
| 38 | return m_history.empty(); |
| 39 | } |
| 40 | |
| 41 | const char* |
| 42 | CommandHistory::FindString (const char* input_str) const |
| 43 | { |
| 44 | Mutex::Locker locker(m_mutex); |
| 45 | if (!input_str) |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 46 | return nullptr; |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 47 | if (input_str[0] != g_repeat_char) |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 48 | return nullptr; |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 49 | if (input_str[1] == '-') |
| 50 | { |
| 51 | bool success; |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 52 | size_t idx = StringConvert::ToUInt32 (input_str+2, 0, 0, &success); |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 53 | if (!success) |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 54 | return nullptr; |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 55 | if (idx > m_history.size()) |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 56 | return nullptr; |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 57 | 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 Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 64 | return nullptr; |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 65 | else |
| 66 | return m_history.back().c_str(); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | bool success; |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 71 | uint32_t idx = StringConvert::ToUInt32 (input_str+1, 0, 0, &success); |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 72 | if (!success) |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 73 | return nullptr; |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 74 | if (idx >= m_history.size()) |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 75 | return nullptr; |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 76 | return m_history[idx].c_str(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | const char* |
| 81 | CommandHistory::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 Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 86 | return nullptr; |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | const char* |
| 90 | CommandHistory::operator [] (size_t idx) const |
| 91 | { |
| 92 | return GetStringAtIndex(idx); |
| 93 | } |
| 94 | |
| 95 | const char* |
| 96 | CommandHistory::GetRecentmostString () const |
| 97 | { |
| 98 | Mutex::Locker locker(m_mutex); |
| 99 | if (m_history.empty()) |
Ed Maste | d78c957 | 2014-04-20 00:31:37 +0000 | [diff] [blame] | 100 | return nullptr; |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 101 | return m_history.back().c_str(); |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | CommandHistory::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 | |
| 120 | void |
| 121 | CommandHistory::Clear () |
| 122 | { |
| 123 | Mutex::Locker locker(m_mutex); |
| 124 | m_history.clear(); |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | CommandHistory::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 Clayton | 6fea17e | 2014-03-03 19:15:20 +0000 | [diff] [blame] | 142 | stream.Printf("%4" PRIu64 ": %s\n", (uint64_t)counter, hist_item.c_str()); |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | } |