]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Interpreter/CommandHistory.cpp
Merge ACPICA 20160930.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Interpreter / CommandHistory.cpp
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
10 #include <inttypes.h>
11
12 #include "lldb/Interpreter/CommandHistory.h"
13 #include "lldb/Host/StringConvert.h"
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)
46         return nullptr;
47     if (input_str[0] != g_repeat_char)
48         return nullptr;
49     if (input_str[1] == '-')
50     {
51         bool success;
52         size_t idx = StringConvert::ToUInt32 (input_str+2, 0, 0, &success);
53         if (!success)
54             return nullptr;
55         if (idx > m_history.size())
56             return nullptr;
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())
64             return nullptr;
65         else
66             return m_history.back().c_str();
67     }
68     else
69     {
70         bool success;
71         uint32_t idx = StringConvert::ToUInt32 (input_str+1, 0, 0, &success);
72         if (!success)
73             return nullptr;
74         if (idx >= m_history.size())
75             return nullptr;
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();
86     return nullptr;
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())
100         return nullptr;
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 + 1, m_history.size());
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();
142             stream.Printf("%4" PRIu64 ": %s\n", (uint64_t)counter, hist_item.c_str());
143         }
144     }
145 }