Browse code

sbuf implementation

Dario Rodriguez authored on 13/03/2014 12:35:39
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,197 @@
1
+/*
2
+ * sbuf.h
3
+ *
4
+ * A string buffer library.
5
+ *
6
+ * History:
7
+ *      13/03/2014 Creation.
8
+ *
9
+ * Author: Dario Rodriguez dario@softhome.net
10
+ * This library is licensed on the terms of the GNU LGPL v2+
11
+ */
12
+
13
+#include <stdlib.h>
14
+#include <unistd.h>
15
+#include <string.h>
16
+#include "sbuf.h"
17
+
18
+/* sbuf, high level api */
19
+sbuf *
20
+sbuf_init(long bufsize)
21
+{
22
+        sbuf *buf;
23
+        if(bufsize<=0 || (buf=malloc(sizeof sbuf))==NULL)
24
+                return(NULL);
25
+        memset(buf,0,sizeof(sbuf));
26
+        if((buf->buf=malloc(bufsize))==NULL) {
27
+                free(buf),buf=NULL;
28
+                return(NULL);
29
+        }
30
+        buf->size=bufsize;
31
+        return(buf);
32
+}
33
+
34
+sbuf *
35
+sbuf_staticinit(sbuf *uninitsbuf, long bufsize, char *buf)
36
+{
37
+        if(uninitsbuf==NULL || buf==NULL || bufsize<=0)
38
+                return(NULL);
39
+        memset(uninitsbuf,0,sizeof(sbuf));
40
+        uninitsbuf->buf=buf;
41
+        uninitsbuf->size=bufsize;
42
+        return(uninitsbuf);
43
+}
44
+
45
+void
46
+sbuf_free(sbuf *buf)
47
+{
48
+        if(buf==NULL)
49
+                return;
50
+        if(buf->buf!=NULL)
51
+                free(buf->buf),buf->buf=NULL;
52
+        buf->size=0;
53
+        free(buf)
54
+}
55
+
56
+void
57
+sbuf_staticfree(sbuf *buf)
58
+{
59
+        if(buf==NULL)
60
+                return;
61
+        memset(buf,0,sizeof(sbuf));
62
+}
63
+
64
+int
65
+sbuf_fill(sbuf *buf, int fd, long numbytes)
66
+{
67
+        long n;
68
+        int read;
69
+        if(buf==NULL || fd==-1 || numbytes<0)
70
+                return(-1);
71
+        n=(numbytes>(buf->size-buf->used))?buf->size-buf->used:numbytes;
72
+        read=read(fd,buf->buf+buf->used,n);
73
+        if(read>=0)
74
+                buf->used+=read;
75
+        return(read);
76
+}
77
+
78
+char *
79
+sbuf_getline(sbuf *buf)
80
+{
81
+        char *start,*end;
82
+        if(buf==NULL)
83
+                return(NULL);
84
+        start=buf->buf+buf->got;
85
+        if((end=memchr(start,'\n',buf->used-buf->got))==NULL)
86
+                return(NULL);
87
+        *end='\0';
88
+        /* we accept lines both with "\n" and "\r\n"; strip the leading \r if necessary */
89
+        if(end>start && end[-1]=='\r')
90
+                end[-1]='\0';
91
+        buf->got+=(end-start+1);
92
+        return(start);
93
+}
94
+
95
+void
96
+sbuf_discard(sbuf *buf)
97
+{
98
+        if(buf==NULL || buf->got==0)
99
+                return;
100
+        memmove(buf->buf,buf->buf+buf->got,buf->used-buf->got);
101
+        buf->used-=buf->got;
102
+        buf->got=0;
103
+}
104
+
105
+void
106
+sbuf_wipe(sbuf *buf)
107
+{
108
+        buf->used=buf->got=0;
109
+}
110
+
111
+/* sbuf, addutional functionality */
112
+char *
113
+sbuf_getbytes(sbuf *buf, long numbytes)
114
+{
115
+        char *res;
116
+        if(buf==NULL || numbytes>(buf->used-buf->got))
117
+                return(NULL);
118
+        res=buf->buf+buf->got;
119
+        buf->got+=numbytes;
120
+        return(res);
121
+}
122
+
123
+long
124
+sbuf_count(sbuf *buf)
125
+{
126
+        if(buf==NULL)
127
+                return(0);
128
+        return(buf->used-buf->got);
129
+}
130
+
131
+char *
132
+sbuf_ptr(sbuf *buf)
133
+{
134
+        return(buf->buf+buf->got);
135
+}
136
+
137
+long
138
+sbuf_add(sbuf *buf, char *data, long datasize)
139
+{
140
+        long added;
141
+        if(buf==NULL || data==NULL || buf->used==buf->size)
142
+                return(0);
143
+        added=(datasize>(buf->size-buf->used))?(buf->size-buf->used)):datasize;
144
+        memcpy(buf->buf+buf->used,data,added);
145
+        buf->used+=added;
146
+        return(added);
147
+}
148
+
149
+long
150
+sbuf_addstr(sbuf *buf, char *str)
151
+{
152
+        if(buf==NULL || str==NULL)
153
+                return(0);
154
+        return(sbuf_add(buf,str,strlen(str)));
155
+}
156
+
157
+long
158
+sbuf_unused(sbuf *buf)
159
+{
160
+        if(buf==NULL)
161
+                return(0);
162
+        return(buf->size-buf->used);
163
+}
164
+
165
+char *
166
+sbuf_ptrunused(sbuf *buf)
167
+{
168
+        if(buf==NULL)
169
+                return(NULL);
170
+        return(buf->buf+buf->used);
171
+}
172
+
173
+long
174
+sbuf_addfromunused(sbuf *buf, long numbytes)
175
+{
176
+        int added;
177
+        if(buf==NULL || numbytes<=0)
178
+                return(0);
179
+        added=(numbytes>(buf->size-buf->used))?(buf->size-buf->used):numbytes;
180
+        buf->used+=added;
181
+        return(added);
182
+}
183
+
184
+long
185
+sbuf_send(sbuf *buf, int fd, long numbytes)
186
+{
187
+        int n;
188
+        int written;
189
+        if(buf==NULL || fd==-1 || numbytes<=0)
190
+                return(0);
191
+        n=(numbytes>(sbuf->used-sbuf->got))?(sbuf->used-sbuf->got):numbytes;
192
+        written=write(fd,buf->buf+buf->got,written);
193
+        if(written>0)
194
+                buf->got+=written;
195
+        return(written);
196
+}
197
+
... ...
@@ -8,6 +8,8 @@
8 8
  * History:
