//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "ins.h"

//---------------------------------------------------------------------------
TIns::TIns()
{
	Fa = Fb = Fc = Faa = Fab = Fac = 0;
}
bool _fastcall TIns::LoadFromInsFile(AnsiString FN)
{
	TStringList *S = new TStringList;
	AnsiString Tmp;
	char *p, *str = NULL;
	bool res = false;
	short params_found = 0;
	try
	{
		S->LoadFromFile(FN);
	}
	catch(...)
	{
		goto exit;
	}
	for( int i=0; i < S->Count; i++ )
	{
		Tmp = S->Strings[i];
		str = new char[Tmp.Length()+1];
		strcpy(str, Tmp.c_str());
		Tmp = strtok(str, " ");
		Tmp = Tmp.LowerCase();
		if( Tmp == "cell" )
		{
			p = strtok(NULL, " ");	// lambda
			if( !p )				goto exit;
			p = strtok(NULL, " ");
			if( !p )				goto exit;
			Fa = atof(p);

			p = strtok(NULL, " ");
			if( !p )				goto exit;
			Fb = atof(p);

			p = strtok(NULL, " ");
			if( !p )				goto exit;
			Fc = atof(p);

			p = strtok(NULL, " ");
			if( !p )				goto exit;
			Faa = atof(p);

			p = strtok(NULL, " ");
			if( !p )				goto exit;
			Fab = atof(p);

			p = strtok(NULL, " ");
			if( !p )				goto exit;
			Fac = atof(p);
			params_found ++;
		}
		if( Tmp == "latt" )
		{
			p = strtok(NULL, " ");
			if( !p )				goto exit;
			FLattice = abs(atoi(p));
			params_found ++;
		}
		if( params_found == 2 )
		{
			res = true;
			goto exit;
		}
		delete [] str;
		str = NULL;
	}
exit:
	if( str )
		delete [] str;
	delete S;
	return res;
}

bool _fastcall TIns::LoadFromCifFile(AnsiString FN)
{
	Fa = Fb = Fc = Faa = Fab = Fac = 0;
	FLattice = 0;
	TStringList *S = new TStringList;
	AnsiString Tmp;
	char *p, *str = NULL;
	bool res = false;
	try
	{
		S->LoadFromFile(FN);
	}
	catch(...)
	{
		goto exit;
	}
	for( int i=0; i < S->Count; i++ )
	{
		Tmp = S->Strings[i].LowerCase();
		if( !Tmp.Length() )
			continue;
		str = new char[Tmp.Length()+1];
		strcpy(str, Tmp.c_str());
		Tmp = strtok(str, " ");
		if( !Tmp.Length() )
			goto next;
		if( Tmp[1] != '_' )
			goto next;
		if( Tmp == "_cell_length_a" )
		{
			p = strtok(NULL, " ");
			if( !p )				goto exit;
			try
			{
				Fa = atof(p);
			}
			catch(...){				goto exit;}
			goto next;
		}
		if( Tmp == "_cell_length_b" )
		{
			p = strtok(NULL, " ");
			if( !p )				goto exit;
			try
			{
				Fb = atof(p);
			}
			catch(...){				goto exit;}
			goto next;
		}
		if( Tmp == "_cell_length_c" )
		{
			p = strtok(NULL, " ");
			if( !p )				goto exit;
			try
			{
				Fc = atof(p);
			}
			catch(...){				goto exit;}
			goto next;
		}
		if( Tmp == "_cell_angle_alpha" )
		{
			p = strtok(NULL, " ");
			if( !p )				goto exit;
			try
			{
				Faa = atof(p);
			}
			catch(...){				goto exit;}
			goto next;
		}
		if( Tmp == "_cell_angle_beta" )
		{
			p = strtok(NULL, " ");
			if( !p )				goto exit;
			try
			{
				Fab = atof(p);
			}
			catch(...){				goto exit;}
			goto next;
		}
		if( Tmp == "_cell_angle_gamma" )
		{
			p = strtok(NULL, " ");
			if( !p )				goto exit;
			try
			{
				Fac = atof(p);
			}
			catch(...){				goto exit;}
			goto next;
		}
		if( Tmp == "_symmetry_space_group_name_h-m" )
		{
			p = strtok(NULL, " ");
			if( !p )				goto exit;
			int j = 0, l = strlen(p);

			while (1)
			{
				switch( p[j] )
				{
					case 'p':	FLattice = 1;		goto next_stp;
					case 'i':	FLattice = 2;		goto next_stp;
					case 'r':	FLattice = 3;		goto next_stp;
					case 'f':	FLattice = 4;		goto next_stp;
					case 'a':	FLattice = 5;		goto next_stp;
					case 'b':	FLattice = 6;		goto next_stp;
					case 'c':	FLattice = 7;		goto next_stp;
					default:
					j++;
					if( j >= l )	// we did not find the centering
						goto next_stp;
				}
			}
next_stp:
		}

		if( Fa && Fb && Fc && Faa && Fab && Fac && FLattice )
		{
			res = true;
			delete [] str;
            str = NULL;
			goto exit;
		}
next:
		delete [] str;
		str = NULL;
	}
exit:
	if( str )
		delete [] str;
	delete S;
	return res;
}

#pragma package(smart_init)

