home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / ISC366.ZIP / SERIAL / BUFF.CPP next >
C/C++ Source or Header  |  1993-09-01  |  8KB  |  266 lines

  1. #include "buff.h";
  2.  
  3. //█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█
  4. //█  Written by: RAM MACHNESS                                                  █
  5. //█  Started on: 24-11-1991                                                    █
  6. //▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
  7.  
  8. BUFF::BUFF()
  9. {
  10.     const unsigned length= 256;
  11.  
  12.     buff= new BYTE[length];
  13.  
  14.     if (buff== NULL)
  15.          return; // not enough memory.
  16.  
  17.     alloc_len= buff_len= length;
  18.     head= current_length= 0;
  19.     self_allocated= TRUE;
  20. }
  21.  
  22. BUFF::BUFF(const unsigned length)  //self allocated constructor
  23. {
  24.     if (length== 0)
  25.        return; // allocation error.
  26.  
  27.     buff= new BYTE[length];
  28.     if (buff== NULL)
  29.        return; // not enough memory.
  30.  
  31.     alloc_len= buff_len= length;
  32.     head= current_length= 0;
  33.     self_allocated= TRUE;
  34. }
  35.  
  36.  
  37. BUFF::BUFF(const BUFF&    source_buff)         //copy constructor
  38. {
  39.     // ╔════════════════════════════════════════════════════╗
  40.     // ║before changing this code- please check operator= . ║
  41.     // ╚════════════════════════════════════════════════════╝
  42.     buff= new BYTE[source_buff.buff_len];
  43.     if (buff== NULL)
  44.        return; // allocation failure (not enough memory).
  45.  
  46.     alloc_len= buff_len= source_buff.buff_len;
  47.     head= 0;
  48.     self_allocated= TRUE;
  49.     current_length= source_buff.current_length;
  50.  
  51.     for (INDEX i= 0; i< source_buff.buff_len; i++)
  52.            buff[i]= source_buff[i];
  53. }
  54.  
  55.  
  56. BUFF::BUFF(const BUFF&    source_buff,       //blocking constructor-
  57.                                              //based on other buff
  58.            const INDEX    block_start ,      //zero represents the first
  59.                                              //item on buff
  60.            const INDEX    block_len)         //default is the last item
  61.                                              //on source buff
  62. {
  63.     if (block_len== 0)
  64.        return; // not enough memory.
  65.  
  66.     // ╔═══════════════════════╗
  67.     // ║block start is cyclic. ║
  68.     // ╚═══════════════════════╝
  69.     head= ((block_start+ source_buff.head)% source_buff.buff_len);
  70.  
  71.  
  72.     // ╔═══════════════════════════════════════════════════════════════════════╗
  73.     // ║current lebgth will be block len unless the size of the new buff is    ║
  74.     // ║     larger than the original buff len. In this case,the current length║
  75.     // ║     will be the remaining number of items left from the original      ║
  76.     // ║     buff. (start counting at head).                                   ║
  77.     // ╚═══════════════════════════════════════════════════════════════════════╝
  78.     current_length= ((block_len+ head)> source_buff.buff_len)?
  79.                   (source_buff.buff_len- head): block_len;
  80.  
  81.     buff_len=source_buff.buff_len;
  82.     buff= source_buff.buff;
  83.     self_allocated= FALSE;
  84.     alloc_len= 0;
  85. }
  86.  
  87.  
  88. BUFF::~BUFF()                                  //destructor
  89. {
  90.     if (self_allocated)
  91.        delete (buff);
  92. }
  93.  
  94. void BUFF::operator= (const BUFF& source_buff)
  95. {
  96.     if (self_allocated)
  97.        delete (buff);
  98.  
  99.     // ╔════════════════════════════════════════════════════════╗
  100.     // ║this source code was copied off of the copy constructor ║
  101.     // ╚════════════════════════════════════════════════════════╝
  102.     buff= new BYTE[source_buff.buff_len];
  103.  
  104.     if (buff== NULL)
  105.        return; // not enough memory.
  106.  
  107.     alloc_len= buff_len= source_buff.buff_len;
  108.     head= 0;
  109.     self_allocated= TRUE;
  110.     current_length= source_buff.current_length;
  111.  
  112.     for (INDEX i= 0;i< source_buff.buff_len; i++)
  113.            buff[i]= source_buff[i];
  114. }
  115.  
  116. unsigned BUFF::len(void) const  //function returning the current length of buffer.
  117. {
  118.     return current_length;
  119. }
  120.  
  121. BUFF_OP  BUFF::operator<< (const BYTE in_byte) //function inserts byte to buffer
  122. {
  123.     if (!self_allocated)
  124.        return BUFF_FAILURE;
  125.     if (current_length== buff_len)
  126.        return BUFF_OVERFLOW;
  127.  
  128.     buff[(head+ current_length)% buff_len]= in_byte;
  129.     current_length++;
  130.  
  131.     if (current_length== 1)
  132.        return BUFF_SINGLE_ITEM;
  133.     if (current_length== buff_len)
  134.        return BUFF_FULL;
  135.     return BUFF_READY;
  136. }
  137.  
  138. BUFF_OP  BUFF::operator>> (BYTE& out_byte) //function gets byte on head of buff.
  139. {
  140.     if (!self_allocated)
  141.        return BUFF_FAILURE;
  142.     if (current_length== 0)
  143.        return BUFF_UNDERFLOW;
  144.  
  145.     out_byte= buff[head];
  146.     head= ((head+ 1)% buff_len);
  147.     current_length--;
  148.  
  149.     if (current_length== 0)
  150.        return BUFF_EMPTY;
  151.     if (current_length== 1)
  152.        return BUFF_SINGLE_ITEM;
  153.     return BUFF_READY;
  154. }
  155.  
  156. BUFF_OP  BUFF::operator>  (BYTE& out_byte) //function to see byte on head of
  157.                                            //buff without poping it out.
  158. {
  159.     if (current_length== 0)
  160.        return BUFF_UNDERFLOW;
  161.  
  162.     out_byte= buff[head];
  163.  
  164.     if (current_length== 0)
  165.        return BUFF_EMPTY;
  166.     if (current_length== 1)
  167.        return BUFF_SINGLE_ITEM;
  168.     return BUFF_READY;
  169. }
  170.  
  171. BUFF_OP  BUFF::operator<< (const BUFF& other_buff)  //function to add another
  172.                                                     //buff to the sertain buffer
  173. {
  174.     if (!self_allocated)
  175.        return BUFF_FAILURE;
  176.  
  177.     // ╔═════════════════════════════════════════════╗
  178.     // ║checking if we need to change size of buffer ║
  179.     // ╚═════════════════════════════════════════════╝
  180.     if ((current_length+ other_buff.current_length)> buff_len)
  181.        reset_size((current_length+ other_buff.current_length),/*reset=*/FALSE);
  182.  
  183.     // ╔═══════════════╗
  184.     // ║copy all items ║
  185.     // ╚═══════════════╝
  186.     for (int i= 0; i< other_buff.current_length; i++)
  187.            (*this)<< other_buff[i];
  188.  
  189.  
  190.     if (current_length== 0)
  191.        return BUFF_EMPTY;
  192.     if (current_length== 1)
  193.        return BUFF_SINGLE_ITEM;
  194.     if (current_length== buff_len)
  195.        return BUFF_FULL;
  196.     return BUFF_READY;
  197. }
  198.  
  199. void BUFF::empty_buff(void)                     //function to empty the buff
  200. {
  201.     if (self_allocated)
  202.        head= current_length= 0;
  203. }
  204.  
  205. BYTE BUFF::operator[]  (const INDEX index) const //function to see an item
  206.                                                  //according to index.
  207.                                                  //you may enter any index.
  208. {
  209.     return buff[(head+ index)% buff_len];
  210. }
  211.  
  212. BYTE& BUFF::operator[]  (const INDEX index) //function to see an item according
  213.                                             //to index. you may enter any index.
  214. {
  215.     return buff[(head+ index)% buff_len];
  216. }
  217.  
  218. BUFF_OP BUFF::reset_size  (const unsigned new_size,  //function to change buffer
  219.                            const BOOLEAN  reset)     //size and reset it.
  220. {
  221.     if (!self_allocated)
  222.        return BUFF_FAILURE;
  223.  
  224.     if (new_size== 0)
  225.        return BUFF_FAILURE; // error in parameter.
  226.  
  227.     // ╔══════════════════════════════════════════╗
  228.     // ║checking if we need to allocate new space ║
  229.     // ╚══════════════════════════════════════════╝
  230.     if (alloc_len< new_size) {
  231.        // ╔════════════════════╗
  232.        // ║allocating new buff ║
  233.        // ╚════════════════════╝
  234.        BYTE* new_buff= new BYTE[new_size];
  235.        if (new_buff== NULL)
  236.           return BUFF_FAILURE; // not enough memory.
  237.  
  238.        // ╔══════════════════════════════════════╗
  239.        // ║do we need to copy the original buff? ║
  240.        // ╚══════════════════════════════════════╝
  241.        if (!reset)
  242.           for (int i= 0; (i<= new_size) && (i<= current_length); i++)
  243.               new_buff[i]= (*this)[i];
  244.  
  245.        delete (buff);
  246.        buff= new_buff;
  247.        head= 0;
  248.        alloc_len= new_size;
  249.     }
  250.  
  251.     if (reset)
  252.        current_length= 0;
  253.     else
  254.         current_length= (new_size> current_length) ? current_length : new_size;
  255.  
  256.     buff_len= new_size;
  257.  
  258.     if (current_length== 0)
  259.        return BUFF_EMPTY;
  260.     if (current_length== 1)
  261.        return BUFF_SINGLE_ITEM;
  262.     if (current_length== buff_len)
  263.        return BUFF_FULL;
  264.     return BUFF_READY;
  265. }
  266.