*** empty log message ***
[h316.git] / pc-tools / ldc2 / src / tape_block.cpp
index abba1d62945371d70ae6f0f7202f7328b4b85b8f..ecc02376a8681590bd9f20b3d2eacd631edbba00 100644 (file)
@@ -59,7 +59,7 @@ tape_block::tape_block(tape_block & org){
 void tape_block::operator=(tape_block &org){
   block_type=org.block_type;
   init_state=org.init_state;
-  data_read=org.data_read; 
+  discarded_bytes=org.discarded_bytes;
   raw_data=0;  
   raw_size=org.raw_size;  
   word_data=0;
@@ -137,6 +137,16 @@ int tape_block::get_raw_size(){
   return raw_size;
 }
 
+/***************************************************************/
+/*!
+ *\brief Get amount of bytes discarded before beginning
+ *       of block sensed.
+ *\return Length of unusable tape before block start delimiter. 
+ */
+int tape_block::get_discarded_bytes(){
+  return discarded_bytes;
+}
+
 /***************************************************************/
 /*!
  *\brief Access internal raw data buffer
@@ -216,6 +226,7 @@ tape_block * tape_block::gen_from_fd(int  fd,
   eot_block * n_eot_block=0;
   discard_block * n_discard_block=0;
   
+  //Use the private constructor which reads in the block from a file descriptor
   n_tape_block=new tape_block(fd,input_start,input_stop,start_stop_arg);
   
   // Retype to data_block, eot_block, discard_block - if possible
@@ -231,8 +242,8 @@ tape_block * tape_block::gen_from_fd(int  fd,
     n_discard_block = new discard_block(*n_tape_block); 
     delete n_tape_block;
     return n_discard_block;
-  default:          
-    return n_tape_block;
+  default: // Unknown block, a bad thing!         
+    return n_tape_block; 
   }
   delete n_tape_block;
 
@@ -256,7 +267,6 @@ tape_block * tape_block::gen_from_fd(int  fd,
   }
   
   // Here only type 0 left
-
   switch(n_data_block_0->get_subtype()){
   case 000: n_data_block_0_x=new data_block_0_0(*n_data_block_0); break;
   case 001: n_data_block_0_x=new data_block_0_1(*n_data_block_0); break;
@@ -335,12 +345,16 @@ tape_block::tape_block (int  fd,
 
   block_type= TBT_DISCARD;
   init_state= TBS_DEFAULT;
-  data_read = 0;
+  discarded_bytes=0;
   raw_data  = 0;
   raw_size  = 0;
   word_data = 0;
   word_size = 0;
-  
+  poolsize  = 1024;
+
+  // Get initial portion of memory
+  raw_data=(unsigned char*)malloc(poolsize);
+
   if (input_start) input_start (start_stop_arg);
 
   /* Look for block start in input stream */
@@ -356,7 +370,7 @@ tape_block::tape_block (int  fd,
       init_state=TBS_IOERR;
       return;
     }
-    data_read++;
+    discarded_bytes++;
     
     /* Look for end ot tape sequence */
     switch(eot_needed){
@@ -372,6 +386,7 @@ tape_block::tape_block (int  fd,
       if (buffer==EOT_SEQ_3){
        raw_data=(unsigned char*)malloc(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;
@@ -383,7 +398,8 @@ tape_block::tape_block (int  fd,
       break;
     }
   } while (buffer != (unsigned char) BLOCK_START_DELIMITER);
-  
+  discarded_bytes--; // Dont want to declare the start delimiter discarded!
+  raw_size=1;        // But we want to account it here!
 
   /* Now input the block data */
   block_complete = 0;
@@ -400,12 +416,16 @@ tape_block::tape_block (int  fd,
       init_state=TBS_IOERR;
       return;
     }
-    data_read++;                  // We have consumed a byte
     
     /* sort in the new byte */
     raw_size++;
-    raw_data=(unsigned char*)realloc(raw_data,raw_size);
-    raw_data[raw_size-1]=buffer;
+    // Need more memory??
+    if (raw_size==poolsize){
+      poolsize+=1024;
+      raw_data=(unsigned char*)realloc(raw_data,poolsize);
+    }
+
+    raw_data[raw_size-2]=buffer;
 
     /* Look for end ot block sequence */
     switch(eob_needed){
@@ -416,7 +436,6 @@ tape_block::tape_block (int  fd,
     case 1:
       if (buffer==BLOCK_END_DELIMITER_2){
        raw_size-=EOB_LENGTH; // Don't want the delimiter(s) in the data!
-       raw_data=(unsigned char*)realloc(raw_data,raw_size);
        block_complete=1;
       }
       else eob_needed=EOB_LENGTH;
@@ -436,7 +455,8 @@ tape_block::tape_block (int  fd,
     init_state=TBS_CHECKSUM;
     return;
   }
-  
+  raw_size+=EOB_LENGTH; // Now we want it...
+
   block_type=TBT_DATA;    
   init_state=TBS_OK;
 }