]> jfr.im git - irc/quakenet/newserv.git/blob - lib/stringbuf.c
A4STATS: remove E style escapes and switch to createtable for indices
[irc/quakenet/newserv.git] / lib / stringbuf.c
1 #include "stringbuf.h"
2
3 void sbinit(StringBuf *buf, char *c, int capacity) {
4 buf->buf = c;
5 buf->capacity = capacity;
6 buf->len = 0;
7 }
8
9 int sbaddchar(StringBuf *buf, char c) {
10 if(buf->len >= buf->capacity - 1)
11 return 0;
12
13 buf->buf[buf->len++] = c;
14
15 return 1;
16 }
17
18 int sbaddstr(StringBuf *buf, char *c) {
19 int remaining = buf->capacity - buf->len - 1;
20 char *p;
21
22 for(p=c;*p;p++) {
23 if(remaining <= 0)
24 return 0;
25
26 remaining--;
27
28 buf->buf[buf->len++] = *p;
29 }
30
31 return 1;
32 }
33
34 int sbaddstrlen(StringBuf *buf, char *c, size_t len) {
35 int remaining = buf->capacity - buf->len - 1;
36 char *p;
37 int i;
38
39 for(p=c,i=0;i<len;i++,p++) {
40 if(remaining <= 0)
41 return 0;
42
43 remaining--;
44
45 buf->buf[buf->len++] = *p;
46 }
47
48 return 1;
49 }
50
51 int sbterminate(StringBuf *buf) {
52 if(buf->capacity - buf->len > 0) {
53 buf->buf[buf->len] = '\0';
54 return 1;
55 }
56 return 0;
57 }