*** empty log message ***
[h316.git] / pc-tools / ldc2 / src / tape_block.cpp
index a7ae129509c0b617ef31c832745a526f4b93c9d7..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
@@ -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;
 }