#include "math.h"
#include "my_font.h"
#include "MyLongString.h"

static void reverse(char *str, int n) 
{
	int p = 0, q = n-1;
	char t;
	while (p < q) {
		t = str[p];
		str[p] = str[q];
		str[q] = t;
		p++;
		q--;
	}
}

void TMyLongString::Clear(void)
{
	Text[0] = 0;
	Text[1] = 0;
	Text[2] = 0;
}

void TMyLongString::Insert(const char *source, int Pst)
{
	//dMYSTRING_LENGTH
	int i,x;
	unsigned int ilen, len;
	unsigned int sp;
	const char *aP = source;
	
	x = GetEndPst();
	if(x <= Pst){	
		AddOnStr(source);
		return;
	}
	
	for(i=0; i<dMYLONGSTRING_LENGTH; i++)TmpChar[i] = 0;
	sp = GetLength();
	x =0;
	for(i=Pst+1; i<sp; i++){
		TmpChar[x] = Text[i];
		x++;
	}
	Text[Pst+1] = 0;
	AddOnStr(source);
	AddOnStr(TmpChar);
	Text[dMYLONGSTRING_LENGTH - 1] = 0;
}

void TMyLongString::Insert(char *source, int Pst)
{
	//dMYSTRING_LENGTH
	int i,x;
	unsigned int ilen, len;
	unsigned int sp;
	char *aP = source;
	
	x = GetEndPst();
	if(x <= Pst){	
		AddOnStr(source);
		return;
	}
	
	for(i=0; i<dMYLONGSTRING_LENGTH; i++)TmpChar[i] = 0;
	sp = GetLength();
	x =0;
	for(i=Pst+1; i<sp; i++){
		TmpChar[x] = Text[i];
		x++;
	}
	Text[Pst+1] = 0;
	AddOnStr(source);
	AddOnStr(TmpChar);
	Text[dMYLONGSTRING_LENGTH - 1] = 0;
}

unsigned int  TMyLongString::Delete(int Pst)
{
	//dMYSTRING_LENGTH
	unsigned int i;
	unsigned int ilen;
	unsigned int sp;
	for(i=0; i<dMYLONGSTRING_LENGTH; i++)TmpChar[i] = 0;
	sp = 0;
	for(i=Pst+1;  i < (dMYLONGSTRING_LENGTH-1); i++){
		TmpChar[sp] = Text[i];
		if(TmpChar[sp] == 0)break;
		sp++;
	}
	if(Pst == -1){
		Clear();
		ilen = 0;
	}else
	if(Pst == 0){
		Clear();
		ilen = 1;
	}else{
		if( (Text[Pst] & 0x80) == 0){
			//this is AscII
			Text[Pst] = 0;
			ilen = 1;
		}else{
			Text[Pst-1] = 0;
			Text[Pst] = 0;
			ilen = 2;
		}
	}
	AddOnStr(TmpChar);
	return ilen;
}

void TMyLongString::FromStr(const char *source)
{
	unsigned int i;
	unsigned int len;
	len = dMYLONGSTRING_LENGTH -1;
	for(i=0; i<len; i++){
		Text[i]=source[i];
		if(source[i] == 0)break;
	}
	Text[len] = 0;
}

void TMyLongString::FromStr(char *source)
{
	unsigned int i;
	unsigned int len;
	len = dMYLONGSTRING_LENGTH -1;
	for(i=0; i<len; i++){
		Text[i]=source[i];
		if(source[i] == 0)break;
	}
	Text[len] = 0;
}

int TMyLongString::GetEndPst(void)
{
	unsigned int i;
	if(Text[0] == 0)return -1;
	for(i=1;i<dMYLONGSTRING_LENGTH;i++){
		if(Text[i] == 0){
			return i-1;
		}
	}
	return -1;
}

int TMyLongString::GetLength(void)
{
	return GetEndPst() + 1;	
}

void TMyLongString::AddOnStr(const char *source)
{
	unsigned int i;
	unsigned int d=0;
	for(i=GetEndPst()+1; i<(dMYLONGSTRING_LENGTH-1); i++){
		Text[i]=source[d];
		if(source[d] == 0)break;
		d++;
	}
	Text[dMYLONGSTRING_LENGTH-1]=0;
}

void TMyLongString::AddOnStr( char *source)
{
	unsigned int i;
	unsigned int d=0;
	for(i=GetEndPst()+1; i<(dMYLONGSTRING_LENGTH-1); i++){
		Text[i]=source[d];
		if(source[d] == 0)break;
		d++;
	}
	Text[dMYLONGSTRING_LENGTH-1]=0;
}

