/*
	$Id: resource_tokenizer.cpp,v 1.2 2003/08/22 20:26:21 grumbel Exp $

	------------------------------------------------------------------------
	ClanLib Game SDK

	This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
	version 2. See COPYING for details.

	For a total list of contributers see CREDITS.

	------------------------------------------------------------------------
*/

#include "resource_tokenizer.h"
#include <ClanLib/Core/System/clanstring.h>

ResourceTokenizer::ResourceTokenizer(std::vector<char>& buffer)
	: config_file_data(buffer)
{
	cur_pos = 0;
	cur_line = 1;
	in_remark = false;
}

ResourceTokenizer::~ResourceTokenizer()
{
}

bool ResourceTokenizer::is_operator(char c)
{
	return
		c == ',' ||
		c == '=' ||
		c == '(' ||
		c == ')' ||
		c == ';' ||
		c == '{' ||
		c == '}';
}

bool ResourceTokenizer::is_space(char c)
{
	return
		c == ' ' ||
		c == '\t' ||
		c == 13 ||
		c == 10;
}

bool ResourceTokenizer::is_remark(char c, int position)
{
	if (in_remark)
	{
		if (c == '\n') 
		{	
			in_remark = false;
		}
		return true;
	}
	else
	{
		if (c == '/')
		{
			if (
				position+1 < total_size &&
				config_file_data[position+1] == '/')
			{
				in_remark = true;
				return true;
			}
		}
		else if (c == '#')
		{
			in_remark = true;
			return true;
		}
	}
	return false;
}
	
std::string ResourceTokenizer::get_next_token()
{
	while (
		cur_pos < total_size && 
		(
			is_remark(config_file_data[cur_pos], cur_pos) ||
			is_space(config_file_data[cur_pos])
		))
	{
		if (config_file_data[cur_pos] == '\n') cur_line++;
		cur_pos++;
	}
	if (cur_pos == total_size) return "";

	if (is_operator(config_file_data[cur_pos]))
	{
		char temp[2];
		temp[0] = config_file_data[cur_pos];
		temp[1] = 0;
		std::string ret(temp);
		cur_pos++;

		return ret;
	}
	else
	{
		int temp_pos = cur_pos;
		if (config_file_data[temp_pos] == '"')
		{
			int num_chars = 0;
			temp_pos++;
			while (temp_pos < total_size)
			{
				if (config_file_data[temp_pos] == '"')
				{
					if (temp_pos+1>=total_size || config_file_data[temp_pos+1] != '"') break;
					else
					{
						temp_pos++;
					}
				}
				temp_pos++;
				num_chars++;
			}
			if (temp_pos == total_size)
			{
				write_error("Missing '""' following '""'-begin");
				return "";
			}

			char *temp = new char[num_chars+1];
			temp[num_chars] = 0;
			int ofs = 1;
			for (int i=0;i<num_chars;i++)
			{
				temp[i] = config_file_data[cur_pos+ofs+i];
				if (config_file_data[cur_pos+ofs+i] == '"') 
				{
					ofs++;
				}
			}
			cur_pos = temp_pos+1;

			std::string ret(temp);
			delete [] temp;

			return ret;
		}
		else
		{
			while (temp_pos < total_size &&
				!is_space(config_file_data[temp_pos]) &&
				!is_operator(config_file_data[temp_pos]) &&
				!is_remark(config_file_data[temp_pos], cur_pos))
			{
				temp_pos++;
			}
			int tok_size = temp_pos-cur_pos;
			char *temp = new char[tok_size+1];
			temp[tok_size] = 0;
			memcpy(temp, &config_file_data[cur_pos], tok_size);
			cur_pos += tok_size;

			std::string ret(temp);
			delete [] temp;

			return ret;
		}
	}
}

std::string ResourceTokenizer::write_error(std::string err_msg)
{
	std::string err;
	
	err = CL_String::format("Error occured during read of config file (line %1): %2", cur_line, err_msg.c_str());

	return err;
}
