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