//Copyright (C) 2007 SiegeLord
//See ReadMe.txt and License.txt for copying information

#include "../include/engine/config.h"
#include "../include/engine/error.h"
#include "../include/engine/fileio.h"

void SetConfigFile(const char* filename, bool use_data_folder)
{
	string datafile;
	string name(filename);

	void* buf;

	if(!use_data_folder)//this is for stuff that is global to the program, never in a zip etc
	{
		PACKFILE* file = pack_fopen(filename, F_READ);
		if(file)
		{
			int length = file_size_ex(name.c_str());
			if(length)
			{
				buf = (char*)malloc(length + 1);
				pack_fread(buf, length, file);
				((char*)buf)[length] = 0;
				set_config_data((char*)buf, length);
				free(buf);
			}
			CloseFile(file);
		}
		return;
	}

	PACKFILE* file = LoadFile(filename);
	if(file)
	{
		int length;
		length = ReadFile(file, &buf);
		set_config_data((char*)buf, length);
		delete[] (char*)buf;
		CloseFile(file);
	}
}

void ParseMultiIntString(const string* restrict str, int* restrict ptr, int num)
{
	_ASSERT(ptr);
	_ASSERT(str);
	int a = 0,b;
	string token;
	for(int ii = 0; ii < num; ii++)
	{
		a = str->find_first_not_of("/t, ",a);
		b = str->find_first_of("/t ,",a);
		token = str->substr(a, b - a);
		a = b + 1;
		ptr[ii] = atoi(token.c_str());
	}
}

int GetColor(const char* str)
{
	int ret;
	if(sscanf(str, "%x",  &ret))
		ret = makeacol32(getb32(ret),getg32(ret),getr32(ret),geta32(ret));
	else
		return 0;

	return ret;
}
