-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircularBuffer.c
223 lines (174 loc) · 3.93 KB
/
circularBuffer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* main.c
*
* Created on: May 11, 2017
* Author: gena
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef enum {false=0, true} bool;
typedef enum {EMPTY = 0, OCCUPIED, FULL} bufferStatus;
//define circular buffer structure
typedef struct buffer
{
int head;
int tail;
int bufferSize;
int occupied;
bufferStatus status;
int sizeRemaining;
char* str;
}buffer;
//define and initialize to NULL sircular buffer pointer
buffer* serialDataBuffer = NULL;
//-------Function declarations--------
buffer* initBuffer(buffer* pBuffer, int size);
buffer* createNewBuffer(buffer* pBuffer);
bool isBufferFull(buffer* pBuf);
bool isBufferEmpty(buffer* pBuf);
bool bufferWrite(buffer* pBuffer, int sizeBytes, char* dataToBeWrite);
bool bufferRead(buffer* pBuffer, int sizeBytes, char* dataToBeRead);
//----MAIN----
int main()
{
int run = 1;
bool ret;
char data;
char* pch;
char ch = 't';
char func;
pch = &ch;
//init buffer
serialDataBuffer = initBuffer(serialDataBuffer, 4);
while(run)
{
printf("\nChoose one of the following options:...\n1. Write data 'w'\n2. Read data 'r'\n3. Print data 'p'\n4. Delete data 'd'\n");
printf("Insert function:...");
scanf(" %c", &func);
switch(func)
{
case 'w':
printf("\nType data to be stored...");
scanf("\n %c", &data);
bufferWrite(serialDataBuffer, 1, &data);
break;
case 'r':
bufferRead(serialDataBuffer, 1, &data);
break;
}
}
return 0;
}
//----------Funtion definitions------------
buffer* initBuffer(buffer* pBuffer, int size)
{
buffer* buf = NULL;
//create buffer
buf = createNewBuffer(pBuffer);
//initialize all members of struct buffer
buf->head = 0;
buf->tail = 0;
buf->bufferSize = size;
buf->occupied = 0;
buf->sizeRemaining = size;
buf->status = EMPTY;
buf->str = (char*)malloc(sizeof(char)*size);
if(!buf->str)
{
printf("\nError memory allocation...");
}
return buf;
}//end func.
buffer* createNewBuffer(buffer* pBuffer)
{
//create new buffer
pBuffer = (buffer*)malloc(sizeof(buffer));
if(!pBuffer)
{
printf("\nError memory allocation...");
}
return pBuffer;
}//end function
//Function check if buffer is full
bool isBufferFull(buffer* pBuf)
{
bool status = false;
if(pBuf->sizeRemaining == 0 && pBuf->status == FULL)
{
status = true;
}
return status;
}//end func.
//Function check if buffer is empty
bool isBufferEmpty(buffer* pBuf)
{
bool status = false;
if(pBuf->sizeRemaining == pBuf->bufferSize && pBuf->status == EMPTY)
{
status = true;
}
return status;
}//end func.
//
bool bufferWrite(buffer* pBuffer, int sizeBytes, char* dataToBeWrite)
{
bool status = false;
//check if buffer is full
if(!isBufferFull(pBuffer))
{
//check if there buffer has enough space to accept new chars
if(pBuffer->sizeRemaining >= sizeBytes)
{
//write to buffer
pBuffer->str[pBuffer->head] = *dataToBeWrite;
pBuffer->status = OCCUPIED;
//increment head by one
pBuffer->head = pBuffer->head + 1;
//update size occupied in buffer
pBuffer->head = (pBuffer->head)%(pBuffer->bufferSize);
pBuffer->sizeRemaining--;
if(pBuffer->sizeRemaining == 0)
{
pBuffer->status = FULL;
}
pBuffer->occupied++;
status = true;
}
else
{
printf("\nNot enough space to write...\n");
status = false;
}
}
else
{
printf("\nNot enough space to write...");
status = false;
}
return status;
}//end func.
bool bufferRead(buffer* pBuffer, int sizeBytes, char* dataToBeRead)
{
bool ret = false;
if(!isBufferEmpty(pBuffer))
{
*dataToBeRead = pBuffer->str[pBuffer->tail];
printf("\ndata %c \n", *dataToBeRead);
//increment tail by one
pBuffer->tail = pBuffer->tail + 1;
//update remaining size
pBuffer->tail = (pBuffer->tail)%(pBuffer->bufferSize);
pBuffer->occupied--;
if(pBuffer->occupied == 0)
{
pBuffer->status = EMPTY;
}
pBuffer->sizeRemaining++;
}
else
{
printf("\nBuffer is empty...\n");
}
return ret;
}//end func.