#include "stdafx.h"
#include "contents.hpp"

CString mattable[19] = {
"grass",
"water",
"concrete",
"greydirt",
"browndirt",
"rock",
"darkconcrete",
"darkgreydirt",
"darkbrowndirt",
"darkrock",
"blacktop",
"snow",
"wood",
"lava",
"glass",
"steel",
"us",
"them",
"blank"
};


CContentData::CContentData (CString& path)
{
	m_Path = path;
	m_Dirty = false;
}

CContentData::~CContentData (void)
{
}


void CContentData::ReadData (CString file)
{
	m_File = file;
	CString filename;
	char *buf;

	CString fred,effect,mat,str;
	int index,find;

	filename = m_Path;
	filename += "\\";
	filename += m_File;

	buf = fred.GetBufferSetLength (10000);
	GetPrivateProfileSection ("EffectList",buf,10000-1,(LPCTSTR) filename);
	index = 0;
	while (*buf)
	{
		str = buf;
		buf += str.GetLength ()+1;
		StripComment (str);
		if (!str.IsEmpty ())
		{
			find = str.Find ('=');
			ASSERT (find != -1);
			str = str.Right (str.GetLength () - find-1);
			find = str.Find (',');
			ASSERT (find != -1);
			effect = str.Left (find);
			mat = str.Right (str.GetLength () - find-1);

			index = FindMatIndex (mat);
			ASSERT (index != -1);
			effect.MakeLower ();
			m_EffectName[index] = effect;
		}																									 

	}
	fred.ReleaseBuffer ();
}

int CContentData::FindMatIndex (CString& mat)
{
	mat.MakeLower ();
	int i;
	for (i=0;i<18;i++)
	{
		if (mat == mattable[i])
			return i;
	}
	return 18;
}


void CContentData::WriteData (void)
{
	if (!m_Dirty)
		return;
	FILE *file;
	CString filename;

	filename = m_Path;
	filename += "\\";
	filename += m_File;

	file = fopen ((LPCTSTR) filename,"w");
	if (!file)
		return;
	fprintf (file,"[EffectList]\n");
	int i;
	for (i=0;i<32;i++)
	{
		if (mattable[i] == "blank")
			continue;
		fprintf (file,"Effect=%s,%s\n",m_EffectName[i],mattable[i]);
	}
	fclose (file);
}

void CContentData::InitialData (CString file)
{
	m_File = file;
}

void CContentData::StripComment (CString& str)
{
	int index;

	str.TrimLeft ();
	str.TrimRight ();

	index = str.Find ("//");
	if (index != -1)
	{
		str = str.Left (index);
	}
}

