317:09676b559091 | 318:60870dbac94f |
---|---|
32 | 32 |
33 #include "writer.h" | 33 #include "writer.h" |
34 | 34 |
35 void writer_init(Writer *w, SYS_NETFD fd, char *buf, size_t len) { | 35 void writer_init(Writer *w, SYS_NETFD fd, char *buf, size_t len) { |
36 w->fd = fd; | 36 w->fd = fd; |
37 w->write = (wr_writefunc)net_write; | |
38 w->buffer = buf; | |
39 w->size = len; | |
40 w->pos = 0; | |
41 w->error = 0; | |
42 } | |
43 | |
44 void writer_init_with_stream(Writer *w, void *stream, wr_writefunc writefunc, char *buf, size_t len) { | |
45 w->fd = stream; | |
46 w->write = writefunc; | |
37 w->buffer = buf; | 47 w->buffer = buf; |
38 w->size = len; | 48 w->size = len; |
39 w->pos = 0; | 49 w->pos = 0; |
40 w->error = 0; | 50 w->error = 0; |
41 } | 51 } |
47 | 57 |
48 size_t pos = 0; | 58 size_t pos = 0; |
49 size_t len = w->pos; | 59 size_t len = w->pos; |
50 | 60 |
51 while(len > 0) { | 61 while(len > 0) { |
52 ssize_t r = net_write(w->fd, w->buffer + pos, len); | 62 ssize_t r = w->write(w->fd, w->buffer + pos, len); |
53 if(r <= 0) { | 63 if(r <= 0) { |
54 break; | 64 break; |
55 } | 65 } |
56 len -= r; | 66 len -= r; |
57 pos += r; | 67 pos += r; |
103 } | 113 } |
104 } | 114 } |
105 w->buffer[w->pos++] = c; | 115 w->buffer[w->pos++] = c; |
106 return 0; | 116 return 0; |
107 } | 117 } |
118 | |
119 int writer_fwrite(const void *s, size_t size, size_t nelem, Writer *out) { | |
120 int w = writer_put(out, s, size*nelem); | |
121 return w/size; | |
122 } |