2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * linebuf.c: Simple line buffer
30 * Known contributors to this file:
31 * Markus Armbruster, 2007
41 * Initialize empty line buffer.
42 * Not necessary if *LBUF is already zeroed.
45 lbuf_init(struct lbuf *lbuf)
47 lbuf->len = lbuf->full = 0;
51 * Return length of currently buffered line.
52 * This includes the newline if present.
55 lbuf_len(struct lbuf *lbuf)
61 * Is LBUF full (i.e. we got the newline)?
64 lbuf_full(struct lbuf *lbuf)
70 * Return a pointer to the currently buffered line.
71 * If you mess with the line in a way that changes the line length,
72 * better call lbuf_init() next.
75 lbuf_line(struct lbuf *lbuf)
77 assert(lbuf->len < sizeof(lbuf->line));
78 lbuf->line[lbuf->len] = 0;
83 * Append CH to the line buffered in LBUF.
84 * LBUF must not be full.
85 * If CH is a newline, the buffer is now full. Return the line
86 * length, including the newline.
87 * Else return 0 if there was space, and -1 if not.
90 lbuf_putc(struct lbuf *lbuf, char ch)
95 assert(lbuf->len + 1 < sizeof(lbuf->line));
96 lbuf->line[lbuf->len++] = ch;
97 lbuf->line[lbuf->len] = 0;
102 if (lbuf->len + 2 >= sizeof(lbuf->line))
103 return -1; /* truncating long line */
105 lbuf->line[lbuf->len++] = ch;