#include <stdio.h>
#include <string.h>
#include <stdlib.h>

extern char lmpFiles[256];

char* CleanString(void)
{
	int k = 0;
	bool foundDot = false;

	while(1)
	{
		if(lmpFiles[k++] == '.')
			foundDot = true;
		if(foundDot == true)
		{
			if(lmpFiles[k++] == 0x20)
			{
				lmpFiles[k] = '\0';
				break;
			}
		}
	}
	return lmpFiles;
}

int FileSizeAdjust(int size)
{
	//in GBADoom, lump sizes must be in multibles of 4.
	if(size % 4 == 0)
		return size;
	else	//begin procedure if not in multibles of 4
	{
		for(;;size++)
			if(size % 4 == 0)	//increase size until within range
				break;
	}
	return size;
}

int findstr(char* body,char* search)
{
	int len = strlen(body);
	int len2 = strlen(search); // search len

	for(int i = 0; i < len; i++)
	{
		if(body[i] == search[0])
		{
			bool ichk = true;

			for(int z = 0; z < len2; z++)
			{
				if(body[i+z] == search[z])
				{
				}
				else
				{
					ichk = false;
				}
			}

			if(ichk == true)
			{
				return i;
			}
		}
	}

	return -1; // failure
}

//taken from Doom
void ExtractFileBase (char *path, char *dest)
{
	char	*src;
	int		length;

	src = path + strlen(path) - 1;
	while (src != path && *(src-1) != '\\' && *(src-1) != '/')
		src--;

	memset (dest,0,8);
	length = 0;
	while (*src && *src != '.')
	{
		*dest++ = toupper((int)*src++);
	}
}

void KillFileName(char *name)
{
	char *search;

	search = name+strlen(name)-1;
	while(search != name)
	{
		if(*search == 92 || *search == 47)
		{
			search++;
			*search = '\0';
			return;
		}
		search--;
	}
}