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