1 /*
2  * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 #include <windows.h>   // includes basic windows functionality
27 #include <iterator>
28 #include "MessageHistory.h"
29 
30 size_t MessageHistory::sm_MaxMessages = 1000;
31 
AddMessage(const char * message)32 void MessageHistory::AddMessage(const char * message) {
33     if ( ( NULL == message ) || ( 0 == message [0] ) ) {
34         return;
35     }
36 
37     if ( m_Messages.size() >= sm_MaxMessages ) {
38         // Remove the oldest message
39         m_Messages.pop_front();
40     }
41 
42     m_Messages.push_back(std::string (message) );
43     m_CurrentPosition = m_Messages.end();
44     -- m_CurrentPosition;
45 }
46 
GetFirstMessage()47 const char * MessageHistory::GetFirstMessage() {
48     if ( m_Messages.empty() ) {
49         return "";
50     }
51 
52     m_CurrentPosition = m_Messages.begin();
53     return (*m_CurrentPosition).c_str();
54 }
55 
GetPreviousMessage()56 const char * MessageHistory::GetPreviousMessage() {
57     if ( m_Messages.empty() ) {
58         return "";
59     }
60 
61     if ( m_CurrentPosition != m_Messages.begin() ) {
62         -- m_CurrentPosition;
63     }
64 
65     return (*m_CurrentPosition).c_str();
66 }
67 
GetNextMessage()68 const char * MessageHistory::GetNextMessage() {
69     if ( m_Messages.empty() ) {
70         return "";
71     }
72 
73     ++ m_CurrentPosition;
74     if ( m_CurrentPosition == m_Messages.end() ) {
75         -- m_CurrentPosition;
76     }
77 
78     return (*m_CurrentPosition).c_str();
79 }
80 
GetLastMessage()81 const char * MessageHistory::GetLastMessage()
82 {
83     if ( m_Messages.empty() ) {
84         return "";
85     }
86 
87     m_CurrentPosition = m_Messages.end();
88     -- m_CurrentPosition;
89     return (*m_CurrentPosition).c_str();
90 }
91 
IsFirstMessage()92 BOOL MessageHistory::IsFirstMessage() {
93     if ( m_Messages.empty() ) {
94         return FALSE;
95     }
96     if ( m_CurrentPosition == m_Messages.begin() ) {
97         return TRUE;
98     }
99     return FALSE;
100 }
101 
IsLastMessage()102 BOOL MessageHistory::IsLastMessage() {
103     if ( m_Messages.empty() ) {
104         return FALSE;
105     }
106     stringlist::const_iterator itTest = m_Messages.end();
107     -- itTest;
108     if ( itTest == m_CurrentPosition ) {
109         return TRUE;
110     }
111     return FALSE;
112 }
113 
GetMessageCount()114 size_t MessageHistory::GetMessageCount() {
115     size_t ret_val = m_Messages.size();
116     return ret_val;
117 }
118 
GetCurrentMessage()119 const char * MessageHistory::GetCurrentMessage() {
120     if ( m_Messages.empty() ) {
121         return "";
122     }
123 
124     return (*m_CurrentPosition).c_str();
125 }
126 
GetMessage(const size_t messageIndex)127 const char * MessageHistory::GetMessage(const size_t messageIndex) {
128     if ( m_Messages.empty() ) {
129         return "";
130     }
131 
132     if ( messageIndex >= m_Messages.size() ) {
133         return "";
134     }
135 
136     stringlist::const_iterator it = m_Messages.begin();
137     std::advance(it, messageIndex);
138     m_CurrentPosition = it;
139 
140     return (*it).c_str();
141 }
142 
GetCurrentMessageIndex()143 size_t MessageHistory::GetCurrentMessageIndex() {
144     if ( m_Messages.empty() ) {
145         return 0;
146     }
147 
148     stringlist::const_iterator itBegin = m_Messages.begin();
149     size_t ret_val = std::distance(itBegin, m_CurrentPosition);
150     return ret_val;
151 }
152 
clear()153 void MessageHistory::clear() {
154     m_Messages.clear();
155 }
156