[epiar-devel] Threads, Networking, etc..

Chris Walton chris.r.walton at gmail.com
Tue Dec 22 08:20:29 PST 2009


> 1) Remove physicsFS code from the repo, I believe we still want the File
> and Filesystem abstraction (for implementing reading from archives),
> unless we desire a separate Archive abstraction so that it's a
> non-transparent operation.  This needs to be answered.  Do we want reading
>  from ordinary files and archive files to be  transparent? If so, we should
> keep it so that it always reads everything from memory rather than direct
> file access.

that would mean loading every file completely into memory first. that sucks :).

There's a better solution, though. This was written in my email
client. no idea if this compiles at all, but I think you get the
general idea:

class Stream
{
public:
	enum SeekPivot
	{
		SP_SET,
		SP_CUR,
		SP_END,
	};
	
	virtual void   close()                                      = 0;
	virtual size_t read(const char* _pbBuf, size_t _cbLen)      = 0;
	virtual size_t write(const char* _pbBuf, size_t _cbLen)     = 0;
	virtual void   seek(size_t _cbPos, SeekPivot _pivot)        = 0;
	virtual size_t tell()                                       = 0;
};

class FileStream
	: public Stream
{
private:
	FILE* m_file;
	
public:
	enum OpenFlags
	{
		OF_READ   = (1 << 0),
		OF_WRITE  = (1 << 1),
		OF_BINARY = (1 << 2),
		
		OF_RW     = OF_READ | OF_WRITE,
		OF_RB     = OF_READ | OF_BINARY,
		OF_WB     = OF_WRITE | OF_BINARY,
		OF_RWB    = OF_RW | OF_BINARY,
	};
	
	FileStream() : m_file(NULL) {}
	
	~FileStream()
	{
		if(m_file)
			close();
	}
	
	bool   open(const char* _szFilename, OpenFlags _of)
	{
		assert(!m_file);
		
		char szOpenMode[4];
		{
			char* p = szOpenMode;
			if(_of & OF_READ)   *p++ = 'r';
			if(_of & OF_WRITE)  *p++ = 'w';
			if(_of & OF_BINARY) *p++ = 'b';
			*p = '\0';
		}
		
		m_file = fopen(_szFilename, szOpenMode);
		return m_file != 0;
	}
	
	void   close()
	{
		assert(m_file);
		fclose(m_file);
		m_file = NULL;
	}
	
	size_t read(const char* _pbBuf, size_t _cbLen)
	{
		assert(m_file);
		return fread(_pbBuf, sizeof(char), _cbLen, m_file);
	}
	
	size_t write(char* _pbBuf, size_t _cbLen)
	{
		assert(m_file);
		return fwrite(_pbBuf, sizeof(char), _cbLen, m_file);
	}
	
	void   seek(size_t _cbPos, SeekPivot _pivot)
	{
		assert(m_file);
		int origin = 0;
		switch(_pivot)
		{
		case SP_SET: origin = SEEK_SET; break;
		case SP_CUR: origin = SEEK_CUR; break;	
		case SP_END: origin = SEEK_END; break;
		default:
			assert(0);
			return;
		}
		fseek(m_file, _cbPos, origin);
	}
	
	size_t tell()
	{
		assert(m_file);
		return ftell(m_file);
	}
};


We could also use SDL RWops - they work the same, but aren't "C++-ey".
In fact, this system and the RWops system could work together just
fine.

-- Chris



More information about the epiar-devel mailing list