*** empty log message ***
[h316.git] / pc-tools / ldc2 / src / tape_block.cpp
index ecc02376a8681590bd9f20b3d2eacd631edbba00..3de7bfb5af1155ff906e1e7ee6c05bcccba6269c 100644 (file)
@@ -1,10 +1,21 @@
+
+
+#include <string>
+#include <vector>
+
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <stdio.h>
 
 #include "tape_block.hh"
+
+#include "silent_code.hh"
+#include "hw_constants.hh"
+
+#include "eot_block.hh"
 #include "data_block.hh"
+#include "broken_block.hh"
 #include "data_block_0.hh"
 #include "data_block_0_0.hh"
 #include "data_block_0_1.hh"
 #include "data_block_6.hh"
 #include "data_block_7.hh"
 
-#include "eot_block.hh"
-#include "discard_block.hh"
-#include "silent_code.hh"
-#include "hw_constants.hh"
-
 using namespace std;
 
 /***************************************************************/
@@ -58,7 +64,6 @@ tape_block::tape_block(tape_block & org){
  */
 void tape_block::operator=(tape_block &org){
   block_type=org.block_type;
-  init_state=org.init_state;
   discarded_bytes=org.discarded_bytes;
   raw_data=0;  
   raw_size=org.raw_size;  
@@ -74,17 +79,6 @@ void tape_block::operator=(tape_block &org){
    }
 }
 
-/***************************************************************/
-/*!
- *\brief Query the block's state.
- *
- * If the state is not TBS_OK, the block must be discarded.
- *\return The block's initialisation state
- */
-tape_block::tb_state_t tape_block::get_state(){
-  return init_state;
-}
-
 /***************************************************************/
 /*!
  *\brief Determine the block's type
@@ -92,7 +86,6 @@ tape_block::tb_state_t tape_block::get_state(){
  * This routine returns the block's type.\\
  *\retval TBT_DATA if it's a data block
  *\retval TBT_EOT if it's an end of tape mark
- *\retval TBR_DISCARD if the block is unusable
  *
  *\note This function is virtual. That means that if the tape block in
  * question is of the subtype data_block which overwrites this method,
@@ -161,13 +154,11 @@ unsigned char * tape_block::get_raw_data(){
 
 /***************************************************************/
 /*!
- *\brief Static factory method with file descriptor support.
+ *\brief Static factory method for tape blocks.
  *
  * This method creates a tape_block by using the private
  * constructor with file descriptor support.\n
- * It always returns a valid pointer, errors
- * or exceptional conditions during creation are marked in the
- * object's state.\n
+ *
  * The factory method tries to determine the block's type and then
  * looks for appropriate subclasses. If there's an appropriate
  * subclass fitting the newly-created object, an
@@ -179,7 +170,6 @@ unsigned char * tape_block::get_raw_data(){
  * methods not already present in tape_block.\n
  * Objects of the  following subclasses are automagically generated:\n
  * - eot_block
- * - discard_block
  * - data_block
  *   - data_block_0
  *     - data_block_0_0
@@ -203,8 +193,8 @@ unsigned char * tape_block::get_raw_data(){
  *   - data_block_6
  *   - data_block_7
  *
- *\return Pointer to an object of type tape_block or a subclass.
- *\param fd A file descriptor where the read is taken from.
+ *\return Pointer to an instance of tape_block or a subclass.
+ *\param fd A file descriptor where data can be read from.
  *\param input_start A pointer to a function 
  *       called before reading data from fd.
  *\param input_stop A pointer to a function
@@ -224,12 +214,20 @@ tape_block * tape_block::gen_from_fd(int  fd,
   data_block_0  * n_data_block_0=0;
   data_block_0  * n_data_block_0_x=0;
   eot_block * n_eot_block=0;
-  discard_block * n_discard_block=0;
-  
+  broken_block * n_broken_block=0;
+
   //Use the private constructor which reads in the block from a file descriptor
+  try{
   n_tape_block=new tape_block(fd,input_start,input_stop,start_stop_arg);
+  }
   
-  // Retype to data_block, eot_block, discard_block - if possible
+  catch(checksum_error_exception e){
+    n_broken_block=new broken_block(*(e.get_block()));
+    delete e.get_block();
+    throw checksum_error_exception(n_broken_block);
+  }
+    
+  // Retype to data_block, eot_block - if possible
   switch(n_tape_block->get_type()){
   case tape_block::TBT_DATA:
     n_data_block = new data_block(*n_tape_block); 
@@ -238,10 +236,6 @@ tape_block * tape_block::gen_from_fd(int  fd,
     n_eot_block=new eot_block(*n_tape_block); 
     delete n_tape_block;
     return n_eot_block;
-  case tape_block::TBT_DISCARD:
-    n_discard_block = new discard_block(*n_tape_block); 
-    delete n_tape_block;
-    return n_discard_block;
   default: // Unknown block, a bad thing!         
     return n_tape_block; 
   }
@@ -314,8 +308,8 @@ string tape_block::get_typestring(){
 /*!
  *\brief Automatic constructor with file descriptor support
  *
- * This constructor is used to initialise the block read in the block's 
- * data via a file descriptor.\n
+ * This constructor is used to initialise the block read in the 
+ * block's data via a file descriptor.\n
  * This is done in the following way:
  * - input_start() is called.
  * - Data is read from fd. Stops on end of file or end of block
@@ -343,8 +337,7 @@ tape_block::tape_block (int  fd,
   int eob_needed=EOB_LENGTH;
   int block_complete=0;
 
-  block_type= TBT_DISCARD;
-  init_state= TBS_DEFAULT;
+  block_type= TBT_BROKEN;
   discarded_bytes=0;
   raw_data  = 0;
   raw_size  = 0;
@@ -361,13 +354,13 @@ tape_block::tape_block (int  fd,
   do { 
     retval=read(fd, &buffer, 1);
     if (retval == 0){             // End of file
-      block_type=TBT_DISCARD;
-      init_state=TBS_EOF_LEGAL;
+      free(raw_data);
+      throw eof_legal_exception(discarded_bytes);
       return;
     }
     if (retval == -1){            // Error reading from fd
-      block_type=TBT_DISCARD;
-      init_state=TBS_IOERR;
+      free(raw_data);
+      throw io_error_exception();
       return;
     }
     discarded_bytes++;
@@ -384,14 +377,13 @@ tape_block::tape_block (int  fd,
       break;
     case 1:
       if (buffer==EOT_SEQ_3){
-       raw_data=(unsigned char*)malloc(3);
+       raw_data=(unsigned char*)realloc(raw_data,3);
        raw_size=3;
        discarded_bytes-=3;
        raw_data[0]=EOT_SEQ_1;
        raw_data[1]=EOT_SEQ_2;
        raw_data[2]=EOT_SEQ_3;
        block_type =TBT_EOT;
-       init_state =TBS_OK;
        return;
       }
       else eot_needed=EOT_LENGTH;
@@ -407,14 +399,13 @@ tape_block::tape_block (int  fd,
     retval = read (fd, &buffer, 1);
 
     if (retval == 0){             // End of file
-      block_type=TBT_DISCARD;
-      init_state=TBS_EOF_ILLEGAL;
-      return;
+      broken_block * b_block=new broken_block(*this);
+      free(raw_data);
+      throw eof_illegal_exception(b_block);
     }
     if (retval == -1){            // Error reading from fd
-      block_type=TBT_DISCARD;
-      init_state=TBS_IOERR;
-      return;
+      free(raw_data);
+      throw io_error_exception();
     }
     
     /* sort in the new byte */
@@ -435,30 +426,32 @@ tape_block::tape_block (int  fd,
       break;
     case 1:
       if (buffer==BLOCK_END_DELIMITER_2){
-       raw_size-=EOB_LENGTH; // Don't want the delimiter(s) in the data!
        block_complete=1;
       }
       else eob_needed=EOB_LENGTH;
       break;
     }
   }while (!block_complete);
