*** empty log message ***
[h316.git] / pc-tools / ldc2 / src / tape_block.cpp
index 81efc8ce8cbecdd467d6cd561071bcf5e0b459a4..abba1d62945371d70ae6f0f7202f7328b4b85b8f 100644 (file)
@@ -1,18 +1,62 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
+#include <stdio.h>
 
 #include "tape_block.hh"
 #include "data_block.hh"
+#include "data_block_0.hh"
+#include "data_block_0_0.hh"
+#include "data_block_0_1.hh"
+#include "data_block_0_2.hh"
+#include "data_block_0_3.hh"
+#include "data_block_0_4.hh"
+#include "data_block_0_10.hh"
+#include "data_block_0_14.hh"
+#include "data_block_0_24.hh"
+#include "data_block_0_44.hh"
+#include "data_block_0_30.hh"
+#include "data_block_0_54.hh"
+#include "data_block_0_60.hh"
+#include "data_block_0_50.hh"
+#include "data_block_0_64.hh"
+#include "data_block_1.hh"
+#include "data_block_2.hh"
+#include "data_block_3.hh"
+#include "data_block_4.hh"
+#include "data_block_5.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"
 
-tape_block::tape_block()
-{
+using namespace std;
+
+/***************************************************************/
+/*!
+ *\brief Destructor.
+ */
+tape_block::~tape_block(){
+  free(raw_data);
+  free(word_data);
 }
 
+/***************************************************************/
+/*!
+ *\brief Copy constructor.
+ */
 tape_block::tape_block(tape_block & org){
+  operator=(org);
+}
+
+/***************************************************************/
+/*!
+ *\brief Assignment operator.
+ */
+void tape_block::operator=(tape_block &org){
   block_type=org.block_type;
   init_state=org.init_state;
   data_read=org.data_read; 
@@ -30,6 +74,253 @@ tape_block::tape_block(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
+ *
+ * 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,
+ * it will return the block type contained in the upper 4 bits of the
+ * first data word! Try to avoid testing for TBT_DATA. \n
+ * Blocks generated by gen_from_fd should never have TBT_DATA.
+ */
+int tape_block::get_type(){
+  return block_type;
+}
+
+/***************************************************************/
+/*!
+ *\brief Determine the block's subtype
+ *
+ * Some data block types have a subtype.
+ *\note This is a virtual method and to be used in derived classes.
+ *\return Always 0
+ */
+int tape_block::get_subtype(){
+  return 0;
+}
+
+/***************************************************************/
+/*!
+ *\brief Get a cleartext description of the block.
+ *\return ldc style descriptive line for the block
+ */
+vector<string> tape_block:: get_description(){
+  vector<string> result;
+  result.insert(result.end(),
+               "*****      "+get_typestring()+"Untyped tape block");
+  return result;
+}
+
+/***************************************************************/
+/*!
+ *\brief Get size of the internal raw data buffer
+ *\return The size of the internal raw data buffer
+ */
+int tape_block::get_raw_size(){
+  return raw_size;
+}
+
+/***************************************************************/
+/*!
+ *\brief Access internal raw data buffer
+ *
+ * The raw data buffer contains all block data except the \    \
+ * block start delimiter and block end delimiter.
+ *\return Pointer to internal raw data buffer of the block
+ */
+unsigned char * tape_block::get_raw_data(){
+  return raw_data;
+}
+
+/***************************************************************/
+/*!
+ *\brief Static factory method with file descriptor support.
+ *
+ * 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
+ * object of the subclass will be created and a pointer to this
+ * object will be returned to the caller.\n
+ * This allows for easy use of the object using it's virtual
+ * interface methods or a cast of the pointer to a sub type pointer 
+ * according to the type information, and then using the subtype's
+ * 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
+ *     - data_block_0_1
+ *     - data_block_0_2
+ *     - data_block_0_3
+ *     - data_block_0_4
+ *     - data_block_0_10
+ *     - data_block_0_14
+ *     - data_block_0_24
+ *     - data_block_0_30
+ *     - data_block_0_50
+ *     - data_block_0_54
+ *     - data_block_0_60
+ *     - data_block_0_64
+ *   - data_block_1
+ *   - data_block_2
+ *   - data_block_3
+ *   - data_block_4
+ *   - data_block_5
+ *   - 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.
+ *\param input_start A pointer to a function 
+ *       called before reading data from fd.
+ *\param input_stop A pointer to a function
+ *       called after reading data from fd.
+ * 
+ *\param start_stop_arg A pointer passed to input_start 
+ *       and input_stop(). Can be anything.
+ */
+tape_block * tape_block::gen_from_fd(int  fd,
+                                    void(*input_start)(void *),
+                                    void (*input_stop)(void *),
+                                    void * start_stop_arg)
+{
+  tape_block  * n_tape_block=0;
+  data_block  * n_data_block=0;
+  data_block  * n_data_block_x=0;
+  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;
+  
+  n_tape_block=new tape_block(fd,input_start,input_stop,start_stop_arg);
+  
+  // Retype to data_block, eot_block, discard_block - if possible
+  switch(n_tape_block->get_type()){
+  case tape_block::TBT_DATA:
+    n_data_block = new data_block(*n_tape_block); 
+    break;
+  case tape_block::TBT_EOT:
+    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:          
+    return n_tape_block;
+  }
+  delete n_tape_block;
+
+  // Now only data blocks are left
+
+  switch(n_data_block->get_type()){
+  case 00: n_data_block_0=new data_block_0(*n_data_block); break;
+  case 01: n_data_block_x=new data_block_1(*n_data_block); break;
+  case 02: n_data_block_x=new data_block_2(*n_data_block); break;
+  case 03: n_data_block_x=new data_block_3(*n_data_block); break;
+  case 04: n_data_block_x=new data_block_4(*n_data_block); break;
+  case 05: n_data_block_x=new data_block_5(*n_data_block); break;
+  case 06: n_data_block_x=new data_block_6(*n_data_block); break;
+  case 07: n_data_block_x=new data_block_7(*n_data_block); break;
+  default:
+    return n_data_block;
+  }
+  if (n_data_block_0==0){ // So we must have found another one
+    delete n_data_block;
+    return n_data_block_x;
+  }
+  
+  // 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;
+  case 002: n_data_block_0_x=new data_block_0_2(*n_data_block_0); break;
+  case 003: n_data_block_0_x=new data_block_0_3(*n_data_block_0); break;
+  case 004: n_data_block_0_x=new data_block_0_4(*n_data_block_0); break;
+  case 010: n_data_block_0_x=new data_block_0_10(*n_data_block_0); break;
+  case 014: n_data_block_0_x=new data_block_0_14(*n_data_block_0); break;
+  case 024: n_data_block_0_x=new data_block_0_24(*n_data_block_0); break;
+  case 044: n_data_block_0_x=new data_block_0_44(*n_data_block_0); break;
+  case 030: n_data_block_0_x=new data_block_0_30(*n_data_block_0); break;
+  case 054: n_data_block_0_x=new data_block_0_54(*n_data_block_0); break;
+  case 060: n_data_block_0_x=new data_block_0_60(*n_data_block_0); break;
+  case 064: n_data_block_0_x=new data_block_0_64(*n_data_block_0); break;
+  case 050: n_data_block_0_x=new data_block_0_50(*n_data_block_0); break;
+  default:
+    return n_data_block_0;
+  }
+  delete n_data_block_0;
+  return n_data_block_0_x;
+}
+
+/***************************************************************/
+/*!
+ *\brief Default constructor.
+ */
+tape_block::tape_block(){}
+
+/***************************************************************/
+/*!
+ *\brief get a short type description string.
+ *\return A small string to be used in description methods.
+ *
+ * The string is always 19 characters long.\n
+ * An example is "(99-99)   ".
+ */
+string tape_block::get_typestring(){
+  char buffer[13];
+  sprintf(buffer,"(%o-%o)      ",get_type(),get_subtype());
+  buffer[10]=0;
+  return string(buffer);
+}
+
+/***************************************************************/
+/*!
+ *\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 is done in the following way:
+ * - input_start() is called.
+ * - Data is read from fd. Stops on end of file or end of block
+ * or IO error. On EOF or IO error input_stop() is NOT called.
+ * - If there was no IO error the block's checksum is calculated 
+ * and checked.
+ * - The block's type field is initialised.
+ * - input_stop() is called.
+ *
+ *\param fd A file descriptor where the read is taken from 
+ *\param input_stop A pointer to a function called at the end of input
+ *\param input_start A pointer to a function called at the beginning 
+ * of input
+ *\param start_stop_arg A pointer passed to input_start and input_stop(). 
+ */
 tape_block::tape_block (int  fd,
                        void(*input_start)(void *),
                        void (*input_stop)(void *),
@@ -150,41 +441,13 @@ tape_block::tape_block (int  fd,
   init_state=TBS_OK;
 }
 
-tape_block::~tape_block(){
-  free(raw_data);
-  free(word_data);
-}
-
-tape_block::tb_state_t tape_block::get_state(){
-  return init_state;
-}
-
-int tape_block::get_raw_size(){
-  return raw_size;
-}
-
-unsigned char * tape_block::get_raw_data(){
-  return raw_data;
-}
-
-int tape_block::get_type(){
-  int result=block_type;
-  if (block_type==TBT_DATA){
-    data_block * mb=new data_block(*this);
-    result=mb->get_type();
-  }
-  return result;
-}
-
-int tape_block::get_subtype(){
-  int result=0;
-  if (block_type==TBT_DATA){
-    data_block * mb=new data_block(*this);
-    result=mb->get_subtype();
-  }
-  return result;
-}
 
+/***************************************************************/
+/*!
+ *\brief Initialize word representation of data.
+ *\retval 0 on success.
+ *\retval 1 on error.
+ */
 int tape_block::init_words(){
   word_size=raw_size/3;
   if (word_size<MINIMUM_DATA_BLOCK_WORDSIZE) return 1; // Block too short!
@@ -194,13 +457,17 @@ int tape_block::init_words(){
   return 0;
 }
 
+/***************************************************************/
+/*!
+ *\brief Calculate and check checksum.
+ *\retval 0 on success.
+ *\retval 1 on error.
+ */
 int tape_block::test_checksum(){
   unsigned short sum=0;
   for (int pos=0; pos<word_size-1;pos++)
-    //    sum^=word_data[pos];
-    sum+=word_data[pos];
-    
-  //  printf("cs:%04x\n",word_data[word_size-1]);
+    sum^=word_data[pos];
   if (sum==word_data[word_size-1]) return 0;
   else return 1;
 }
+