9 9
  *      21/01/2014 Creation.
10 10
  *      27/01/2014 Finish initial API.
11
+ *      13/03/2014 removed the low-level API, as it is redundant with
12
+ *                 _staticinit()/_staticfree().
11 13
  *
12 14
  * Author: Dario Rodriguez dario@softhome.net
13 15
  * This library is licensed on the terms of the GNU LGPL v2+
... ...
@@ -34,7 +36,7 @@ char *sbuf_getline(sbuf *buf);
34 36
 void sbuf_discard(sbuf *buf);
35 37
 void sbuf_wipe(sbuf *buf);
36 38
 
37
-/* sbuf, addutional functionality */
39
+/* sbuf, additional functionality */
38 40
 char *sbuf_getbytes(sbuf *buf, long numbytes);
39 41
 long sbuf_count(sbuf *buf);
40 42
 char *sbuf_ptr(sbuf *buf);
... ...
@@ -45,10 +47,5 @@ char *sbuf_ptrunused(sbuf *buf);
45 47
 long sbuf_addfromunused(sbuf *buf, long numbytes);
46 48
 long sbuf_send(sbuf *buf, int fd, long numbytes);
47 49
 
48
-/* low level api */
49
-long strbuf_fill(char *strbuf, long strbufsize, long *used, int fd. long numbytes);
50
-char *strbuf_getline(char *strbuf, long strbufsize, long used, long *got);
51
-void strbuf_discard(char *strbuf, long strbufsize, long *used, long *got);
52
-
53 50
 #endif
54 51