Sun, 03 Nov 2013 16:41:42 +0100
added error directive for custom error pages
--- a/src/server/admin/admin.c Mon Oct 14 13:36:28 2013 +0200 +++ b/src/server/admin/admin.c Sun Nov 03 16:41:42 2013 +0100 @@ -104,7 +104,10 @@ // service admin page if(page) { - page->service(page, NULL); + AdminRequest arq; + arq.sn = sn; + arq.rq = rq; + page->service(page, &arq); } else { net_printf(sn->csd, "page not found\n"); }
--- a/src/server/config/conf.c Mon Oct 14 13:36:28 2013 +0200 +++ b/src/server/config/conf.c Sun Nov 03 16:41:42 2013 +0100 @@ -379,21 +379,25 @@ */ int cfg_get_directive_type_num(sstr_t type) { /* get nsapi function type */ + + // TODO: replace hard coded numbers int dt = -1; if(sstrcmp(type, sstr("AuthTrans")) == 0) { - dt = 0; + dt = NSAPIAuthTrans; } else if(sstrcmp(type, sstr("NameTrans")) == 0) { - dt = 1; + dt = NSAPINameTrans; } else if(sstrcmp(type, sstr("PathCheck")) == 0) { - dt = 2; + dt = NSAPIPathCheck; } else if(sstrcmp(type, sstr("ObjectType")) == 0) { - dt = 3; + dt = NSAPIObjectType; } else if(sstrcmp(type, sstr("Service")) == 0) { - dt = 4; + dt = NSAPIService; + } else if(sstrcmp(type, sstr("Error")) == 0) { + dt = NSAPIError; } else if(sstrcmp(type, sstr("AddLog")) == 0) { - dt = 5; + dt = NSAPIAddLog; } else if(sstrcmp(type, sstr("Init")) == 0) { - dt = 6; + dt = INIT_DIRECTIVE; } return dt; }
--- a/src/server/config/conf.h Mon Oct 14 13:36:28 2013 +0200 +++ b/src/server/config/conf.h Sun Nov 03 16:41:42 2013 +0100 @@ -37,6 +37,8 @@ #include <ucx/mempool.h> #include <ucx/string.h> +#include "../util/object.h" + #ifdef __cplusplus extern "C" { #endif @@ -61,6 +63,9 @@ #define TAG_ELSE 3 #define TAG_CLIENT 4 + +#define INIT_DIRECTIVE 16 + typedef struct _cfg_line { sstr_t line; // raw line string void *object; // pointer to data struct
--- a/src/server/config/initconf.c Mon Oct 14 13:36:28 2013 +0200 +++ b/src/server/config/initconf.c Sun Nov 03 16:41:42 2013 +0100 @@ -69,7 +69,7 @@ } d->begin = begin; d->end = end; - if(d->type_num == 6) { + if(d->type_num == INIT_DIRECTIVE) { conf->directives = ucx_list_append(conf->directives, d); } else { fprintf(stderr, "Warning: Non Init directive in init.conf\n");
--- a/src/server/config/objconf.h Mon Oct 14 13:36:28 2013 +0200 +++ b/src/server/config/objconf.h Sun Nov 03 16:41:42 2013 +0100 @@ -42,7 +42,7 @@ sstr_t name; sstr_t ppath; // directives - UcxList *directives[6]; + UcxList *directives[7]; } ConfigObject; /*
--- a/src/server/daemon/config.c Mon Oct 14 13:36:28 2013 +0200 +++ b/src/server/daemon/config.c Sun Nov 03 16:41:42 2013 +0100 @@ -734,7 +734,7 @@ conf->objects[i] = obj; // add directives - for(int i=0;i<6;i++) { + for(int i=0;i<NUM_NSAPI_TYPES-1;i++) { UcxList *dirs = cob->directives[i]; while(dirs != NULL) { ConfigDirective *cfgdir = dirs->data;
--- a/src/server/daemon/httprequest.c Mon Oct 14 13:36:28 2013 +0200 +++ b/src/server/daemon/httprequest.c Sun Nov 03 16:41:42 2013 +0100 @@ -319,7 +319,6 @@ int nsapi_handle_request(NSAPISession *sn, NSAPIRequest *rq) { int r = REQ_NOACTION; - do { switch(rq->phase) { case NSAPIAuthTrans: { @@ -363,29 +362,50 @@ if(r != REQ_PROCEED) { break; } - rq->phase++; + rq->phase = NSAPIAddLog; // skip NSAPIError nsapi_context_next_stage(&rq->context); } case NSAPIAddLog: { //printf(">>> AddLog\n"); r = nsapi_addlog(sn, rq); - if(r != REQ_PROCESSING && r != REQ_ABORTED) { + if(r == REQ_PROCESSING) { break; } - rq->phase++; - nsapi_context_next_stage(&rq->context); + // finish request + r = REQ_PROCEED; + break; } default: // should not happen, but when it does, finish the req case REQ_FINISH: { //printf(">>> Finish\n"); - r = nsapi_finish_request(sn, rq); + //r = nsapi_finish_request(sn, rq); + r = REQ_PROCEED; + break; + } + case NSAPIError: { + //printf(">>> Error\n"); + r = nsapi_error(sn, rq); + if(r == REQ_PROCEED) { + // restart the loop to switch to AddLog directive + r = REQ_RESTART; + rq->phase = NSAPIAddLog; + nsapi_context_next_stage(&rq->context); + } else { + /* + * an error occured while handling an error + * leave loop to finish the request (without AddLog) + */ + r = REQ_PROCEED; + break; + } } } if(r == REQ_ABORTED) { - // if an error occurred, we send an error page - nsapi_error_request((Session*)sn, (Request*)rq); - break; + // switch to NSAPIError + rq->phase = NSAPIError; + nsapi_context_next_stage(&rq->context); + r = REQ_RESTART; } } while (r == REQ_RESTART); @@ -706,6 +726,69 @@ return ret; } +int nsapi_error(NSAPISession *sn, NSAPIRequest *rq) { + //printf("nsapi_error\n"); + httpd_objset *objset = rq->rq.os; + + if(NCX_OI(rq) == -1) { + NCX_OI(rq) = objset->pos - 1; + } + + int ret = rq->context.last_req_code; + for(int i=NCX_OI(rq);i>=0;i--) { + httpd_object *obj = objset->obj[i]; + dtable *dt = object_get_dtable(obj, NSAPIError); + + // execute directives + for(int j=NCX_DI(rq);j<dt->ndir;j++) { + if(ret == REQ_NOACTION) { + directive *d = dt->dirs[j]; + + // check status code parameter + char *status = pblock_findkeyval(pb_key_type, d->param); + if(status) { + int statuscode = atoi(status); + if(statuscode != rq->rq.status_num) { + continue; + } + } + + // execute the saf + ret = nsapi_exec(d, sn, rq); + } + + if(ret == REQ_ABORTED) { + // if an error directive fails, we use the default + // error handler + break; + } + if(ret != REQ_NOACTION) { + if(ret == REQ_PROCEED) { + /* + * flush buffer and add termination if chunked encoding + * is enabled + */ + net_finish(sn->sn.csd); + } else if(ret == REQ_PROCESSING) { + // save nsapi context + rq->context.objset_index = i; + + // add +1 to start next round with next function + rq->context.dtable_index = j + 1; + } + return ret; + } + } + } + + if(ret != REQ_PROCEED) { + // default error handler + nsapi_error_request((Session*)sn, (Request*)rq); + } + + return ret; +} + int nsapi_addlog(NSAPISession *sn, NSAPIRequest *rq) { //printf("nsapi_addlog\n"); httpd_objset *objset = rq->rq.os;
--- a/src/server/daemon/httprequest.h Mon Oct 14 13:36:28 2013 +0200 +++ b/src/server/daemon/httprequest.h Sun Nov 03 16:41:42 2013 +0100 @@ -91,6 +91,7 @@ int nsapi_pathcheck(NSAPISession *sn, NSAPIRequest *rq); int nsapi_objecttype(NSAPISession *sn, NSAPIRequest *rq); int nsapi_service(NSAPISession *sn, NSAPIRequest *rq); +int nsapi_error(NSAPISession *sn, NSAPIRequest *rq); int nsapi_addlog(NSAPISession *sn, NSAPIRequest *rq); int nsapi_exec(directive *d, NSAPISession *sn, NSAPIRequest *rq);
--- a/src/server/ucx/Makefile Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,31 +0,0 @@ -# -# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. -# -# Copyright 2011 Olaf Wintermann. All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: -# -# 1. Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# -# 2. Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the distribution. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE -# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -# POSSIBILITY OF SUCH DAMAGE. -# - -$(UCX_OBJPRE)%.o: ucx/%.c - $(CC) -o $@ -c $(CFLAGS) $< -
--- a/src/server/ucx/allocator.c Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,59 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <stdlib.h> -#include "allocator.h" - -UcxAllocator default_allocator = { - NULL, - ucx_default_malloc, - ucx_default_calloc, - ucx_default_realloc, - ucx_default_free -}; - -UcxAllocator *ucx_default_allocator() { - UcxAllocator *allocator = &default_allocator; - return allocator; -} - -void *ucx_default_malloc(void *ignore, size_t n) { - return malloc(n); -} - -void *ucx_default_calloc(void *ignore, size_t n, size_t size) { - return calloc(n, size); -} - -void *ucx_default_realloc(void *ignore, void *data, size_t n) { - return realloc(data, n); -} - -void ucx_default_free(void *ignore, void *data) { - free(data); -}
--- a/src/server/ucx/allocator.h Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,67 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef ALLOCATOR_H -#define ALLOCATOR_H - -#include "ucx.h" - -#ifdef __cplusplus -extern "C" { -#endif - -typedef void*(*ucx_allocator_malloc)(void *pool, size_t n); -typedef void*(*ucx_allocator_calloc)(void *pool, size_t n, size_t size); -typedef void*(*ucx_allocator_realloc)(void *pool, void *data, size_t n); -typedef void(*ucx_allocator_free)(void *pool, void *data); - -typedef struct { - void *pool; - ucx_allocator_malloc malloc; - ucx_allocator_calloc calloc; - ucx_allocator_realloc realloc; - ucx_allocator_free free; -} UcxAllocator; - -UcxAllocator *ucx_default_allocator(); - -void *ucx_default_malloc(void *ignore, size_t n); -void *ucx_default_calloc(void *ignore, size_t n, size_t size); -void *ucx_default_realloc(void *ignore, void *data, size_t n); -void ucx_default_free(void *ignore, void *data); - -#define UCX_ALLOCATOR_DEFAULT {NULL, \ - ucx_default_malloc, ucx_default_calloc, ucx_default_realloc, \ - ucx_default_free } - -#ifdef __cplusplus -} -#endif - -#endif /* ALLOCATOR_H */ -
--- a/src/server/ucx/buffer.c Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,260 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "buffer.h" -#include <stdarg.h> -#include <stdlib.h> -#include <string.h> - -UcxBuffer *ucx_buffer_new(void *space, size_t size, int flags) { - UcxBuffer *buffer = (UcxBuffer*) malloc(sizeof(UcxBuffer)); - if (buffer) { - buffer->flags = flags; - if (!space) { - buffer->space = (char*)malloc(size); - if (!buffer->space) { - free(buffer); - return NULL; - } - memset(buffer->space, 0, size); - buffer->flags |= UCX_BUFFER_AUTOFREE; - } else { - buffer->space = (char*)space; - } - buffer->capacity = size; - buffer->size = 0; - - buffer->pos = 0; - } - - return buffer; -} - -void ucx_buffer_free(UcxBuffer *buffer) { - if ((buffer->flags & UCX_BUFFER_AUTOFREE) == UCX_BUFFER_AUTOFREE) { - free(buffer->space); - } - free(buffer); -} - -UcxBuffer* ucx_buffer_extract( - UcxBuffer *src, size_t start, size_t length, int flags) { - if(src->size == 0) { - return NULL; - } - if (length == 0) { - length = src->size - start; - } - if (start+length > src->size) { - return NULL; - } - - UcxBuffer *dst = (UcxBuffer*) malloc(sizeof(UcxBuffer)); - if (dst) { - dst->space = (char*)malloc(length); - if (!dst->space) { - free(dst); - return NULL; - } - dst->capacity = length; - dst->size = length; - dst->flags = flags | UCX_BUFFER_AUTOFREE; - dst->pos = 0; - memcpy(dst->space, src->space+start, length); - } - return dst; -} - -int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence) { - size_t npos = 0; - switch (whence) { - case SEEK_SET: - npos = 0; - break; - case SEEK_CUR: - npos = buffer->pos; - break; - case SEEK_END: - npos = buffer->size; - break; - } - - npos += offset; - - if (npos > buffer->size) { - return -1; - } else { - buffer->pos = npos; - return 0; - } - -} - -int ucx_buffer_eof(UcxBuffer *buffer) { - return buffer->pos >= buffer->size; -} - -int ucx_buffer_extend(UcxBuffer *buffer, size_t len) { - size_t newcap = buffer->capacity; - while (buffer->pos + len > newcap) newcap <<= 1; - - char *newspace = (char*)realloc(buffer->space, newcap); - if (newspace) { - memset(newspace+buffer->size, 0, newcap-buffer->size); - buffer->space = newspace; - buffer->capacity = newcap; - } else { - return -1; - } - - return 0; -} - -size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, - UcxBuffer *buffer) { - size_t len = size * nitems; - if (buffer->pos + len > buffer->capacity) { - if ((buffer->flags & UCX_BUFFER_AUTOEXTEND) == UCX_BUFFER_AUTOEXTEND) { - if(ucx_buffer_extend(buffer, len)) { - return -1; - } - } else { - len = buffer->capacity - buffer->pos; - if (size > 1) len -= len%size; - } - } - - if (len <= 0) { - return len; - } - - memcpy(buffer->space + buffer->pos, ptr, len); - buffer->pos += len; - if(buffer->pos > buffer->size) { - buffer->size = buffer->pos; - } - - return len / size; -} - -size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems, - UcxBuffer *buffer) { - size_t len = size * nitems; - if (buffer->pos + len > buffer->size) { - len = buffer->size - buffer->pos; - if (size > 1) len -= len%size; - } - - if (len <= 0) { - return len; - } - - memcpy(ptr, buffer->space + buffer->pos, len); - buffer->pos += len; - - return len / size; -} - -int ucx_buffer_putc(UcxBuffer *buffer, int c) { - if(buffer->pos >= buffer->capacity) { - if ((buffer->flags & UCX_BUFFER_AUTOEXTEND) == UCX_BUFFER_AUTOEXTEND) { - if(ucx_buffer_extend(buffer, 1)) { - return EOF; - } - } else { - return EOF; - } - } - - c &= 0xFF; - buffer->space[buffer->pos] = (char) c; - buffer->pos++; - if(buffer->pos > buffer->size) { - buffer->size = buffer->pos; - } - return c; -} - -int ucx_buffer_getc(UcxBuffer *buffer) { - if (ucx_buffer_eof(buffer)) { - return EOF; - } else { - int c = buffer->space[buffer->pos]; - buffer->pos++; - return c; - } -} - -size_t ucx_buffer_generic_copy(void *s1, void *s2, - read_func readfnc, write_func writefnc, size_t bufsize) { - size_t ncp = 0; - char *buf = (char*)malloc(bufsize); - if(buf == NULL) { - return 0; - } - - size_t r; - while((r = readfnc(buf, 1, bufsize, s1)) != 0) { - r = writefnc(buf, 1, r, s2); - ncp += r; - if(r == 0) { - break; - } - } - - free(buf); - return ncp; -} - -size_t ucx_buffer_generic_ncopy(void *s1, void *s2, - read_func readfnc, write_func writefnc, size_t bufsize, size_t n) { - if(n == 0) { - return 0; - } - - size_t ncp = 0; - char *buf = (char*)malloc(bufsize); - if(buf == NULL) { - return 0; - } - - size_t r; - size_t rn = bufsize > n ? n : bufsize; - while((r = readfnc(buf, 1, rn, s1)) != 0) { - r = writefnc(buf, 1, r, s2); - ncp += r; - n -= r; - rn = bufsize > n ? n : bufsize; - if(r == 0 || n == 0) { - break; - } - } - - free(buf); - return ncp; -}
--- a/src/server/ucx/buffer.h Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,134 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef BUFFER_H -#define BUFFER_H - -#include "ucx.h" -#include <sys/types.h> -#include <stdio.h> - -#ifdef __cplusplus -extern "C" { -#endif - -/* no autoextend or autofree behaviour */ -#define UCX_BUFFER_DEFAULT 0x00 -/* the buffer shall free the occupied memory space */ -#define UCX_BUFFER_AUTOFREE 0x01 -/* the buffer may automatically double its size on write operations */ -#define UCX_BUFFER_AUTOEXTEND 0x02 - -/* the user shall not modify values, but may get the latest pointer */ -typedef struct { - char *space; - size_t pos; - size_t capacity; - size_t size; - int flags; -} UcxBuffer; - -/* if space is NULL, new space is allocated and the autofree flag is enforced */ -UcxBuffer *ucx_buffer_new(void *space, size_t size, int flags); -void ucx_buffer_free(UcxBuffer* buffer); - -/* - * the autofree flag is enforced for the new buffer - * if length is zero, the whole remaining buffer shall be extracted - * the position of the new buffer is set to zero - */ -UcxBuffer* ucx_buffer_extract(UcxBuffer *src, - size_t start, size_t length, int flags); -#define ucx_buffer_clone(src,flags) \ - ucx_buffer_extract(src, 0, 0, flags) - -/* - * Moves the position of the buffer to a new position relative to whence. - * - * SEEK_SET marks the start of the buffer - * SEEK_CUR marks the current position - * SEEK_END marks the first 0-byte in the buffer - * - * ucx_buffer_seek returns 0 on success and -1 if the new position is beyond the - * bounds of the allocated buffer. In that case the position of the buffer - * remains unchanged. - * - */ -int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence); - -#define ucx_buffer_clear(buffer) memset(buffer->space, 0, buffer->size); \ - buffer->size = 0; buffer->pos = 0; - -/* - * returns non-zero, if the current buffer position has exceeded the last - * available byte of the underlying buffer - * - */ -int ucx_buffer_eof(UcxBuffer *buffer); - - -int ucx_buffere_extend(UcxBuffer *buffer, size_t len); - -size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, - UcxBuffer *buffer); - -size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems, - UcxBuffer *buffer); - -int ucx_buffer_putc(UcxBuffer *b, int c); -int ucx_buffer_getc(UcxBuffer *b); - - -/* - * copies all bytes from s1 to s2 - * uses the read function r to read from s1 und writes the data using the - * write function w to s2 - * returns the number of bytes copied - */ -size_t ucx_buffer_generic_copy(void *s1, void *s2, read_func r, write_func w, - size_t bufsize); - -size_t ucx_buffer_generic_ncopy(void *s1, void *s2, read_func r, write_func w, - size_t bufsize, size_t n); - -#define UCX_DEFAULT_BUFFER_SIZE 0x1000 - -#define ucx_buffer_copy(s1,s2,r,w) \ - ucx_buffer_generic_copy(s1, s2, (read_func)r, (write_func)w, \ - UCX_DEFAULT_BUFFER_SIZE) - -#define ucx_buffer_ncopy(s1,s2,r,w, n) \ - ucx_buffer_generic_ncopy(s1, s2, (read_func)r, (write_func)w, \ - UCX_DEFAULT_BUFFER_SIZE, n) - -#ifdef __cplusplus -} -#endif - -#endif /* BUFFER_H */ -
--- a/src/server/ucx/dlist.c Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,270 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "dlist.h" - -UcxDlist *ucx_dlist_clone(UcxDlist *l, copy_func fnc, void *data) { - UcxDlist *ret = NULL; - while (l != NULL) { - if (fnc != NULL) { - ret = ucx_dlist_append(ret, fnc(l->data, data)); - } else { - ret = ucx_dlist_append(ret, l->data); - } - l = l->next; - } - return ret; -} - -int ucx_dlist_equals(const UcxDlist *l1, const UcxDlist *l2, - cmp_func fnc, void* data) { - if (l1 == l2) return 1; - - while (l1 != NULL && l2 != NULL) { - if (fnc == NULL) { - if (l1->data != l2->data) return 0; - } else { - if (fnc(l1->data, l2->data, data) != 0) return 0; - } - l1 = l1->next; - l2 = l2->next; - } - - return (l1 == NULL && l2 == NULL); -} - -void ucx_dlist_free(UcxDlist *l) { - UcxDlist *e = l, *f; - while (e != NULL) { - f = e; - e = e->next; - free(f); - } -} - -UcxDlist *ucx_dlist_append(UcxDlist *l, void *data) { - UcxDlist *nl = (UcxDlist*) malloc(sizeof(UcxDlist)); - if (nl == NULL) return NULL; - - nl->data = data; - nl->next = NULL; - if (l == NULL) { - nl->prev = NULL; - return nl; - } else { - UcxDlist *t = ucx_dlist_last(l); - t->next = nl; - nl->prev = t; - return l; - } -} - -UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data) { - UcxDlist *nl = ucx_dlist_append(NULL, data); - if (nl == NULL) return NULL; - - if (l != NULL) { - nl->next = l; - l->prev = nl; - } - return nl; -} - -UcxDlist *ucx_dlist_concat(UcxDlist *l1, UcxDlist *l2) { - if (l1 == NULL) { - return l2; - } else { - UcxDlist *last = ucx_dlist_last(l1); - last->next = l2; - l2->prev = last; - return l1; - } -} - -UcxDlist *ucx_dlist_last(const UcxDlist *l) { - if (l == NULL) return NULL; - - const UcxDlist *e = l; - while (e->next != NULL) { - e = e->next; - } - return (UcxDlist*)e; -} - -UcxDlist *ucx_dlist_get(const UcxDlist *l, int index) { - if (l == NULL) return NULL; - - const UcxDlist *e = l; - while (e->next != NULL && index > 0) { - e = e->next; - index--; - } - - return (UcxDlist*)(index == 0 ? e : NULL); -} - -int ucx_dlist_contains(UcxDlist *l, void *elem, cmp_func fnc, void *cmpdata) { - UCX_FOREACH(UcxDlist*, l, e) { - if (!fnc(elem, e->data, cmpdata)) { - return 1; - } - } - return 0; -} - -size_t ucx_dlist_size(const UcxDlist *l) { - if (l == NULL) return 0; - - const UcxDlist *e = l; - size_t s = 1; - while (e->next != NULL) { - e = e->next; - s++; - } - - return s; -} - -UcxDlist *ucx_dlist_sort_merge(int length, - UcxDlist* restrict ls, UcxDlist* restrict le, UcxDlist* restrict re, - cmp_func fnc, void* data) { - - UcxDlist** sorted = (UcxDlist**) malloc(sizeof(UcxDlist*)*length); - UcxDlist *rc, *lc; - - lc = ls; rc = le; - int n = 0; - while (lc && lc != le && rc != re) { - if (fnc(lc->data, rc->data, data) <= 0) { - sorted[n] = lc; - lc = lc->next; - } else { - sorted[n] = rc; - rc = rc->next; - } - n++; - } - while (lc && lc != le) { - sorted[n] = lc; - lc = lc->next; - n++; - } - while (rc && rc != re) { - sorted[n] = rc; - rc = rc->next; - n++; - } - - // Update pointer - sorted[0]->prev = NULL; - for (int i = 0 ; i < length-1 ; i++) { - sorted[i]->next = sorted[i+1]; - sorted[i+1]->prev = sorted[i]; - } - sorted[length-1]->next = NULL; - - UcxDlist *ret = sorted[0]; - free(sorted); - return ret; -} - -UcxDlist *ucx_dlist_sort(UcxDlist *l, cmp_func fnc, void *data) { - if (l == NULL) { - return NULL; - } - - UcxDlist *lc; - int ln = 1; - - UcxDlist *restrict ls = l, *restrict le, *restrict re; - lc = ls; - while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) { - lc = lc->next; - ln++; - } - le = lc->next; - - if (le == NULL) { - return l; // this list is already sorted :) - } else { - UcxDlist *rc; - int rn = 1; - rc = le; - while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) { - rc = rc->next; - rn++; - } - re = rc->next; - - // Something left? Sort it! - UcxDlist *remainder = re; - size_t remainder_length = ucx_dlist_size(remainder); - if (remainder != NULL) { - remainder = ucx_dlist_sort(remainder, fnc, data); - } - - // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them - UcxDlist *sorted = ucx_dlist_sort_merge(ln+rn, - ls, le, re, - fnc, data); - - // merge sorted list with (also sorted) remainder - l = ucx_dlist_sort_merge(ln+rn+remainder_length, - sorted, remainder, NULL, fnc, data); - - return l; - } -} - -/* dlist specific functions */ -UcxDlist *ucx_dlist_first(const UcxDlist *l) { - if (l == NULL) return NULL; - - const UcxDlist *e = l; - while (e->prev != NULL) { - e = e->prev; - } - return (UcxDlist *)e; -} - -UcxDlist *ucx_dlist_remove(UcxDlist *l, UcxDlist *e) { - if (e->prev == NULL) { - if(e->next != NULL) { - e->next->prev = NULL; - l = e->next; - } else { - l = NULL; - } - - } else { - e->prev->next = e->next; - e->next->prev = e->prev; - } - free(e); - return l; -}
--- a/src/server/ucx/dlist.h Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,70 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef DLIST_H -#define DLIST_H - -#include "ucx.h" -#include <stddef.h> - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct UcxDlist UcxDlist; -struct UcxDlist { - void *data; - UcxDlist *next; - UcxDlist *prev; -}; - -UcxDlist *ucx_dlist_clone(UcxDlist *l, copy_func fnc, void* data); -int ucx_dlist_equals(const UcxDlist *l1, const UcxDlist *l2, - cmp_func fnc, void* data); - -void ucx_dlist_free(UcxDlist *l); -UcxDlist *ucx_dlist_append(UcxDlist *l, void *data); -UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data); -UcxDlist *ucx_dlist_concat(UcxDlist *l1, UcxDlist *l2); -UcxDlist *ucx_dlist_last(const UcxDlist *l); -UcxDlist *ucx_dlist_get(const UcxDlist *l, int index); -size_t ucx_dlist_size(const UcxDlist *l); -int ucx_dlist_contains(UcxDlist *l, void *elem, cmp_func fnc, void *cmpdata); - -UcxDlist *ucx_dlist_sort(UcxDlist *l, cmp_func fnc, void *data); - -/* dlist specific functions */ -UcxDlist *ucx_dlist_first(const UcxDlist *l); -UcxDlist *ucx_dlist_remove(UcxDlist *l, UcxDlist *e); - -#ifdef __cplusplus -} -#endif - -#endif /* DLIST_H */ -
--- a/src/server/ucx/list.c Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,255 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "list.h" - -UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void *data) { - UcxList *ret = NULL; - while (l != NULL) { - if (fnc != NULL) { - ret = ucx_list_append(ret, fnc(l->data, data)); - } else { - ret = ucx_list_append(ret, l->data); - } - l = l->next; - } - return ret; -} - -int ucx_list_equals(const UcxList *l1, const UcxList *l2, - cmp_func fnc, void* data) { - if (l1 == l2) return 1; - - while (l1 != NULL && l2 != NULL) { - if (fnc == NULL) { - if (l1->data != l2->data) return 0; - } else { - if (fnc(l1->data, l2->data, data) != 0) return 0; - } - l1 = l1->next; - l2 = l2->next; - } - - return (l1 == NULL && l2 == NULL); -} - -void ucx_list_free(UcxList *l) { - UcxList *e = l, *f; - while (e != NULL) { - f = e; - e = e->next; - free(f); - } -} - -UcxList *ucx_list_append(UcxList *l, void *data) { - UcxList *nl = (UcxList*) malloc(sizeof(UcxList)); - if (nl == NULL) return NULL; - - nl->data = data; - nl->next = NULL; - if (l == NULL) { - return nl; - } else { - UcxList *t = ucx_list_last(l); - t->next = nl; - return l; - } -} - -UcxList *ucx_list_prepend(UcxList *l, void *data) { - UcxList *nl = ucx_list_append(NULL, data); - if (nl == NULL) return NULL; - - if (l != NULL) { - nl->next = l; - } - return nl; -} - -UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) { - if (l1 == NULL) { - return l2; - } else { - UcxList *last = ucx_list_last(l1); - last->next = l2; - return l1; - } -} - -UcxList *ucx_list_last(const UcxList *l) { - if (l == NULL) return NULL; - - const UcxList *e = l; - while (e->next != NULL) { - e = e->next; - } - return (UcxList*)e; -} - -UcxList *ucx_list_get(const UcxList *l, int index) { - if (l == NULL) return NULL; - - const UcxList *e = l; - while (e->next != NULL && index > 0) { - e = e->next; - index--; - } - - return (UcxList*)(index == 0 ? e : NULL); -} - -int ucx_list_contains(UcxList *l, void *elem, cmp_func fnc, void *cmpdata) { - UCX_FOREACH(UcxList*, l, e) { - if (!fnc(elem, e->data, cmpdata)) { - return 1; - } - } - return 0; -} - -size_t ucx_list_size(const UcxList *l) { - if (l == NULL) return 0; - - const UcxList *e = l; - size_t s = 1; - while (e->next != NULL) { - e = e->next; - s++; - } - - return s; -} - -UcxList *ucx_list_sort_merge(int length, - UcxList* restrict ls, UcxList* restrict le, UcxList* restrict re, - cmp_func fnc, void* data) { - - UcxList** sorted = (UcxList**) malloc(sizeof(UcxList*)*length); - UcxList *rc, *lc; - - lc = ls; rc = le; - int n = 0; - while (lc && lc != le && rc != re) { - if (fnc(lc->data, rc->data, data) <= 0) { - sorted[n] = lc; - lc = lc->next; - } else { - sorted[n] = rc; - rc = rc->next; - } - n++; - } - while (lc && lc != le) { - sorted[n] = lc; - lc = lc->next; - n++; - } - while (rc && rc != re) { - sorted[n] = rc; - rc = rc->next; - n++; - } - - // Update pointer - for (int i = 0 ; i < length-1 ; i++) { - sorted[i]->next = sorted[i+1]; - } - sorted[length-1]->next = NULL; - - UcxList *ret = sorted[0]; - free(sorted); - return ret; -} - -UcxList *ucx_list_sort(UcxList *l, cmp_func fnc, void *data) { - if (l == NULL) { - return NULL; - } - - UcxList *lc; - int ln = 1; - - UcxList *restrict ls = l, *restrict le, *restrict re; - lc = ls; - while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) { - lc = lc->next; - ln++; - } - le = lc->next; - - if (le == NULL) { - return l; // this list is already sorted :) - } else { - UcxList *rc; - int rn = 1; - rc = le; - while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) { - rc = rc->next; - rn++; - } - re = rc->next; - - // Something left? Sort it! - UcxList *remainder = re; - size_t remainder_length = ucx_list_size(remainder); - if (remainder != NULL) { - remainder = ucx_list_sort(remainder, fnc, data); - } - - // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them - UcxList *sorted = ucx_list_sort_merge(ln+rn, - ls, le, re, - fnc, data); - - // merge sorted list with (also sorted) remainder - l = ucx_list_sort_merge(ln+rn+remainder_length, - sorted, remainder, NULL, fnc, data); - - return l; - } -} - -/* list specific functions */ -UcxList *ucx_list_remove(UcxList *l, UcxList *e) { - if (e == l) { - l = e->next; - free(e); - } else { - UcxList *f = l; - while (f->next != NULL && f->next != e) { - f = f->next; - } - /* perform remove if this element is found in this list */ - if (f->next == e) { - f->next = e->next; - free(e); - } - } - return l; -}
--- a/src/server/ucx/list.h Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,68 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef LIST_H -#define LIST_H - -#include "ucx.h" -#include <stddef.h> - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct UcxList UcxList; -struct UcxList { - void *data; - UcxList *next; -}; - -UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void *data); -int ucx_list_equals(const UcxList *l1, const UcxList *l2, - cmp_func fnc, void *data); - -void ucx_list_free(UcxList *l); -UcxList *ucx_list_append(UcxList *l, void *data); -UcxList *ucx_list_prepend(UcxList *l, void *data); -UcxList *ucx_list_concat(UcxList *l1, UcxList *l2); -UcxList *ucx_list_last(const UcxList *l); -UcxList *ucx_list_get(const UcxList *l, int index); -size_t ucx_list_size(const UcxList *l); -int ucx_list_contains(UcxList *l, void *elem, cmp_func fnc, void *cmpdata); - -UcxList *ucx_list_sort(UcxList *l, cmp_func fnc, void *data); - -/* list specific functions */ -UcxList *ucx_list_remove(UcxList *l, UcxList *e); - -#ifdef __cplusplus -} -#endif - -#endif /* LIST_H */ -
--- a/src/server/ucx/logging.c Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,102 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "logging.h" -#include <stdlib.h> -#include <string.h> -#include <stdarg.h> -#include <time.h> - -UcxLogger *ucx_logger_new(void *stream, unsigned int level, unsigned int mask) { - UcxLogger *logger = (UcxLogger*) malloc(sizeof(UcxLogger)); - if (logger != NULL) { - logger->stream = stream; - logger->writer = (write_func)fwrite; - logger->dateformat = (char*) "%F %T %z "; - logger->level = level; - logger->mask = mask; - logger->levels = ucx_map_new(8); - - unsigned int l; - l = UCX_LOGGER_ERROR; - ucx_map_int_put(logger->levels, l, (void*) "[ERROR]"); - l = UCX_LOGGER_WARN; - ucx_map_int_put(logger->levels, l, (void*) "[WARNING]"); - l = UCX_LOGGER_INFO; - ucx_map_int_put(logger->levels, l, (void*) "[INFO]"); - l = UCX_LOGGER_TRACE; - ucx_map_int_put(logger->levels, l, (void*) "[TRACE]"); - } - - return logger; -} - -void ucx_logger_free(UcxLogger *logger) { - ucx_map_free(logger->levels); - free(logger); -} - -void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file, - const unsigned int line, const char *format, ...) { - if (level <= logger->level) { - const size_t max = 4096; // estimated maximum message length - char msg[max]; - char *text; - size_t k = 0; - size_t n; - - if ((logger->mask & UCX_LOGGER_LEVEL) > 0) { - text = (char*) ucx_map_int_get(logger->levels, level); - n = strlen(text); - memcpy(msg+k, text, n); - k += n; - msg[k++] = ' '; - } - if ((logger->mask & UCX_LOGGER_TIMESTAMP) > 0) { - time_t now = time(NULL); - k += strftime(msg+k, 128, logger->dateformat, localtime(&now)); - } - if ((logger->mask & UCX_LOGGER_SOURCE) > 0) { - n = strlen(file); - memcpy(msg+k, file, n); - k += n; - k += sprintf(msg+k, ":%d ", line); - } - - msg[k++] = '-'; msg[k++] = ' '; - - va_list args; - va_start (args, format); - k += vsnprintf(msg+k, max-k-1, format, args); - va_end (args); - - msg[k++] = '\n'; - - logger->writer(msg, 1, k, logger->stream); - } -}
--- a/src/server/ucx/logging.h Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,80 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef LOGGING_H -#define LOGGING_H - -#include "ucx.h" -#include "map.h" -#include "string.h" -#include <stdio.h> - -#ifdef __cplusplus -extern "C" { -#endif - -/* leave enough space for custom log levels */ -#define UCX_LOGGER_ERROR 0x00 -#define UCX_LOGGER_WARN 0x10 -#define UCX_LOGGER_INFO 0x20 -#define UCX_LOGGER_TRACE 0x30 - -#define UCX_LOGGER_LEVEL 0x01 -#define UCX_LOGGER_TIMESTAMP 0x02 -#define UCX_LOGGER_SOURCE 0x04 - -typedef struct { - void *stream; - write_func writer; - char *dateformat; - unsigned int level; - unsigned int mask; - UcxMap* levels; -} UcxLogger; - -UcxLogger *ucx_logger_new(void *stream, unsigned int level, unsigned int mask); -void ucx_logger_free(UcxLogger* logger); - -void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file, - const unsigned int line, const char* format, ...); -#define ucx_logger_log(logger, level, ...) \ - ucx_logger_logf(logger, level, __FILE__, __LINE__, __VA_ARGS__) -#define ucx_logger_error(logger, ...) \ - ucx_logger_log(logger, UCX_LOGGER_ERROR, __VA_ARGS__) -#define ucx_logger_info(logger, ...) \ - ucx_logger_log(logger, UCX_LOGGER_INFO, __VA_ARGS__) -#define ucx_logger_warn(logger, ...) \ - ucx_logger_log(logger, UCX_LOGGER_WARN, __VA_ARGS__) -#define ucx_logger_trace(logger, ...) \ - ucx_logger_log(logger, UCX_LOGGER_TRACE, __VA_ARGS__) - -#ifdef __cplusplus -} -#endif - -#endif /* LOGGING_H */
--- a/src/server/ucx/map.c Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,315 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <stdlib.h> -#include <string.h> - -#include "map.h" - -UcxMap *ucx_map_new(size_t size) { - return ucx_map_new_allocator(size, NULL); -} - -UcxMap *ucx_map_new_allocator(size_t size, UcxAllocator *allocator) { - if(size == 0) { - size = 16; - } - - if(!allocator) { - allocator = ucx_default_allocator(); - } - - UcxMap *map = (UcxMap*)allocator->malloc(allocator->pool, sizeof(UcxMap)); - if(map == NULL) { - return NULL; - } - - map->allocator = allocator; - map->map = (UcxMapElement**)allocator->calloc( - allocator->pool, - size, - sizeof(UcxMapElement*)); - if(map->map == NULL) { - allocator->free(allocator->pool, map); - return NULL; - } - map->size = size; - map->count = 0; - - return map; -} - -void ucx_map_free_elmlist(UcxMap *map) { - for (size_t n = 0 ; n < map->size ; n++) { - UcxMapElement *elem = map->map[n]; - if (elem != NULL) { - do { - UcxMapElement *next = elem->next; - map->allocator->free(map->allocator->pool, elem->key.data); - map->allocator->free(map->allocator->pool, elem); - elem = next; - } while (elem != NULL); - } - } - map->allocator->free(map->allocator->pool, map->map); -} - -void ucx_map_free(UcxMap *map) { - ucx_map_free_elmlist(map); - map->allocator->free(map->allocator->pool, map); -} - -int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to, - copy_func fnc, void *data) { - UcxMapIterator i = ucx_map_iterator(from); - void *value; - UCX_MAP_FOREACH(key, value, i) { - int ret = ucx_map_put(to, i.cur->key, fnc ? fnc(value, data) : value); - if(ret != 0) { - return 1; - } - } - return 0; -} - -UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data) { - size_t bs = (map->count * 5) >> 1; - UcxMap *newmap = ucx_map_new(bs > map->size ? bs : map->size); - if(newmap == NULL) { - return NULL; - } - ucx_map_copy(map, newmap, fnc, data); - return newmap; -} - -int ucx_map_rehash(UcxMap *map) { - size_t load = (map->size * 3) >> 2; - if (map->count > load) { - UcxMap oldmap; - oldmap.map = map->map; - oldmap.size = map->size; - oldmap.count = map->count; - oldmap.allocator = map->allocator; - - map->size = (map->count * 5) >> 1; - map->map = (UcxMapElement**)map->allocator->calloc( - map->allocator->pool, - map->size, - sizeof(UcxMapElement*)); - if(map->map == NULL) { - *map = oldmap; - return 1; - } - map->count = 0; - ucx_map_copy(&oldmap, map, NULL, NULL); - - /* free the UcxMapElement list of oldmap */ - ucx_map_free_elmlist(&oldmap); - } - return 0; -} - -int ucx_map_put(UcxMap *map, UcxKey key, void *data) { - UcxAllocator *allocator = map->allocator; - - if(key.hash == 0) { - key.hash = ucx_hash((char*)key.data, key.len); - } - - size_t slot = key.hash%map->size; - UcxMapElement *restrict elm = map->map[slot]; - UcxMapElement *restrict prev = NULL; - - while (elm != NULL && elm->key.hash < key.hash) { - prev = elm; - elm = elm->next; - } - - if (elm == NULL || elm->key.hash != key.hash) { - UcxMapElement *e = (UcxMapElement*)allocator->malloc( - allocator->pool, - sizeof(UcxMapElement)); - if(e == NULL) { - return -1; - } - e->key.data = NULL; - if (prev) { - prev->next = e; - } else { - map->map[slot] = e; - } - e->next = elm; - elm = e; - } - - if(elm->key.data == NULL) { - void *kd = allocator->malloc(allocator->pool, key.len); - if (kd == NULL) { - return -1; - } - memcpy(kd, key.data, key.len); - key.data = kd; - elm->key = key; - map->count++; - } - elm->data = data; - - return 0; -} - -void* ucx_map_get_and_remove(UcxMap *map, UcxKey key, _Bool remove) { - if(key.hash == 0) { - key.hash = ucx_hash((char*)key.data, key.len); - } - - size_t slot = key.hash%map->size; - UcxMapElement *restrict elm = map->map[slot]; - UcxMapElement *restrict pelm = NULL; - while (elm && elm->key.hash <= key.hash) { - if(elm->key.hash == key.hash) { - int n = (key.len > elm->key.len) ? elm->key.len : key.len; - if (memcmp(elm->key.data, key.data, n) == 0) { - void *data = elm->data; - if (remove) { - if (pelm) { - pelm->next = elm->next; - } else { - map->map[slot] = elm->next; - } - map->allocator->free(map->allocator->pool, elm); - map->count--; - } - - return data; - } - } - pelm = elm; - elm = pelm->next; - } - - return NULL; -} - -void *ucx_map_get(UcxMap *map, UcxKey key) { - return ucx_map_get_and_remove(map, key, 0); -} - -void *ucx_map_remove(UcxMap *map, UcxKey key) { - return ucx_map_get_and_remove(map, key, 1); -} - -UcxKey ucx_key(void *data, size_t len) { - UcxKey key; - key.data = data; - key.len = len; - key.hash = ucx_hash((const char*) data, len); - return key; -} - - -int ucx_hash(const char *data, size_t len) { - /* murmur hash 2 */ - - int m = 0x5bd1e995; - int r = 24; - - int h = 25 ^ len; - - int i = 0; - while (len >= 4) { - int k = data[i + 0] & 0xFF; - k |= (data[i + 1] & 0xFF) << 8; - k |= (data[i + 2] & 0xFF) << 16; - k |= (data[i + 3] & 0xFF) << 24; - - k *= m; - k ^= k >> r; - k *= m; - - h *= m; - h ^= k; - - i += 4; - len -= 4; - } - - switch (len) { - case 3: h ^= (data[i + 2] & 0xFF) << 16; - /* no break */ - case 2: h ^= (data[i + 1] & 0xFF) << 8; - /* no break */ - case 1: h ^= (data[i + 0] & 0xFF); h *= m; - /* no break */ - } - - h ^= h >> 13; - h *= m; - h ^= h >> 15; - - return h; -} - -UcxMapIterator ucx_map_iterator(UcxMap *map) { - UcxMapIterator i; - i.map = map; - i.cur = NULL; - i.index = 0; - return i; -} - -int ucx_map_iter_next(UcxMapIterator *i, UcxKey *key, void **elm) { - UcxMapElement *e = i->cur; - - if(e == NULL) { - e = i->map->map[0]; - } else { - e = e->next; - } - - while(i->index < i->map->size) { - if(e != NULL) { - if(e->data != NULL) { - i->cur = e; - *elm = e->data; - *key = e->key; - return 0; - } - - e = e->next; - } else { - i->index++; - - if(i->index < i->map->size) { - e = i->map->map[i->index]; - } - } - } - - return 1; -} -
--- a/src/server/ucx/map.h Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,131 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef MAP_H -#define MAP_H - -#include "ucx.h" -#include "string.h" -#include "mempool.h" -#include <stdio.h> - -#ifdef __cplusplus -extern "C" { -#endif - -#define UCX_MAP_FOREACH(key,elm,iter) \ - for(UcxKey key;ucx_map_iter_next(&iter,&key, (void**)&elm)==0;) - -typedef struct UcxMap UcxMap; -typedef struct UcxKey UcxKey; -typedef struct UcxMapElement UcxMapElement; -typedef struct UcxMapIterator UcxMapIterator; - -/* - * param 1: element - * param 2: additional data - * param 3: size of encoded data will be stored here - * returns encoded / decoded string or NULL on failure - */ -typedef void*(*ucx_map_coder)(void*,void*,size_t*); - -struct UcxMap { - UcxAllocator *allocator; - UcxMapElement **map; - size_t size; - size_t count; -}; - -struct UcxKey { - void *data; - size_t len; - int hash; -}; - -struct UcxMapElement { - void *data; - UcxMapElement *next; - UcxKey key; -}; - -struct UcxMapIterator { - UcxMap *map; - UcxMapElement *cur; - size_t index; -}; - - -UcxMap *ucx_map_new(size_t size); -UcxMap *ucx_map_new_allocator(size_t size, UcxAllocator *allocator); -void ucx_map_free(UcxMap *map); -/* you cannot clone maps with more than 390 mio entries */ -int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to, - copy_func fnc, void *data); -UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data); -int ucx_map_rehash(UcxMap *map); - -int ucx_map_put(UcxMap *map, UcxKey key, void *data); -void* ucx_map_get(UcxMap *map, UcxKey key); -void* ucx_map_remove(UcxMap *map, UcxKey key); - -#define ucx_map_sstr_put(m, s, d) \ - ucx_map_put(m, ucx_key(s.ptr, s.length), (void*)d) -#define ucx_map_cstr_put(m, s, d) \ - ucx_map_put(m, ucx_key((void*)s, strlen(s)), (void*)d) -#define ucx_map_int_put(m, i, d) \ - ucx_map_put(m, ucx_key((void*)&i, sizeof(i)), (void*)d) - -#define ucx_map_sstr_get(m, s) \ - ucx_map_get(m, ucx_key(s.ptr, s.length)) -#define ucx_map_cstr_get(m, s) \ - ucx_map_get(m, ucx_key((void*)s, strlen(s))) -#define ucx_map_int_get(m, i) \ - ucx_map_get(m, ucx_key((void*)&i, sizeof(int))) - -#define ucx_map_sstr_remove(m, s) \ - ucx_map_remove(m, ucx_key(s.ptr, s.length)) -#define ucx_map_cstr_remove(m, s) \ - ucx_map_remove(m, ucx_key((void*)s, strlen(s))) -#define ucx_map_int_remove(m, i) \ - ucx_map_remove(m, ucx_key((void*)&i, sizeof(i))) - -UcxKey ucx_key(void *data, size_t len); - -int ucx_hash(const char *data, size_t len); - -UcxMapIterator ucx_map_iterator(UcxMap *map); - -int ucx_map_iter_next(UcxMapIterator *i, UcxKey *key, void **elm); - - -#ifdef __cplusplus -} -#endif - -#endif /* MAP_H */ -
--- a/src/server/ucx/mempool.c Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,184 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <stdlib.h> -#include <string.h> -#include <stdio.h> -#ifdef __cplusplus -#define __STDC_FORMAT_MACROS -#endif -#include <inttypes.h> - -#include "mempool.h" - -typedef struct { - ucx_destructor destructor; - char c; -} ucx_memchunk; - -typedef struct { - ucx_destructor destructor; - void *ptr; -} ucx_regdestr; - -void ucx_mempool_shared_destr(void* ptr) { - ucx_regdestr *rd = (ucx_regdestr*)ptr; - rd->destructor(rd->ptr); -} - -UcxMempool *ucx_mempool_new(size_t n) { - UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool)); - if (pool == NULL) return NULL; - - pool->data = (void**) malloc(n * sizeof(void*)); - if (pool->data == NULL) { - free(pool); - return NULL; - } - - pool->ndata = 0; - pool->size = n; - return pool; -} - -int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) { - void **data = (void**) realloc(pool->data, newcap*sizeof(void*)); - if (data == NULL) { - return 1; - } else { - pool->data = data; - pool->size = newcap; - return EXIT_SUCCESS; - } -} - -void *ucx_mempool_malloc(UcxMempool *pool, size_t n) { - ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n); - if (mem == NULL) return NULL; - - if (pool->ndata >= pool->size) { - ucx_mempool_chcap(pool, pool->size + 16); - } - - mem->destructor = NULL; - pool->data[pool->ndata] = mem; - pool->ndata++; - - return &mem->c; -} - -void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) { - void *ptr = ucx_mempool_malloc(pool, nelem*elsize); - if(ptr == NULL) { - return NULL; - } - memset(ptr, 0, nelem * elsize); - return ptr; -} - -void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { - char *mem = ((char*)ptr) - sizeof(ucx_destructor); - char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor)); - if (newm == NULL) return NULL; - if (mem != newm) { - for(size_t i=0 ; i < pool->ndata ; i++) { - if(pool->data[i] == mem) { - pool->data[i] = newm; - return newm + sizeof(ucx_destructor); - } - } - fprintf(stderr, "FATAL: 0x%08"PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", - (intptr_t)ptr, (intptr_t)pool); - exit(1); - } else { - return newm + sizeof(ucx_destructor); - } -} - -void ucx_mempool_free(UcxMempool *pool, void *ptr) { - ucx_memchunk *chunk = (ucx_memchunk*)((char*)ptr-sizeof(ucx_destructor)); - for(size_t i=0 ; i<pool->ndata ; i++) { - if(chunk == pool->data[i]) { - if(chunk->destructor != NULL) { - chunk->destructor(&chunk->c); - } - free(chunk); - size_t last_index = pool->ndata - 1; - if(i != last_index) { - pool->data[i] = pool->data[last_index]; - } - pool->ndata--; - return; - } - } - fprintf(stderr, "FATAL: 0x%08"PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", - (intptr_t)ptr, (intptr_t)pool); - exit(1); -} - -void ucx_mempool_destroy(UcxMempool *pool) { - ucx_memchunk *chunk; - for(size_t i=0 ; i<pool->ndata ; i++) { - chunk = (ucx_memchunk*) pool->data[i]; - if(chunk) { - if(chunk->destructor != NULL) { - chunk->destructor(&chunk->c); - } - free(chunk); - } - } - free(pool->data); - free(pool); -} - -void ucx_mempool_set_destr(void *ptr, ucx_destructor func) { - *(ucx_destructor*)((char*)ptr-sizeof(ucx_destructor)) = func; -} - -void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr) { - ucx_regdestr *rd = (ucx_regdestr*)ucx_mempool_malloc( - pool, - sizeof(ucx_regdestr)); - rd->destructor = destr; - rd->ptr = ptr; - ucx_mempool_set_destr(rd, ucx_mempool_shared_destr); -} - -UcxAllocator* ucx_mempool_allocator(UcxMempool *pool) { - UcxAllocator *allocator = (UcxAllocator*)ucx_mempool_malloc( - pool, sizeof(UcxAllocator)); - if(!allocator) { - return NULL; - } - allocator->malloc = (ucx_allocator_malloc)ucx_mempool_malloc; - allocator->calloc = (ucx_allocator_calloc)ucx_mempool_calloc; - allocator->realloc = (ucx_allocator_realloc)ucx_mempool_realloc; - allocator->free = (ucx_allocator_free)ucx_mempool_free; - allocator->pool = pool; - return allocator; -}
--- a/src/server/ucx/mempool.h Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,70 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef MPOOL_H -#define MPOOL_H - -#include "ucx.h" -#include <stddef.h> -#include "allocator.h" - -#ifdef __cplusplus -extern "C" { -#endif - -typedef void(*ucx_destructor)(void*); - -typedef struct { - void **data; - size_t ndata; - size_t size; -} UcxMempool; - - -#define ucx_mempool_new_default() ucx_mempool_new(16) -UcxMempool *ucx_mempool_new(size_t n); -int ucx_mempool_chcap(UcxMempool *pool, size_t newcap); - -void *ucx_mempool_malloc(UcxMempool *pool, size_t n); -void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize); -void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n); -void ucx_mempool_free(UcxMempool *pool, void *ptr); - -void ucx_mempool_destroy(UcxMempool *pool); - -void ucx_mempool_set_destr(void *ptr, ucx_destructor func); -void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr); - -UcxAllocator* ucx_mempool_allocator(UcxMempool *pool); - -#ifdef __cplusplus -} -#endif - -#endif /* MPOOL_H */ -
--- a/src/server/ucx/objs.mk Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,47 +0,0 @@ -# -# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. -# -# Copyright 2011 Olaf Wintermann. All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: -# -# 1. Redistributions of source code must retain the above copyright notice, -# this list of conditions and the following disclaimer. -# -# 2. Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the distribution. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE -# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -# POSSIBILITY OF SUCH DAMAGE. -# - -UCX_SRC_DIR = server/ucx/ - -UCX_OBJPRE = $(OBJ_DIR)$(UCX_SRC_DIR) - -UCXOBJ = list.o -UCXOBJ += dlist.o -UCXOBJ += map.o -UCXOBJ += mempool.o -UCXOBJ += string.o -UCXOBJ += allocator.o -UCXOBJ += utils.o -UCXOBJ += buffer.o -UCXOBJ += test.o -UCXOBJ += properties.o -UCXOBJ += logging.o - -UCXOBJS = $(UCXOBJ:%=$(UCX_OBJPRE)%) -UCXSOURCE = $(UCXOBJ:%.o=ucx/%.c) -
--- a/src/server/ucx/properties.c Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,265 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include "properties.h" - -UcxProperties *ucx_properties_new() { - UcxProperties *parser = (UcxProperties*)malloc( - sizeof(UcxProperties)); - if(!parser) { - return NULL; - } - - parser->buffer = NULL; - parser->buflen = 0; - parser->pos = 0; - parser->tmp = NULL; - parser->tmplen = 0; - parser->tmpcap = 0; - parser->error = 0; - parser->delimiter = '='; - parser->comment1 = '#'; - parser->comment2 = 0; - parser->comment3 = 0; - - return parser; -} - -void ucx_properties_free(UcxProperties *parser) { - if(parser->tmp) { - free(parser->tmp); - } - free(parser); -} - -void ucx_properties_fill(UcxProperties *parser, char *buf, size_t len) { - parser->buffer = buf; - parser->buflen = len; - parser->pos = 0; -} - -static void parser_tmp_append(UcxProperties *parser, char *buf, size_t len) { - if(parser->tmpcap - parser->tmplen < len) { - size_t newcap = parser->tmpcap + len + 64; - parser->tmp = (char*)realloc(parser->tmp, newcap); - parser->tmpcap = newcap; - } - memcpy(parser->tmp + parser->tmplen, buf, len); - parser->tmplen += len; -} - -int ucx_properties_next(UcxProperties *parser, sstr_t *name, sstr_t *value) { - if(parser->tmplen > 0) { - char *buf = parser->buffer + parser->pos; - size_t len = parser->buflen - parser->pos; - sstr_t str = sstrn(buf, len); - sstr_t nl = sstrchr(str, '\n'); - if(nl.ptr) { - size_t newlen = (size_t)(nl.ptr - buf) + 1; - parser_tmp_append(parser, buf, newlen); - // the tmp buffer contains exactly one line now - - char *orig_buf = parser->buffer; - size_t orig_len = parser->buflen; - - parser->buffer = parser->tmp; - parser->buflen = parser->tmplen; - parser->pos = 0; - parser->tmp = NULL; - parser->tmpcap = 0; - parser->tmplen = 0; - // run ucx_properties_next with the tmp buffer as main buffer - int ret = ucx_properties_next(parser, name, value); - - // restore original buffer - parser->tmp = parser->buffer; - parser->buffer = orig_buf; - parser->buflen = orig_len; - parser->pos = newlen; - - /* - * if ret == 0 the tmp buffer contained just space or a comment - * we parse again with the original buffer to get a name/value - * or a new tmp buffer - */ - return ret ? ret : ucx_properties_next(parser, name, value); - } else { - parser_tmp_append(parser, buf, len); - return 0; - } - } else if(parser->tmp) { - free(parser->tmp); - parser->tmp = NULL; - } - - char comment1 = parser->comment1; - char comment2 = parser->comment2; - char comment3 = parser->comment3; - char delimiter = parser->delimiter; - - // get one line and parse it - while(parser->pos < parser->buflen) { - char *buf = parser->buffer + parser->pos; - size_t len = parser->buflen - parser->pos; - - /* - * First we check if we have at least one line. We also get indices of - * delimiter and comment chars - */ - size_t delimiter_index = 0; - size_t comment_index = 0; - int has_comment = 0; - - size_t i = 0; - char c = 0; - for(;i<len;i++) { - c = buf[i]; - if(c == comment1 || c == comment2 || c == comment3) { - if(comment_index == 0) { - comment_index = i; - has_comment = 1; - } - } else if(c == delimiter) { - if(delimiter_index == 0 && !has_comment) { - delimiter_index = i; - } - } else if(c == '\n') { - break; - } - } - - if(c != '\n') { - // we don't have enough data for a line - // store remaining bytes in temporary buffer for next round - parser->tmpcap = len + 128; - parser->tmp = (char*)malloc(parser->tmpcap); - parser->tmplen = len; - memcpy(parser->tmp, buf, len); - return 0; - } - - sstr_t line = has_comment ? sstrn(buf, comment_index) : sstrn(buf, i); - // check line - if(delimiter_index == 0) { - line = sstrtrim(line); - if(line.length != 0) { - parser->error = 1; - } - } else { - sstr_t n = sstrn(buf, delimiter_index); - sstr_t v = sstrn(buf+delimiter_index+1, i-delimiter_index-1); - n = sstrtrim(n); - v = sstrtrim(v); - if(n.length != 0 || v.length != 0) { - *name = n; - *value = v; - parser->pos += i + 1; - return 1; - } else { - parser->error = 1; - } - } - - parser->pos += i + 1; - } - - return 0; -} - -int ucx_properties2map(UcxProperties *parser, UcxMap *map) { - sstr_t name; - sstr_t value; - while(ucx_properties_next(parser, &name, &value)) { - name = sstrdup_alloc(map->allocator, name); - if(!name.ptr) { - return 1; - } - value = sstrdup_alloc(map->allocator, value); - if(!value.ptr) { - map->allocator->free(map->allocator->pool, value.ptr); - return 1; - } - if(ucx_map_sstr_put(map, name, value.ptr)) { - map->allocator->free(map->allocator->pool, name.ptr); - map->allocator->free(map->allocator->pool, value.ptr); - return 1; - } - } - if(parser->error) { - return 1; - } else { - return 0; - } -} - -int ucx_properties_load(UcxMap *map, FILE *file) { - UcxProperties *parser = ucx_properties_new(); - if(!parser || !map || !file) { - return 1; - } - - int error = 0; - size_t r; - char buf[1024]; - while((r = fread(buf, 1, 1024, file)) != 0) { - ucx_properties_fill(parser, buf, r); - if(ucx_properties2map(parser, map)) { - error = 1; - break; - } - } - - ucx_properties_free(parser); - return error; -} - -int ucx_properties_store(UcxMap *map, FILE *file) { - UcxMapIterator iter = ucx_map_iterator(map); - char *v; - sstr_t value; - size_t written; - - UCX_MAP_FOREACH(k, v, iter) { - value = sstr(v); - - written = 0; - written += fwrite(k.data, 1, k.len, file); - written += fwrite(" = ", 1, 3, file); - written += fwrite(value.ptr, 1, value.length, file); - written += fwrite("\n", 1, 1, file); - - if (written != k.len + value.length + 4) return 1; - } - - return 0; -} -
--- a/src/server/ucx/properties.h Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,68 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef PROPERTIES_H -#define PROPERTIES_H - -#include "ucx.h" -#include "map.h" - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct { - char *buffer; - size_t buflen; - size_t pos; - char *tmp; - size_t tmplen; - size_t tmpcap; - int error; - char delimiter; - char comment1; - char comment2; - char comment3; -} UcxProperties; - - -UcxProperties *ucx_properties_new(); -void ucx_properties_free(UcxProperties *parser); -void ucx_properties_fill(UcxProperties *parser, char *buf, size_t len); -int ucx_properties_next(UcxProperties *parser, sstr_t *name, sstr_t *value); -int ucx_properties2map(UcxProperties *parser, UcxMap *map); - -int ucx_properties_load(UcxMap *map, FILE *file); -int ucx_properties_store(UcxMap *map, FILE *file); - -#ifdef __cplusplus -} -#endif - -#endif /* PROPERTIES_H */ -
--- a/src/server/ucx/string.c Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,283 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <stdlib.h> -#include <string.h> -#include <stdarg.h> - -#include "string.h" -#include "allocator.h" - -sstr_t sstr(char *s) { - sstr_t string; - string.ptr = s; - string.length = strlen(s); - return string; -} - -sstr_t sstrn(char *s, size_t n) { - sstr_t string; - string.ptr = s; - string.length = n; - return string; -} - -size_t sstrnlen(size_t n, sstr_t s, ...) { - va_list ap; - size_t size = s.length; - va_start(ap, s); - - for (size_t i = 0 ; i < n-1 ; i++) { - sstr_t str = va_arg(ap, sstr_t); - size += str.length; - } - va_end(ap); - - return size; -} - -sstr_t sstrncat(size_t n, sstr_t s, sstr_t c1, ...) { - va_list ap; - va_start(ap, c1); - s.ptr[0] = 0; - - size_t len = s.length; - size_t cplen = c1.length > len ? len : c1.length; - char *ptr = s.ptr; - - memcpy(ptr, c1.ptr, cplen); - len -= cplen; - ptr += cplen; - for (size_t i = 0 ; i < n-1 ; i++) { - sstr_t str = va_arg (ap, sstr_t); - cplen = str.length > len ? len : str.length; - if(cplen <= 0) { - va_end(ap); - return s; - } - memcpy(ptr, str.ptr, cplen); - len -= cplen; - ptr += cplen; - } - va_end(ap); - s.length = ptr - s.ptr; - - return s; -} - -sstr_t sstrsubs(sstr_t s, size_t start) { - return sstrsubsl (s, start, s.length-start); -} - -sstr_t sstrsubsl(sstr_t s, size_t start, size_t length) { - sstr_t new_sstr; - if (start >= s.length) { - return s; - } - if (length > s.length-start) { - length = s.length-start; - } - new_sstr.ptr = &s.ptr[start]; - new_sstr.length = length; - return new_sstr; -} - -sstr_t sstrchr(sstr_t s, int c) { - for(size_t i=0;i<s.length;i++) { - if(s.ptr[i] == c) { - return sstrsubs(s, i); - } - } - sstr_t n; - n.ptr = NULL; - n.length = 0; - return n; -} - -sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n) { - if (d.length == 0) { - return NULL; - } - - sstr_t* result; - size_t nmax = *n; - *n = 1; - - /* special case: exact match - no processing needed */ - if (s.length == d.length && strncmp(s.ptr, d.ptr, s.length) == 0) { - *n = 0; - return NULL; - } - sstr_t sv = sstrdup(s); - - for (size_t i = 0 ; i < s.length ; i++) { - if (sv.ptr[i] == d.ptr[0]) { - _Bool match = 1; - for (size_t j = 1 ; j < d.length ; j++) { - if (j+i < s.length) { - match &= (sv.ptr[i+j] == d.ptr[j]); - } else { - match = 0; - break; - } - } - if (match) { - (*n)++; - for (size_t j = 0 ; j < d.length ; j++) { - sv.ptr[i+j] = 0; - } - i += d.length; - } - } - if ((*n) == nmax) break; - } - result = (sstr_t*) malloc(sizeof(sstr_t) * (*n)); - - char *pptr = sv.ptr; - for (size_t i = 0 ; i < *n ; i++) { - size_t l = strlen(pptr); - char* ptr = (char*) malloc(l + 1); - memcpy(ptr, pptr, l); - ptr[l] = 0; - - result[i] = sstrn(ptr, l); - pptr += l + d.length; - } - - free(sv.ptr); - - return result; -} - -int sstrcmp(sstr_t s1, sstr_t s2) { - return strncmp(s1.ptr, s2.ptr, s1.length>s2.length ? s2.length: s1.length); -} - -sstr_t sstrdup(sstr_t s) { - sstr_t newstring; - newstring.ptr = (char*) malloc(s.length + 1); - if (newstring.ptr) { - newstring.length = s.length; - newstring.ptr[newstring.length] = 0; - - memcpy(newstring.ptr, s.ptr, s.length); - } else { - newstring.length = 0; - } - - return newstring; -} - -sstr_t sstrdup_alloc(UcxAllocator *allocator, sstr_t s) { - sstr_t newstring; - newstring.ptr = (char*)allocator->malloc(allocator->pool, s.length + 1); - if (newstring.ptr) { - newstring.length = s.length; - newstring.ptr[newstring.length] = 0; - - memcpy(newstring.ptr, s.ptr, s.length); - } else { - newstring.length = 0; - } - - return newstring; -} - -sstr_t sstrtrim(sstr_t string) { - sstr_t newstr = string; - if (string.length == 0) { - return newstr; - } - - size_t i; - for(i=0;i<string.length;i++) { - char c = string.ptr[i]; - if(c > 32) { - break; - } - } - newstr.ptr = &string.ptr[i]; - newstr.length = string.length - i; - - if(newstr.length == 0) { - return newstr; - } - - i = newstr.length - 1; - for(;;) { - char c = newstr.ptr[i]; - if(c > 32) { - break; - } - if(i > 0) { - i--; - } else { - break; - } - } - newstr.length = i + 1; - - return newstr; -} - -// webserver extension - -int sstr_startswith(sstr_t string, sstr_t cmp) { - sstr_t sub = sstrsubsl(string, 0, cmp.length); - if(!sstrcmp(sub, cmp)) { - return 1; - } else { - return 0; - } -} - -sstr_t sstrdup_mp(UcxMempool *pool, sstr_t s) { - sstr_t newstring; - newstring.ptr = (char*)ucx_mempool_malloc(pool, s.length + 1); - if (newstring.ptr != NULL) { - newstring.length = s.length; - newstring.ptr[newstring.length] = 0; - - memcpy(newstring.ptr, s.ptr, s.length); - } - - return newstring; -} - -sstr_t sstrdup_pool(pool_handle_t *pool, sstr_t s) { - sstr_t newstring; - newstring.ptr = (char*)pool_malloc(pool, s.length + 1); - if (newstring.ptr != NULL) { - newstring.length = s.length; - newstring.ptr[newstring.length] = 0; - - memcpy(newstring.ptr, s.ptr, s.length); - } - - return newstring; -}
--- a/src/server/ucx/string.h Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,136 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef _SSTRING_H -#define _SSTRING_H - -#include "ucx.h" -#include "allocator.h" -#include "mempool.h" -#include "../public/nsapi.h" -#include <stddef.h> - -/* use macros for literals only */ -#define S(s) { (char*)s, sizeof(s)-1 } -#define ST(s) sstrn((char*)s, sizeof(s)-1) - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct sstring { - char *ptr; - size_t length; -} sstr_t; - -/* - * creates a new sstr_t from a null terminated string - * - * s null terminated string - */ -sstr_t sstr(char *s); - -/* - * creates a new sstr_t from a string and length - * - * s string - * n length of string - */ -sstr_t sstrn(char *s, size_t n); - - -/* - * gets the length of n sstr_t strings - * - * n number of strings - * s string - * ... strings - */ -size_t sstrnlen(size_t n, sstr_t s, ...); - - -/* - * concatenates n strings - * - * n number of strings - * s new string with enough memory allocated - * ... strings - */ -sstr_t sstrncat(size_t n, sstr_t s, sstr_t c1, ...); - - -/* - * - */ -sstr_t sstrsubs(sstr_t s, size_t start); - -/* - * - */ -sstr_t sstrsubsl(sstr_t s, size_t start, size_t length); - -/* - * - */ -sstr_t sstrchr(sstr_t s, int c); - -/* - * splits s into n parts - * - * s the string to split - * d the delimiter string - * n the maximum size of the resulting list - * a size of 0 indicates an unbounded list size - * the actual size of the list will be stored here - * - * Hint: use this value to avoid dynamic reallocation of the result list - * - * Returns a list of the split strings - * NOTE: this list needs to be freed manually after usage - * - * Returns NULL on error - */ -sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n); - -int sstrcmp(sstr_t s1, sstr_t s2); - -sstr_t sstrdup(sstr_t s); -sstr_t sstrdup_alloc(UcxAllocator *allocator, sstr_t s); - -sstr_t sstrtrim(sstr_t string); - -// webserver extension -int sstr_startswith(sstr_t string, sstr_t cmp); -sstr_t sstrdup_mp(UcxMempool *pool, sstr_t s); -sstr_t sstrdup_pool(pool_handle_t *pool, sstr_t s); - -#ifdef __cplusplus -} -#endif - -#endif /* _SSTRING_H */
--- a/src/server/ucx/test.c Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,90 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "test.h" - -UcxTestSuite* ucx_test_suite_new() { - UcxTestSuite* suite = (UcxTestSuite*) malloc(sizeof(UcxTestSuite)); - if (suite != NULL) { - suite->success = 0; - suite->failure = 0; - suite->tests = NULL; - } - return suite; -} - -void ucx_test_suite_free(UcxTestSuite* suite) { - UcxTestList *l = suite->tests; - while (l != NULL) { - UcxTestList *e = l; - l = l->next; - free(e); - } - free(suite); -} - -int ucx_test_register(UcxTestSuite* suite, UcxTest test) { - if (suite->tests) { - UcxTestList *newelem = (UcxTestList*) malloc(sizeof(UcxTestList)); - if (newelem) { - newelem->test = test; - newelem->next = NULL; - - UcxTestList *last = suite->tests; - while (last->next) { - last = last->next; - } - last->next = newelem; - - return EXIT_SUCCESS; - } else { - return EXIT_FAILURE; - } - } else { - suite->tests = (UcxTestList*) malloc(sizeof(UcxTestList)); - if (suite->tests) { - suite->tests->test = test; - suite->tests->next = NULL; - - return EXIT_SUCCESS; - } else { - return EXIT_FAILURE; - } - } -} - -void ucx_test_run(UcxTestSuite* suite, FILE* output) { - suite->success = 0; - suite->failure = 0; - UCX_FOREACH (UcxTestList*, suite->tests, e) { - e->test(suite, output); - } - fwrite("\nAll test completed.\n", 1, 21, output); - fprintf(output, " Total: %d\n Success: %d\n Failure: %d\n", - suite->success+suite->failure, suite->success, suite->failure); -}
--- a/src/server/ucx/test.h Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,119 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/* - * - * Usage of this test framework: - * - * **** IN HEADER FILE: **** - * - * UCX_TEST_DECLARE(function_name) - * - * **** IN SOURCE FILE: **** - * - * UCX_TEST_IMPLEMENT(function_name) { - * <memory allocation and other stuff here> - * UCX_TEST_BEGIN - * <tests with UCX_TEST_ASSERT here> - * UCX_TEST_END - * <cleanup of memory here> - * } - * - * PLEASE NOTE: if a test fails, a longjump is performed - * back to the UCX_TEST_BEGIN macro! - * - * You may use multiple BEGIN-END blocks if you are aware of the - * longjmp behaviour. - * - */ - -#ifndef TEST_H -#define TEST_H - -#include "ucx.h" -#include <stdio.h> -#include <string.h> -#include <setjmp.h> - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef __FUNCTION__ -#define __FUNCTION__ __func__ -#endif - -typedef struct UcxTestList UcxTestList; -typedef struct UcxTestSuite UcxTestSuite; -typedef void(*UcxTest)(UcxTestSuite*,FILE*); - -struct UcxTestList{ - UcxTest test; - UcxTestList *next; -}; - -struct UcxTestSuite { - unsigned int success; - unsigned int failure; - UcxTestList *tests; -}; - -UcxTestSuite* ucx_test_suite_new(); -void ucx_test_suite_free(UcxTestSuite*); - -int ucx_test_register(UcxTestSuite*, UcxTest); -void ucx_test_run(UcxTestSuite*, FILE*); - -#define UCX_TEST_DECLARE(name) void name(UcxTestSuite*,FILE *) -#define UCX_TEST_IMPLEMENT(name) void name(UcxTestSuite* _suite_,FILE *_output_) - -#define UCX_TEST_BEGIN fwrite("Running ", 1, 8, _output_);\ - fwrite(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\ - fwrite("... ", 1, 4, _output_);\ - jmp_buf _env_; \ - if (!setjmp(_env_)) { - -#define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \ - fwrite(message".\n", 1, 2+strlen(message), _output_); \ - _suite_->failure++; \ - longjmp(_env_, 1);\ - } - -#define UCX_TEST_SUBROUTINE(name,...) void name(UcxTestSuite* _suite_,\ - FILE *_output_, jmp_buf _env_, __VA_ARGS__) -#define UCX_TEST_CALL_SUBROUTINE(name,...) \ - name(_suite_,_output_,_env_,__VA_ARGS__); - -#define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;} - -#ifdef __cplusplus -} -#endif - -#endif /* TEST_H */ -
--- a/src/server/ucx/ucx.h Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,62 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef UCX_H -#define UCX_H - -#include <stdlib.h> - -#ifdef __cplusplus -#ifndef _Bool -#define _Bool bool -#define restrict -#endif -extern "C" { -#endif - -#define UCX_FOREACH(type,list,elem) \ - for (type elem = list ; elem != NULL ; elem = elem->next) - -/* element1,element2,custom data -> {-1,0,1} */ -typedef int(*cmp_func)(void*,void*,void*); - -/* element,custom data -> copy of element */ -typedef void*(*copy_func)(void*,void*); - -/* buffer, element size, element count, stream */ -typedef size_t(*write_func)(const void*, size_t, size_t, void*); - -/* buffer, element size, element count, stream */ -typedef size_t(*read_func)(void*, size_t, size_t, void*); - -#ifdef __cplusplus -} -#endif - -#endif /* UCX_H */ -
--- a/src/server/ucx/utils.c Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,100 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "utils.h" -#include "math.h" - -/* COPY FUCNTIONS */ -void* ucx_strcpy(void* s, void* data) { - char *str = (char*) s; - size_t n = 1+strlen(str); - char *cpy = (char*) malloc(n); - memcpy(cpy, str, n); - return cpy; -} - -void* ucx_memcpy(void* m, void* n) { - size_t k = *((size_t*)n); - void *cpy = malloc(k); - memcpy(cpy, m, k); - return cpy; -} - -/* COMPARE FUNCTION */ - -int ucx_strcmp(void *s1, void *s2, void *data) { - return strcmp((char*)s1, (char*)s2); -} - -int ucx_strncmp(void *s1, void *s2, void *n) { - return strncmp((char*)s1, (char*)s2, *((size_t*) n)); -} - -int ucx_intcmp(void *i1, void *i2, void *data) { - int a = *((int*) i1); - int b = *((int*) i2); - if (a == b) { - return 0; - } else { - return a < b ? -1 : 1; - } -} - -int ucx_floatcmp(void *f1, void *f2, void *epsilon) { - float a = *((float*) f1); - float b = *((float*) f2); - float e = !epsilon ? 1e-6f : *((float*)epsilon); - if (fabsf(a - b) < e) { - return 0; - } else { - return a < b ? -1 : 1; - } -} - -int ucx_doublecmp(void *d1, void *d2, void *epsilon) { - double a = *((float*) d1); - double b = *((float*) d2); - double e = !epsilon ? 1e-14 : *((double*)epsilon); - if (fabs(a - b) < e) { - return 0; - } else { - return a < b ? -1 : 1; - } -} - -int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) { - if (ptr1 == ptr2) { - return 0; - } else { - return ptr1 < ptr2 ? -1 : 1; - } -} - -int ucx_memcmp(void *ptr1, void *ptr2, void *n) { - return memcmp(ptr1, ptr2, *((size_t*)n)); -}
--- a/src/server/ucx/utils.h Mon Oct 14 13:36:28 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,131 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2013 Olaf Wintermann. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef COMPARATOR_H -#define COMPARATOR_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include "ucx.h" -#include <string.h> - -/** - * Copies a string. - * @param s the string to copy - * @param data omitted - * @return a pointer to a copy of s1 that can be passed to free(void*) - */ -void *ucx_strcpy(void *s, void *data); - -/** - * Copies a memory area. - * @param m a pointer to the memory area - * @param n a pointer to the size_t containing the size of the memory area - * @return a pointer to a copy of the specified memory area that can - * be passed to free(void*) - */ -void *ucx_memcpy(void *m, void *n); - -/** - * Wraps the strcmp function. - * @param s1 string one - * @param s2 string two - * @param data omitted - * @return the result of strcmp(s1, s2) - */ -int ucx_strcmp(void *s1, void *s2, void *data); - -/** - * Wraps the strncmp function. - * @param s1 string one - * @param s2 string two - * @param n a pointer to the size_t containing the third strncmp parameter - * @return the result of strncmp(s1, s2, *n) - */ -int ucx_strncmp(void *s1, void *s2, void *n); - -/** - * Compares two integers of type int. - * @param i1 pointer to integer one - * @param i2 pointer to integer two - * @param data omitted - * @return -1, if *i1 is less than *i2, 0 if both are equal, - * 1 if *i1 is greater than *i2 - */ - -int ucx_intcmp(void *i1, void *i2, void *data); - -/** - * Compares two real numbers of type float. - * @param f1 pointer to float one - * @param f2 pointer to float two - * @param if provided: a pointer to precision (default: 1e-6f) - * @return -1, if *f1 is less than *f2, 0 if both are equal, - * 1 if *f1 is greater than *f2 - */ - -int ucx_floatcmp(void *f1, void *f2, void *data); - -/** - * Compares two real numbers of type double. - * @param f1 pointer to double one - * @param f2 pointer to double two -* @param if provided: a pointer to precision (default: 1e-14) - * @return -1, if *d1 is less than *d2, 0 if both are equal, - * 1 if *d1 is greater than *d2 - */ - -int ucx_doublecmp(void *d1, void *d2, void *data); - -/** - * Compares two pointers. - * @param ptr1 pointer one - * @param ptr2 pointer two - * @param data omitted - * @return -1 if ptr1 is less than ptr2, 0 if both are equal, - * 1 if ptr1 is greater than ptr2 - */ -int ucx_ptrcmp(void *ptr1, void *ptr2, void *data); - -/** - * Compares two memory areas. - * @param ptr1 pointer one - * @param ptr2 pointer two - * @param n a pointer to the size_t containing the third parameter for memcmp - * @return the result of memcmp(ptr1, ptr2, *n) - */ -int ucx_memcmp(void *ptr1, void *ptr2, void *n); - -#ifdef __cplusplus -} -#endif - -#endif /* COMPARATOR_H */ -
--- a/src/server/util/object.h Mon Oct 14 13:36:28 2013 +0200 +++ b/src/server/util/object.h Sun Nov 03 16:41:42 2013 +0100 @@ -43,6 +43,7 @@ NSAPIPathCheck, NSAPIObjectType, NSAPIService, + NSAPIError, NSAPIAddLog, REQ_FINISH, NUM_NSAPI_TYPES
--- a/templates/config/obj.conf Mon Oct 14 13:36:28 2013 +0200 +++ b/templates/config/obj.conf Sun Nov 03 16:41:42 2013 +0100 @@ -6,7 +6,7 @@ <Object name="default"> NameTrans fn="assign-name" from="/hello" name="hello" -NameTrans fn="assign-name" from="/admin name="admin" +NameTrans fn="assign-name" from="/admin" name="admin" ObjectType fn="type-by-extension" Service fn="send-options" method="OPTIONS" Service fn="common-index" type="internal/directory"