//The parameters passed must be arrays of characters because space 
//is not allocated automatically when using pointers!

#include <stdlib.h>
#include <stdio.h>
void Spaces(char *msg)		//This definitely works
{
	int x=0;
	while(msg[x])
	{
		if(msg[x]=='+') msg[x]=' ';
		x++;
	}
}
bool isHex(char c)			//This definitely works
{
	return c=='0'||c=='1'||c=='2'||c=='3'||c=='4'||c=='5'||c=='6'||c=='7'||c=='8'||c=='9'||c=='A'||c=='B'||c=='C'||c=='D'||c=='E'||c=='F';
}
char toNum(char c)			//This definitely works
{
	if(c=='0') return 0;
	if(c=='1') return 1;
	if(c=='2') return 2;
	if(c=='3') return 3;
	if(c=='4') return 4;
	if(c=='5') return 5;
	if(c=='6') return 6;
	if(c=='7') return 7;
	if(c=='8') return 8;
	if(c=='9') return 9;
	if(c=='A') return 10;
	if(c=='B') return 11;
	if(c=='C') return 12;
	if(c=='D') return 13;
	if(c=='E') return 14;
	if(c=='F') return 15;
	//return -1;
}
void Ascii(char *msg)			//This definitely works
{
	int x=0;
	while(msg[x])
	{
		if(msg[x]=='%'&&isHex(msg[x+1])&&isHex(msg[x+2]))
		{
			msg[x]=char(toNum(msg[x+1])*16)+toNum(msg[x+2]);
			int y=x+1;
			while(msg[y+1]) {msg[y]=msg[y+2];y++;} //Shift past 2 hex cleared
		}
		x++;
	}
}
void Transparency(char *msg)
{
	int x=0;
	while(msg[x])
	{
		if(msg[x]=='=') msg[x]=1;
		if(msg[x]=='&') msg[x]=2;
		x++;
	}
}
void Get(char *msg)						
{
	char *in;
	in=getenv("QUERY_STRING");
	Transparency(in);
	Spaces(in);
	Ascii(in);
	int x=0;
	while(in[x])
	{
		msg[x]=in[x];
		x++;
	}
	msg[x]=0;
}

void Post(char *msg)					//The old problem was it never returned a value
{
	char in[10000];
	int c;
	for(c=0;!feof(stdin);c++)
	{
		in[c]=getc(stdin);
	}
	in[c+1]=0;
	Transparency(in);
	Spaces(in);
	Ascii(in);
	int x=0;
	while(in[x])
	{
		msg[x]=in[x];
		x++;
	}
	msg[x]=0;
}

void NextParam(char *msg,char *param)
{
	int x=0,c=0;
	int again=1;
	while(msg[x]!=0&&again==1)
	{
		if(msg[x]==1)		//Assigned to wherever there is an equal sign
		{
			msg[x]=2;	//Replace '=' with '&' so it skips this param next time
			x++;	//To skip the '=' sign
			while(msg[x]!=2&&msg[x])	//Stop when it finds '&' or end of string
			{
				param[c]=msg[x];
				x++;c++;
			}
			param[c]=0;
			again=0;
		}
		x++;
	}
	if(again==1) param[0]=0;	//dont display garbage if nothing is found
}