void TMyLongString::ToStr(char *Dest, unsigned int len)
{
	unsigned int i;
	unsigned int limited;
	if(len<(dMYLONGSTRING_LENGTH-1)) limited=len; else limited=dMYLONGSTRING_LENGTH-1;
	i = sizeof(Text);
	if(i<limited) limited = i;
	for(i=0; i<limited; i++){
		Dest[i] = Text[i];
		if(Text[i] == 0)break;
	}
	Dest[i] = 0;
}

void TMyLongString::FromInt(int data)
{
	unsigned int i;
	unsigned int sig;
	unsigned int d,e;
	
	for(i=0;i<32;i++)TmpChar[i] = 0;
	
	if(data < 0){
		sig = 0;
		d = (unsigned int)(0 - data);
	}else{
		sig = 1;
		d = (unsigned int)data;
	}

	if(d == 0){
		Text[0] = '0';
		Text[1] = 0;
	}else{
		i = 0;
		while(d>0){
			e = d % 10;
			TmpChar[i] = e + '0';
			i++;
			d /= 10;
		}
		if(sig){
			d = i;
			while(i>0){
				Text[d-i] = TmpChar[i-1];
				i--;
			}
			Text[d] = 0;			
		}else{
			d = i;
			while(i>0){
				Text[d-i+1] = TmpChar[i-1];
				i--;
			}
			Text[0] = '-';
			Text[d+1] = 0;			
		}
	}	
}

void TMyLongString::FromUInt4Dg(unsigned int data)
{
	unsigned int d,e;
	
	d = data/1000;
	e = d % 10;
	Text[0] = e + '0';
	
	d = data/100;
	e = d % 10;
	Text[1] = e + '0';
	
	d = data/10;
	e = d % 10;
	Text[2] = e + '0';
	
	e = data % 10;
	Text[3] = e + '0';
	
	Text[4] = 0;
}

void TMyLongString::FromUInt6Dg(unsigned int data)
{
	unsigned int d,e;
	
	d = data/100000;
	e = d % 10;
	Text[0] = e + '0';
	
	d = data/10000;
	e = d % 10;
	Text[1] = e + '0';
	
	d = data/1000;
	e = d % 10;
	Text[2] = e + '0';
	
	d = data/100;
	e = d % 10;
	Text[3] = e + '0';
	
	d = data/10;
	e = d % 10;
	Text[4] = e + '0';
	
	e = data % 10;
	Text[5] = e + '0';
	
	Text[6] = 0;
}


void TMyLongString::AddOnDgMax6(unsigned int data)
{
	unsigned int d,e,i,pst, IsFound;
	pst = GetEndPst() +1;
	if(pst > (dMYLONGSTRING_LENGTH - 8))return;
	
	IsFound = 0;
	
	d = data/100000;
	e = d % 10;
	if(e>0){
		Text[pst] = e + '0';
		pst++;
		IsFound = 1;
	}
	
	d = data/10000;
	e = d % 10;
	if((e>0)||(IsFound)){
		Text[pst] = e + '0';
		pst++;
		IsFound = 1;
	}
	
	d = data/1000;
	e = d % 10;
	if((e>0)||(IsFound)){
		Text[pst] = e + '0';
		pst++;
		IsFound = 1;
	}
	
	d = data/100;
	e = d % 10;
	if((e>0)||(IsFound)){
		Text[pst] = e + '0';
		pst++;
		IsFound = 1;
	}
	
	d = data/10;
	e = d % 10;
	if((e>0)||(IsFound)){
		Text[pst] = e + '0';
		pst++;
		IsFound = 1;
	}
	
	e = data % 10;
	Text[pst++] = e + '0';
	
	Text[pst++] = 0;
	Text[pst++] = 0;	
}

void TMyLongString::AddOn6Dg(unsigned int data)
{
	unsigned int d,e,i,pst;
	pst = GetEndPst() +1;
	if(pst > (dMYLONGSTRING_LENGTH - 8))return;
	
	d = data/100000;
	e = d % 10;
	Text[pst] = e + '0';
	
	d = data/10000;
	e = d % 10;
	Text[pst+1] = e + '0';
	
	d = data/1000;
	e = d % 10;
	Text[pst+2] = e + '0';
	
	d = data/100;
	e = d % 10;
	Text[pst+3] = e + '0';
	
	d = data/10;
	e = d % 10;
	Text[pst+4] = e + '0';
	
	e = data % 10;
	Text[pst+5] = e + '0';
	
	Text[pst+6] = 0;
}

