?? memfile.cpp
字號:
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2003, Ulink Telecom Equipment Co., Ltd. All rights reserved.
//
// File:
//
// MemFile.cpp
//
// Abstract:
//
// implementation of the CMemFile class. CMemFile is the CFile-derived class
// that support memory files.
//
// History:
//
// V1.0 2003-03-12 Alex Duan Original version.
// V1.1 2003-11-11 Alex Duan Fixed a bug in Find function when find in
// more than one block.
//
///////////////////////////////////////////////////////////////////////////////
#include "MemFile.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMemFile::CMemFile(UINT nGrowBytes)
{
m_nGrowBytes = nGrowBytes;
}
CMemFile::~CMemFile()
{
Close(); // Call CMemFile::Close()
}
///////////////////////////////////////////////////////////////////////////////
// Parameters:
// lpBuf A pointer to the user-supplied buffer that contains the data
// to be written to the file.
// nCount The number of bytes to be transferred from the buffer.
// Remarks:
// Writes data from a buffer to the file associated with the CFile object.
void CMemFile::Write(const void *lpBuf, UINT nCount)
{
ASSERT(lpBuf != NULL || nCount == 0);
ASSERT(((DWORD)lpBuf & 0x01) == 0); // started from even address required.
if (nCount == 0)
return;
if (m_nPosition + nCount > GetBufferSize())
GrowFile(m_nPosition + nCount);
ASSERT(GetBufferSize() >= m_nPosition + nCount);
BYTE *pSrc = (BYTE*)lpBuf; // Pointer of the source buffer
BYTE *pDes = NULL; // Pointer of the destination buffer
UINT nOffset; // Zero-based offset address in a buffer
UINT nBuffer; // Zero-based index of the buffer number
UINT nCopyCount; // Count of bytes at one copy operation
while (nCount > 0)
{
nOffset = m_nPosition % m_nGrowBytes;
nBuffer = m_nPosition / m_nGrowBytes;
VERIFY(m_mapBuffers.Lookup(nBuffer, pDes));
nCopyCount = m_nGrowBytes - nOffset;
if (nCopyCount > nCount) nCopyCount = nCount;
memcpy(pDes + nOffset, pSrc, nCopyCount);
nCount -= nCopyCount; // nCount is the count of remainder bytes
pSrc += nCopyCount;
m_nPosition += nCopyCount; // new file pointer
}
if (m_nPosition > m_nFileSize)
m_nFileSize = m_nPosition;
}
///////////////////////////////////////////////////////////////////////////////
// Parameters:
// dwNewLen Desired length of the file in bytes. This value can be
// larger or smaller than the current length of the file. The
// file will be extended or truncated as appropriate.
// Remarks:
// Call this function to change the length of the file.
void CMemFile::SetLength(DWORD dwNewLen)
{
GrowFile(dwNewLen);
ASSERT(GetBufferSize() >= dwNewLen);
m_nFileSize = dwNewLen;
}
///////////////////////////////////////////////////////////////////////////////
// Parameters:
// dwNewLen New minimum size of the memory file
// Remarks:
// Allocates more memories for the file.
void CMemFile::GrowFile(DWORD dwNewLen)
{
ASSERT_VALID(this);
ASSERT(m_nGrowBytes != 0);
DWORD dwNewBufferSize = GetBufferSize();
if (dwNewLen > dwNewBufferSize && m_nGrowBytes > 0)
{// grow the buffer
// determine new buffer size
UINT nCount = m_mapBuffers.GetCount();
while (dwNewBufferSize < dwNewLen)
{
BYTE *pByte = new BYTE[m_nGrowBytes];
ASSERT(pByte != NULL);
m_mapBuffers.SetAt(nCount++, pByte);
dwNewBufferSize += m_nGrowBytes;
}
}
}
///////////////////////////////////////////////////////////////////////////////
// Parameters:
// lpBuf Pointer to the user-supplied buffer that is to receive the
// data read from the file.
// nCount The maximum number of bytes to be read from the file.
// Return Value:
// The number of bytes transferred to the buffer.
// Remarks:
// Reads data into a buffer from the file associated with the CFile object.
UINT CMemFile::Read(void *lpBuf, UINT nCount)
{
ASSERT(lpBuf != NULL || nCount == 0);
ASSERT(((DWORD)lpBuf & 0x01) == 0); // started from even address required.
if (nCount == 0)
return 0;
if (m_nPosition > m_nFileSize)
return 0;
UINT nRead;
if (m_nPosition + nCount > m_nFileSize)
nRead = (UINT)(m_nFileSize - m_nPosition);
else
nRead = nCount;
BYTE *pDes = (BYTE*)lpBuf; // Pointer of the destination buffer
BYTE *pSrc = NULL; // Pointer of the source buffer
UINT nOffset; // Zero-based offset address in a buffer
UINT nBuffer; // Zero-based index of the buffer number
UINT nCopyCount; // Count of bytes at one copy operation
nCount = nRead;
while (nCount > 0)
{
nOffset = m_nPosition % m_nGrowBytes;
nBuffer = m_nPosition / m_nGrowBytes;
VERIFY(m_mapBuffers.Lookup(nBuffer, pSrc));
nCopyCount = m_nGrowBytes - nOffset;
if (nCopyCount > nCount) nCopyCount = nCount;
memcpy(pDes, pSrc + nOffset, nCopyCount);
nCount -= nCopyCount;
pDes += nCopyCount;
m_nPosition += nCopyCount;
}
return nRead;
}
// Close the file, free the memories
void CMemFile::Close()
{
m_nPosition = 0;
m_nFileSize = 0;
m_nOpenFlags = 0;
BYTE *puByte = NULL;
UINT nBuffer;
POSITION pos = m_mapBuffers.GetStartPosition();
while (pos != NULL)
{
m_mapBuffers.GetNextAssoc(pos, nBuffer, puByte);
if (puByte != NULL)
{
delete[] puByte;
puByte = NULL;
}
}
m_mapBuffers.RemoveAll();
}
///////////////////////////////////////////////////////////////////////////////
// Text file operations
///////////////////////////////////////////////////////////////////////////////
// Parameters:
// lpszString A string to search for
// Return Value:
// The zero-based index relative to current file pointer of the first
// character in this file that matches the requested string;
// -1 if the string is not found.
// Remarks:
// Finds a string inside a text format file from current position.
int CMemFile::Find(LPCSTR lpszString) const
{
if (lpszString == NULL)
return -1;
LPCSTR lpsz; // string pointer returned by strstr()
int nRelative = 0; // string position relative to current file pointer
// Pointer of the file buffer
BYTE *pBuffer = NULL;
// Zero-based offset address in a buffer
int nOffset = m_nPosition % m_nGrowBytes;
// Zero-based index of the buffer number
int nBuffer = m_nPosition / m_nGrowBytes;
for (; nBuffer < m_mapBuffers.GetCount(); nBuffer++)
{
VERIFY(m_mapBuffers.Lookup(nBuffer, pBuffer));
ASSERT(pBuffer);
pBuffer += nOffset;
// find string from buffer
lpsz = strstr((LPCSTR)pBuffer, lpszString);
if (lpsz)
{// string found
nRelative += (int)(lpsz - (LPCSTR)pBuffer);
break;
}
nRelative += m_nGrowBytes - nOffset;
nOffset = 0;
}
return (nRelative + m_nPosition > m_nFileSize) ? -1 : nRelative;
}
///////////////////////////////////////////////////////////////////////////////
// Parameters:
// rString A reference to a CString object that will contain the string
// when the function returns.
// Return Value:
// The number of bytes read from the file.
// Remarks:
// Read a line of string from the file.
UINT CMemFile::ReadLine(CString &rString)
{
UINT nCount = Find("\r\n"); // if not found, nCount=-1 (UINT_MAX)
// nCount includes "\r\n"
nCount = (nCount == -1) ? (m_nFileSize - m_nPosition) : (nCount + 2);
nCount = Read(rString.GetBuffer(nCount), nCount);
rString.ReleaseBuffer(nCount);
return nCount;
}
///////////////////////////////////////////////////////////////////////////////
// Parameters:
// lpszString Specifies a pointer to a buffer containing a null-terminated
// text string.
// Remarks:
// Write a line of string to the file. "\r\n" are appended automatically.
void CMemFile::WriteLine(LPCSTR lpszString)
{
if (lpszString)
{
Write(lpszString, strlen(lpszString));
Write("\r\n", 2);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -