//***************************************************************************
// Author:  Scotty Smith
//
// Purpose: Implement a class that reads and stores 512 bits worth of a
//          file at a time.
//***************************************************************************

#include <iostream>
using namespace std;

#include <cstdio> //For the EOF check
#include "Read512.h"
#include "constants.h"

//Pre:  None
//Post: Creates the msg array, and initializes all blocks in msg to
//      be 0's. 
Read512::Read512(){
  msg = new unsigned int[BLOCKS_IN_512];
  
  for(int index = 0; index < BLOCKS_IN_512; index++){
    msg[index] = 0;
  }
}

//Pre:  in is a well defined input stream reference, which may or
//      may not be at EOF.
//      data_holder is a well defined Read512 reference.
//Post: Returns a reference to the istream object, with 512 bits
//      read out of the stream.
//      Modifies the data_holder reference, so that data_holder.msg
//      points to the correct bits from the input file.
istream& operator>>(istream& in, Read512& data_holder){
  char input_char = in.get();
  //We want Read512 to store the characters "left-to-right", so start
  //high and go low.
  for(int curr_integer = BLOCKS_IN_512 - 1;
      curr_integer >= 0; 
      curr_integer--){

    //Reset to zero, to remove anything that was there previously
    data_holder.msg[curr_integer] = 0;
    
    for(int i = 0; i < CHARS_PER_BLOCK && input_char != EOF; i++){
      //Shift whatever is currently in msg by 8
      data_holder.msg[curr_integer] <<= SHIFT_AMOUNT;

      //And bitwise or it with the character
      data_holder.msg[curr_integer] |= input_char;

      input_char = in.get();
    }
    //ASSERT: data_holder.msg[curr_integer] now holds 32 bits of the
    //message, or we hit the end of the input stream and it holds less
    //than that.

  }
}
