Initial import.
This commit is contained in:
commit
8603645700
8 changed files with 674 additions and 0 deletions
207
cfwarray.c
Normal file
207
cfwarray.c
Normal file
|
@ -0,0 +1,207 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Jonathan Schleifer <js@webkeks.org>
|
||||||
|
* 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 <stdlib.h>
|
||||||
|
|
||||||
|
#include "cfwobject.h"
|
||||||
|
#include "cfwarray.h"
|
||||||
|
|
||||||
|
struct CFWArray {
|
||||||
|
CFWObject obj;
|
||||||
|
void **data;
|
||||||
|
size_t size;
|
||||||
|
};
|
||||||
|
|
||||||
|
static void
|
||||||
|
ctor(void *ptr)
|
||||||
|
{
|
||||||
|
CFWArray *array = ptr;
|
||||||
|
|
||||||
|
array->data = NULL;
|
||||||
|
array->size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
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->clsptr != 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
static CFWClass class = {
|
||||||
|
.name = "CFWArray",
|
||||||
|
.size = sizeof(CFWArray),
|
||||||
|
.ctor = ctor,
|
||||||
|
.dtor = dtor,
|
||||||
|
.equal = equal,
|
||||||
|
.copy = copy
|
||||||
|
};
|
||||||
|
CFWClass *cfw_array = &class;
|
41
cfwarray.h
Normal file
41
cfwarray.h
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Jonathan Schleifer <js@webkeks.org>
|
||||||
|
* 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*);
|
||||||
|
|
||||||
|
#endif
|
42
cfwclass.h
Normal file
42
cfwclass.h
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Jonathan Schleifer <js@webkeks.org>
|
||||||
|
* 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 <stddef.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
typedef struct CFWClass {
|
||||||
|
const char *name;
|
||||||
|
size_t size;
|
||||||
|
void (*ctor)(void*);
|
||||||
|
void (*dtor)(void*);
|
||||||
|
bool (*equal)(void*, void*);
|
||||||
|
void* (*copy)(void*);
|
||||||
|
} CFWClass;
|
||||||
|
|
||||||
|
#endif
|
104
cfwobject.c
Normal file
104
cfwobject.c
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Jonathan Schleifer <js@webkeks.org>
|
||||||
|
* 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 <stdlib.h>
|
||||||
|
|
||||||
|
#include "cfwobject.h"
|
||||||
|
|
||||||
|
void*
|
||||||
|
cfw_new(CFWClass *class)
|
||||||
|
{
|
||||||
|
CFWObject *obj;
|
||||||
|
|
||||||
|
if ((obj = malloc(class->size)) == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
obj->clsptr = class;
|
||||||
|
obj->ref_cnt = 1;
|
||||||
|
|
||||||
|
if (class->ctor != NULL)
|
||||||
|
class->ctor(obj);
|
||||||
|
|
||||||
|
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->clsptr->dtor != NULL)
|
||||||
|
obj->clsptr->dtor(obj);
|
||||||
|
|
||||||
|
free(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
cfw_equal(void *ptr1, void *ptr2)
|
||||||
|
{
|
||||||
|
CFWObject *obj1 = ptr1, *obj2 = ptr2;
|
||||||
|
|
||||||
|
if (obj1->clsptr->equal != NULL) {
|
||||||
|
return obj1->clsptr->equal(obj1, obj2);
|
||||||
|
} else
|
||||||
|
return (obj1 == obj2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void*
|
||||||
|
cfw_copy(void *ptr)
|
||||||
|
{
|
||||||
|
CFWObject *obj = ptr;
|
||||||
|
|
||||||
|
if (obj->clsptr->copy != NULL)
|
||||||
|
return obj->clsptr->copy(obj);
|
||||||
|
else
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static CFWClass class = {
|
||||||
|
.name = "CFWObject",
|
||||||
|
.size = sizeof(CFWObject),
|
||||||
|
};
|
||||||
|
CFWClass *cfw_object = &class;
|
45
cfwobject.h
Normal file
45
cfwobject.h
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Jonathan Schleifer <js@webkeks.org>
|
||||||
|
* 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 *clsptr;
|
||||||
|
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
|
132
cfwstring.c
Normal file
132
cfwstring.c
Normal file
|
@ -0,0 +1,132 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Jonathan Schleifer <js@webkeks.org>
|
||||||
|
* 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 <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "cfwobject.h"
|
||||||
|
#include "cfwstring.h"
|
||||||
|
|
||||||
|
struct CFWString {
|
||||||
|
CFWObject obj;
|
||||||
|
char *cstr;
|
||||||
|
size_t len;
|
||||||
|
};
|
||||||
|
|
||||||
|
static void
|
||||||
|
ctor(void *ptr)
|
||||||
|
{
|
||||||
|
CFWString *str = ptr;
|
||||||
|
|
||||||
|
str->cstr = NULL;
|
||||||
|
str->len = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
dtor(void *ptr)
|
||||||
|
{
|
||||||
|
CFWString *str = ptr;
|
||||||
|
|
||||||
|
if (str->cstr != NULL)
|
||||||
|
free(str->cstr);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
equal(void *ptr1, void *ptr2)
|
||||||
|
{
|
||||||
|
CFWObject *obj2 = ptr2;
|
||||||
|
CFWString *str1, *str2;
|
||||||
|
|
||||||
|
if (obj2->clsptr != cfw_string)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
str1 = ptr1;
|
||||||
|
str2 = ptr2;
|
||||||
|
|
||||||
|
if (str1->len != str2->len)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return !strcmp(str1->cstr, str2->cstr);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void*
|
||||||
|
copy(void *ptr)
|
||||||
|
{
|
||||||
|
CFWString *str = ptr;
|
||||||
|
CFWString *new;
|
||||||
|
|
||||||
|
if ((new = cfw_new(cfw_string)) == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if ((new->cstr = malloc(str->len + 1)) == NULL) {
|
||||||
|
cfw_unref(new);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
new->len = str->len;
|
||||||
|
|
||||||
|
memcpy(new->cstr, str->cstr, str->len + 1);
|
||||||
|
|
||||||
|
return new;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char*
|
||||||
|
cfw_string_c(CFWString *string)
|
||||||
|
{
|
||||||
|
return string->cstr;
|
||||||
|
}
|
||||||
|
|
||||||
|
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->cstr != NULL)
|
||||||
|
free(str->cstr);
|
||||||
|
|
||||||
|
str->cstr = copy;
|
||||||
|
str->len = strlen(copy);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static CFWClass class = {
|
||||||
|
.name = "CFWString",
|
||||||
|
.size = sizeof(CFWString),
|
||||||
|
.ctor = ctor,
|
||||||
|
.dtor = dtor,
|
||||||
|
.equal = equal,
|
||||||
|
.copy = copy
|
||||||
|
};
|
||||||
|
CFWClass *cfw_string = &class;
|
38
cfwstring.h
Normal file
38
cfwstring.h
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Jonathan Schleifer <js@webkeks.org>
|
||||||
|
* 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*);
|
||||||
|
|
||||||
|
#endif
|
65
test.c
Normal file
65
test.c
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Jonathan Schleifer <js@webkeks.org>
|
||||||
|
* 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 <stdio.h>
|
||||||
|
|
||||||
|
#include "cfwobject.h"
|
||||||
|
#include "cfwstring.h"
|
||||||
|
#include "cfwarray.h"
|
||||||
|
|
||||||
|
int
|
||||||
|
main()
|
||||||
|
{
|
||||||
|
CFWArray *a;
|
||||||
|
CFWString *s;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
a = cfw_new(cfw_array);
|
||||||
|
|
||||||
|
for (i = 0; i < 3; i++) {
|
||||||
|
s = cfw_new(cfw_string);
|
||||||
|
|
||||||
|
switch (i) {
|
||||||
|
case 0:
|
||||||
|
cfw_string_set(s, "Hello");
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
cfw_string_set(s, "World");
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
cfw_string_set(s, "!");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
cfw_array_push(a, s);
|
||||||
|
cfw_unref(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < cfw_array_size(a); i++)
|
||||||
|
puts(cfw_string_c(cfw_array_get(a, i)));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue