Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Byte Queue- A simple byte queue written in C
- // Copyright (C) 2014 Erick "RetroZelda" Folckemer
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- #include <stdio.h>
- #define ALLOC_SIZE 512
- #define BYTE unsigned char
- #define USHORT unsigned short
- #define UINT unsigned int
- #define ULL unsigned long long
- #define BOOL BYTE
- #define TRUE (BOOL)1
- #define FALSE (BOOL)0
- // allowed memory pool
- const BYTE g_memory[ALLOC_SIZE];
- typedef struct _Byte_Queue
- {
- USHORT _count;
- union
- {
- BYTE _head;
- BYTE _data[ALLOC_SIZE - 2];
- };
- } Byte_Queue;
- BOOL Push(Byte_Queue* _queue, BYTE value)
- {
- // ensure we have space
- if(_queue->_count == ALLOC_SIZE - 2)
- {
- return FALSE;
- }
- // add to the end of the queue
- _queue->_data[_queue->_count] = value;
- _queue->_count++;
- return TRUE;
- }
- BOOL Pop(Byte_Queue* _queue, BYTE* _Output)
- {
- // ensure we have something to pop
- if(_queue->_count == 0)
- {
- *_Output = (BYTE)0;
- return FALSE;
- }
- // set the output;
- *_Output = _queue->_head;
- // shift everything over
- UINT queueIndex = 0;
- USHORT* curItem = (USHORT*)_queue->_data;
- BYTE shiftCount = sizeof(BYTE) * 8;
- for(;queueIndex < _queue->_count; ++queueIndex)
- {
- // shift the data
- *curItem = (*curItem) >> shiftCount;
- // move on
- curItem = (USHORT*)((UINT)curItem + (UINT)1);
- }
- // set the count
- _queue->_count--;
- return TRUE;
- }
- int main(int argc, char* argv[])
- {
- // create the queue
- Byte_Queue* queue = (Byte_Queue*)g_memory;
- queue->_count = 0;
- // queue the alphabet
- char alphabet = 'A';
- BOOL pushed = TRUE;
- for(;alphabet <= 'Z'; alphabet += (char)1)
- {
- pushed = Push(queue, (BYTE)alphabet);
- if(pushed == FALSE)
- {
- printf("Unable to push \'%c\'\n", alphabet);
- }
- }
- printf("Queue has %d items inside!\n", queue->_count);
- printf("Popping everything...\n\t");
- BOOL MoreData = TRUE;
- char letter = '/0';
- while(MoreData == TRUE)
- {
- MoreData = Pop(queue, (BYTE*)&letter);
- // only print if valid
- if(letter != '\0')
- {
- printf("%c ", letter);
- }
- };
- printf("\nQueue has %d items inside!\n", queue->_count);
- // fill the queue
- alphabet = 'A';
- do
- {
- MoreData = Push(queue, (BYTE)alphabet);
- alphabet += (char)1;
- if(alphabet > 'Z')
- {
- alphabet = 'A';
- }
- } while(MoreData == TRUE);
- printf("\nQueue has %d items inside!\n", queue->_count);
- // now remove everything again
- printf("Popping everything...\n\t");
- MoreData = TRUE;
- letter = '/0';
- while(MoreData == TRUE)
- {
- MoreData = Pop(queue, (BYTE*)&letter);
- // only print if valid
- if(letter != '\0')
- {
- printf("%c ", letter);
- }
- };
- printf("\nQueue has %d items inside!\n", queue->_count);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement