/*************************************************************************************************************
* This file contains all important core functions
* 2012/8 by Dlxko
* Version 3.0
**************************************************************************************************************/
#include "Functions.h"

// Local static variables
static int index;
static int int_packet_len;
static int i;
static int counter;
static bool found;

// Function to extract data from char array
void function_midcopy ( char* input, char* output, int start_pos, int stop_pos)
{
	// Counter
	index = RESET;
	i = start_pos;

	// Cycle
	do
	{
		// Transfer
		output[index] = input[i];
		index++;
		i++;
	}
	while ( i < stop_pos);

	// Output value
	output[index] = 0;
}

// Function to convert package lenght from char to int
int function_convert_packet_lenght(char function_char_lenght[LENGHT_SIZE+1])
{
	int_packet_len = 0;
	char *char_packet_len;

	// Convert and get informations
	if (strlen(function_char_lenght) > 0)
	{
		char_packet_len = function_char_lenght;
		int_packet_len = atoi( char_packet_len );
	}
	else int_packet_len = 0;

	return int_packet_len;
}

// Function to get buddy name from number
int function_get_buddy_number(char nick_name[10])
{
	// Use counter and found bool
	counter = 0;
	found = false;

	//Cycle
	do
	{
		// Prevent crash and compare
		if ( client[counter].nick_name )
			if ( (strcmp(client[counter].nick_name, nick_name)) == 0 )
			{
				found = true;
				break;
			}
			++counter;
	}
	while ( counter < MAX_CLIENTS );

	// Check if found
	if (found == false)
		counter = MAX_CLIENTS + 1;

	// Return buddy number
	return counter;
}

// Function to get buddy name from number
gboolean function_junk_broadcast(gpointer data)
{
	// Use counter
	counter = 0;

	//Cycle
	do
	{
		// If connected, Send junk message
		if ( client[counter].connected)
			function_send_data(&client[counter], "0", 1);
		++counter;
	}
	while ( counter < MAX_CLIENTS );

	// Return allways true
	return true;
}