#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#define BUF_SIZE 2048
#define MAX_FEATURES 100
#define MIN_LEN 5
#define SPACE " "


#include "CEncode.h"
#include "CSupport.h"

void writeBinList(FILE *fd, float *list, int  len, char *sep = SPACE)
{
	int i;
	CEncode E;
	for(i=0;i<len;i++)
	{
		fprintf(fd, "%s%s",
			E.BinEncode16((double)list[i], -1, 1), sep);
	}

}

float *normList(float *retList, float *inList, float *normList, int len)
{
	int i;
	for(i=0;i<len;i++)
	{
		retList[i] = inList[i] / normList[i];

	}

	return retList;
}


void writeBin6List(FILE *fd, float *list, int  len, char *sep = SPACE)
{
	int i, j;
	const int bits = 8;
	double* temp = new double[bits];
	CSupport S;
	for(i=0;i<len;i++)
	{
		temp = S.Int2BinVector(temp, (int) ((list[i]+1)*127), bits);
		for(j=0;j<bits-1;j++)
		{
			fprintf(fd, "%i ", (int) temp[j]);
		}
		fprintf(fd, "%i%s", (int) temp[bits-1], sep);
	}

	delete [] temp;

}


void writeList(FILE *fd, float *list, int  len, char *sep = SPACE)
{
	int i;
	for(i=0;i<len;i++)
	{
		fprintf(fd, "%f%s", list[i], sep);
	}
}


