3 * regexpl - Console Registry Explorer
5 * Copyright (C) 2000-2005 Nedko Arnaudov <nedko@users.sourceforge.net>
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.
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.
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.
23 // TextHistory.cpp: implementation of the CTextHistory class.
25 //////////////////////////////////////////////////////////////////////
28 #include "TextHistory.h"
30 //////////////////////////////////////////////////////////////////////
31 // Construction/Destruction
32 //////////////////////////////////////////////////////////////////////
34 CTextHistory::CTextHistory()
36 m_pHistoryBuffer
= NULL
;
37 m_dwMaxHistoryLines
= 0;
40 CTextHistory::~CTextHistory()
42 if (m_pHistoryBuffer
) delete m_pHistoryBuffer
;
45 BOOL
CTextHistory::Init(DWORD dwMaxHistoryLineSize
, DWORD dwMaxHistoryLines
)
47 if (!dwMaxHistoryLines
)
52 if (m_pHistoryBuffer
) delete m_pHistoryBuffer
;
53 m_dwFirstHistoryIndex
= 0;
54 m_dwLastHistoryIndex
= 0;
56 m_dwMaxHistoryLines
= dwMaxHistoryLines
;
57 m_dwMaxHistoryLineSize
= dwMaxHistoryLineSize
;
58 m_pHistoryBuffer
= new TCHAR
[m_dwMaxHistoryLines
*dwMaxHistoryLineSize
];
59 if (!m_pHistoryBuffer
) return FALSE
;
63 void CTextHistory::AddHistoryLine(const TCHAR
*pchLine
)
65 if (!m_pHistoryBuffer
) return;
66 if (_tcslen(pchLine
) == 0) return;
67 if (_tcslen(pchLine
) >= m_dwMaxHistoryLineSize
)
72 if (m_dwHisoryFull
== m_dwMaxHistoryLines
) // if buffer is full, replace last
74 ASSERT(m_dwFirstHistoryIndex
== m_dwLastHistoryIndex
);
75 m_dwLastHistoryIndex
= (m_dwLastHistoryIndex
+1)%m_dwMaxHistoryLines
;
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
++;
84 const TCHAR
* CTextHistory::GetHistoryLine(DWORD dwIndex
)
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
;