CharRing
Simple character ring buffer for mocrocontrollers
charring.c
Go to the documentation of this file.
00001 
00014 #include "charring.h"
00015 #include <stdlib.h>
00016 
00017 
00026 CharRingBuf *CharRing_new(int16_t len)
00027 {
00028     CharRingBuf *buffer = calloc(1, sizeof(*buffer));
00029     buffer->len = len;
00030     buffer->buf = calloc(buffer->len, sizeof(buffer->buf));
00031 
00032     return buffer;
00033 }
00034 
00035 
00043 void CharRing_free(CharRingBuf *b)
00044 {
00045     free(b->buf);
00046     free(b);
00047 }
00048 
00049 
00066 void CharRing_putchar(CharRingBuf *b, char c)
00067 {
00068     if (CharRing_full(b)) {
00069         /* buffer is full, read out char to make room */
00070         CharRing_getchar(b);
00071     }
00072     b->buf[b->head] = c;
00073     b->head++;
00074     b->head %= b->len;
00075     b->count++;
00076 }
00077 
00078 
00098 char CharRing_getchar(CharRingBuf *b)
00099 {
00100     char c = 0;
00101     if (CharRing_available(b)) {
00102         c = b->buf[b->tail];
00103         b->tail++;
00104         b->tail %= b->len;
00105         b->count--;
00106     }
00107     return c;
00108 }
 All Data Structures Files Functions Variables Defines