void TMyLongString::AddOn4Dg(unsigned int data)
{
	unsigned int d,e,i,pst;
	pst = GetEndPst() +1;
	if(pst > (dMYLONGSTRING_LENGTH - 6))return;
	
	d = data/1000;
	e = d % 10;
	Text[pst] = e + '0';
	
	d = data/100;
	e = d % 10;
	Text[pst+1] = e + '0';
	
	d = data/10;
	e = d % 10;
	Text[pst+2] = e + '0';
	
	e = data % 10;
	Text[pst+3] = e + '0';
	
	Text[pst+4] = 0;
}

void TMyLongString::FromUInt3Dg(unsigned int data)
{
	unsigned int i;
	unsigned int d,e;
	
	d = data/100;
	e = d % 10;
	Text[0] = e + '0';
	
	d = data/10;
	e = d % 10;
	Text[1] = e + '0';
	
	e = data % 10;
	Text[2] = e + '0';
	
	Text[3] = 0;
	Text[4] = 0;
}

void TMyLongString::AddOn3Dg(unsigned int data)
{
	unsigned int i,pst;
	unsigned int d,e;
	
	pst = GetEndPst() +1;
	if(pst > (dMYLONGSTRING_LENGTH - 5))return;
	
	d = data/100;
	e = d % 10;
	Text[pst] = e + '0';
	
	d = data/10;
	e = d % 10;
	Text[pst+1] = e + '0';
	
	e = data % 10;
	Text[pst+2] = e + '0';
	
	Text[pst+3] = 0;
	Text[pst+4] = 0;
}

void TMyLongString::FromUInt2Dg(unsigned int data)
{
	unsigned int i;
	unsigned int d,e;
	
	d = data/10;
	Text[0] = d + '0';
	
	e = data % 10;
	Text[1] = e + '0';
	
	Text[2] = 0;
	Text[3] = 0;
}

void TMyLongString::AddOn2Dg(unsigned int data)
{
	unsigned int i,pst;
	unsigned int d,e;
	
	pst = GetEndPst() +1;
	if(pst > (dMYLONGSTRING_LENGTH - 4))return;
	
	d = data/10;
	Text[pst] = d + '0';
	
	e = data % 10;
	Text[pst+1] = e + '0';
	
	Text[pst+2] = 0;
	Text[pst+3] = 0;
}
	
void TMyLongString::FormFloat(float fd)
{
	//unsigned int ava_bit = 0;
	float f = fd;
	unsigned int sig;
	signed int i = 0;
	signed int n = (int)f;
	
	if(f < 0){
		sig = 0;
		f *= -1;
	}else{
		sig = 1;
	}
	n = (unsigned int)f;
	f -= n;
	i = 0;
	do{
		Text[i++] = n % 10 + '0';
		n /= 10;
	}while(n > 0);
	if(0 == sig) Text[i++] = '-';

	reverse(Text , i);
	Text[i++] = '.';
	n = 0;
	do{
		int t = (int)(f * 10);
		Text[i++] = t + '0';
		f = f * 10 - t;
		n++;
	}while(f > 0 && n < 9);
	Text[i] = '\0';	
}

int TMyLongString::ToInteger(void)
{
	signed int Result;
	unsigned int i;
	unsigned int sig;

	if(Text[0] == '-'){
		sig = 0;
		i = 1;
	}else{
		sig = 1;
		i = 0;
	}
	Result = 0;
	while(1){
		if(Text[i] < '0' || Text[i] > '9'){
			if(Text[i] == 0) break;
			Result = 0;
			break;
		}else{	
			Result *= 10;
			Result += Text[i] - '0';
		}
		i++;
	}
	if(sig == 0) Result = 0 - Result;
	return Result;
	
}

