
#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>

#include "../plx_lib/plx.h"






/* run this program using the console pauser or add your own getch, system("pause") or input loop */



int main(int argc, char** argv) {
	
	int nextcol = 1;
	int row = 0; 
	std::map<int,int> mapping; 
	std::vector<std::vector<double> *> store;
	std::vector<const char *> headings;
	
	FILE  * f;
	if(argc > 1) {
	
	   f=fopen(argv[1],"r");
	   if(f){
	   	  while (!feof(f))
				{
				int key;
				int index;
			  int value;
						  
				
				int c = fscanf(f,"%d,%d,%d\n",&key,&index,&value);
				
				
				int search = key * 10 + index; 
				
				int & col = mapping[search];
				
				if(col == 0)
				{
					col = nextcol++;
					mapping[search] = col;
				}
				
			
				
       if (col > store.size())
        {
        	headings.push_back( PLX_Obs_Names [key]); // save column heading 
        	store.push_back( new std::vector<double> ); // save value 
        	printf("Alloc heading %s\n",headings[col-1]);
				}
	      std::vector<double> * vec = store[col-1];
					
				
				// decode scaling 
				double res = 0; 
				switch (key)
				{
					
					case PLX_AFR:
			    	 vec->push_back(ConveriMFDRaw2Data(key , AFR_Gasoline, value)) ;
						 break;
				  case PLX_RPM:
						 vec->push_back( ConveriMFDRaw2Data(key ,0, value)) ;
          		 break;

          case PLX_X_CHT:
       	     vec->push_back(ConveriMFDRaw2Data(key ,TEMP_Celsius, value)); 
					 		 				 break;
					 		 				 
					default:
						break;
				}
				
				if(col == 1)
				{
					row++;
				}
				
			//	printf("%3d %3d %10d\n", key,index,value);
					
				}
			}
	   	
}
	
	// having gathered all the data print it
	
	for(auto it = headings.begin(); it != headings.end() ;it++)
	{
		printf("%s,",*it);
	}
		printf("\n");
	
	for(size_t print = 0; print < row ; print++)
	{
	

	for(auto it = store.begin(); it != store.end() ;it++)
		  
		  
		  {

			       double val = 0.0;
			       	if(*it != nullptr) 
			 		 	  {
						  std::vector<double>  vec =  **it;
			        if(print < vec.size())
 		  	      val = vec[print];
							}
		  	     	printf("%0.2f," ,val);
			     }
		
		printf("\n");
		
	}
	
	
	return 0;
}

