/*********************************************************************

Name:		Raytracer
Author:		Kamil Szatkowski "Netrix" (C)
Website:	http://www.netrix.org.pl/

*********************************************************************/
#include "SceneDesc.hpp"
#include "NLib/nLogger.hpp"
#include <stdio.h>
#include <string.h>

#pragma warning(disable: 4996)

namespace 
{
	struct NRaiiFile
	{
		NRaiiFile(const char* szFile, const char* szFlags)
		{
			pFile = fopen(szFile, szFlags);
		}

		~NRaiiFile()
		{
			if(pFile != null) { fclose(pFile); }
		}

		FILE* pFile;
	};
}

bool SceneDesc::loadDescription(const char* szFile)
{
	if(strnlen(szFile, MAX_PATH_SIZE) > MAX_PATH_LENGTH)
	{
		NLogError("Invalid string: 'szFile'");
		return true;
	}

	NRaiiFile raiiFile(szFile, "r");
	FILE* pFile = raiiFile.pFile;

	if(pFile == null)
	{
		NLogHead(); NLogMessage("Failed to open file: %s", szFile);
		return true;
	}

	fgets(m_szScenePath, sizeof(m_szScenePath), pFile);	
	m_szScenePath[sizeof(m_szScenePath) - 1] = 0;			// for safety
	trimCRNL(m_szScenePath);

	fgets(m_szCameraPath, sizeof(m_szCameraPath), pFile);
	m_szCameraPath[sizeof(m_szCameraPath) - 1] = 0;			// for safety
	trimCRNL(m_szCameraPath);

	fgets(m_szAttributesPath, sizeof(m_szAttributesPath), pFile);
	m_szAttributesPath[sizeof(m_szAttributesPath) - 1] = 0;	// for safety
	trimCRNL(m_szAttributesPath);

	fgets(m_szLightsPath, sizeof(m_szLightsPath), pFile);
	m_szLightsPath[sizeof(m_szLightsPath) - 1] = 0;			// for safety
	trimCRNL(m_szLightsPath);

	m_bLoaded = true;
	return false;
}

void SceneDesc::trimCRNL(char* szString)
{
	while(*szString != 0 && *szString != 0xA && *szString != 0xD) { szString++; }
	if(*szString != 0 && (*szString == 0xA || *szString == 0xD)) { *szString = 0; }
}