float TMyLongString::ToFloat(void)
{
	float Result;

	float f;
	unsigned int i;
	unsigned int sig;
	unsigned int dot_found = 0;
	signed int Decimal_counts = 0;
	unsigned int valid_char_counts = 0;
	if(Text[0] == '-'){
		sig = 0;
		i = 1;
	}else{
		sig = 1;
		i = 0;
	}
	Result = 0;
	dot_found = 0;
	Decimal_counts = 0;
	while(1){
		if((Text[i] < '0' || Text[i] > '9') && (Text[i] != '.')){
			if(Text[i] != 0){
				Result = 0;
				break;
			}else{
				break;
			}
		}else{
			if(Text[i] == '.'){
				dot_found++;
			}else{
				if(Text[i] == '0'){
					if(valid_char_counts)valid_char_counts++;
				}else{
					valid_char_counts++;
				}	
				
				if(dot_found == 0){
					Result *= 10;
					Result += Text[i] - '0';
				}else if(dot_found == 1){
					Decimal_counts -=1;
					if(valid_char_counts > 12)break;
//					f = (Text[i] - '0') * pow(10,Decimal_counts);
					Result += f;
				}else{
					// error '0.0.' more then one dot found
					Result = 0;
					break;	
				}
			}
		}
		i++;
	}
	if(sig == 0) Result *= -1;
	return Result;
	
}


//Static function---------------->
void TMyLongString::sFromStr(char *source, char *dest)
{
	unsigned int i;
	unsigned int len;
	len = dMYLONGSTRING_LENGTH -1;
	for(i=0; i<len; i++){
		dest[i]=source[i];
		if(dest[i] == 0){
			break;
		}
	}
}
void TMyLongString::sFromStr(const char *source, char *dest)
{
	unsigned int i;
	unsigned int len;
	len = dMYLONGSTRING_LENGTH -1;
	for(i=0; i<len; i++){
		dest[i]=source[i];
		if(dest[i] == 0){
			break;
		}
	}
}

void TMyLongString::sFrom3Dg(unsigned int data, char *dest)
{
	unsigned int i;
	unsigned int d,e;
	d = data/100;
	e = d % 10;
	dest[0] = e + '0';
	
	d = data/10;
	e = d % 10;
	dest[1] = e + '0';
	
	e = data % 10;
	dest[2] = e + '0';
	
	dest[3] = 0;
	dest[4] = 0;
}

void TMyLongString::sFrom4Dg(unsigned int data, char *dest)
{
	unsigned int i;
	unsigned int d,e;
	
	d = data/1000;
	e = d % 10;
	dest[0] = e + '0';
	
	d = data/100;
	e = d % 10;
	dest[1] = e + '0';
	
	d = data/10;
	e = d % 10;
	dest[2] = e + '0';
	
	e = data % 10;
	dest[3] = e + '0';
	
	dest[4] = 0;
	dest[5] = 0;
}

void TMyLongString::sAddOnStr(const char *source, char *dest)
{
	unsigned int i;
	unsigned int d=0;
	int len = 0;
	for(i=0; i<(dMYLONGSTRING_LENGTH-1); i++){
		if(dest[i] != 0)len++;
		else break;
	}	
	for(i=len; i<(dMYLONGSTRING_LENGTH-1); i++){
		dest[i]=source[d];
		if(source[d] == 0)break;
		d++;
	}
}

void TMyLongString::sAddOnDgMax6(unsigned int data, char *dest)
{
	unsigned int d,e,i,pst, IsFound;
	pst = 0;
	for(i=0; i<(dMYLONGSTRING_LENGTH-1); i++){
		if(dest[i] != 0)pst++;
		else break;
	}
	pst;
	if(pst > (dMYLONGSTRING_LENGTH - 8))return;
	
	IsFound = 0;
	
	d = data/100000;
	e = d % 10;
	if(e>0){
		dest[pst] = e + '0';
		pst++;
		IsFound = 1;
	}
	
	d = data/10000;
	e = d % 10;
	if((e>0)||(IsFound)){
		dest[pst] = e + '0';
		pst++;
		IsFound = 1;
	}
	
	d = data/1000;
	e = d % 10;
	if((e>0)||(IsFound)){
		dest[pst] = e + '0';
		pst++;
		IsFound = 1;
	}
	
	d = data/100;
	e = d % 10;
	if((e>0)||(IsFound)){
		dest[pst] = e + '0';
		pst++;
		IsFound = 1;
	}
	
	d = data/10;
	e = d % 10;
	if((e>0)||(IsFound)){
		dest[pst] = e + '0';
		pst++;
		IsFound = 1;
	}
	
	e = data % 10;
	dest[pst++] = e + '0';
	
	dest[pst++] = 0;
	dest[pst++] = 0;	
}


