]> git.pond.sub.org Git - empserver/blob - src/client/linebuf.c
5025fc533f425aa4bd24b874519c93e9d0a52c5f
[empserver] / src / client / linebuf.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  linebuf.c: Simple line buffer
29  *
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2007
32  */
33
34 #include <config.h>
35
36 #include <assert.h>
37 #include <stdlib.h>
38 #include "linebuf.h"
39
40 /*
41  * Initialize empty line buffer.
42  * Not necessary if *LBUF is already zeroed.
43  */
44 void
45 lbuf_init(struct lbuf *lbuf)
46 {
47     lbuf->len = lbuf->full = 0;
48 }
49
50 /*
51  * Return length of currently buffered line.
52  * This includes the newline if present.
53  */
54 int
55 lbuf_len(struct lbuf *lbuf)
56 {
57     return lbuf->len;
58 }
59
60 /*
61  * Is LBUF full (i.e. we got the newline)?
62  */
63 int
64 lbuf_full(struct lbuf *lbuf)
65 {
66     return lbuf->full;
67 }
68
69 /*
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.
73  */
74 char *
75 lbuf_line(struct lbuf *lbuf)
76 {
77     assert(lbuf->len < sizeof(lbuf->line));
78     lbuf->line[lbuf->len] = 0;
79     return lbuf->line;
80 }
81
82 /*
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.
88  */
89 int
90 lbuf_putc(struct lbuf *lbuf, char ch)
91 {
92     assert(!lbuf->full);
93
94     if (ch == '\n') {
95         assert(lbuf->len + 1 < sizeof(lbuf->line));
96         lbuf->line[lbuf->len++] = ch;
97         lbuf->line[lbuf->len] = 0;
98         lbuf->full = 1;
99         return lbuf->len;
100     }
101
102     if (lbuf->len + 2 >= sizeof(lbuf->line))
103         return -1;              /* truncating long line */
104
105     lbuf->line[lbuf->len++] = ch;
106     return 0;
107 }