/*
//////////////////////////////////////////////////////
//////                misc.c                     //////
//////////////////////////////////////////////////////
*/


/*
/////////////////////////////
//
// Author: Gayatri Krishnan
// Assignment # 8
// Internet Applications
// 
/////////////////////////////
*/


/*
////
// include files
////
*/
#include "common.h"

/* 
 ** Function    : print_constants()
 ** Description : Outputs Constant Pools, Fields and Methods
 */ 
void print_constants(int totalc)
{
  int status, cPool_index, init_counter;
  int length, counter;

  for(cPool_index=1; cPool_index<totalc; cPool_index++)
    {
     
	status = read_bytes(1);
	if (status==-1) exit(0);
	
	cPool[cPool_index].fieldFlag = 0;
	cPool[cPool_index].num = cPool_index;
	switch(byte[0])
	  {
	  case 7:
	    { 
	      /*##########ConstantClass##########*/
	      status = read_bytes(2);
	      if (status==-1) exit(0);
	      /* name index */
	      cPool[cPool_index].nIndex = (byte[0]<<8) | byte[1];
	      cPool[cPool_index].str = (char*) malloc(1);
	      break;
	    }
	  case 9:
	    {
	      /*##########Method##########*/
	      status = read_bytes(2);
	      if (status ==-1) exit(0);
	      /* Classindex */
	      cPool[cPool_index].cIndex = ((byte[0] <<8) | byte[1]);
	      
	      status = read_bytes(2);
	      if(status==-1) exit(0);
	      /* Name&TypeIndex */
	      cPool[cPool_index].ntIndex = ((byte[0] <<8) | byte[1]);
	      
	      cPool[cPool_index].str = (char*) malloc(1);
	      cPool[cPool_index].fieldFlag = 2;
	      break;
	    }
	  case 10:
	    {
	      /*##########MethodRef##########*/
	      status = read_bytes(2);
	      if (status ==-1) exit(0);
	      /* Classindex */
	      cPool[cPool_index].cIndex = ((byte[0] <<8) | byte[1]);

	      status = read_bytes(2);
	      if(status==-1) exit(0);

	      /* Name&TypeIndex */
	      cPool[cPool_index].ntIndex = ((byte[0] <<8) | byte[1]);

	      cPool[cPool_index].str = (char*) malloc(1);
	      cPool[cPool_index].fieldFlag = 1;
	      break;
	    }
	  case 11:
	    {
	      /*##########Method##########*/
	      status = read_bytes(2);
	      if (status ==-1) exit(0);
	      /* Classindex */
	      cPool[cPool_index].cIndex = ((byte[0] <<8) | byte[1]);
	      
	      status = read_bytes(2);
	      if(status==-1) exit(0);
	      /* Name&TypeIndex */
	      cPool[cPool_index].ntIndex = ((byte[0] <<8) | byte[1]);
	      
	      cPool[cPool_index].str = (char*) malloc(1);
	      cPool[cPool_index].fieldFlag = 2;
	      break;
	    }

	  case 8:
	    {
	      /*##########String##########*/
	      status = read_bytes(2);
	      if (status ==-1) exit(0);
	      /*Stringindex*/
	      cPool[cPool_index].strIndex = (byte[0]<<8) | byte[1];
	      break;
	    }

	  case 3:
	    {
	      /*##########IntegerConstant##########*/
	      status = read_bytes(4);
	      if (status == -1) exit(0);
	      break;
	    }
	  case 4:
	    {
	      /*##########FloatingConstant##########*/
	      status = read_bytes(4);
	      if (status == -1) exit(0);
	      break;
	    }
	  case 5:
	    {
	      /*##########LongConstant##########*/
	      /* High Bytes */
	      status = read_bytes(4);
	      if (status == -1) exit(0);
	      /* Low Bytes */
	      status = read_bytes(4);
	      if (status == -1) exit(0);
	      break;
	    }
	  case 6:
	    {
	      /*##########DoubleConstant##########*/
	      /* High Bytes */
	      status = read_bytes(4);
	      if (status == -1) exit(0);
	      /* Low Bytes */
	      status = read_bytes(4);
	      if (status == -1) exit(0);
	      break;
	    }
	  case 12:
	    {
	      /*##########name&Type##########*/
	      status = read_bytes(2);
	      if (status ==-1) exit(0);

	      /* Nameindex */
	      cPool[cPool_index].nIndex = (byte[0]<<8) | byte[1];
	      cPool[cPool_index].str = (char*) malloc(1);

	      /*SignatureIndex*/
	      status = read_bytes(2);
	      if(status==-1) exit(0);
	      break;
	    }
	  case 1:
	    { 
	      /*##########Constant Utf8##########*/
	      status = read_bytes(2);
	      if (status ==-1) exit(0);
	      length=(byte[0]<<8) | byte[1];

	      /*##########Actual String##########*/
	      cPool[cPool_index].str = (char*) malloc(length);
	      for (init_counter = 0; init_counter < length; init_counter++)
		cPool[cPool_index].str[init_counter] = '\0';
	      cPool[cPool_index].len = length;
              for(counter=0;counter<length;counter++)
		{
		  read_bytes(1);
		  if (status ==-1) exit(0);
		  if (printFlag)
		    printf("%c",byte[0]);
		  cPool[cPool_index].str[counter] = byte[0];
		}
	      if (printFlag)
		printf("\n");
	      break;
	    }
	  case 2:
	    { 
	      int length,counter;
	      /*##########Constant Unicode##########*/
	      status = read_bytes(2);
	      if (status ==-1) exit(0);
	      length=(byte[0]<<8) | byte[1];
	      /*##########Actual String##########*/
	      cPool[cPool_index].str = (char*) malloc(length);
	      for (init_counter = 0; init_counter < length; init_counter++)
		cPool[cPool_index].str[init_counter] = '\0';
	      cPool[cPool_index].num = cPool_index;
	      cPool[cPool_index].len = length;
              for(counter=0;counter<length;counter++)
		{
		  read_bytes(2);
		  if (status ==-1) exit(0);
		  cPool[cPool_index].str[counter] = byte[0];
		}
	    }
	  default: 
	    {
	      if (printFlag) {
		printf("How did it get Here\n");
		printf("%d\n",byte[0]);
	      }
	    }
	  }
      }
}





