dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
buffer.cpp
Go to the documentation of this file.
1 
14 /*
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.
19  *
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
23  * for more details.
24  *
25  * You should have received a copy of the GNU General Public License along
26  * with this program; if not, see <http://www.gnu.org/licenses/>
27  */
28 
30 //*****************************************************************************
31 //
32 // File Name : 'buffer.c'
33 // Title : Multipurpose byte buffer structure and methods
34 // Author : Pascal Stang - Copyright (C) 2001-2002
35 // Created : 9/23/2001
36 // Revised : 9/23/2001
37 // Version : 1.0
38 // Target MCU : any
39 // Editor Tabs : 4
40 //
41 // This code is distributed under the GNU Public License
42 // which can be found at http://www.gnu.org/licenses/gpl.txt
43 //
44 //*****************************************************************************
45 
46 #include "buffer.h"
47 
48 // global variables
49 
50 // initialization
51 
52 void bufferInit(cBuffer *buffer, unsigned char *start, unsigned short size)
53 {
54  // set start pointer of the buffer
55  buffer->dataptr = start;
56  buffer->size = size;
57  // initialize index and length
58  buffer->dataindex = 0;
59  buffer->datalength = 0;
60 }
61 
62 // access routines
64 {
65  unsigned char data = 0;
66 
67  // check to see if there's data in the buffer
68  if (buffer->datalength) {
69  // get the first character from buffer
70  data = buffer->dataptr[buffer->dataindex];
71  // move index down and decrement length
72  buffer->dataindex++;
73  if (buffer->dataindex >= buffer->size) {
74  buffer->dataindex %= buffer->size;
75  }
76  buffer->datalength--;
77  }
78  // return
79  return data;
80 }
81 
82 void bufferDumpFromFront(cBuffer *buffer, unsigned short numbytes)
83 {
84  // dump numbytes from the front of the buffer
85  // are we dumping less than the entire buffer?
86  if (numbytes < buffer->datalength) {
87  // move index down by numbytes and decrement length by numbytes
88  buffer->dataindex += numbytes;
89  if (buffer->dataindex >= buffer->size) {
90  buffer->dataindex %= buffer->size;
91  }
92  buffer->datalength -= numbytes;
93  } else {
94  // flush the whole buffer
95  buffer->datalength = 0;
96  }
97 }
98 
99 unsigned char bufferGetAtIndex(cBuffer *buffer, unsigned short index)
100 {
101  // return character at index in buffer
102  return buffer->dataptr[(buffer->dataindex + index) % (buffer->size)];
103 }
104 
105 unsigned char bufferAddToEnd(cBuffer *buffer, unsigned char data)
106 {
107  // make sure the buffer has room
108  if (buffer->datalength < buffer->size) {
109  // save data byte at end of buffer
110  buffer->dataptr[(buffer->dataindex + buffer->datalength) % buffer->size] = data;
111  // increment the length
112  buffer->datalength++;
113  // return success
114  return -1;
115  } else
116  return 0;
117 }
118 
120 {
121  // check to see if the buffer has room
122  // return true if there is room
123  return (buffer->datalength < buffer->size);
124 }
125 
127 {
128  // flush contents of the buffer
129  buffer->datalength = 0;
130 }
unsigned short datalength
the length of the data currently in the buffer
Definition: buffer.h:58
unsigned short dataindex
the index into the buffer where the data starts
Definition: buffer.h:59
void bufferInit(cBuffer *buffer, unsigned char *start, unsigned short size)
initialize a buffer to start at a given address and have given size
Definition: buffer.cpp:52
end buffer
unsigned char * dataptr
the physical memory address where the buffer is stored
Definition: buffer.h:56
void bufferDumpFromFront(cBuffer *buffer, unsigned short numbytes)
dump (discard) the first numbytes from the front of the buffer
Definition: buffer.cpp:82
DataFields data
unsigned char bufferGetFromFront(cBuffer *buffer)
get the first byte from the front of the buffer
Definition: buffer.cpp:63
void bufferFlush(cBuffer *buffer)
flush (clear) the contents of the buffer
Definition: buffer.cpp:126
unsigned short size
the allocated size of the buffer
Definition: buffer.h:57
unsigned char bufferGetAtIndex(cBuffer *buffer, unsigned short index)
get a byte at the specified index in the buffer (kind of like array access)
Definition: buffer.cpp:99
cBuffer structure
Definition: buffer.h:54
unsigned char bufferAddToEnd(cBuffer *buffer, unsigned char data)
add a byte to the end of the buffer
Definition: buffer.cpp:105
Multipurpose byte buffer structure and methods.
unsigned char bufferIsNotFull(cBuffer *buffer)
check if the buffer is full/not full (returns non-zero value if not full)
Definition: buffer.cpp:119