Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -1,15 +1,15 @@ SHARED_LIB = ${LIB_PREFIX}corefw${LIB_SUFFIX} STATIC_LIB = libcorefw.a LIB_MAJOR = 0 LIB_MINOR = 0 -SRCS = cfwarray.c \ - cfwclass.c \ - cfwobject.c \ - cfwrange.c \ - cfwstring.c +SRCS = array.c \ + class.c \ + object.c \ + range.c \ + string.c INCLUDES = ${SRCS:.c=.h} \ corefw.h include ../buildsys.mk ADDED src/array.c Index: src/array.c ================================================================== --- /dev/null +++ src/array.c @@ -0,0 +1,263 @@ +/* + * Copyright (c) 2012, Jonathan Schleifer + * 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 OWNER 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 +#include + +#include "object.h" +#include "array.h" + +struct CFWArray { + CFWObject obj; + void **data; + size_t size; +}; + +static bool +ctor(void *ptr, va_list args) +{ + CFWArray *array = ptr; + void *obj; + + array->data = NULL; + array->size = 0; + + while ((obj = va_arg(args, void*)) != NULL) + if (!cfw_array_push(array, obj)) + return false; + + return true; +} + +static void +dtor(void *ptr) +{ + CFWArray *array = ptr; + size_t i; + + for (i = 0; i < array->size; i++) + cfw_unref(array->data[i]); + + if (array->data != NULL) + free(array->data); +} + +static bool +equal(void *ptr1, void *ptr2) +{ + CFWObject *obj2 = ptr2; + CFWArray *array1, *array2; + size_t i; + + if (obj2->cls != cfw_array) + return false; + + array1 = ptr1; + array2 = ptr2; + + if (array1->size != array2->size) + return false; + + for (i = 0; i < array1->size; i++) + if (cfw_equal(array1->data[i], array2->data[i])) + return false; + + return true; +} + +static void* +copy(void *ptr) +{ + CFWArray *array = ptr; + CFWArray *new; + size_t i; + + if ((new = cfw_new(cfw_array)) == NULL) + return NULL; + + if ((new->data = malloc(sizeof(void*) * array->size)) == NULL) { + cfw_unref(new); + return NULL; + } + new->size = array->size; + + for (i = 0; i < array->size; i++) + new->data[i] = cfw_ref(array->data[i]); + + return new; +} + +void* +cfw_array_get(CFWArray *array, size_t index) +{ + if (index >= array->size) + return NULL; + + return array->data[index]; +} + +size_t +cfw_array_size(CFWArray *array) +{ + return array->size; +} + +bool +cfw_array_set(CFWArray *array, size_t index, void *ptr) +{ + CFWObject *obj = ptr; + CFWObject *old; + + if (index >= array->size) + return false; + + cfw_ref(obj); + old = array->data[index]; + array->data[index] = obj; + cfw_unref(old); + + return true; +} + +bool +cfw_array_push(CFWArray *array, void *ptr) +{ + CFWObject *obj = ptr; + void **new; + + if (array->data == NULL) + new = malloc(sizeof(void*)); + else + new = realloc(array->data, sizeof(void*) * (array->size + 1)); + + if (new == NULL) + return false; + + new[array->size] = cfw_ref(obj); + + array->data = new; + array->size++; + + return true; +} + +void* +cfw_array_last(CFWArray *array) +{ + if (array->size == 0) + return NULL; + + return array->data[array->size - 1]; +} + +bool +cfw_array_pop(CFWArray *array) +{ + void **new; + void *last; + + if (array->size == 0) + return NULL; + + if (array->size == 1) { + cfw_unref(array->data[0]); + free(array->data); + array->data = NULL; + array->size = 0; + return true; + } + + last = array->data[array->size - 1]; + + new = realloc(array->data, sizeof(void*) * (array->size - 1)); + if (new == NULL) + return false; + + cfw_unref(last); + + array->data = new; + array->size--; + + return true; +} + +bool +cfw_array_contains(CFWArray *array, void *ptr) +{ + size_t i; + + for (i = 0; i < array->size; i++) + if (cfw_equal(array->data[i], ptr)) + return true; + + return false; +} + +bool +cfw_array_contains_ptr(CFWArray *array, void *ptr) +{ + size_t i; + + for (i = 0; i < array->size; i++) + if (array->data[i] == ptr) + return true; + + return false; +} + +size_t +cfw_array_find(CFWArray *array, void *ptr) +{ + size_t i; + + for (i = 0; i < array->size; i++) + if (cfw_equal(array->data[i], ptr)) + return i; + + return SIZE_MAX; +} + +size_t +cfw_array_find_ptr(CFWArray *array, void *ptr) +{ + size_t i; + + for (i = 0; i < array->size; i++) + if (array->data[i] == ptr) + return i; + + return SIZE_MAX; +} + +static CFWClass class = { + .name = "CFWArray", + .size = sizeof(CFWArray), + .ctor = ctor, + .dtor = dtor, + .equal = equal, + .copy = copy +}; +CFWClass *cfw_array = &class; ADDED src/array.h Index: src/array.h ================================================================== --- /dev/null +++ src/array.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2012, Jonathan Schleifer + * 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 OWNER 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 __CFWARRAY_H__ +#define __CFWARRAY_H__ + +#include "class.h" + +typedef struct CFWArray CFWArray; +extern CFWClass *cfw_array; +extern size_t cfw_array_size(CFWArray*); +extern void* cfw_array_get(CFWArray*, size_t); +extern bool cfw_array_set(CFWArray*, size_t, void*); +extern bool cfw_array_push(CFWArray*, void*); +extern void* cfw_array_last(CFWArray*); +extern bool cfw_array_pop(CFWArray*); +extern bool cfw_array_contains(CFWArray*, void*); +extern bool cfw_array_contains_ptr(CFWArray*, void*); +extern size_t cfw_array_find(CFWArray*, void*); +extern size_t cfw_array_find_ptr(CFWArray*, void*); + +#endif DELETED src/cfwarray.c Index: src/cfwarray.c ================================================================== --- src/cfwarray.c +++ /dev/null @@ -1,263 +0,0 @@ -/* - * Copyright (c) 2012, Jonathan Schleifer - * 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 OWNER 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 -#include - -#include "cfwobject.h" -#include "cfwarray.h" - -struct CFWArray { - CFWObject obj; - void **data; - size_t size; -}; - -static bool -ctor(void *ptr, va_list args) -{ - CFWArray *array = ptr; - void *obj; - - array->data = NULL; - array->size = 0; - - while ((obj = va_arg(args, void*)) != NULL) - if (!cfw_array_push(array, obj)) - return false; - - return true; -} - -static void -dtor(void *ptr) -{ - CFWArray *array = ptr; - size_t i; - - for (i = 0; i < array->size; i++) - cfw_unref(array->data[i]); - - if (array->data != NULL) - free(array->data); -} - -static bool -equal(void *ptr1, void *ptr2) -{ - CFWObject *obj2 = ptr2; - CFWArray *array1, *array2; - size_t i; - - if (obj2->cls != cfw_array) - return false; - - array1 = ptr1; - array2 = ptr2; - - if (array1->size != array2->size) - return false; - - for (i = 0; i < array1->size; i++) - if (cfw_equal(array1->data[i], array2->data[i])) - return false; - - return true; -} - -static void* -copy(void *ptr) -{ - CFWArray *array = ptr; - CFWArray *new; - size_t i; - - if ((new = cfw_new(cfw_array)) == NULL) - return NULL; - - if ((new->data = malloc(sizeof(void*) * array->size)) == NULL) { - cfw_unref(new); - return NULL; - } - new->size = array->size; - - for (i = 0; i < array->size; i++) - new->data[i] = cfw_ref(array->data[i]); - - return new; -} - -void* -cfw_array_get(CFWArray *array, size_t index) -{ - if (index >= array->size) - return NULL; - - return array->data[index]; -} - -size_t -cfw_array_size(CFWArray *array) -{ - return array->size; -} - -bool -cfw_array_set(CFWArray *array, size_t index, void *ptr) -{ - CFWObject *obj = ptr; - CFWObject *old; - - if (index >= array->size) - return false; - - cfw_ref(obj); - old = array->data[index]; - array->data[index] = obj; - cfw_unref(old); - - return true; -} - -bool -cfw_array_push(CFWArray *array, void *ptr) -{ - CFWObject *obj = ptr; - void **new; - - if (array->data == NULL) - new = malloc(sizeof(void*)); - else - new = realloc(array->data, sizeof(void*) * (array->size + 1)); - - if (new == NULL) - return false; - - new[array->size] = cfw_ref(obj); - - array->data = new; - array->size++; - - return true; -} - -void* -cfw_array_last(CFWArray *array) -{ - if (array->size == 0) - return NULL; - - return array->data[array->size - 1]; -} - -bool -cfw_array_pop(CFWArray *array) -{ - void **new; - void *last; - - if (array->size == 0) - return NULL; - - if (array->size == 1) { - cfw_unref(array->data[0]); - free(array->data); - array->data = NULL; - array->size = 0; - return true; - } - - last = array->data[array->size - 1]; - - new = realloc(array->data, sizeof(void*) * (array->size - 1)); - if (new == NULL) - return false; - - cfw_unref(last); - - array->data = new; - array->size--; - - return true; -} - -bool -cfw_array_contains(CFWArray *array, void *ptr) -{ - size_t i; - - for (i = 0; i < array->size; i++) - if (cfw_equal(array->data[i], ptr)) - return true; - - return false; -} - -bool -cfw_array_contains_ptr(CFWArray *array, void *ptr) -{ - size_t i; - - for (i = 0; i < array->size; i++) - if (array->data[i] == ptr) - return true; - - return false; -} - -size_t -cfw_array_find(CFWArray *array, void *ptr) -{ - size_t i; - - for (i = 0; i < array->size; i++) - if (cfw_equal(array->data[i], ptr)) - return i; - - return SIZE_MAX; -} - -size_t -cfw_array_find_ptr(CFWArray *array, void *ptr) -{ - size_t i; - - for (i = 0; i < array->size; i++) - if (array->data[i] == ptr) - return i; - - return SIZE_MAX; -} - -static CFWClass class = { - .name = "CFWArray", - .size = sizeof(CFWArray), - .ctor = ctor, - .dtor = dtor, - .equal = equal, - .copy = copy -}; -CFWClass *cfw_array = &class; DELETED src/cfwarray.h Index: src/cfwarray.h ================================================================== --- src/cfwarray.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2012, Jonathan Schleifer - * 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 OWNER 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 __CFWARRAY_H__ -#define __CFWARRAY_H__ - -#include "cfwclass.h" - -typedef struct CFWArray CFWArray; -extern CFWClass *cfw_array; -extern size_t cfw_array_size(CFWArray*); -extern void* cfw_array_get(CFWArray*, size_t); -extern bool cfw_array_set(CFWArray*, size_t, void*); -extern bool cfw_array_push(CFWArray*, void*); -extern void* cfw_array_last(CFWArray*); -extern bool cfw_array_pop(CFWArray*); -extern bool cfw_array_contains(CFWArray*, void*); -extern bool cfw_array_contains_ptr(CFWArray*, void*); -extern size_t cfw_array_find(CFWArray*, void*); -extern size_t cfw_array_find_ptr(CFWArray*, void*); - -#endif DELETED src/cfwclass.c Index: src/cfwclass.c ================================================================== --- src/cfwclass.c +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2012, Jonathan Schleifer - * 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 OWNER 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 "cfwclass.h" - -const char* -cfw_class_name(CFWClass *cls) -{ - return cls->name; -} DELETED src/cfwclass.h Index: src/cfwclass.h ================================================================== --- src/cfwclass.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2012, Jonathan Schleifer - * 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 OWNER 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 __CFWCLASS_H__ -#define __CFWCLASS_H__ - -#include -#include -#include - -typedef struct CFWClass { - const char *name; - size_t size; - bool (*ctor)(void*, va_list args); - void (*dtor)(void*); - bool (*equal)(void*, void*); - void* (*copy)(void*); -} CFWClass; - -extern const char* cfw_class_name(CFWClass*); - -#endif DELETED src/cfwobject.c Index: src/cfwobject.c ================================================================== --- src/cfwobject.c +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (c) 2012, Jonathan Schleifer - * 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 OWNER 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 - -#include "cfwobject.h" - -void* -cfw_new(CFWClass *class, ...) -{ - CFWObject *obj; - - if ((obj = malloc(class->size)) == NULL) - return NULL; - - obj->cls = class; - obj->ref_cnt = 1; - - if (class->ctor != NULL) { - va_list args; - va_start(args, class); - - if (!class->ctor(obj, args)) { - cfw_unref(obj); - return NULL; - } - - va_end(args); - } - - return obj; -} - -void* -cfw_ref(void *ptr) -{ - CFWObject *obj = ptr; - - obj->ref_cnt++; - - return obj; -} - -void -cfw_unref(void *ptr) -{ - CFWObject *obj = ptr; - - if (--obj->ref_cnt == 0) - cfw_free(obj); -} - -void -cfw_free(void *ptr) -{ - CFWObject *obj = ptr; - - if (obj->cls->dtor != NULL) - obj->cls->dtor(obj); - - free(obj); -} - -bool -cfw_equal(void *ptr1, void *ptr2) -{ - CFWObject *obj1 = ptr1, *obj2 = ptr2; - - if (obj1->cls->equal != NULL) { - return obj1->cls->equal(obj1, obj2); - } else - return (obj1 == obj2); -} - -void* -cfw_copy(void *ptr) -{ - CFWObject *obj = ptr; - - if (obj->cls->copy != NULL) - return obj->cls->copy(obj); - else - return NULL; -} - -static CFWClass class = { - .name = "CFWObject", - .size = sizeof(CFWObject), -}; -CFWClass *cfw_object = &class; DELETED src/cfwobject.h Index: src/cfwobject.h ================================================================== --- src/cfwobject.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2012, Jonathan Schleifer - * 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 OWNER 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 __CFWOBJECT_H__ -#define __CFWOBJECT_H__ - -#include "cfwclass.h" - -typedef struct CFWObject { - CFWClass *cls; - int ref_cnt; -} CFWObject; - -extern CFWClass *cfw_object; -extern void* cfw_new(CFWClass*, ...); -extern void* cfw_ref(void*); -extern void cfw_unref(void*); -extern void cfw_free(void*); -extern bool cfw_equal(void*, void*); -extern void* cfw_copy(void*); - -#endif DELETED src/cfwrange.c Index: src/cfwrange.c ================================================================== --- src/cfwrange.c +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2012, Jonathan Schleifer - * 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 OWNER 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 -#include - -#include "cfwrange.h" - -cfw_range_t cfw_range_all = { 0, SIZE_MAX }; - -cfw_range_t -cfw_range(size_t start, size_t length) -{ - cfw_range_t range = { start, length }; - - return range; -} DELETED src/cfwrange.h Index: src/cfwrange.h ================================================================== --- src/cfwrange.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2012, Jonathan Schleifer - * 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 OWNER 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 __CFWRANGE_H__ -#define __CFW_RANGE_H__ - -typedef struct cfw_range_t { - size_t start; - size_t length; -} cfw_range_t; - -extern cfw_range_t cfw_range_all; -extern cfw_range_t cfw_range(size_t, size_t); - -#endif DELETED src/cfwstring.c Index: src/cfwstring.c ================================================================== --- src/cfwstring.c +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Copyright (c) 2012, Jonathan Schleifer - * 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 OWNER 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 -#include - -#include "cfwobject.h" -#include "cfwstring.h" - -struct CFWString { - CFWObject obj; - char *data; - size_t len; -}; - -static bool -ctor(void *ptr, va_list args) -{ - CFWString *str = ptr; - const char *cstr = va_arg(args, const char*); - - if (cstr != NULL) { - if ((str->data = strdup(cstr)) == NULL) - return false; - - str->len = strlen(cstr); - } else { - str->data = NULL; - str->len = 0; - } - - return true; -} - -static void -dtor(void *ptr) -{ - CFWString *str = ptr; - - if (str->data != NULL) - free(str->data); -} - -static bool -equal(void *ptr1, void *ptr2) -{ - CFWObject *obj2 = ptr2; - CFWString *str1, *str2; - - if (obj2->cls != cfw_string) - return false; - - str1 = ptr1; - str2 = ptr2; - - if (str1->len != str2->len) - return false; - - return !memcmp(str1->data, str2->data, str1->len); -} - -static void* -copy(void *ptr) -{ - CFWString *str = ptr; - CFWString *new; - - if ((new = cfw_new(cfw_string)) == NULL) - return NULL; - - if ((new->data = malloc(str->len + 1)) == NULL) { - cfw_unref(new); - return NULL; - } - new->len = str->len; - - memcpy(new->data, str->data, str->len + 1); - - return new; -} - -const char* -cfw_string_c(CFWString *str) -{ - return str->data; -} - -size_t -cfw_string_len(CFWString *string) -{ - return string->len; -} - -bool -cfw_string_set(CFWString *str, const char *cstr) -{ - char *copy; - - if ((copy = strdup(cstr)) == NULL) - return false; - - if (str->data != NULL) - free(str->data); - - str->data = copy; - str->len = strlen(copy); - - return true; -} - -bool -cfw_string_append(CFWString *str, CFWString *append) -{ - char *new; - - if ((new = realloc(str->data, str->len + append->len + 1)) == NULL) - return false; - - memcpy(new + str->len, append->data, append->len); - new[str->len + append->len] = 0; - - str->data = new; - str->len += append->len; - - return true; -} - -static CFWClass class = { - .name = "CFWString", - .size = sizeof(CFWString), - .ctor = ctor, - .dtor = dtor, - .equal = equal, - .copy = copy -}; -CFWClass *cfw_string = &class; DELETED src/cfwstring.h Index: src/cfwstring.h ================================================================== --- src/cfwstring.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2012, Jonathan Schleifer - * 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 OWNER 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 __CFWSTRING_H__ -#define __CFWSTRING_H__ - -#include "cfwclass.h" - -typedef struct CFWString CFWString; -extern CFWClass *cfw_string; -extern const char* cfw_string_c(CFWString*); -extern size_t cfw_string_len(CFWString*); -extern bool cfw_string_set(CFWString*, const char*); -extern bool cfw_string_append(CFWString*, CFWString*); - -#endif ADDED src/class.c Index: src/class.c ================================================================== --- /dev/null +++ src/class.c @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2012, Jonathan Schleifer + * 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 OWNER 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 "class.h" + +const char* +cfw_class_name(CFWClass *cls) +{ + return cls->name; +} ADDED src/class.h Index: src/class.h ================================================================== --- /dev/null +++ src/class.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2012, Jonathan Schleifer + * 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 OWNER 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 __CFWCLASS_H__ +#define __CFWCLASS_H__ + +#include +#include +#include + +typedef struct CFWClass { + const char *name; + size_t size; + bool (*ctor)(void*, va_list args); + void (*dtor)(void*); + bool (*equal)(void*, void*); + void* (*copy)(void*); +} CFWClass; + +extern const char* cfw_class_name(CFWClass*); + +#endif Index: src/corefw.h ================================================================== --- src/corefw.h +++ src/corefw.h @@ -1,9 +1,9 @@ #ifndef __COREFW_H__ #define __COREFW_H__ -#include "cfwclass.h" -#include "cfwobject.h" -#include "cfwstring.h" -#include "cfwarray.h" +#include "class.h" +#include "object.h" +#include "string.h" +#include "array.h" #endif ADDED src/object.c Index: src/object.c ================================================================== --- /dev/null +++ src/object.c @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2012, Jonathan Schleifer + * 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 OWNER 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 + +#include "object.h" + +void* +cfw_new(CFWClass *class, ...) +{ + CFWObject *obj; + + if ((obj = malloc(class->size)) == NULL) + return NULL; + + obj->cls = class; + obj->ref_cnt = 1; + + if (class->ctor != NULL) { + va_list args; + va_start(args, class); + + if (!class->ctor(obj, args)) { + cfw_unref(obj); + return NULL; + } + + va_end(args); + } + + return obj; +} + +void* +cfw_ref(void *ptr) +{ + CFWObject *obj = ptr; + + obj->ref_cnt++; + + return obj; +} + +void +cfw_unref(void *ptr) +{ + CFWObject *obj = ptr; + + if (--obj->ref_cnt == 0) + cfw_free(obj); +} + +void +cfw_free(void *ptr) +{ + CFWObject *obj = ptr; + + if (obj->cls->dtor != NULL) + obj->cls->dtor(obj); + + free(obj); +} + +bool +cfw_equal(void *ptr1, void *ptr2) +{ + CFWObject *obj1 = ptr1, *obj2 = ptr2; + + if (obj1->cls->equal != NULL) { + return obj1->cls->equal(obj1, obj2); + } else + return (obj1 == obj2); +} + +void* +cfw_copy(void *ptr) +{ + CFWObject *obj = ptr; + + if (obj->cls->copy != NULL) + return obj->cls->copy(obj); + else + return NULL; +} + +static CFWClass class = { + .name = "CFWObject", + .size = sizeof(CFWObject), +}; +CFWClass *cfw_object = &class; ADDED src/object.h Index: src/object.h ================================================================== --- /dev/null +++ src/object.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2012, Jonathan Schleifer + * 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 OWNER 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 __CFWOBJECT_H__ +#define __CFWOBJECT_H__ + +#include "class.h" + +typedef struct CFWObject { + CFWClass *cls; + int ref_cnt; +} CFWObject; + +extern CFWClass *cfw_object; +extern void* cfw_new(CFWClass*, ...); +extern void* cfw_ref(void*); +extern void cfw_unref(void*); +extern void cfw_free(void*); +extern bool cfw_equal(void*, void*); +extern void* cfw_copy(void*); + +#endif ADDED src/range.c Index: src/range.c ================================================================== --- /dev/null +++ src/range.c @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2012, Jonathan Schleifer + * 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 OWNER 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 +#include + +#include "range.h" + +cfw_range_t cfw_range_all = { 0, SIZE_MAX }; + +cfw_range_t +cfw_range(size_t start, size_t length) +{ + cfw_range_t range = { start, length }; + + return range; +} ADDED src/range.h Index: src/range.h ================================================================== --- /dev/null +++ src/range.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2012, Jonathan Schleifer + * 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 OWNER 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 __CFWRANGE_H__ +#define __CFW_RANGE_H__ + +typedef struct cfw_range_t { + size_t start; + size_t length; +} cfw_range_t; + +extern cfw_range_t cfw_range_all; +extern cfw_range_t cfw_range(size_t, size_t); + +#endif ADDED src/string.c Index: src/string.c ================================================================== --- /dev/null +++ src/string.c @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2012, Jonathan Schleifer + * 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 OWNER 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 +#include + +#include "object.h" +#include "string.h" + +struct CFWString { + CFWObject obj; + char *data; + size_t len; +}; + +static bool +ctor(void *ptr, va_list args) +{ + CFWString *str = ptr; + const char *cstr = va_arg(args, const char*); + + if (cstr != NULL) { + if ((str->data = strdup(cstr)) == NULL) + return false; + + str->len = strlen(cstr); + } else { + str->data = NULL; + str->len = 0; + } + + return true; +} + +static void +dtor(void *ptr) +{ + CFWString *str = ptr; + + if (str->data != NULL) + free(str->data); +} + +static bool +equal(void *ptr1, void *ptr2) +{ + CFWObject *obj2 = ptr2; + CFWString *str1, *str2; + + if (obj2->cls != cfw_string) + return false; + + str1 = ptr1; + str2 = ptr2; + + if (str1->len != str2->len) + return false; + + return !memcmp(str1->data, str2->data, str1->len); +} + +static void* +copy(void *ptr) +{ + CFWString *str = ptr; + CFWString *new; + + if ((new = cfw_new(cfw_string)) == NULL) + return NULL; + + if ((new->data = malloc(str->len + 1)) == NULL) { + cfw_unref(new); + return NULL; + } + new->len = str->len; + + memcpy(new->data, str->data, str->len + 1); + + return new; +} + +const char* +cfw_string_c(CFWString *str) +{ + return str->data; +} + +size_t +cfw_string_len(CFWString *string) +{ + return string->len; +} + +bool +cfw_string_set(CFWString *str, const char *cstr) +{ + char *copy; + + if ((copy = strdup(cstr)) == NULL) + return false; + + if (str->data != NULL) + free(str->data); + + str->data = copy; + str->len = strlen(copy); + + return true; +} + +bool +cfw_string_append(CFWString *str, CFWString *append) +{ + char *new; + + if ((new = realloc(str->data, str->len + append->len + 1)) == NULL) + return false; + + memcpy(new + str->len, append->data, append->len); + new[str->len + append->len] = 0; + + str->data = new; + str->len += append->len; + + return true; +} + +static CFWClass class = { + .name = "CFWString", + .size = sizeof(CFWString), + .ctor = ctor, + .dtor = dtor, + .equal = equal, + .copy = copy +}; +CFWClass *cfw_string = &class; ADDED src/string.h Index: src/string.h ================================================================== --- /dev/null +++ src/string.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2012, Jonathan Schleifer + * 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 OWNER 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 __CFWSTRING_H__ +#define __CFWSTRING_H__ + +#include "class.h" + +typedef struct CFWString CFWString; +extern CFWClass *cfw_string; +extern const char* cfw_string_c(CFWString*); +extern size_t cfw_string_len(CFWString*); +extern bool cfw_string_set(CFWString*, const char*); +extern bool cfw_string_append(CFWString*, CFWString*); + +#endif