/*  $Id: outputsource_file.cpp,v 1.5 2003/07/20 22:30:53 grumbel Exp $
**
**  ClanLib Game SDK
**  Copyright (C) 2003  The ClanLib Team
**  For a total list of contributers see the file CREDITS.
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**
**  This library is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**  Lesser General Public License for more details.
**
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
**
*/

#include "Core/precomp.h"
#include "API/Core/IOData/outputsource_file.h"
#include "API/Core/System/cl_assert.h"
//#include "API/Core/System/error.h"

CL_OutputSource_File::CL_OutputSource_File(const std::string &_filename)
{
	filename = _filename;
	pos = 0;
	file = NULL;
	open();
}

CL_OutputSource_File::CL_OutputSource_File()
{
	pos = 0;
	file = NULL;
}

CL_OutputSource_File::~CL_OutputSource_File()
{
	close();
}

int CL_OutputSource_File::write(const void *data, int size)
{
	cl_assert(file != NULL);
	fwrite(data, size, 1, file);
	pos += size;
	return size;
}

void CL_OutputSource_File::open()
{
	if(file != NULL)
		return;

	file = fopen(filename.c_str(), "w+b");
	if(file == NULL)
		throw CL_Error("could not create file");
}

void CL_OutputSource_File::close()
{
	if(file == NULL)
		return;

	fclose(file);
	file = NULL;
}

CL_OutputSource *CL_OutputSource_File::clone()
{
	cl_assert(false); // not implemented yet.
	return NULL;
}

int CL_OutputSource_File::tell() const
{
	return pos;
}

int CL_OutputSource_File::size() const
{
	return pos;
}
