#include <lib/ringbuf.h>

void ring_buf_init(ring_buf_t* rb) {
    rb->head = 0;
    rb->tail = 0;
}

int ring_buf_is_empty(ring_buf_t* rb) {
    return rb->head == rb->tail;
}

int ring_buf_is_full(ring_buf_t* rb) {
    return ((rb->head + 1) % PTY_BUFFER_SIZE) == rb->tail;
}

int ring_buf_write(ring_buf_t *buf, char c) {
    if (ring_buf_is_full(buf)) {
        return -1;
    }
    
    buf->data[buf->head] = c;
    buf->head = (buf->head + 1) % PTY_BUFFER_SIZE;
    return 0;
}

int ring_buf_read(ring_buf_t *buf, char *c) {
    if (ring_buf_is_empty(buf)) {
        return -1;
    }
    
    *c = buf->data[buf->tail];
    buf->tail = (buf->tail + 1) % PTY_BUFFER_SIZE;
    return 0;
}