1*c2c66affSColin Finck /*
2*c2c66affSColin Finck  * regexpl - Console Registry Explorer
3*c2c66affSColin Finck  *
4*c2c66affSColin Finck  * Copyright (C) 2000-2005 Nedko Arnaudov <nedko@users.sourceforge.net>
5*c2c66affSColin Finck  *
6*c2c66affSColin Finck  * This program is free software; you can redistribute it and/or modify
7*c2c66affSColin Finck  * it under the terms of the GNU General Public License as published by
8*c2c66affSColin Finck  * the Free Software Foundation; either version 2 of the License, or
9*c2c66affSColin Finck  * (at your option) any later version.
10*c2c66affSColin Finck  *
11*c2c66affSColin Finck  * This program is distributed in the hope that it will be useful,
12*c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*c2c66affSColin Finck  * GNU General Public License for more details.
15*c2c66affSColin Finck  *
16*c2c66affSColin Finck  * You should have received a copy of the GNU General Public License
17*c2c66affSColin Finck  * along with this program; see the file COPYING.  If not, write to
18*c2c66affSColin Finck  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19*c2c66affSColin Finck  * Boston, MA 02111-1307, USA.
20*c2c66affSColin Finck  */
21*c2c66affSColin Finck 
22*c2c66affSColin Finck // TextHistory.cpp: implementation of the CTextHistory class.
23*c2c66affSColin Finck //
24*c2c66affSColin Finck //////////////////////////////////////////////////////////////////////
25*c2c66affSColin Finck 
26*c2c66affSColin Finck #include "ph.h"
27*c2c66affSColin Finck #include "TextHistory.h"
28*c2c66affSColin Finck 
29*c2c66affSColin Finck //////////////////////////////////////////////////////////////////////
30*c2c66affSColin Finck // Construction/Destruction
31*c2c66affSColin Finck //////////////////////////////////////////////////////////////////////
32*c2c66affSColin Finck 
CTextHistory()33*c2c66affSColin Finck CTextHistory::CTextHistory()
34*c2c66affSColin Finck {
35*c2c66affSColin Finck 	m_pHistoryBuffer = NULL;
36*c2c66affSColin Finck 	m_dwMaxHistoryLines = 0;
37*c2c66affSColin Finck }
38*c2c66affSColin Finck 
~CTextHistory()39*c2c66affSColin Finck CTextHistory::~CTextHistory()
40*c2c66affSColin Finck {
41*c2c66affSColin Finck 	if (m_pHistoryBuffer) delete[] m_pHistoryBuffer;
42*c2c66affSColin Finck }
43*c2c66affSColin Finck 
Init(DWORD dwMaxHistoryLineSize,DWORD dwMaxHistoryLines)44*c2c66affSColin Finck BOOL CTextHistory::Init(DWORD dwMaxHistoryLineSize, DWORD dwMaxHistoryLines)
45*c2c66affSColin Finck {
46*c2c66affSColin Finck 	if (!dwMaxHistoryLines)
47*c2c66affSColin Finck 	{
48*c2c66affSColin Finck 		ASSERT(FALSE);
49*c2c66affSColin Finck 		return FALSE;
50*c2c66affSColin Finck 	}
51*c2c66affSColin Finck 	if (m_pHistoryBuffer) delete[] m_pHistoryBuffer;
52*c2c66affSColin Finck 	m_dwFirstHistoryIndex = 0;
53*c2c66affSColin Finck 	m_dwLastHistoryIndex = 0;
54*c2c66affSColin Finck 	m_dwHisoryFull = 0;
55*c2c66affSColin Finck 	m_dwMaxHistoryLines = dwMaxHistoryLines;
56*c2c66affSColin Finck 	m_dwMaxHistoryLineSize = dwMaxHistoryLineSize;
57*c2c66affSColin Finck 	m_pHistoryBuffer = new (std::nothrow) TCHAR [m_dwMaxHistoryLines*dwMaxHistoryLineSize];
58*c2c66affSColin Finck 	if (!m_pHistoryBuffer) return FALSE;
59*c2c66affSColin Finck 	return TRUE;
60*c2c66affSColin Finck }
61*c2c66affSColin Finck 
AddHistoryLine(const TCHAR * pchLine)62*c2c66affSColin Finck void CTextHistory::AddHistoryLine(const TCHAR *pchLine)
63*c2c66affSColin Finck {
64*c2c66affSColin Finck 	if (!m_pHistoryBuffer) return;
65*c2c66affSColin Finck 	if (_tcslen(pchLine) == 0) return;
66*c2c66affSColin Finck 	if (_tcslen(pchLine) >= m_dwMaxHistoryLineSize)
67*c2c66affSColin Finck 	{
68*c2c66affSColin Finck 		ASSERT(FALSE);
69*c2c66affSColin Finck 		return;
70*c2c66affSColin Finck 	}
71*c2c66affSColin Finck 	if (m_dwHisoryFull == m_dwMaxHistoryLines)	// if buffer is full, replace last
72*c2c66affSColin Finck 	{
73*c2c66affSColin Finck 		ASSERT(m_dwFirstHistoryIndex == m_dwLastHistoryIndex);
74*c2c66affSColin Finck 		m_dwLastHistoryIndex = (m_dwLastHistoryIndex+1)%m_dwMaxHistoryLines;
75*c2c66affSColin Finck 	}
76*c2c66affSColin Finck 	ASSERT(m_dwFirstHistoryIndex < m_dwMaxHistoryLines);
77*c2c66affSColin Finck 	_tcscpy(m_pHistoryBuffer+m_dwFirstHistoryIndex*m_dwMaxHistoryLineSize,pchLine);
78*c2c66affSColin Finck 	m_dwFirstHistoryIndex = (m_dwFirstHistoryIndex+1)%m_dwMaxHistoryLines;
79*c2c66affSColin Finck 	ASSERT(m_dwHisoryFull <= m_dwMaxHistoryLines);
80*c2c66affSColin Finck 	if (m_dwHisoryFull < m_dwMaxHistoryLines) m_dwHisoryFull++;
81*c2c66affSColin Finck }
82*c2c66affSColin Finck 
GetHistoryLine(DWORD dwIndex)83*c2c66affSColin Finck const TCHAR * CTextHistory::GetHistoryLine(DWORD dwIndex)
84*c2c66affSColin Finck {
85*c2c66affSColin Finck 	if (!m_pHistoryBuffer) return NULL;
86*c2c66affSColin Finck 	ASSERT(m_dwHisoryFull <= m_dwMaxHistoryLines);
87*c2c66affSColin Finck 	if (dwIndex >= m_dwHisoryFull) return NULL;
88*c2c66affSColin Finck 	dwIndex = m_dwHisoryFull - dwIndex - 1;
89*c2c66affSColin Finck 	dwIndex = (dwIndex+m_dwLastHistoryIndex) % m_dwMaxHistoryLines;
90*c2c66affSColin Finck 	ASSERT(dwIndex < m_dwMaxHistoryLines);
91*c2c66affSColin Finck 	return m_pHistoryBuffer+dwIndex*m_dwMaxHistoryLineSize;
92*c2c66affSColin Finck }
93