#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>


#define N_CONTROLLERS 5

#define N_FIELDS_0 2    //Battery Controller Data
#define FIELDS_0 {0,5} //BattVoltage, Hydrogen

#define N_FIELDS_1 4  //Spring Controller Data
#define FIELDS_1 {0,1,2,3}  //Load Cell, RangeFinder, Upper Pressure, Lower Pressure

#define N_FIELDS_2 3  // Power Converter Data
#define FIELDS_2 {0,4,5} //RPM, Total Power, Battery Current

#define N_FIELDS_3 2   //CrossBows data
#define FIELDS_3 {12,13} // Lat, Lon

#define N_FIELDS_4 8  //Trefoil Controller Data 
#define FIELDS_4 {0,1,2,3,19,20,21,22} //

#define MAXFIELDSIZE 32

#define MAXLINELENGTH 2048
#define DELIMITER ','

void main(void)
{
const char delim = DELIMITER;

int i,j,k;

int *controller[5];
int n_fields[N_CONTROLLERS] = {N_FIELDS_0, N_FIELDS_1, N_FIELDS_2, N_FIELDS_3, N_FIELDS_4};
int fields_0[N_FIELDS_0] = FIELDS_0;
int fields_1[N_FIELDS_1] = FIELDS_1;
int fields_2[N_FIELDS_2] = FIELDS_2;
int fields_3[N_FIELDS_3] = FIELDS_3;
int fields_4[N_FIELDS_4] = FIELDS_4;

int offset[N_CONTROLLERS] = {2+N_FIELDS_2,
			     2+N_FIELDS_2+N_FIELDS_0+N_FIELDS_3,
                             2,
                             2+N_FIELDS_2+N_FIELDS_0,
                             2+N_FIELDS_0+N_FIELDS_1+N_FIELDS_2+N_FIELDS_3};

char **field_buffers;

char *temp_buf;

int N_fieldbuffers;

int controller_number;

char *linebuf;
ssize_t n;
size_t N = MAXLINELENGTH;


for(n=0;n<N_CONTROLLERS;n++)
	printf("%d,",offset[n]);
printf("\n");

linebuf = (char *) malloc(N*sizeof(char));
if(linebuf == NULL)
	{
	perror("Unable to allocate buffer");
	exit(1);
	}

//Figure out how many field buffers are needed.
for(N_fieldbuffers=0,i=0;i<N_CONTROLLERS;i++)
  N_fieldbuffers = N_fieldbuffers + n_fields[i];
N_fieldbuffers = N_fieldbuffers+2;  //Add to to include controller number and timestamp at beginning.

//Allocate field buffers
field_buffers = (char **) malloc(N_fieldbuffers*sizeof(char *));
for(i=0;i<N_fieldbuffers;i++)
  {
  field_buffers[i] = (char *) malloc(MAXFIELDSIZE*sizeof(char));
  sprintf(field_buffers[i]," ",i);
  }


controller[0] = fields_0;
controller[1] = fields_1;
controller[2] = fields_2;
controller[3] = fields_3;
controller[4] = fields_4;

while(n = getline(&linebuf,&N,stdin) > 0)
{
   if(!isdigit(linebuf[0]))  //Ignore any line that doesn't start with a digit, i.e. the header line.
	continue;

  i = 0;
  strcpy(field_buffers[i++],(const char *) strtok(linebuf,&delim)); 
  sscanf(field_buffers[i-1],"%d",&controller_number);  //Extract controller number
  strcpy(field_buffers[i++],(const char *) strtok(NULL,&delim)); 
  
//Skip blank fields
  do
  {
    temp_buf = strtok(NULL,&delim);
  }
  while(temp_buf[1]==0);


  k = 0;
  j = 0;
  do
  {
  if(j == controller[controller_number][k])
    {
    strcpy(field_buffers[offset[controller_number]+k++],(const char *) temp_buf); 
    }
  temp_buf = strtok(NULL,&delim); //Get next token
  j++;
  }
  while(k<n_fields[controller_number]);

   printf("%s,",field_buffers[0]);
   for(i=1;i<N_fieldbuffers;i++)
	printf("%6s,",field_buffers[i]);
   printf("\n");
   
}


free(linebuf);
for(i=0;i<N_fieldbuffers;i++)
  free(field_buffers[i]);
free(field_buffers);
}