void TMyLongString::sAddOn5Dg(unsigned int data, char *dest)
{
	unsigned int i;
	unsigned int d,e;
	int pst = 0;
	for(i=0; i<(dMYLONGSTRING_LENGTH-1); i++){
		if(dest[i] != 0)pst++;
		else break;
	}
	pst;
	if(pst > (dMYLONGSTRING_LENGTH - 7))return;
	
	d = data/10000;
	e = d % 10;
	dest[pst] = e + '0';
	
	d = data/1000;
	e = d % 10;
	dest[pst+1] = e + '0';
	
	d = data/100;
	e = d % 10;
	dest[pst+2] = e + '0';
	
	d = data/10;
	e = d % 10;
	dest[pst+3] = e + '0';
	
	e = data % 10;
	dest[pst+4] = e + '0';
	
	dest[pst+5] = 0;
}


void TMyLongString::sAddOn4Dg(unsigned int data, char *dest)
{
	unsigned int i;
	unsigned int d,e;
	int pst = 0;
	for(i=0; i<(dMYLONGSTRING_LENGTH-1); i++){
		if(dest[i] != 0)pst++;
		else break;
	}
	pst;
	if(pst > (dMYLONGSTRING_LENGTH - 7))return;
	
	d = data/1000;
	e = d % 10;
	dest[pst] = e + '0';
	
	d = data/100;
	e = d % 10;
	dest[pst+1] = e + '0';
	
	d = data/10;
	e = d % 10;
	dest[pst+2] = e + '0';
	
	e = data % 10;
	dest[pst+3] = e + '0';
	
	dest[pst+4] = 0;
}


void TMyLongString::sAddOn3Dg(unsigned int data, char *dest)
{
	unsigned int i;
	unsigned int d,e;
	int pst = 0;
	for(i=0; i<(dMYLONGSTRING_LENGTH-1); i++){
		if(dest[i] != 0)pst++;
		else break;
	}
	pst;
	if(pst > (dMYLONGSTRING_LENGTH - 5))return;
	
	d = data/100;
	e = d % 10;
	dest[pst] = e + '0';
	
	d = data/10;
	e = d % 10;
	dest[pst+1] = e + '0';
	
	e = data % 10;
	dest[pst+2] = e + '0';
	
	dest[pst+3] = 0;
}

void TMyLongString::sAddOn1Dg(unsigned int data, char *dest)
{
	unsigned int i;
	unsigned int d,e;
	int pst = 0;
	for(i=0; i<(dMYLONGSTRING_LENGTH-1); i++){
		if(dest[i] != 0)pst++;
		else break;
	}
	pst;
	if(pst > (dMYLONGSTRING_LENGTH - 5))return;
	
	e = data % 10;
	dest[pst] = e + '0';
	
	dest[pst+1] = 0;
}

void TMyLongString::sAddOn2Dg(unsigned int data, char *dest)
{
	unsigned int i;
	unsigned int d,e;
	int pst = 0;
	for(i=0; i<(dMYLONGSTRING_LENGTH-1); i++){
		if(dest[i] != 0)pst++;
		else break;
	}
	pst;
	if(pst > (dMYLONGSTRING_LENGTH - 5))return;
	
	d = data/10;
	e = d % 10;
	dest[pst] = e + '0';
	
	e = data % 10;
	dest[pst+1] = e + '0';
	
	dest[pst+2] = 0;
}

int TMyLongString::sGetLen(char *source)
{
	int i,aLen;
	aLen = 0;
	for(i=0; i<1024; i++){
		if(*source !=0 ){
			aLen++;
		}else{
			break;
		}
		source++;
	}
	return aLen;
}

void TMyLongString::sFillFixLen(char *source, unsigned int aLen)
{
	unsigned int len, i;
	len = sGetLen(source);
	for(i=len; i< aLen; i++){
		source[i] = ' ';
	}
	source[aLen] = 0;
}

void TMyLongString::sClear(char *dest)
{
	dest[0] = 0;
	dest[1] = 0;
	dest[2] = 0;
	dest[3] = 0;
}

int TMyLongString::sToInteger(char *Src)
{
	signed int Result;
	unsigned int i;
	unsigned int sig;

	if(Src[0] == '-'){
		sig = 0;
		i = 1;
	}else{
		sig = 1;
		i = 0;
	}
	Result = 0;
	while(1){
		if(Src[i] < '0' || Src[i] > '9'){
			if(Src[i] == 0) break;
			Result = 0;
			break;
		}else{	
			Result *= 10;
			Result += Src[i] - '0';
		}
		i++;
	}
	if(sig == 0) Result = 0 - Result;
	return Result;
	
}









