2007-05-15 17:39:34 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2007-06-19 15:20:31 +02:00
|
|
|
#include <stdarg.h>
|
2007-05-15 17:39:34 +02:00
|
|
|
|
|
|
|
#include "linebuffer.h"
|
|
|
|
|
2007-06-19 15:20:31 +02:00
|
|
|
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
|
|
|
|
|
2007-05-15 17:39:34 +02:00
|
|
|
struct linebuffer * create_linebuffer(int size)
|
|
|
|
{
|
2007-06-19 15:20:31 +02:00
|
|
|
struct linebuffer *buf = malloc(sizeof(struct linebuffer));
|
|
|
|
if (buf == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
buf->size = size;
|
|
|
|
buf->data = malloc(buf->size);
|
|
|
|
if (buf->data == NULL) {
|
|
|
|
free(buf);
|
2007-05-15 17:39:34 +02:00
|
|
|
return NULL;
|
2007-06-19 15:20:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
buf->pos = 0;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void linebuffer_free(struct linebuffer *buf)
|
|
|
|
{
|
|
|
|
free(buf->data);
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
int linebuffer_clear(struct linebuffer *buf)
|
|
|
|
{
|
|
|
|
int oldpos = buf->pos;
|
|
|
|
buf->pos = 0;
|
|
|
|
return oldpos;
|
|
|
|
}
|
|
|
|
|
|
|
|
int linebuffer_put(struct linebuffer *buf, const char *src, unsigned int size)
|
|
|
|
{
|
|
|
|
int len = MIN(buf->size - buf->pos, size);
|
|
|
|
memcpy(buf->data + buf->pos, src, len);
|
|
|
|
return len;
|
|
|
|
}
|
2007-05-15 17:39:34 +02:00
|
|
|
|
2007-06-19 15:20:31 +02:00
|
|
|
int linebuffer_readfd(struct linebuffer *buf, int fd)
|
|
|
|
{
|
|
|
|
int len = read(fd, buf->data + buf->pos, buf->size - buf->pos);
|
|
|
|
if (len <= 0)
|
2007-06-19 15:44:57 +02:00
|
|
|
return -1;
|
2007-06-19 15:20:31 +02:00
|
|
|
|
|
|
|
buf->pos += len;
|
|
|
|
return len;
|
2007-05-15 17:39:34 +02:00
|
|
|
}
|
|
|
|
|
2007-06-19 15:20:31 +02:00
|
|
|
int linebuffer_parsefd(struct linebuffer *buf, int fd)
|
2007-05-15 17:39:34 +02:00
|
|
|
{
|
2007-06-19 15:20:31 +02:00
|
|
|
char tmp[32];
|
|
|
|
int len = read(fd, tmp, sizeof(tmp));
|
2007-05-15 17:39:34 +02:00
|
|
|
if (len <= 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
/* "understand" backspace */
|
2007-06-19 15:20:31 +02:00
|
|
|
if (tmp[i] == 0x08 && buf->pos > 0) {
|
|
|
|
buf->pos--;
|
2007-05-15 17:39:34 +02:00
|
|
|
|
|
|
|
/* copy */
|
2007-06-19 15:20:31 +02:00
|
|
|
} else if (tmp[i] >= ' ' || tmp[i] == '\n') {
|
|
|
|
*(buf->data + buf->pos++) = tmp[i];
|
2007-05-15 17:39:34 +02:00
|
|
|
}
|
|
|
|
|
2007-06-19 15:20:31 +02:00
|
|
|
if (buf->pos > buf->size)
|
2007-05-15 17:39:34 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-06-19 15:44:57 +02:00
|
|
|
int linebuffer_writefd(struct linebuffer *buf, int fd)
|
|
|
|
{
|
|
|
|
int len = write(fd, buf->data, buf->pos);
|
|
|
|
if (len <= 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* TODO: now assuming everything is written */
|
|
|
|
buf->pos = 0;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2007-06-19 15:20:31 +02:00
|
|
|
int linebuffer_printf(struct linebuffer *buf, const char *fmt, ...)
|
2007-05-15 17:39:34 +02:00
|
|
|
{
|
2007-06-19 15:20:31 +02:00
|
|
|
va_list az;
|
|
|
|
va_start(az, fmt);
|
|
|
|
int len = vsnprintf(buf->data + buf->pos, buf->size - buf->pos, fmt, az);
|
|
|
|
va_end(az);
|
2007-05-15 17:39:34 +02:00
|
|
|
|
2007-06-19 15:20:31 +02:00
|
|
|
if (len < 0 || len >= (buf->size - buf->pos))
|
|
|
|
return -1;
|
2007-05-15 17:39:34 +02:00
|
|
|
|
2007-06-19 15:20:31 +02:00
|
|
|
buf->pos += len;
|
|
|
|
return len;
|
|
|
|
}
|
2007-05-15 17:39:34 +02:00
|
|
|
|
2007-06-19 15:20:31 +02:00
|
|
|
char * linebuffer_getline(struct linebuffer *buf, int *len)
|
|
|
|
{
|
|
|
|
buf->newline = memchr(buf->data, '\n', buf->pos);
|
|
|
|
if (buf->newline == NULL)
|
|
|
|
return NULL;
|
2007-05-15 17:39:34 +02:00
|
|
|
|
2007-06-19 15:20:31 +02:00
|
|
|
*(buf->newline) = '\0';
|
|
|
|
|
|
|
|
if (len != NULL)
|
|
|
|
*len = buf->newline - buf->data;
|
|
|
|
|
|
|
|
return buf->data;
|
2007-05-15 17:39:34 +02:00
|
|
|
}
|
|
|
|
|
2007-06-19 15:20:31 +02:00
|
|
|
int linebuffer_freeline(struct linebuffer *buf)
|
2007-05-15 17:39:34 +02:00
|
|
|
{
|
2007-06-19 15:20:31 +02:00
|
|
|
buf->pos -= (buf->newline - buf->data) +1;
|
|
|
|
memmove(buf->data, buf->newline +1, buf->pos);
|
|
|
|
buf->newline = NULL;
|
|
|
|
return buf->pos;
|
2007-05-15 17:39:34 +02:00
|
|
|
}
|