📄 filerecordset.cpp
字号:
#include "stdafx.h"
#include "filerecordset.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFileSet
int recordsize = sizeof(_DBRECORD);
IMPLEMENT_DYNCREATE(CFileSet, CObject)
CFileSet::CFileSet()
{
filename = _T("");
Status = FALSE;
position = 0;
length = 0;
unsigned char buff[1024];
memset(buff,0,1024);
::GetTempPath(1024,(char *)&buff);
temppath.Format("%s",buff);
memset(&record,0,sizeof(record));
UserName=_T("");
UserPswd=_T("");
UserLevel=_T("");
m_Type="";
m_FeeRate="";
m_OFeeRate="";
m_STLen="";
m_StartTLen="";//for xiamen
}
CFileSet::~CFileSet()
{
Close();
}
/////////////////////////////////////////////////////////////////////////////
// CFileSet message handlers
BOOL CFileSet::Open(CString m_strFile)
{
filename = m_strFile;
UINT OpenFlags=CFile::modeReadWrite|CFile::shareDenyNone;
Status = file.Open( filename, OpenFlags,NULL);
if (!Status){
OpenFlags|=CFile::modeCreate;
Status = file.Open(filename, OpenFlags,NULL);
}
if (Status)
{
length = (file.GetLength())/recordsize;//?
//file.SeekToBegin();
if (length>0)
Read();
}
return Status;
}
void CFileSet::Close()
{
if (Status)
{
file.Close();
Status = FALSE;
position = 0;
length = 0;
}
}
void CFileSet::Move(long nRows)
{
ASSERT(Status == TRUE);
int mv = nRows;
int sz = position+mv;
if (sz<0)
{
mv=-position;
}
else if (sz>=(length-1))
{
mv=length-1-position;
}
if (file.Seek(recordsize*mv,CFile::current) <0 )
{
return;
}
position += mv;
Read();
}
void CFileSet::MoveFirst()
{
ASSERT(Status == TRUE);
file.SeekToBegin();
position = 0;
Read();
}
void CFileSet::MoveLast()
{
ASSERT(Status == TRUE);
// as we known length if larger than 0
if (length == 0 || length ==1)
{
MoveFirst();
}
else
{
file.Seek(-recordsize,CFile::end);
position = length-1;
}
Read();
}
void CFileSet::MovePrev()
{
ASSERT(Status == TRUE);
if ((position == 0) || (length == 0))
{
return;
}
if (file.Seek(-recordsize,CFile::current) <0 )
{
return;
}
position--;
Read();
}
void CFileSet::MoveNext()
{
ASSERT(Status == TRUE);
if ((length == 0) || (position == (length)) )
{
return;
}
if (file.Seek(recordsize,CFile::current) <0 )
{
return;
}
position++;
Read();
}
BOOL CFileSet::IsBOF()
{
ASSERT(Status == TRUE);
return(position == 0);
}
BOOL CFileSet::IsEOF()
{
ASSERT(Status == TRUE);
return(position == length);
}
BOOL CFileSet::AddNew()
{
ASSERT(Status == TRUE);
file.SeekToEnd();
if (Write())
{
length++;
position = length -1;
}
return TRUE;
}
BOOL CFileSet::Delete()
{
ASSERT(Status == TRUE);
if ((length == 0) || (position == length))
{
return FALSE;
}
CFile temp;
CString tempfile;
// delete
tempfile.Format("%sd001.tmp",temppath);
temp.Open(tempfile,CFile::modeCreate|CFile::modeReadWrite,NULL);
unsigned char buff[1024];
memset(buff,0,1024);
DWORD pos = file.GetPosition();
file.Seek(recordsize,CFile::current);
int sz=0;
while((sz=file.Read(&buff,1024))>0)
{
temp.Write(buff,sz);
temp.Flush();
}
file.Seek(pos,CFile::begin);
temp.SeekToBegin();
while((sz=temp.Read(&buff,1024))>0)
{
file.Write(buff,sz);
}
temp.Close();
file.SetLength(file.GetLength()-recordsize);
CFile::Remove(tempfile);
// reload
file.Flush();
length = file.GetLength()/recordsize;
return TRUE;
}
BOOL CFileSet::Update()
{
ASSERT(Status == TRUE);
if ((length == 0) || (position == length))
{
return FALSE;
}
Write();
return TRUE;
}
BOOL CFileSet::Read()
{
file.Read(&record,sizeof(record));
file.Seek(-recordsize,CFile::current);//?
UpdateData(FALSE);
return TRUE;
}
BOOL CFileSet::Write()
{
UpdateData(TRUE);
file.Write(&record,sizeof(record));
file.Flush();
file.Seek(-recordsize,CFile::current);
return TRUE;
}
void CFileSet::UpdateData(BOOL bUpdate)
{
if (bUpdate)//增加和修改时
{
strcpy((char *)record.UserName,LPCTSTR(UserName));
record.UserName[10] = 0;
strcpy((char *)record.UserPswd,LPCTSTR(UserPswd));
record.UserPswd[20] = 0;
strcpy((char *)record.UserLevel,LPCTSTR(UserLevel));
record.UserLevel[20] = 0;
strcpy((char *)record.m_Type,LPCTSTR(m_Type));
record.m_Type[20] = 0;
strcpy((char *)record.m_FeeRate,LPCTSTR(m_FeeRate));
record.m_FeeRate[20] = 0;
strcpy((char *)record.m_OFeeRate,LPCTSTR(m_OFeeRate));
record.m_OFeeRate[20] = 0;
strcpy((char *)record.m_STLen,LPCTSTR(m_STLen));
record.m_STLen[10] = 0;
strcpy((char *)record.m_StartTLen,LPCTSTR(m_StartTLen));//for xiamen
record.m_StartTLen[5]=0;
}
else//读时
{
UserName.Format("%s",record.UserName);
UserPswd.Format("%s",record.UserPswd);
UserLevel.Format("%s",record.UserLevel);
m_Type.Format("%s",record.m_Type);
m_FeeRate.Format("%s",record.m_FeeRate);
m_OFeeRate.Format("%s",record.m_OFeeRate);
m_STLen.Format("%s",record.m_STLen);
m_StartTLen.Format("%s",record.m_StartTLen);//for xiamen
}
}
BOOL CFileSet::Check()
{
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -