Thu, 08 Sep 2016 15:12:56 +0200
updates documenting comments
universe@103 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@103 | 3 | * |
universe@225 | 4 | * Copyright 2016 Olaf Wintermann. All rights reserved. |
universe@103 | 5 | * |
universe@103 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@103 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@103 | 8 | * |
universe@103 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@103 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@103 | 11 | * |
universe@103 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@103 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@103 | 14 | * documentation and/or other materials provided with the distribution. |
universe@103 | 15 | * |
universe@103 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@103 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@103 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@103 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@103 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@103 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@103 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@103 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@103 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@103 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@103 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@103 | 27 | */ |
universe@103 | 28 | |
universe@60 | 29 | #include "buffer.h" |
universe@56 | 30 | #include <stdarg.h> |
universe@56 | 31 | #include <stdlib.h> |
universe@56 | 32 | #include <string.h> |
universe@56 | 33 | |
universe@169 | 34 | UcxBuffer *ucx_buffer_new(void *space, size_t capacity, int flags) { |
universe@60 | 35 | UcxBuffer *buffer = (UcxBuffer*) malloc(sizeof(UcxBuffer)); |
universe@60 | 36 | if (buffer) { |
universe@61 | 37 | buffer->flags = flags; |
universe@56 | 38 | if (!space) { |
universe@169 | 39 | buffer->space = (char*)malloc(capacity); |
universe@60 | 40 | if (!buffer->space) { |
universe@60 | 41 | free(buffer); |
universe@56 | 42 | return NULL; |
universe@56 | 43 | } |
universe@169 | 44 | memset(buffer->space, 0, capacity); |
universe@61 | 45 | buffer->flags |= UCX_BUFFER_AUTOFREE; |
universe@56 | 46 | } else { |
olaf@78 | 47 | buffer->space = (char*)space; |
universe@56 | 48 | } |
universe@169 | 49 | buffer->capacity = capacity; |
olaf@76 | 50 | buffer->size = 0; |
universe@56 | 51 | |
universe@60 | 52 | buffer->pos = 0; |
universe@56 | 53 | } |
universe@56 | 54 | |
universe@60 | 55 | return buffer; |
universe@56 | 56 | } |
universe@56 | 57 | |
universe@60 | 58 | void ucx_buffer_free(UcxBuffer *buffer) { |
universe@63 | 59 | if ((buffer->flags & UCX_BUFFER_AUTOFREE) == UCX_BUFFER_AUTOFREE) { |
universe@60 | 60 | free(buffer->space); |
universe@56 | 61 | } |
universe@60 | 62 | free(buffer); |
universe@56 | 63 | } |
universe@56 | 64 | |
olaf@76 | 65 | UcxBuffer* ucx_buffer_extract( |
olaf@76 | 66 | UcxBuffer *src, size_t start, size_t length, int flags) { |
universe@169 | 67 | |
universe@169 | 68 | if (src->size == 0 || length == 0 || start+length > src->capacity) { |
universe@62 | 69 | return NULL; |
universe@62 | 70 | } |
universe@62 | 71 | |
olaf@76 | 72 | UcxBuffer *dst = (UcxBuffer*) malloc(sizeof(UcxBuffer)); |
universe@62 | 73 | if (dst) { |
olaf@78 | 74 | dst->space = (char*)malloc(length); |
universe@62 | 75 | if (!dst->space) { |
universe@62 | 76 | free(dst); |
universe@62 | 77 | return NULL; |
universe@62 | 78 | } |
olaf@76 | 79 | dst->capacity = length; |
universe@62 | 80 | dst->size = length; |
universe@62 | 81 | dst->flags = flags | UCX_BUFFER_AUTOFREE; |
universe@62 | 82 | dst->pos = 0; |
olaf@76 | 83 | memcpy(dst->space, src->space+start, length); |
universe@62 | 84 | } |
universe@62 | 85 | return dst; |
universe@62 | 86 | } |
universe@62 | 87 | |
universe@60 | 88 | int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence) { |
universe@140 | 89 | size_t npos; |
universe@56 | 90 | switch (whence) { |
universe@56 | 91 | case SEEK_CUR: |
universe@60 | 92 | npos = buffer->pos; |
universe@56 | 93 | break; |
universe@56 | 94 | case SEEK_END: |
olaf@76 | 95 | npos = buffer->size; |
universe@56 | 96 | break; |
universe@169 | 97 | case SEEK_SET: |
universe@169 | 98 | npos = 0; |
universe@169 | 99 | break; |
universe@140 | 100 | default: |
universe@169 | 101 | return -1; |
universe@56 | 102 | } |
universe@56 | 103 | |
universe@169 | 104 | size_t opos = npos; |
universe@56 | 105 | npos += offset; |
olaf@76 | 106 | |
universe@169 | 107 | if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) { |
universe@169 | 108 | return -1; |
universe@169 | 109 | } |
universe@169 | 110 | |
universe@169 | 111 | if (npos >= buffer->size) { |
universe@56 | 112 | return -1; |
universe@56 | 113 | } else { |
universe@60 | 114 | buffer->pos = npos; |
universe@56 | 115 | return 0; |
universe@56 | 116 | } |
universe@56 | 117 | |
universe@56 | 118 | } |
universe@56 | 119 | |
universe@60 | 120 | int ucx_buffer_eof(UcxBuffer *buffer) { |
universe@62 | 121 | return buffer->pos >= buffer->size; |
universe@56 | 122 | } |
universe@56 | 123 | |
olaf@76 | 124 | int ucx_buffer_extend(UcxBuffer *buffer, size_t len) { |
olaf@76 | 125 | size_t newcap = buffer->capacity; |
universe@169 | 126 | |
universe@169 | 127 | if (buffer->capacity + len < buffer->capacity) { |
universe@169 | 128 | return -1; |
universe@169 | 129 | } |
universe@169 | 130 | |
universe@169 | 131 | while (buffer->capacity + len > newcap) { |
universe@169 | 132 | newcap <<= 1; |
universe@169 | 133 | if (newcap < buffer->capacity) { |
universe@169 | 134 | return -1; |
universe@169 | 135 | } |
universe@169 | 136 | } |
olaf@76 | 137 | |
olaf@78 | 138 | char *newspace = (char*)realloc(buffer->space, newcap); |
olaf@76 | 139 | if (newspace) { |
olaf@76 | 140 | memset(newspace+buffer->size, 0, newcap-buffer->size); |
olaf@76 | 141 | buffer->space = newspace; |
olaf@76 | 142 | buffer->capacity = newcap; |
olaf@76 | 143 | } else { |
olaf@76 | 144 | return -1; |
olaf@76 | 145 | } |
olaf@76 | 146 | |
olaf@76 | 147 | return 0; |
olaf@76 | 148 | } |
olaf@76 | 149 | |
olaf@76 | 150 | size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, |
olaf@76 | 151 | UcxBuffer *buffer) { |
olaf@76 | 152 | size_t len = size * nitems; |
universe@169 | 153 | size_t required = buffer->pos + len; |
universe@169 | 154 | if (buffer->pos > required) { |
universe@169 | 155 | return 0; |
universe@169 | 156 | } |
universe@169 | 157 | |
universe@169 | 158 | if (required > buffer->capacity) { |
olaf@76 | 159 | if ((buffer->flags & UCX_BUFFER_AUTOEXTEND) == UCX_BUFFER_AUTOEXTEND) { |
universe@169 | 160 | if (ucx_buffer_extend(buffer, required - buffer->capacity)) { |
universe@169 | 161 | return 0; |
universe@64 | 162 | } |
universe@64 | 163 | } else { |
olaf@76 | 164 | len = buffer->capacity - buffer->pos; |
universe@169 | 165 | if (size > 1) { |
universe@169 | 166 | len -= len%size; |
universe@169 | 167 | } |
universe@64 | 168 | } |
universe@56 | 169 | } |
olaf@76 | 170 | |
universe@169 | 171 | if (len == 0) { |
universe@64 | 172 | return len; |
universe@56 | 173 | } |
olaf@76 | 174 | |
olaf@76 | 175 | memcpy(buffer->space + buffer->pos, ptr, len); |
olaf@76 | 176 | buffer->pos += len; |
olaf@76 | 177 | if(buffer->pos > buffer->size) { |
olaf@76 | 178 | buffer->size = buffer->pos; |
olaf@76 | 179 | } |
olaf@76 | 180 | |
olaf@76 | 181 | return len / size; |
olaf@76 | 182 | } |
universe@56 | 183 | |
olaf@76 | 184 | size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems, |
olaf@76 | 185 | UcxBuffer *buffer) { |
olaf@76 | 186 | size_t len = size * nitems; |
olaf@76 | 187 | if (buffer->pos + len > buffer->size) { |
olaf@76 | 188 | len = buffer->size - buffer->pos; |
olaf@76 | 189 | if (size > 1) len -= len%size; |
universe@56 | 190 | } |
olaf@76 | 191 | |
olaf@76 | 192 | if (len <= 0) { |
olaf@76 | 193 | return len; |
olaf@76 | 194 | } |
olaf@76 | 195 | |
olaf@76 | 196 | memcpy(ptr, buffer->space + buffer->pos, len); |
olaf@76 | 197 | buffer->pos += len; |
olaf@76 | 198 | |
olaf@76 | 199 | return len / size; |
universe@56 | 200 | } |
universe@56 | 201 | |
universe@60 | 202 | int ucx_buffer_putc(UcxBuffer *buffer, int c) { |
olaf@76 | 203 | if(buffer->pos >= buffer->capacity) { |
olaf@76 | 204 | if ((buffer->flags & UCX_BUFFER_AUTOEXTEND) == UCX_BUFFER_AUTOEXTEND) { |
olaf@76 | 205 | if(ucx_buffer_extend(buffer, 1)) { |
olaf@76 | 206 | return EOF; |
olaf@76 | 207 | } |
olaf@76 | 208 | } else { |
olaf@76 | 209 | return EOF; |
olaf@76 | 210 | } |
universe@56 | 211 | } |
olaf@76 | 212 | |
olaf@76 | 213 | c &= 0xFF; |
olaf@76 | 214 | buffer->space[buffer->pos] = (char) c; |
olaf@76 | 215 | buffer->pos++; |
olaf@76 | 216 | if(buffer->pos > buffer->size) { |
olaf@76 | 217 | buffer->size = buffer->pos; |
olaf@76 | 218 | } |
olaf@76 | 219 | return c; |
universe@56 | 220 | } |
universe@56 | 221 | |
universe@60 | 222 | int ucx_buffer_getc(UcxBuffer *buffer) { |
universe@60 | 223 | if (ucx_buffer_eof(buffer)) { |
universe@56 | 224 | return EOF; |
universe@56 | 225 | } else { |
olaf@76 | 226 | int c = buffer->space[buffer->pos]; |
universe@60 | 227 | buffer->pos++; |
universe@56 | 228 | return c; |
universe@56 | 229 | } |
universe@56 | 230 | } |
olaf@76 | 231 | |
universe@140 | 232 | size_t ucx_buffer_puts(UcxBuffer *buffer, char *str) { |
universe@140 | 233 | return ucx_buffer_write((const void*)str, 1, strlen(str), buffer); |
olaf@76 | 234 | } |