Browse code

iobuf implementation

Dario Rodriguez authored on 12/03/2014 12:35:36
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,70 @@
1
+/*
2
+ * iobuf.c
3
+ *
4
+ * An I/O buffer library based on sbuf.
5
+ *
6
+ * History:
7
+ *      12/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 "iobuf.h"
15
+
16
+/* exported funcions */
17
+siobuf *
18
+iobuf_init(int insize,int outsize)
19
+{
20
+        siobuf *iobuf;
21
+        if((iobuf=malloc(sizeof(siobuf)))==NULL)
22
+                return(NULL);
23
+        memset(iobuf,0,sizeof(siobuf));
24
+        if((iobuf->in=sbuf_init(insize))==NULL) {
25
+                free(iobuf),iobuf=NULL;
26
+                return(NULL);
27
+        }
28
+        if((iobuf->out=sbuf_init(outsize))==NULL) {
29
+                sbuf_free(iobuf->in),iobuf->in=NULL;
30
+                free(iobuf),iobuf=NULL;
31
+                return(NULL);
32
+        }
33
+        return(iobuf);
34
+}
35
+
36
+siobuf *
37
+iobuf_staticinit(siobuf *uninitiobuf, sbuf *uninitinbuf, sbuf *uninitoutbuf,int insize, int outsize, char *inbuf, char *outbuf)
38
+{
39
+        memset(uninitiobuf,0,sizeof(siobuf));
40
+        if((siobuf->in=sbuf_staticinit(uninitinbuf,insize,inbuf))==NULL)
41
+                return(NULL);
42
+        if((siobuf->out=sbuf_staticinit(uninitoutbuf,outsize,outbuf))==NULL)
43
+                return(NULL);
44
+        return(uninitiobuf);
45
+}
46
+
47
+void
48
+iobuf_free(siobuf *iobuf)
49
+{
50
+        if(iobuf==NULL)
51
+                return;
52
+        if(iobuf->in!=NULL)
53
+                sbuf_free(iobuf->in),iobuf->in=NULL;
54
+        if(iobuf->out!=NULL)
55
+                sbuf_free(iobuf->out),iobuf->out=NULL;
56
+        free(iobuf);
57
+}
58
+
59
+void
60
+iobuf_staticfree(siobuf *iobuf)
61
+{
62
+        if(iobuf==NULL)
63
+                return;
64
+        sbuf_staticfree(iobuf->in),iobuf->in=NULL;
65
+        sbuf_staticfree(iobuf->out),iobuf->out=NULL;
66
+        return;
67
+}
68
+
69
+#endif
70
+
... ...
@@ -1,12 +1,13 @@
1 1
 /*
2 2
  * iobuf.h
3 3
  *
4
- * A O/O buffer library based on sbuf.
4
+ * An I/O buffer library based on sbuf.
5 5
  *
6 6
  * Header file.
7 7
  *
8 8
  * History:
9 9
  *      21/01/2014 Creation.
10
+ *      12/03/2014 Fix _staticinit() and  _*free() prototypes.
10 11
  *
11 12
  * Author: Dario Rodriguez dario@softhome.net
12 13
  * This library is licensed on the terms of the GNU LGPL v2+
... ...
@@ -25,10 +26,10 @@ typedef struct {
25 26
 
26 27
 /* iobuf */
27 28
 siobuf *iobuf_init(int insize,int outsize);
28
-siobuf *iobuf_staticinit(siobuf *uninitiobuf,int insize, int outsize, char *inbuf, char *outbuf);
29
+siobuf *iobuf_staticinit(siobuf *uninitiobuf, sbuf *uninitinbuf, sbuf *uninitoutbuf,int insize, int outsize, char *inbuf, char *outbuf);
29 30
 
30
-siobuf *iobuf_free(siobuf *);
31
-siobuf *iobuf_staticfree(siobuf *);
31
+void iobuf_free(siobuf *iobuf);
32
+void iobuf_staticfree(siobuf *iobuf);
32 33
 
33 34
 #endif
34 35