torrent-stats/linebuffer.c

154 lines
3.8 KiB
C
Raw Normal View History

2007-07-14 15:31:27 +02:00
/***************************************************************************
* Copyright (C) 07/2007 by Olaf Rempel *
* razzor@kopf-tisch.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; version 2 of the License *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
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);
2007-07-14 15:43:35 +02:00
buf->pos += len;
2007-06-19 15:20:31 +02:00
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-07-14 15:43:35 +02:00
int linebuffer_vprintf(struct linebuffer *buf, const char *fmt, va_list az)
2007-05-15 17:39:34 +02:00
{
2007-06-19 15:20:31 +02:00
int len = vsnprintf(buf->data + buf->pos, buf->size - buf->pos, fmt, az);
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-07-14 15:43:35 +02:00
int linebuffer_printf(struct linebuffer *buf, const char *fmt, ...)
{
va_list az;
va_start(az, fmt);
int ret = linebuffer_vprintf(buf, fmt, az);
va_end(az);
return ret;
}
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
}