linebuf.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 /**
00022  * @file
00023  *
00024  * Line buffering
00025  *
00026  */
00027 
00028 #include <stdint.h>
00029 #include <string.h>
00030 #include <stdlib.h>
00031 #include <errno.h>
00032 #include <gpxe/linebuf.h>
00033 
00034 /**
00035  * Retrieve buffered-up line
00036  *
00037  * @v linebuf           Line buffer
00038  * @ret line            Buffered line, or NULL if no line ready to read
00039  */
00040 char * buffered_line ( struct line_buffer *linebuf ) {
00041         return ( linebuf->ready ? linebuf->data : NULL );
00042 }
00043 
00044 /**
00045  * Discard line buffer contents
00046  *
00047  * @v linebuf           Line buffer
00048  */
00049 void empty_line_buffer ( struct line_buffer *linebuf ) {
00050         free ( linebuf->data );
00051         linebuf->data = NULL;
00052         linebuf->len = 0;
00053         linebuf->ready = 0;
00054 }
00055 
00056 /**
00057  * Buffer up received data by lines
00058  *
00059  * @v linebuf                   Line buffer
00060  * @v data                      New data to add
00061  * @v len                       Length of new data to add
00062  * @ret len                     Consumed length, or negative error number
00063  *
00064  * After calling line_buffer(), use buffered_line() to determine
00065  * whether or not a complete line is available.  Carriage returns and
00066  * newlines will have been stripped, and the line will be
00067  * NUL-terminated.  This buffered line is valid only until the next
00068  * call to line_buffer() (or to empty_line_buffer()).
00069  *
00070  * Note that line buffers use dynamically allocated storage; you
00071  * should call empty_line_buffer() before freeing a @c struct @c
00072  * line_buffer.
00073  */
00074 ssize_t line_buffer ( struct line_buffer *linebuf,
00075                       const char *data, size_t len ) {
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 }

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