UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2018 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <string.h> 32 33 #include "writer.h" 34 35 void writer_init(Writer *w, SYS_NETFD fd, char *buf, size_t len) { 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; 47 w->buffer = buf; 48 w->size = len; 49 w->pos = 0; 50 w->error = 0; 51 } 52 53 int writer_flush(Writer *w) { 54 if(w->error) { 55 return w->error; 56 } 57 58 size_t pos = 0; 59 size_t len = w->pos; 60 61 while(len > 0) { 62 //fwrite(w->buffer+pos, 1, len, stdout); 63 //fflush(stdout); 64 ssize_t r = w->write(w->fd, w->buffer + pos, len); 65 if(r <= 0) { 66 break; 67 } 68 len -= r; 69 pos += r; 70 } 71 72 if(pos != w->pos) { 73 w->error = 1; 74 return 1; 75 } 76 77 w->pos = 0; 78 return 0; 79 } 80 81 int writer_put(Writer *w, const char *s, size_t len) { 82 if(w->error) { 83 return w->error; 84 } 85 86 // available bytes 87 size_t a = w->size - w->pos; 88 if(a == 0) { 89 if(writer_flush(w)) { 90 return 1; 91 } 92 } 93 94 size_t cplen = len > a ? a : len; // number of bytes we can copy 95 memcpy(w->buffer+w->pos, s, cplen); 96 w->pos += cplen; 97 98 if(cplen < len) { 99 // not all bytes copied -> call writer_put again 100 // the number of available bytes is 0 then, therefore flush is called 101 return writer_put(w, s + cplen, len - cplen); 102 } else { 103 return 0; 104 } 105 } 106 107 int writer_puts(Writer *w, cxstring s) { 108 return writer_put(w, s.ptr, s.length); 109 } 110 111 int writer_putc(Writer *w, char c) { 112 if(w->pos == w->size) { 113 if(writer_flush(w)) { 114 return 1; 115 } 116 } 117 w->buffer[w->pos++] = c; 118 return 0; 119 } 120 121 int writer_fwrite(const void *s, size_t size, size_t nelem, Writer *out) { 122 int w = writer_put(out, s, size*nelem); 123 return w/size; 124 } 125