#include <iostream>
#include <sstream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
#include <string>
#include <time.h>
#include <math.h>

using namespace std;

#define DEBUG 1
#define MAXTRAIN 7131
#define MAXTEST  7131
#define M 78
#define N 69

double convertFromString(const std::string& s) {
  std::istringstream i(s);
  double x;
  if (i >> x)
    return x;

  // some sort of error handling goes here...
  //
  cout << "WARNING: not a good covert!!" << endl;
  return 0.0;
} 


int main(int argc, char **argv) {
  long K;
  int i,j;
  double duration;
  int correctness = 0, amltoall = 0, alltoaml = 0;
  int amlcorrect = 0, allcorrect = 0;
  clock_t start;
  
  char* class_result[80];
  for(long i = 0; i < 80; i++) {
    class_result[i] = new char[4];
    class_result[i][0] = '\0';
  }
  
  if(argc != 3) {
    cout<<"wrong parameter number\n";
    cout<<"Usage: exefile trainfile testfile"<<endl;
    exit(1);
  }

  start = clock();
  char trainfilename[256];   // filename of the train dataset
  char testfilename[256];    // filename of the test dataset
  
  strcpy(trainfilename, argv[1]);
  strcpy(testfilename, argv[2]);
  
  cout << "train file is: [" << (char*) trainfilename << "]\n";
  cout << "test file is: [" << (char*) testfilename << "]\n";
  
  fstream trainfile;
  trainfile.open(trainfilename, ios::in);
  if ( !trainfile ) {
    return -1;
  }

  fstream testfile;
  testfile.open(testfilename, ios::out);
  if ( !testfile ) {
    return -1;
  }  
  
  string xyz;

  string ** train_pattern;
  string ** test_pattern;  

  int train_elements;
  int test_elements;

  int train_lines;
  int test_lines;
  
  train_pattern = new string*[MAXTRAIN];
  for(long i = 0; i < MAXTRAIN; i++)
    train_pattern[i] = new string[M];

  test_pattern = new string*[MAXTEST];
  for(long i = 0; i < MAXTEST; i++)
    test_pattern[i] = new string[N];

  char ch;
  char prev;
  int lines =0;
  int maxelement = 0;;
  int elements = 0;

  while (trainfile.get(ch)) {
    // cout<<"["<<prev<<ch<<"] ";
    if(ch =='\n') {
      if(prev=='.') {
	//xyz += "\0";
	int pos = xyz.find('.');
	cout <<pos<<endl;
	cout<<xyz<<endl;
	xyz.substr(0, pos);
	cout<<"["<<xyz<<"]"<<endl;
	cout<<"["<<xyz.substr(0, pos)<<"]"<<endl;	
	
	train_pattern[lines][elements++] = xyz.substr(0, pos);
	xyz.erase();
	lines++;
	elements =0;
      }
      else {
	prev=ch;
	xyz.erase();
	if(elements == 0) continue;
	lines++;
	if(maxelement <elements)
	  maxelement = elements;
	elements = 0;
	cout <<"lines="<<lines<<"\n";
	continue;
      }
    }

    if(ch == ':' || ch == ',') {       
      //      cout <<xyz<<"\n";
      //xyz += "\0";

      train_pattern[lines][elements++] = xyz;
      xyz.erase();
    }
    else {
      xyz+=ch;
    }
    prev=ch;

  }

  for (long i=0; i<lines; i++) {
    for (long j =1; j<maxelement; j++) {
      if(train_pattern[i][j] == "") {
	if(j==1) {
	  testfile<<"building: "<<train_pattern[i][0]<<endl;
	  testfile<<"name: "<<train_pattern[i][0]<<endl;
	}
	break;
      }
      testfile<<"building: "<<train_pattern[i][0]<<endl;
      testfile<<"name: "<<train_pattern[i][j]<<endl;      
    }
    //    cout<<endl;
  }
    
  duration=(double)(clock()-start)/CLOCKS_PER_SEC;
  printf( "\nTotal time is: %8.2f seconds\n", duration );

  return 0;
}





