//------------------------------------------------------------------------------
//	File:					buffer.h
//	Date:					4/10/99
//	Author:				Chris Phillips
//
//	Description:
//
//------------------------------------------------------------------------------


#ifndef buffer_h
#define buffer_h
#include "binfile.h"
#include "common.h"
//------------------------------------------------------------------------------
//	enums
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//	classes
//------------------------------------------------------------------------------


class Buffer{
private:

public:
	unsigned char *data;
	unsigned long size;
	unsigned int pos;
	Buffer()
		{
		data=NULL;
		size=0;
		pos=0;
		}

	Buffer(unsigned int sz)
		{
		size=sz;
		data=(unsigned char*)malloc(size);
		pos=0;
		}

	~Buffer()
		{
		if(data)
			free(data);
		size=0;
		pos=0;
		}
	bool load(char *name)
		{
		Binfile file(name);
		if(file.size()>0)
			{
			resize(file.size());
			file.read((char *)data);
			pos=0;
			return(TRUE);
			}
		return(FALSE);
		}
	void resize(uint sz)
		{
		if(size==sz)
			return;
		if(sz==0)
			{
			if(data)
				{
				free(data);
				data=NULL;
				size=sz;
				return;
				}
			}
		if(data)
			data=(uchar*)realloc(data,sz);
		else
			data=(uchar*)malloc(sz);
		size=sz;
		}
	uchar  next_byte(void){return(data[pos++]);};
	ushort next_word(void){
	ushort w=data[pos]+((ushort)(data[pos+1])<<8);pos+=2;return(w);};
};


#endif buffer_h	