+
   if (input_stop) input_stop (start_stop_arg);
   
+
   /* Now we have the data in. */
   
   if (init_words()!=0){
-    init_state=TBS_CHECKSUM;
-    return;
+    broken_block * b_block=new broken_block(*this);
+    free(raw_data);
+    free(word_data);
+    throw checksum_error_exception(b_block);
   }
-  
   if (test_checksum()!=0){
-    init_state=TBS_CHECKSUM;
-    return;
+    broken_block * b_block=new broken_block(*this);
+    free(raw_data);
+    free(word_data);
+        throw checksum_error_exception(b_block);
   }
-  raw_size+=EOB_LENGTH; // Now we want it...
-
   block_type=TBT_DATA;    
-  init_state=TBS_OK;
 }
 
 
@@ -469,9 +462,9 @@ tape_block::tape_block (int  fd,
  *\retval 1 on error.
  */
 int tape_block::init_words(){
-  word_size=raw_size/3;
+  word_size=(raw_size-EOB_LENGTH)/3;
   if (word_size<MINIMUM_DATA_BLOCK_WORDSIZE) return 1; // Block too short!
-  word_data=(unsigned short *)malloc(raw_size*sizeof(unsigned short));
+  word_data=(unsigned short *)malloc(word_size*sizeof(unsigned short));
   for (int pos=0; pos<word_size; pos++)
     word_data[pos]=combine466(raw_data+3*pos);
   return 0;
@@ -491,3 +484,82 @@ int tape_block::test_checksum(){
   else return 1;
 }
 
+/***************************************************************/
+/*!
+ *\brief Standard constructor of exception base class.
+ */
+tape_block::exception::exception(){
+}
+
+/***************************************************************/
+/*!
+ *\brief Standard desctructor.
+ */
+tape_block::exception::~exception(){
+}
+
+/***************************************************************/
+/*!
+ *\brief Get pointer to erroneous tape_block
+ *\return A pointer to a tape_block instance.
+ *\note The user has to take care to free the instance contained
+ *      in objects of classes supporting this method.
+ */
+tape_block * tape_block::exception::get_block(){
+  return m_broken_block;
+}
+
+/***************************************************************/
+/*!
+ *\brief Constructor
+ */
+tape_block::checksum_error_exception::
+checksum_error_exception (tape_block * broken_block){
+  this->m_broken_block=broken_block;
+}
+
+/***************************************************************/
+/*!
+ *\brief Constructor
+ */
+tape_block::eof_illegal_exception::
+eof_illegal_exception (tape_block * broken_block){
+  this->m_broken_block=broken_block;
+}
+
+/***************************************************************/
+/*!
+ *\brief Constructor
+ *\param bytes_consumed Amount of bytes read while looking
+ *       for a block.
+ */
+tape_block::eof_legal_exception::
+eof_legal_exception(int bytes_consumed){
+  this->m_broken_block=NULL;
+  this->bytes_consumed=bytes_consumed;
+}
+
+/***************************************************************/
+/*!
+ *\brief Get amount of consumed data before EOF came along.
+ *\return Amound of bytes read from fd while looking for
+ *        new block or EOF.
+ */
+int tape_block::eof_legal_exception::get_consumed(){
+  return bytes_consumed;
+}
+
+/***************************************************************/
+/*!
+ *\brief Constructor
+ */
+tape_block::io_error_exception::
+io_error_exception(){
+  this->m_broken_block=NULL;
+}
+
+
+
+
+
+