int main()
{
	char in_file[30], out_file1[30], buf[BUF_SIZE],
	     restStr[BUF_SIZE], attrStr[10];
	char *firstStr, *rest;
	int i, cl_no,  sets=0, wrongs=0;
	float feature[MAX_FEATURES];
	float normFeature[MAX_FEATURES];
	float normVal[MAX_FEATURES];
	FILE *in, *out1;

	cout << "\nin file: ";
	cin >> in_file;

	cout << "\nBIN-out file: ";
	cin >> out_file1;


	if ((in = fopen(in_file, "rt"))
		    == NULL)
	{
	   cout << ERR_MSG << "Cannot open " << in_file << " !!!\n";
	   return -1;
	}

	if ((out1 = fopen(out_file1, "wt"))
		    == NULL)
	{
	   cout << ERR_MSG << "Cannot open " << out_file1 << " !!!\n";
	   return -1;
	}


	do
	{
	   strcpy(buf,"");
	   fgets(buf,BUF_SIZE, in);
	   strcpy(restStr,buf);

	   if(strlen(buf) > MIN_LEN)
	   {
		   // A1: a,b, ?
		   firstStr = strtok(restStr, ",");
		   sscanf(firstStr, "%s", attrStr);
		   if (attrStr[0] == 'a') feature[0] = 0;
		   else if (attrStr[0] == 'b') feature[0] = 1;
			else feature[0] = 0.5;
		   #if DEBUG
			printf("firstStr: %s\n", firstStr);
			printf("%f", feature[0]);
		   #endif
		   strcpy(restStr, restStr+(strlen(firstStr)+1));
		   #if DEBUG
			     printf("%s", restStr);
		   #endif


		   // A2: continious (13-80)
		   firstStr = strtok(restStr, ",");
		   sscanf(firstStr, "%f", &feature[1]);
		   feature[1] = feature[1]-13.75;
		   #if DEBUG
			printf("firstStr: %s\n", firstStr);
			printf("%f", feature[1]);
		   #endif
		   strcpy(restStr, restStr+(strlen(firstStr)+1));
		   #if DEBUG
			     printf("%s", restStr);
		   #endif


		   // A3: continious (0-20)
		   firstStr = strtok(restStr, ",");
		   sscanf(firstStr, "%f", &feature[2]);
		   #if DEBUG
			printf("firstStr: %s\n", firstStr);
			printf("%f", feature[2]);
		   #endif
		   strcpy(restStr, restStr+(strlen(firstStr)+1));
		   #if DEBUG
			     printf("%s", restStr);
		   #endif


		   // A4: u, y, l, t, ?
		   firstStr = strtok(restStr, ",");
		   sscanf(firstStr, "%s", attrStr);
		   switch (attrStr[0]) {
			case 'u' : feature[3] = 1; break;
			case 'y' : feature[3] = 2; break;
			case 'l' : feature[3] = 3; break;
			case 't' : feature[3] = 4; break;
			default:   feature[3] = 0;
		   }
		   #if DEBUG
			printf("firstStr: %s\n", firstStr);
			printf("%f", feature[3]);
		   #endif
		   strcpy(restStr, restStr+(strlen(firstStr)+1));
		   #if DEBUG
			     printf("%s", restStr);
		   #endif


		   // A5: g, p, gg, ?
		   firstStr = strtok(restStr, ",");
		   sscanf(firstStr, "%s", attrStr);
		   switch (attrStr[0]) {
			case 'p' :                      feature[4] = 1; break;
			case 'g' : if (attrStr[1] == 'g') feature[4] = 2;
				      else              feature[4] = 3;
				   break;
			default:   feature[4] = 0;
		   }
		   #if DEBUG
			printf("firstStr: %s\n", firstStr);
			printf("%f", feature[4]);
		   #endif
		   strcpy(restStr, restStr+(strlen(firstStr)+1));
		   #if DEBUG
			     printf("%s", restStr);
		   #endif


		   // A6: c, cc, d, i, j, k, m, r, q, w, x, e, aa, ff ?
		   firstStr = strtok(restStr, ",");
		   sscanf(firstStr, "%s", attrStr);
		   switch (attrStr[0]) {
			case 'c' : if (attrStr[1] == 'c') feature[5] = 1;
				      else              feature[5] = 2;
				   break;
			case 'd' :                      feature[5] = 3; break;
			case 'i' :                      feature[5] = 4; break;
			case 'j' :                      feature[5] = 5; break;
			case 'k' :                      feature[5] = 6; break;
			case 'm' :                      feature[5] = 7; break;
			case 'r' :                      feature[5] = 8; break;
			case 'q' :                      feature[5] = 9; break;
			case 'w' :                      feature[5] =10; break;
			case 'x' :                      feature[5] =11; break;
			case 'e' :                      feature[5] =12; break;
			case 'a' :                      feature[5] =13; break;
			case 'f' :                      feature[5] =14; break;
			default:   feature[5] = 0;
		   }
		   #if DEBUG
			printf("firstStr: %s\n", firstStr);
			printf("%f", feature[5]);
		   #endif
		   strcpy(restStr, restStr+(strlen(firstStr)+1));
		   #if DEBUG
			     printf("%s", restStr);
		   #endif



		   // A7: v, h, bb, j, n, z, dd, ff, o, ?
		   firstStr = strtok(restStr, ",");
		   sscanf(firstStr, "%s", attrStr);
		   switch (attrStr[0]) {
			case 'v' :                      feature[6] = 1; break;
			case 'h' :                      feature[6] = 2; break;
			case 'b' :                      feature[6] = 3; break;
			case 'j' :                      feature[6] = 4; break;
			case 'n' :                      feature[6] = 5; break;
			case 'z' :                      feature[6] = 6; break;
			case 'd' :                      feature[6] = 7; break;
			case 'f' :                      feature[6] = 8; break;
			case 'o' :                      feature[6] = 9; break;
			default:   feature[6] = 0;
		   }
		   #if DEBUG
			printf("firstStr: %s\n", firstStr);
			printf("%f", feature[6]);
		   #endif
		   strcpy(restStr, restStr+(strlen(firstStr)+1));
		   #if DEBUG
			     printf("%s", restStr);
		   #endif


		   // A8: continious ()
		   firstStr = strtok(restStr, ",");
		   sscanf(firstStr, "%f", &feature[7]);
		   #if DEBUG
			printf("firstStr: %s\n", firstStr);
			printf("%f", feature[7]);
		   #endif
		   strcpy(restStr, restStr+(strlen(firstStr)+1));
		   #if DEBUG
			     printf("%s", restStr);
		   #endif


		   // A9: t,f, ?
		   firstStr = strtok(restStr, ",");
		   sscanf(firstStr, "%s", attrStr);
		   if (attrStr[0] == 't') feature[8] = 0;
		   else if (attrStr[0] == 'f') feature[8] = 1;
			else feature[8] = 0.5;
		   #if DEBUG
			printf("firstStr: %s\n", firstStr);
			printf("%f", feature[8]);
		   #endif
		   strcpy(restStr, restStr+(strlen(firstStr)+1));
		   #if DEBUG
			     printf("%s", restStr);
		   #endif


		   // A10: t,f, ?
		   firstStr = strtok(restStr, ",");
		   sscanf(firstStr, "%s", attrStr);
		   if (attrStr[0] == 't') feature[9] = 0;
		   else if (attrStr[0] == 'f') feature[9] = 1;
			else feature[9] = 0.5;
		   #if DEBUG
			printf("firstStr: %s\n", firstStr);
			printf("%f", feature[9]);
		   #endif
		   strcpy(restStr, restStr+(strlen(firstStr)+1));
		   #if DEBUG
			     printf("%s", restStr);
		   #endif



		   // A11: continious ()
		   firstStr = strtok(restStr, ",");
		   sscanf(firstStr, "%f", &feature[10]);
		   #if DEBUG
			printf("firstStr: %s\n", firstStr);
			printf("%f", feature[10]);
		   #endif
		   strcpy(restStr, restStr+(strlen(firstStr)+1));
		   #if DEBUG
			     printf("%s", restStr);
		   #endif


		   // A12: t,f, ?
		   firstStr = strtok(restStr, ",");
		   sscanf(firstStr, "%s", attrStr);
		   if (attrStr[0] == 't') feature[11] = 0;
		   else if (attrStr[0] == 'f') feature[11] = 1;
			else feature[11] = 0.5;
		   #if DEBUG
			printf("firstStr: %s\n", firstStr);
			printf("%f", feature[11]);
		   #endif
		   strcpy(restStr, restStr+(strlen(firstStr)+1));
		   #if DEBUG
			     printf("%s", restStr);
		   #endif



		   // A13: g,p,s, ?
		   firstStr = strtok(restStr, ",");
		   sscanf(firstStr, "%s", attrStr);
		   switch (attrStr[0]) {
			case 'g' :                      feature[12] = 1; break;
			case 'p' :                      feature[12] = 2; break;
			case 's' :                      feature[12] = 3; break;
			default:   feature[12] = 0;
		   }
		   #if DEBUG
			printf("firstStr: %s\n", firstStr);
			printf("%f", feature[12]);
		   #endif
		   strcpy(restStr, restStr+(strlen(firstStr)+1));
		   #if DEBUG
			     printf("%s", restStr);
		   #endif


		   // A14: continious ()
		   firstStr = strtok(restStr, ",");
		   sscanf(firstStr, "%f", &feature[13]);
		   #if DEBUG
			printf("firstStr: %s\n", firstStr);
			printf("%f", feature[13]);
		   #endif
		   strcpy(restStr, restStr+(strlen(firstStr)+1));
		   #if DEBUG
			     printf("%s", restStr);
		   #endif


		   // A15: continious ()
		   firstStr = strtok(restStr, ",");
		   sscanf(firstStr, "%f", &feature[14]);
		   #if DEBUG
			printf("firstStr: %s\n", firstStr);
			printf("%f", feature[14]);
		   #endif
		   strcpy(restStr, restStr+(strlen(firstStr)+1));
		   #if DEBUG
			     printf("%s", restStr);
		   #endif


		   sscanf(restStr, "%s\n", attrStr);
		   if (attrStr[0] == '+') cl_no = 0;
			else cl_no = 1;
		   #if DEBUG
		     printf("\n result for %i: >%i<\n", i, cl_no);
		   #endif
	   }
	   else cl_no ='-1';

	   if (cl_no != 1 && cl_no != 0)
	   {

		cout << WARN_MSG << " Wrong result type !!!\n";
		wrongs ++;
	   }
	   else
	   {

		normVal[0]= 1;
		normVal[1]= 81;
		normVal[2]= 28;
		normVal[3]= 3;
		normVal[4]= 3;
		normVal[5]= 14;
		normVal[6]= 9;
		normVal[7]= 28.5;
		normVal[8]= 1;
		normVal[9]= 1;
		normVal[10]= 67;
		normVal[11]= 1;
		normVal[12]= 3;
		normVal[13]= 2000;
		normVal[14]= 100000;

		normList(normFeature, feature, normVal, 15);
		// writeList(out1, normFeature, 15);
		// writeBinList(out1, normFeature, 15);
		writeBin6List(out1, normFeature, 15);

		fprintf(out1, ": %i ;\n", cl_no);
		// fprintf(out1, "\n");

		sets ++;
	   }

	}
	while (!feof(in));

	fclose(out1);
	fclose(in);
	cout << "\n\n" << sets << " records read from file!";
	cout << "\n" << wrongs << " are not ok! (not written)\n\n";
	return 0;
}
