linebuf.h File Reference

Line buffering. More...

#include <stdint.h>
#include <stddef.h>

Go to the source code of this file.

Data Structures

struct  line_buffer
 A line buffer. More...

Functions

 FILE_LICENCE (GPL2_OR_LATER)
char * buffered_line (struct line_buffer *linebuf)
 Retrieve buffered-up line.
ssize_t line_buffer (struct line_buffer *linebuf, const char *data, size_t len)
 Buffer up received data by lines.
void empty_line_buffer (struct line_buffer *linebuf)
 Discard line buffer contents.


Detailed Description

Line buffering.

Definition in file linebuf.h.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER   ) 

char* buffered_line ( struct line_buffer linebuf  ) 

Retrieve buffered-up line.

Parameters:
linebuf Line buffer
Return values:
line Buffered line, or NULL if no line ready to read

Definition at line 40 of file linebuf.c.

References line_buffer::data, NULL, and line_buffer::ready.

Referenced by http_socket_deliver_iob(), and linebuf_test().

00040                                                      {
00041         return ( linebuf->ready ? linebuf->data : NULL );
00042 }

ssize_t line_buffer ( struct line_buffer linebuf,
const char *  data,
size_t  len 
)

Buffer up received data by lines.

Parameters:
linebuf Line buffer
data New data to add
len Length of new data to add
Return values:
len Consumed length, or negative error number
After calling line_buffer(), use buffered_line() to determine whether or not a complete line is available. Carriage returns and newlines will have been stripped, and the line will be NUL-terminated. This buffered line is valid only until the next call to line_buffer() (or to empty_line_buffer()).

Note that line buffers use dynamically allocated storage; you should call empty_line_buffer() before freeing a struct line_buffer.

Definition at line 74 of file linebuf.c.

References line_buffer::data, empty_line_buffer(), ENOMEM, line_buffer::len, memchr(), memcpy, line_buffer::ready, and realloc().

Referenced by http_socket_deliver_iob(), and linebuf_test().

00075                                                      {
00076         const char *eol;
00077         size_t consume;
00078         size_t new_len;
00079         char *new_data;
00080 
00081         /* Free any completed line from previous iteration */
00082         if ( linebuf->ready )
00083                 empty_line_buffer ( linebuf );
00084 
00085         /* Search for line terminator */
00086         if ( ( eol = memchr ( data, '\n', len ) ) ) {
00087                 consume = ( eol - data + 1 );
00088         } else {
00089                 consume = len;
00090         }
00091 
00092         /* Reallocate data buffer and copy in new data */
00093         new_len = ( linebuf->len + consume );
00094         new_data = realloc ( linebuf->data, ( new_len + 1 ) );
00095         if ( ! new_data )
00096                 return -ENOMEM;
00097         memcpy ( ( new_data + linebuf->len ), data, consume );
00098         new_data[new_len] = '\0';
00099         linebuf->data = new_data;
00100         linebuf->len = new_len;
00101 
00102         /* If we have reached end of line, trim the line and mark as ready */
00103         if ( eol ) {
00104                 linebuf->data[--linebuf->len] = '\0'; /* trim NL */
00105                 if ( linebuf->data[linebuf->len - 1] == '\r' )
00106                         linebuf->data[--linebuf->len] = '\0'; /* trim CR */
00107                 linebuf->ready = 1;
00108         }
00109 
00110         return consume;
00111 }

void empty_line_buffer ( struct line_buffer linebuf  ) 

Discard line buffer contents.

Parameters:
linebuf Line buffer

Definition at line 49 of file linebuf.c.

References line_buffer::data, free(), line_buffer::len, NULL, and line_buffer::ready.

Referenced by http_free(), http_rx_header(), line_buffer(), and linebuf_test().

00049                                                        {
00050         free ( linebuf->data );
00051         linebuf->data = NULL;
00052         linebuf->len = 0;
00053         linebuf->ready = 0;
00054 }


Generated on Tue Apr 6 20:01:51 2010 for gPXE by  doxygen 1.5.7.1