/* --- STRINGS.CPP ---
/  
/ Written by Phil Himsworth, 2001-2002.
/
/*/

#include <windows.h>
#include "wamessage.h"
#include "types.h"
#include "main.h"

// This would be so much easier if Clib didn't have memory leaks within CreateThread...
// Oh well, here are the random string doodles

// (And if I had found the Win32 string functions first...!)


// Returns a substring ending with the delimiter
char * GetDelimitedString(char * buffer, char delimiter, char * result, int result_len)
{
	int ptr = 0;
	while(buffer[ptr] != delimiter && ptr < result_len)
	{
		result[ptr] = buffer[ptr];
		if (buffer[ptr] == 0)			// Check for end of string
			return NULL;
		ptr++;
	}
	result[ptr] = 0;

	return buffer+ptr;
}


// --------------------------------------------------------------------------------
// Gets a string not in quotes
bool GetUnquotedString(char * buffer, char * result, int result_len)
{
	int resptr = 0, ptr = 0;
	while(buffer[ptr] != ' ' && buffer[ptr] != ',' && buffer[ptr] != 0 && ptr != result_len)	// copy string
	{
		result[resptr] = buffer[ptr];
		ptr++;
		resptr++;
	}
	result[resptr] = 0;

	return true;
}

// --------------------------------------------------------------------------------
// Gets a string within quotes
bool GetQuotedString(char * buffer, char * result, int result_len)
{
	int ptr = 0;
	while(buffer[ptr] != '\"')		// find first "
	{
		if (buffer[ptr] == 0)			// end of string and no quote start found; fail
			return false;
		ptr++;
	}
	
	// ptr points to first "; move on 1 to start of string
	ptr++;

	int resptr = 0;
	while(buffer[ptr] != '\"' && buffer[ptr] != 0 && ptr != result_len)		// copy string until closing "
	{													// or end of buffer
		result[resptr] = buffer[ptr];
		ptr++;
		resptr++;
	}
	result[resptr] = 0;									// make last char 0
	
	return true;
}



// --------------------------------------------------------------------------------
// Checks if string2 begins with string1
bool StartString(char* string1, char* string2)
{
	//char temp[2048];
	//wsprintf(temp,"1: %s\n2:%s",string1,string2);
	//MessageBox(NULL,temp,"gen_httpSrv StartString",MB_OK);
	int ptr = 0; 

	while(ToLowerCase(string1[ptr]) == ToLowerCase(string2[ptr]))		// Keep going until they're different
	{
		if (string1[ptr] == 0)				// if string1 finishes, they're the same
			return true;
		ptr++;
	}

	if (string1[ptr] == 0)					// If they're different because string1 has ended, yes
		return true;
	else
		return false;						// If they're different and string1 has not ended, no
}
	
// --------------------------------------------------------------------------------
// Retrieves next token from buffer
char * GetAString(char* chptr, char* text)			// Old GetAString - only accepts single words
{
	int bptr = 0;
	int tptr = 0;
	while (chptr[bptr] == ' ' || chptr[bptr] == '\n' || chptr[bptr] == '\r')	// skip leading spaces and stuff
		bptr++;

	while (chptr[bptr] != 0 && chptr[bptr] != ' ' && chptr[bptr] != '\n' && chptr[bptr] != '\r' && bptr < 255)	// while current char is a letter 
	{
		text[tptr] = chptr[bptr];					// add letter to text[]
		bptr++;
		tptr++;
	}
	text[tptr] = 0;
	chptr = chptr + bptr;
	return chptr;					// return pointer to the end of this string
}

// --------------------------------------------------------------------------------
// Mangles the time into a displayable format
void PrintTime(connection * conn, int mins,int seconds)
{
	char tbuffer[20];
	if (seconds<10)		// Avoids it printing eg. '4:2' when it should be '4:02' 
		wsprintf(tbuffer,"%d:0%d",mins,seconds);
	else
		wsprintf(tbuffer,"%d:%d",mins,seconds);
	prints(conn,tbuffer);
}


void PrintTrackTime(connection * conn)
{
	int tracklen;
	int trackpos = SendMessage(WAwnd(),WM_USER, 0, WAU_TRACKPOS) / 1000;
	int mins = trackpos / 60;
	int seconds = trackpos % 60;
	prints(conn, " (");

	PrintTime(conn,mins,seconds);
	
	tracklen = SendMessage(WAwnd(),WM_USER, 1, WAU_TRACKLENGTH);
	mins = tracklen / 60;
	seconds = tracklen % 60;
	prints(conn, " / ");
	PrintTime(conn,mins,seconds);
	prints(conn, ")\n\r");
}

// --------------------------------------------------------------------------------
// Convert a string to lower case
void ToLowerCase(char * string)
{
	int i=0;
	while (string[i] != 0 && i < 255)
	{
		if (string[i] >= 'A' && string[i] <= 'Z')
			string[i] += 32;
		i++;
	}
}

// --------------------------------------------------------------------------------
// Convert a char to lower case
char ToLowerCase(char c)
{
	if (c >= 'A' && c <= 'Z')
		c += 32;
	return c;
}


// --------------------------------------------------------------------------------
// Blatantly better Strcmp using win32 lstrcmp
bool StrComp(char * string1, char * string2)
{
	if (lstrcmp(string1, string2) == 0)			// Converts result from lstrcmp to a bool
		return true;
	else
		return false;
}

// --------------------------------------------------------------------------------
// Converts '/' characters to '\'... it's Opera trying to be helpful, dammit!
void ReplaceSlashes(char * string)
{
	int ptr=0;
	while (string[ptr]!=0)
	{
		if (string[ptr]=='/')
			string[ptr] = '\\';
		ptr++;
	}
}

// --------------------------------------------------------------------------------
// Retrieves a filename from a full path
char * GetFilename(char * path)
{
	char * filename;

	for (int i=0;path[i] != 0;i++)
		if (path[i]=='\\')
			filename = path+i;

	return filename+1;				// ignore leading slash
}

// --------------------------------------------------------------------------------
// Non-clib version of atoi
int atoi2(const char *str )
{
  int val = 0, mlt = 1;
  char *p;
  p = (char *) (str + lstrlen( str ) - 1);
  for( ; p >= str; --p, mlt *= 10 ) {
    val += (mlt * ((int)*p - '0'));
  }
  return val;
}

// --------------------------------------------------------------------------------
// Checks if string contains a character
bool contains(char * echars,char ch)
{
	int x=0;
	while(echars[x] != 0)
	{
		if (echars[x] == ch)
			return true;
		x++;
	}
	return false;
}

// --------------------------------------------------------------------------------
// Finds substring
char * strstr2(char *string, char *substring)
{
    char *a, *b;

    b = substring;
    if (*b == 0) {
	return string;
    }
    for ( ; *string != 0; string += 1) {
	if (*string != *b) {
	    continue;
	}
	a = string;
	while (1) {
	    if (*b == 0) {
		return string;
	    }
	    if (*a++ != *b++) {
		break;
	    }
	}
	b = substring;
    }
    return NULL;
}
