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