Main Page   Data Structures   File List   Data Fields   Globals  

/buffer.c

Go to the documentation of this file.
00001 /*! \file buffer.c \brief Multipurpose byte buffer structure and methods. */
00002 //*****************************************************************************
00003 //
00004 // File Name    : 'buffer.c'
00005 // Title        : Multipurpose byte buffer structure and methods
00006 // Author       : Pascal Stang - Copyright (C) 2001-2002
00007 // Created      : 9/23/2001
00008 // Revised      : 9/23/2001
00009 // Version      : 1.0
00010 // Target MCU   : any
00011 // Editor Tabs  : 4
00012 //
00013 // This code is distributed under the GNU Public License
00014 //      which can be found at http://www.gnu.org/licenses/gpl.txt
00015 //
00016 //*****************************************************************************
00017 
00018 #include "buffer.h"
00019 
00020 // global variables
00021 
00022 // initialization
00023 
00024 void bufferInit(cBuffer* buffer, unsigned char *start, unsigned short size)
00025 {
00026     // set start pointer of the buffer
00027     buffer->dataptr = start;
00028     buffer->size = size;
00029     // initialize index and length
00030     buffer->dataindex = 0;
00031     buffer->datalength = 0;
00032 }
00033 
00034 // access routines
00035 unsigned char  bufferGetFromFront(cBuffer* buffer)
00036 {
00037     unsigned char data = 0;
00038     
00039     // check to see if there's data in the buffer
00040     if(buffer->datalength)
00041     {
00042         // get the first character from buffer
00043         data = buffer->dataptr[buffer->dataindex];
00044         // move index down and decrement length
00045         buffer->dataindex++;
00046         if(buffer->dataindex >= buffer->size)
00047         {
00048             buffer->dataindex %= buffer->size;
00049         }
00050         buffer->datalength--;
00051     }
00052     // return
00053     return data;
00054 }
00055 
00056 void bufferDumpFromFront(cBuffer* buffer, unsigned short numbytes)
00057 {
00058     // dump numbytes from the front of the buffer
00059     // are we dumping less than the entire buffer?
00060     if(numbytes < buffer->datalength)
00061     {
00062         // move index down by numbytes and decrement length by numbytes
00063         buffer->dataindex += numbytes;
00064         if(buffer->dataindex >= buffer->size)
00065         {
00066             buffer->dataindex %= buffer->size;
00067         }
00068         buffer->datalength -= numbytes;
00069     }
00070     else
00071     {
00072         // flush the whole buffer
00073         buffer->datalength = 0;
00074     }
00075 }
00076 
00077 unsigned char bufferGetAtIndex(cBuffer* buffer, unsigned short index)
00078 {
00079     // return character at index in buffer
00080     return buffer->dataptr[(buffer->dataindex+index)%(buffer->size)];
00081 }
00082 
00083 unsigned char bufferAddToEnd(cBuffer* buffer, unsigned char data)
00084 {
00085     // make sure the buffer has room
00086     if(buffer->datalength < buffer->size)
00087     {
00088         // save data byte at end of buffer
00089         buffer->dataptr[(buffer->dataindex + buffer->datalength) % buffer->size] = data;
00090         // increment the length
00091         buffer->datalength++;
00092         // return success
00093         return -1;
00094     }
00095     else return 0;
00096 }
00097 
00098 unsigned char bufferIsNotFull(cBuffer* buffer)
00099 {
00100     // check to see if the buffer has room
00101     // return true if there is room
00102     return (buffer->datalength < buffer->size);
00103 }
00104 
00105 void bufferFlush(cBuffer* buffer)
00106 {
00107     // flush contents of the buffer
00108     buffer->datalength = 0;
00109 }
00110 

Generated on Fri Aug 1 10:42:40 2003 for Procyon AVRlib by doxygen1.2.18