fileinputstream.cpp

来自「一个symbian 冒险游戏代码」· C++ 代码 · 共 78 行

CPP
78
字号
#include "dprintf.h"
#include <io/IOException.h>
#include <io/FileInputStream.h>
#include <lang/Globals.h>
#include <stdio.h>
#include <string.h>
#include <config.h>


using namespace lang;


namespace io
{


FileInputStream::FileInputStream( const char* filename )
{
	String s(filename);
	m_name = s;


#ifdef __SYMBIAN32__
	if ( strlen(filename) >= 255 )
		INTERNAL_ERROR();
	char buf[256];
	strcpy( buf, filename );
	for ( int i = 0 ; buf[i] ; ++i )
		if ( buf[i] == '/' )
			buf[i] = '\\';
	filename = buf;
#endif

	m_fh = fopen( filename, "rb" );
	if ( !m_fh )
	{
		dprintf( "Failed to open file %s\n", filename );
		throwError( IOException(Format("Failed to open file {0}", filename)) );
	}
	dprintf( "File %s open ok\n", filename );
}

FileInputStream::~FileInputStream()
{
	FILE* fh = reinterpret_cast<FILE*>(m_fh);
	if ( fh )
		fclose( fh );
}

int FileInputStream::read( void* data, int size )
{
	FILE* fh = reinterpret_cast<FILE*>(m_fh);

	int bytes = fread( data, 1, size, fh );
	if ( bytes < size && ferror(fh) )
		INTERNAL_ERROR(); //( IOException( Format("Failed to read {1} bytes from {0}", toString(), size) ) );
	return bytes;
}

int FileInputStream::available() const
{
	FILE* fh = reinterpret_cast<FILE*>(m_fh);
	if ( !fh )
		return 0;
	int cur = ftell( fh );
	fseek( fh, 0, SEEK_END );
	int end = ftell( fh );
	fseek( fh, cur, SEEK_SET );
	if ( ferror(fh) )
		INTERNAL_ERROR(); //( IOException( Format("Failed to seek {0}", toString()) ) );
	return end-cur;
}


} // io


⌨️ 快捷键说明

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