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

📄 inputfile.cpp

📁 《Visual C++视频/音频开发实用工程案例精选》一书的源代码
💻 CPP
字号:
/**************************************************************************************
 *                                                                                    *
 *                                                                                    *
 **************************************************************************************/

#include "InputFile.h"

/*
 *
 *  ——读本地文件
 *
 */

/*
 * 构造器
 * ------
 * 
 * 
 *
 */

MediaInputFile::MediaInputFile()
{
	this->file = NULL;
}

/*
 * 析构器
 * ------
 * 
 * 
 *
 */


MediaInputFile::~MediaInputFile()
{

}

/*
 *
 *  ——得到媒体项的类型
 *
 */

media_type_t MediaInputFile::GetType()
{
	return MEDIA_TYPE_INPUT;
}

/*
 *
 *  ——得到媒体项的类型的名称
 *
 */

char *MediaInputFile::GetName()
{
	return "FILE Input";
}

/*
 * Connect:
 * --------
 *
 *  ——不接受
 *
 */

MP_RESULT MediaInputFile::Connect(MediaItem *item)
{
	return MP_RESULT_ERROR;
}

MP_RESULT MediaInputFile::ReleaseConnections()
{
	return MP_RESULT_ERROR;
}

DWORD         MediaInputFile::GetCaps()
{
	return 0;
}

MP_RESULT     MediaInputFile::Configure(HINSTANCE hInstance, HWND hwnd)
{
	return MP_RESULT_ERROR;
}

/*
 *
 *  ——打开文件
 *
 */

MP_RESULT MediaInputFile::Open(char *url, media_input_mode_t mode) 
{
	this->file = NULL;

	if(url != NULL) {

		switch(mode) {

			case INPUT_OPEN_BINARY:

				this->file = fopen(url, "rb");
				break;

			case INPUT_OPEN_ASCII:

				this->file = fopen(url, "rt");
				break;

			default:
			
				this->file = fopen(url, "rb");
				break;
		}
	}

	if(this->file == NULL) {

		return MP_RESULT_ERROR;
	}

	/*
	 * 得到文件大小
	 */

	this->Seek(0, INPUT_SEEK_END);
	this->size = this->Seek(0, INPUT_SEEK_CUR);
	this->Seek(0, INPUT_SEEK_SET);

	return MP_RESULT_OK;
}


long MediaInputFile::GetSize()
{
	return MP_RESULT_ERROR;
}

long MediaInputFile::GetBufferSize()
{
	return MP_RESULT_ERROR;
}

long MediaInputFile::GetBufferPosition()
{
	return MP_RESULT_ERROR;
}

long MediaInputFile::GetBufferingSize()
{
	return MP_RESULT_ERROR;
}

/*
 *
 *  ——读某些二进制数据
 *
 */

unsigned int MediaInputFile::Read(MediaBuffer *mb, unsigned int size)
{
	if(!mb || !this->file)
		return MP_RESULT_ERROR;

	if(size > mb->GetSize())
		mb->ReAlloc(size);

	return fread(mb->GetData(), 1, size, this->file);
}

/*
 *
 *  ——在文件中搜索
 *
 */

unsigned int MediaInputFile::Seek(int size, media_input_seek_t method)
{
	if(!this->file)
		return MP_RESULT_ERROR;

	switch(method) {

		case INPUT_SEEK_SET:

			return fseek(this->file, size, SEEK_SET);
			break;

			
		case INPUT_SEEK_CUR:

			if(size == 0) {
				return ftell(this->file);	
			}
			else {
				return fseek(this->file, size, SEEK_CUR);
			}
			break;

		case INPUT_SEEK_END:

			return fseek(this->file, size, SEEK_END);
			break;
	}

	return MP_RESULT_ERROR;
}

/*
 *
 *  ——得到完整的行
 *
 */

unsigned int MediaInputFile::GetLine(MediaBuffer *mb)
{
	if(!this->file)
		return MP_RESULT_ERROR;

	fgets((char *) mb->GetData(), mb->GetSize(), this->file);

	return MP_RESULT_OK;
}

/*
 *
 *  ——文件结束则返回真
 *
 */

BOOL MediaInputFile::EndOfFile()
{
	if(!this->file)
		return TRUE;

	return feof(this->file);
}


/*
 *
 *  ——关闭文件
 *
 */

MP_RESULT MediaInputFile::Close()
{
	if(!this->file)
		return MP_RESULT_ERROR;

	fclose(this->file);

	return MP_RESULT_OK;
}

⌨️ 快捷键说明

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