Advertisement
RetroZelda

C Byte Queue

Mar 24th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.69 KB | None | 0 0
  1. // Byte Queue- A simple byte queue written in C
  2. // Copyright (C) 2014  Erick "RetroZelda" Folckemer
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16.  
  17. #include <stdio.h>
  18.  
  19. #define ALLOC_SIZE 512
  20. #define BYTE unsigned char
  21. #define USHORT unsigned short
  22. #define UINT unsigned int
  23. #define ULL unsigned long long
  24. #define BOOL BYTE
  25. #define TRUE (BOOL)1
  26. #define FALSE (BOOL)0
  27.  
  28. // allowed memory pool
  29. const BYTE g_memory[ALLOC_SIZE];
  30.  
  31. typedef struct _Byte_Queue
  32. {
  33.     USHORT _count;
  34.     union
  35.     {
  36.         BYTE _head;
  37.         BYTE _data[ALLOC_SIZE - 2];
  38.     };
  39. } Byte_Queue;
  40.  
  41. BOOL Push(Byte_Queue* _queue, BYTE value)
  42. {
  43.     // ensure we have space
  44.     if(_queue->_count == ALLOC_SIZE - 2)
  45.     {
  46.         return FALSE;
  47.     }
  48.  
  49.     // add to the end of the queue
  50.     _queue->_data[_queue->_count] = value;
  51.     _queue->_count++;
  52.  
  53.     return TRUE;
  54. }
  55.  
  56. BOOL Pop(Byte_Queue* _queue, BYTE* _Output)
  57. {
  58.  
  59.     // ensure we have something to pop
  60.     if(_queue->_count == 0)
  61.     {
  62.         *_Output = (BYTE)0;
  63.         return FALSE;
  64.     }
  65.     // set the output;
  66.     *_Output = _queue->_head;
  67.  
  68.  
  69.     // shift everything over
  70.     UINT queueIndex = 0;
  71.     USHORT* curItem = (USHORT*)_queue->_data;
  72.     BYTE shiftCount = sizeof(BYTE) * 8;
  73.     for(;queueIndex < _queue->_count; ++queueIndex)
  74.     {
  75.         // shift the data
  76.         *curItem = (*curItem) >> shiftCount;
  77.  
  78.         // move on
  79.         curItem = (USHORT*)((UINT)curItem + (UINT)1);
  80.     }
  81.  
  82.     // set the count
  83.     _queue->_count--;
  84.  
  85.     return TRUE;
  86. }
  87.  
  88.  
  89.  
  90. int main(int argc, char* argv[])
  91. {
  92.     // create the queue
  93.     Byte_Queue* queue = (Byte_Queue*)g_memory;
  94.     queue->_count = 0;
  95.  
  96.     // queue the alphabet
  97.     char alphabet = 'A';
  98.     BOOL pushed = TRUE;
  99.     for(;alphabet <= 'Z'; alphabet += (char)1)
  100.     {
  101.         pushed = Push(queue, (BYTE)alphabet);
  102.         if(pushed == FALSE)
  103.         {
  104.             printf("Unable to push \'%c\'\n", alphabet);
  105.         }
  106.     }
  107.     printf("Queue has %d items inside!\n", queue->_count);
  108.     printf("Popping everything...\n\t");
  109.  
  110.     BOOL MoreData = TRUE;
  111.     char letter = '/0';
  112.     while(MoreData == TRUE)
  113.     {
  114.         MoreData = Pop(queue, (BYTE*)&letter);
  115.  
  116.         // only print if valid
  117.         if(letter != '\0')
  118.         {
  119.             printf("%c ", letter);
  120.         }
  121.     };
  122.     printf("\nQueue has %d items inside!\n", queue->_count);
  123.  
  124.     // fill the queue
  125.     alphabet = 'A';
  126.     do
  127.     {
  128.        MoreData = Push(queue, (BYTE)alphabet);
  129.        alphabet += (char)1;
  130.        if(alphabet > 'Z')
  131.        {
  132.            alphabet = 'A';
  133.        }
  134.     } while(MoreData == TRUE);
  135.     printf("\nQueue has %d items inside!\n", queue->_count);
  136.  
  137.     // now remove everything again
  138.     printf("Popping everything...\n\t");
  139.     MoreData = TRUE;
  140.     letter = '/0';
  141.     while(MoreData == TRUE)
  142.     {
  143.         MoreData = Pop(queue, (BYTE*)&letter);
  144.  
  145.         // only print if valid
  146.         if(letter != '\0')
  147.         {
  148.             printf("%c ", letter);
  149.         }
  150.     };
  151.     printf("\nQueue has %d items inside!\n", queue->_count);
  152.  
  153.     return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement