⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 archive.cpp

📁 多媒体电话记录程序
💻 CPP
字号:
/////////////////////////////////////////////////////////////////////////////
//  Name: archive.cpp
//  Copyright: wellgain
//  Author: bet
//  Date: 2003-10-10 
//  Description:  the implement for class CArchive 
/////////////////////////////////////////////////////////////////////////////
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <iostream.h>
#include "archive.h"


/////////////////////////////////////////////////////////////////////////////

void CArchive::Flush()
{
	assert(m_pFile!=NULL);
	fflush(m_pFile);
}


int CArchive::Seek(long offset, CArchive::seek_dir dir)
{
	assert(m_pFile!=NULL);
	return fseek(m_pFile, offset, dir);
}

long CArchive::Tell() const
{
	assert(m_pFile!=NULL);
	return ftell(m_pFile);
}


CArchive& CArchive::operator<<(char ch)
{	
	Write(&ch, sizeof(char));
	return *this;
}

CArchive& CArchive::operator<<(bool b)
{
	//if errors do nothing until clear errors 
	Write(&b, sizeof(bool));
	return *this;
}


CArchive& CArchive::operator<<(int i)
{
	Write(&i, sizeof(int));
	return *this;
}

CArchive& CArchive::operator<<(long l)
{
	Write(&l, sizeof(long));
	return *this;
}


CArchive& CArchive::operator<<(float f)
{
	Write(&f, sizeof(float));
	return *this;
}

CArchive& CArchive::operator<<(double d)
{
	Write(&d, sizeof(double));
	return *this;
}


CArchive& CArchive::operator>>(char& ch)
{
	Read(&ch, sizeof(char));
	return *this;
}

CArchive& CArchive::operator>>(bool& b)
{
	Read(&b, sizeof(bool));
	return *this;
}

CArchive& CArchive::operator>>(int& i)
{
	Read(&i, sizeof(int));
	return *this;
}
CArchive& CArchive::operator>>(long& l)
{
	Read(&l, sizeof(long));
	return *this;
}


CArchive& CArchive::operator>>(float& f)
{
	Read(&f, sizeof(float));
	return *this;
}

CArchive& CArchive::operator>>(double& d)
{
	Read(&d, sizeof(double));
	return *this;
}


CArchive::CArchive(FILE* pFile, CArchive::Mode nMode)	
{
	assert(pFile!=NULL);

	// initialize members
	m_nMode = nMode;
	m_pFile = pFile;
	m_nErrorNo = NO_ERROR;
}

CArchive::~CArchive()
{
	Close();

	m_pFile = NULL;
}

void CArchive::Close()
{
	if(m_pFile!=NULL)
		Flush();
	// disconnect from the file	
	m_pFile = NULL;
}

UINT CArchive::Read(void* lpBuf, UINT nLen)
{
	UINT nBytes;
	//if errors do nothing until clear errors 
	if(*this){
		assert(m_pFile!=NULL);	
		if(nLen<=0) return 0;

		if((nBytes = fread(lpBuf, sizeof(char), nLen, m_pFile))!=nLen){			
			if(feof(m_pFile)){
				m_nErrorNo = -1;
				//cerr<<"*** Archive Error *** "<<"End of file"<<endl;
			}
			else{
				m_nErrorNo = errno;
				cerr<<"*** Archive Error *** "<<strerror(m_nErrorNo)<<endl;
			}
		}
	}
	return sizeof(char)*nBytes;
}

UINT CArchive::Write(const void* lpBuf, UINT nLen)
{
	UINT nBytes;
	if(*this){
		assert(m_pFile!=NULL);	
		if(nLen<=0) return 0;

		if((nBytes = fwrite(lpBuf, sizeof(char), nLen, m_pFile))!=nLen){			
			m_nErrorNo = errno;
			cerr<<"*** Archive Error *** "<<strerror(m_nErrorNo)<<endl;
		}
	}
	return sizeof(char)*nBytes;
}

void CArchive::WriteCount(UINT nCount)
{
	int nTemp;
	nTemp = (int)nCount;
	*this<<nTemp;
}

UINT CArchive::ReadCount()
{
	int nTemp;
	*this >> nTemp;
	return nTemp;
}

void CArchive::WriteString(char* lpsz)
{
	assert(lpsz!=NULL);
	Write(lpsz, strlen(lpsz) * sizeof(char));
	*this<<(char)0;
}

char* CArchive::ReadString(char* lpsz, UINT nMax)
{
	UINT nRead= 0;
	char ch;
	do{
		*this>>ch;
		if(nRead<nMax-1){
			lpsz[nRead++] = ch;
		}
	}while((*this) && ch!='\0');

	lpsz[nRead] = '\0';
	return lpsz;
}



/////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -