2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * linebuf.c: Simple line buffer
29 * Known contributors to this file:
30 * Markus Armbruster, 2007
40 * Initialize empty line buffer.
41 * Not necessary if *LBUF is already zeroed.
44 lbuf_init(struct lbuf *lbuf)
46 lbuf->len = lbuf->full = 0;
50 * Return length of currently buffered line.
51 * This includes the newline if present.
54 lbuf_len(struct lbuf *lbuf)
60 * Is LBUF full (i.e. we got the newline)?
63 lbuf_full(struct lbuf *lbuf)
69 * Return a pointer to the currently buffered line.
70 * If you mess with the line in a way that changes the line length,
71 * better call lbuf_init() next.
74 lbuf_line(struct lbuf *lbuf)
76 assert(lbuf->len < sizeof(lbuf->line));
77 lbuf->line[lbuf->len] = 0;
82 * Append CH to the line buffered in LBUF.
83 * LBUF must not be full.
84 * If CH is a newline, the buffer is now full. Return the line
85 * length, including the newline.
86 * Else return 0 if there was space, and -1 if not.
89 lbuf_putc(struct lbuf *lbuf, char ch)
94 assert(lbuf->len + 1 < sizeof(lbuf->line));
95 lbuf->line[lbuf->len++] = ch;
96 lbuf->line[lbuf->len] = 0;
101 if (lbuf->len + 2 >= sizeof(lbuf->line))
102 return -1; /* truncating long line */
104 lbuf->line[lbuf->len++] = ch;