|
| iMatix home page | << | < | > | >> |
SFLVersion 2.11 |
#include "sfllbuf.h" void linebuf_append (LINEBUF *buffer, const char *line)
Appends a line to the line buffer. If the buffer was full, the oldest line is lost. Updates the buffer head and tail as needed.
{
int
length, /* Size of line to insert */
room_left, /* Space left between head and top */
tail_old, /* Offset of tail into buffer */
head_old, /* Offset of head before insert */
head_new; /* Offset of head after insert */
char
*linedata; /* Address of data to store */
ASSERT (buffer);
ASSERT (line);
linedata = (char *) line;
length = strlen (line) + 1; /* Include trailing null */
room_left = (int) (buffer-> top - buffer-> head);
/* We need to make space for the new line; we calculate the new head
* and if the tail falls between the old and new head, it must be moved
* up to the next line start. We compare 'ints' not 'char *' because
* they can be negative.
*/
tail_old = (int) (buffer-> tail - buffer-> data);
head_old = (int) (buffer-> head - buffer-> data);
if (head_old > tail_old) /* Shift head_old down to get it */
head_old -= buffer-> size; /* somewhere before tail_old */
head_new = head_old + length; /* And calculate head_new */
/* If the line is too large for the remaining space, copy what we can */
if (length > room_left)
{
memcpy (buffer-> head, linedata, room_left);
linedata += room_left;
length -= room_left;
buffer-> head = buffer-> data; /* Bump head to start of buffer */
}
/* Copy rest of line to buffer */
memcpy (buffer-> head, linedata, length);
buffer-> head += length; /* Bump head past string */
if (buffer-> head == buffer-> top) /* and maybe wrap-around */
buffer-> head = buffer-> data;
ASSERT (buffer-> head <= buffer-> top);
if (head_old < tail_old /* If tail falls between head_old */
&& tail_old <= head_new) /* and/on head_new, bump it up */
buffer-> tail = start_next_line (buffer, buffer-> head);
}
| | << | < | > | >> |
|