Browse code

Fix comments

Dario Rodriguez authored on 02/07/2014 10:34:30
Showing 1 changed files
... ...
@@ -1,12 +1,8 @@
1 1
 /*
2
- * sbuf.h
2
+ * sbuf.c
3 3
  *
4 4
  * A string buffer library.
5 5
  *
6
- * History:
7
- *      13/03/2014 Creation.
8
- *      14/03/2014 Compilation fixes.
9
- *
10 6
  * Author: Dario Rodriguez dario@softhome.net
11 7
  * This library is licensed on the terms of the GNU LGPL v2+
12 8
  */
Browse code

webkernel: completed initial implementation

Dario Rodriguez authored on 17/06/2014 20:32:33
Showing 1 changed files
... ...
@@ -136,7 +136,7 @@ sbuf_ptr(sbuf *buf)
136 136
 }
137 137
 
138 138
 long
139
-sbuf_add(sbuf *buf, char *data, long datasize)
139
+sbuf_add(sbuf *buf, const char *data, long datasize)
140 140
 {
141 141
         long added;
142 142
         if(buf==NULL || data==NULL || buf->used==buf->size)
... ...
@@ -148,7 +148,7 @@ sbuf_add(sbuf *buf, char *data, long datasize)
148 148
 }
149 149
 
150 150
 long
151
-sbuf_addstr(sbuf *buf, char *str)
151
+sbuf_addstr(sbuf *buf, const char *str)
152 152
 {
153 153
         if(buf==NULL || str==NULL)
154 154
                 return(0);
Browse code

Compilation fixes. Implementation of loglib and parselib

Dario Rodriguez authored on 14/03/2014 12:42:03
Showing 1 changed files
... ...
@@ -5,6 +5,7 @@
5 5
  *
6 6
  * History:
7 7
  *      13/03/2014 Creation.
8
+ *      14/03/2014 Compilation fixes.
8 9
  *
9 10
  * Author: Dario Rodriguez dario@softhome.net
10 11
  * This library is licensed on the terms of the GNU LGPL v2+
... ...
@@ -20,7 +21,7 @@ sbuf *
20 21
 sbuf_init(long bufsize)
21 22
 {
22 23
         sbuf *buf;
23
-        if(bufsize<=0 || (buf=malloc(sizeof sbuf))==NULL)
24
+        if(bufsize<=0 || (buf=malloc(sizeof(sbuf)))==NULL)
24 25
                 return(NULL);
25 26
         memset(buf,0,sizeof(sbuf));
26 27
         if((buf->buf=malloc(bufsize))==NULL) {
... ...
@@ -50,7 +51,7 @@ sbuf_free(sbuf *buf)
50 51
         if(buf->buf!=NULL)
51 52
                 free(buf->buf),buf->buf=NULL;
52 53
         buf->size=0;
53
-        free(buf)
54
+        free(buf);
54 55
 }
55 56
 
56 57
 void
... ...
@@ -65,14 +66,14 @@ int
65 66
 sbuf_fill(sbuf *buf, int fd, long numbytes)
66 67
 {
67 68
         long n;
68
-        int read;
69
+        int nread;
69 70
         if(buf==NULL || fd==-1 || numbytes<0)
70 71
                 return(-1);
71 72
         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);
73
+        nread=read(fd,buf->buf+buf->used,n);
74
+        if(nread>=0)
75
+                buf->used+=nread;
76
+        return(nread);
76 77
 }
77 78
 
78 79
 char *
... ...
@@ -140,7 +141,7 @@ sbuf_add(sbuf *buf, char *data, long datasize)
140 141
         long added;
141 142
         if(buf==NULL || data==NULL || buf->used==buf->size)
142 143
                 return(0);
143
-        added=(datasize>(buf->size-buf->used))?(buf->size-buf->used)):datasize;
144
+        added=(datasize>(buf->size-buf->used))?(buf->size-buf->used):datasize;
144 145
         memcpy(buf->buf+buf->used,data,added);
145 146
         buf->used+=added;
146 147
         return(added);
... ...
@@ -188,8 +189,8 @@ sbuf_send(sbuf *buf, int fd, long numbytes)
188 189
         int written;
189 190
         if(buf==NULL || fd==-1 || numbytes<=0)
190 191
                 return(0);
191
-        n=(numbytes>(sbuf->used-sbuf->got))?(sbuf->used-sbuf->got):numbytes;
192
-        written=write(fd,buf->buf+buf->got,written);
192
+        n=(numbytes>(buf->used-buf->got))?(buf->used-buf->got):numbytes;
193
+        written=write(fd,buf->buf+buf->got,n);
193 194
         if(written>0)
194 195
                 buf->got+=written;
195 196
         return(written);
Browse code

sbuf implementation

Dario Rodriguez authored on 13/03/2014 12:35:39
Showing 1 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
+