2 ******************************************************************************
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
7 * As with all modules only the initialize function is exposed all other
8 * interactions with the module take place through the event queue and
10 * @see The GNU Public License (GPL) Version 3
12 *****************************************************************************/
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 /*! \file buffer.c \brief Multipurpose byte buffer structure and methods. */
31 // *****************************************************************************
33 // File Name : 'buffer.c'
34 // Title : Multipurpose byte buffer structure and methods
35 // Author : Pascal Stang - Copyright (C) 2001-2002
36 // Created : 9/23/2001
37 // Revised : 9/23/2001
42 // This code is distributed under the GNU Public License
43 // which can be found at http://www.gnu.org/licenses/gpl.txt
45 // *****************************************************************************
53 void bufferInit(cBuffer
*buffer
, unsigned char *start
, unsigned short size
)
55 // set start pointer of the buffer
56 buffer
->dataptr
= start
;
58 // initialize index and length
59 buffer
->dataindex
= 0;
60 buffer
->datalength
= 0;
64 unsigned char bufferGetFromFront(cBuffer
*buffer
)
66 unsigned char data
= 0;
68 // check to see if there's data in the buffer
69 if (buffer
->datalength
) {
70 // get the first character from buffer
71 data
= buffer
->dataptr
[buffer
->dataindex
];
72 // move index down and decrement length
74 if (buffer
->dataindex
>= buffer
->size
) {
75 buffer
->dataindex
%= buffer
->size
;
83 void bufferDumpFromFront(cBuffer
*buffer
, unsigned short numbytes
)
85 // dump numbytes from the front of the buffer
86 // are we dumping less than the entire buffer?
87 if (numbytes
< buffer
->datalength
) {
88 // move index down by numbytes and decrement length by numbytes
89 buffer
->dataindex
+= numbytes
;
90 if (buffer
->dataindex
>= buffer
->size
) {
91 buffer
->dataindex
%= buffer
->size
;
93 buffer
->datalength
-= numbytes
;
95 // flush the whole buffer
96 buffer
->datalength
= 0;
100 unsigned char bufferGetAtIndex(cBuffer
*buffer
, unsigned short index
)
102 // return character at index in buffer
103 return buffer
->dataptr
[(buffer
->dataindex
+ index
) % (buffer
->size
)];
106 unsigned char bufferAddToEnd(cBuffer
*buffer
, unsigned char data
)
108 // make sure the buffer has room
109 if (buffer
->datalength
< buffer
->size
) {
110 // save data byte at end of buffer
111 buffer
->dataptr
[(buffer
->dataindex
+ buffer
->datalength
) % buffer
->size
] = data
;
112 // increment the length
113 buffer
->datalength
++;
119 unsigned char bufferIsNotFull(cBuffer
*buffer
)
121 // check to see if the buffer has room
122 // return true if there is room
123 return buffer
->datalength
< buffer
->size
;
126 void bufferFlush(cBuffer
*buffer
)
128 // flush contents of the buffer
129 buffer
->datalength
= 0;