/* libsswf_tag_sceneframedata.c++ -- written by Alexis WILKE for Made to Order Software Corp. (c) 2002-2008 */

/*

Copyright (c) 2002-2008 Made to Order Software Corp.

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and
associated documentation files (the "Software"), to
deal in the Software without restriction, including
without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/

/** \file
 *
 * \brief The implementation of the sswf::TagMetadata class
 *
 * This file declares the body of the functions which are not
 * inline. It is part of the SSWF library.
 */

#include	"sswf/libsswf.h"

using namespace sswf;



/////////////////////////////////////////// TagSceneFrameData

/** \class sswf::TagSceneFrameData
 *
 * \brief Raw data for your scene and frame.
 *
 * This object includes raw data for your scene and frame.
 *
 * \sa <a href="http://sswf.m2osw.com/SWFalexref.html#tag_definesceneandframedata">Alexis' SWF Reference&mdash;Define Scene and Frame Data</a>
 * \sa <a href="../SWFalexref.html#swf_tag">SWF Alexis' Reference&mdash;swf_tag</a>
 */

/** \brief Initializes the TagSceneFrameData object
 *
 * The constructor initializes an empty TagSceneFrameData.
 *
 * \param[in] parent The sswf::TagHeader where this sswf::TagSceneFrameData is inserted
 */
TagSceneFrameData::TagSceneFrameData(TagBase *parent)
	: TagBase("sceneframedata", parent)
{
}



/** \brief Return the type of the scene and frame data tag
 *
 * This function returns the type flags of the TagSceneFrameData.
 * The TagSceneFrameData is a definition.
 *
 * \return The set of flags representing an sswf::TagSceneFrameData
 */
TagBase::swf_type_t TagSceneFrameData::TypeFlags(void) const
{
	return SWF_TYPE_DEFINE;
}


/** \brief Set a file data in the scene and frame data tag
 *
 * This function reads the data from a file and uses it as the block
 * of data of a Scene and Frame Data tag.
 *
 * \param[in] filename The name of the file to read
 *
 * \return true when the file was succesfully read.
 *
 * \sa sswf::TagSceneFrameData::SetSceneFrameData(const char *data, size_t size)
 */
bool TagSceneFrameData::SetFileData(const char *filename)
{
	FILE		*f;
	bool		result;

	MemClean(&f_data);
	f_size = 0;

	f = fopen(filename, "rb");
	if(f == 0) {
		OnError(ErrorManager::ERROR_CODE_IO, "cannot open \"%s\" for reading (%d)", filename, errno);
		return false;
	}

	result = true;

	fseek(f, 0, SEEK_END);
	f_size = ftell(f);
	fseek(f, 0, SEEK_SET);

	if(f_size != 0) {
		f_data = (char *) MemAlloc(f_size, "TagSceneFrameData::SetFileData() -- caching the file data");
		result = fread(f_data, 1, f_size, f) == f_size;
	}

	fclose(f);

	return result;
}





/** \brief Set the data in the scene and frame data tag
 *
 * This function defines the block of data of a Scene and Frame Data tag.
 *
 * \param[in] data A pointer to a block of data
 * \param[in] size The size of the block of data in bytes
 *
 * \sa TagSceneFrameData::SetFileData(const char *filename)
 */
void TagSceneFrameData::SetSceneFrameData(const char *data, size_t size)
{
	MemClean(&f_data);
	f_size = size;
	if(f_size != 0) {
		f_data = (char *) MemAlloc(f_size, "TagSceneFrameData::SetSceneFrameData() -- caching the data parameter");
		memcpy(f_data, data, f_size);
	}
}





/** \brief Inform the system that version 9 is required
 *
 * This function is used by the TagSceneFrameData to tell the
 * Save() command that to save this tag a version 9 movie
 * is required.
 *
 * \return ErrorManager::ERROR_CODE_NONE when no error occurs,
 * 		some other ErrorManager::error_code_t otherwise
 */
ErrorManager::error_code_t TagSceneFrameData::PreSave(void)
{
	MinimumVersion(9);

	return ErrorManager::ERROR_CODE_NONE;
}



/** \brief Save the TagSceneFrameData tag
 *
 * This function saves the TagSceneFrameData in the specified Data buffer.
 *
 * \param[in] data The Data buffer where the scene frame data is saved
 *
 * \return ErrorManager::ERROR_CODE_NONE when no error occurs,
 * 		some other ErrorManager::error_code_t otherwise
 */
ErrorManager::error_code_t TagSceneFrameData::Save(Data& data)
{
	if(f_size == 0) {
		// if there is no data we do not save anything at all
		return ErrorManager::ERROR_CODE_NONE;
	}

	SaveTag(data, SWF_TAG_SCENE_FRAME_DATA, f_size);
	data.Write(f_data, f_size);

	return ErrorManager::ERROR_CODE_NONE;
}





/* The following options fold the documentation; use 'zi' to turn on and off
 *
 * vim: foldexpr=getline(v\:lnum)!~'^/\\*\\*'&&getline(v\:lnum)!~'^\ \\*'?0\:1 foldcolumn=2 foldmethod=expr
 */
