/////////////////////////// FUNCTIONS ///////////////////////////
#include <string.h>
#include <fstream.h>

//Reading line from line
//Return: 1 - Successful
//        0 - EOF
//       -1 - Error
int Rd(ifstream file, char *buffer, unsigned int MaxStrLen)
{
	char ch;
	unsigned int i=0;

	do
	{
		if (i>MaxStrLen) return -1;
		file.get(ch);
			if (ch==EOF) return 0;
			if (ch=='\n') ch=0;
		buffer[i++]=ch;//first array, then i++
	}
	while ((ch!=EOF) && (ch!=0));

	return 1;
}

//Getting (Extracting) parameter
//Return: 0 - Successful
//       -1 - Input line is empty
//       -2 - Input line is too short
//       -3 - Can't find 'param'
int GetParam(char *bufferI, char *param, 
			 char *bufferO, unsigned int StrMaxLen)
{
	unsigned int lenI, lenparam;

	lenI=strlen(bufferI);
	lenparam=strlen(param);
	
	if (lenI==0) return -1;
	if (lenparam>=lenI) return -2;
	
	if (strncmp(bufferI, param, lenparam)!=0) return -3;

	strcpy(bufferO,&bufferI[lenparam]);
	
	return 0;
}

//Hex (in string) to unsigned int
unsigned int hextoint(char *hex)
{
	int i; //must be with sign !!!
	unsigned int len=strlen(hex);
	unsigned int res=0, f=1;
	char c;
	unsigned int c1;
	
	if (len!=0)
	{
		for (i=len-1; i>=0; i--)
		{
			c=hex[i];

			if      ((c>='0') && (c<='9')) c1=c-'0';
			else if ((c>='A') && (c<='F')) c1=c-'A'+10;
			else if ((c>='a') && (c<='f')) c1=c-'a'+10;
			else break;

			res+=c1*f;
			f*=16;
		}
	}
	
	return res;
}
