Merge remote-tracking branch 'upstream/master'

Conflicts:
	src/cfwstring.c
	src/cfwstring.h
This commit is contained in:
Jos Kuijpers 2012-04-08 22:00:36 +02:00
commit 603ac44d1a
12 changed files with 164 additions and 30 deletions

View file

@ -1,4 +1,4 @@
AC_INIT(CoreFW, 0.1, js@webkeks.org)
AC_INIT(corefw, 0.1, js@webkeks.org)
AC_CONFIG_SRCDIR(src)
AC_CANONICAL_HOST
@ -10,6 +10,8 @@ AC_PROG_RANLIB
AC_PROG_LN_S
AC_PROG_INSTALL
CFLAGS="$CFLAGS -Wall"
BUILDSYS_SHARED_LIB
BUILDSYS_INIT
BUILDSYS_TOUCH_DEPS

View file

@ -4,7 +4,11 @@ LIB_MAJOR = 0
LIB_MINOR = 0
SRCS = cfwarray.c \
cfwclass.c \
cfwobject.c \
cfwstring.c
INCLUDES = ${SRCS:.c=.h} \
corefw.h
include ../buildsys.mk

View file

@ -25,6 +25,7 @@
*/
#include <stdlib.h>
#include <stdint.h>
#include "cfwobject.h"
#include "cfwarray.h"
@ -71,7 +72,7 @@ equal(void *ptr1, void *ptr2)
CFWArray *array1, *array2;
size_t i;
if (obj2->clsptr != cfw_array)
if (obj2->cls != cfw_array)
return false;
array1 = ptr1;
@ -203,6 +204,54 @@ cfw_array_pop(CFWArray *array)
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),

View file

@ -37,5 +37,9 @@ 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

33
src/cfwclass.c Normal file
View file

@ -0,0 +1,33 @@
/*
* 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 "cfwclass.h"
const char*
cfw_class_name(CFWClass *cls)
{
return cls->name;
}

View file

@ -40,4 +40,6 @@ typedef struct CFWClass {
void* (*copy)(void*);
} CFWClass;
extern const char* cfw_class_name(CFWClass*);
#endif

View file

@ -36,7 +36,7 @@ cfw_new(CFWClass *class, ...)
if ((obj = malloc(class->size)) == NULL)
return NULL;
obj->clsptr = class;
obj->cls = class;
obj->ref_cnt = 1;
if (class->ctor != NULL) {
@ -78,8 +78,8 @@ cfw_free(void *ptr)
{
CFWObject *obj = ptr;
if (obj->clsptr->dtor != NULL)
obj->clsptr->dtor(obj);
if (obj->cls->dtor != NULL)
obj->cls->dtor(obj);
free(obj);
}
@ -89,8 +89,8 @@ cfw_equal(void *ptr1, void *ptr2)
{
CFWObject *obj1 = ptr1, *obj2 = ptr2;
if (obj1->clsptr->equal != NULL) {
return obj1->clsptr->equal(obj1, obj2);
if (obj1->cls->equal != NULL) {
return obj1->cls->equal(obj1, obj2);
} else
return (obj1 == obj2);
}
@ -100,8 +100,8 @@ cfw_copy(void *ptr)
{
CFWObject *obj = ptr;
if (obj->clsptr->copy != NULL)
return obj->clsptr->copy(obj);
if (obj->cls->copy != NULL)
return obj->cls->copy(obj);
else
return NULL;
}

View file

@ -30,7 +30,7 @@
#include "cfwclass.h"
typedef struct CFWObject {
CFWClass *clsptr;
CFWClass *cls;
int ref_cnt;
} CFWObject;

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2012, Jonathan Schleifer <js@webkeks.org>
* Copyright (c) 2012, Jos Kuijpers <jos@kuijpersvof.nl>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -32,7 +33,7 @@
struct CFWString {
CFWObject obj;
char *cstr;
char *data;
size_t len;
};
@ -43,12 +44,12 @@ ctor(void *ptr, va_list args)
const char *cstr = va_arg(args, const char*);
if (cstr != NULL) {
if ((str->cstr = strdup(cstr)) == NULL)
if ((str->data = strdup(cstr)) == NULL)
return false;
str->len = strlen(cstr);
} else {
str->cstr = NULL;
str->data = NULL;
str->len = 0;
}
@ -60,8 +61,8 @@ dtor(void *ptr)
{
CFWString *str = ptr;
if (str->cstr != NULL)
free(str->cstr);
if (str->data != NULL)
free(str->data);
}
static bool
@ -70,7 +71,7 @@ equal(void *ptr1, void *ptr2)
CFWObject *obj2 = ptr2;
CFWString *str1, *str2;
if (obj2->clsptr != cfw_string)
if (obj2->cls != cfw_string)
return false;
str1 = ptr1;
@ -79,7 +80,7 @@ equal(void *ptr1, void *ptr2)
if (str1->len != str2->len)
return false;
return !strcmp(str1->cstr, str2->cstr);
return !memcmp(str1->data, str2->data, str1->len);
}
static void*
@ -91,21 +92,21 @@ copy(void *ptr)
if ((new = cfw_new(cfw_string)) == NULL)
return NULL;
if ((new->cstr = malloc(str->len + 1)) == NULL) {
if ((new->data = malloc(str->len + 1)) == NULL) {
cfw_unref(new);
return NULL;
}
new->len = str->len;
memcpy(new->cstr, str->cstr, str->len + 1);
memcpy(new->data, str->data, str->len + 1);
return new;
}
const char*
cfw_string_c(CFWString *string)
cfw_string_c(CFWString *str)
{
return string->cstr;
return str->data;
}
size_t
@ -122,10 +123,10 @@ cfw_string_set(CFWString *str, const char *cstr)
if ((copy = strdup(cstr)) == NULL)
return false;
if (str->cstr != NULL)
free(str->cstr);
if (str->data != NULL)
free(str->data);
str->cstr = copy;
str->data = copy;
str->len = strlen(copy);
return true;
@ -135,14 +136,14 @@ cfw_unichar
cfw_string_char(CFWString *str, size_t index)
{
if(len > index)
return str->cstr[index];
return str->data[index];
return NULL;
}
size_t
cfw_string_find(CFWString *strA, CFWString *strB, cfw_range_t range)
{
char *cstrA = strA->cstr+range.location;
char *cstrA = strA->data+range.location;
size_t i, max = MIN(range.length+range.location, strB->len);
if(strA->len == 0)
@ -155,13 +156,30 @@ cfw_string_find(CFWString *strA, CFWString *strB, cfw_range_t range)
i <= strA->len-strB->len && i <= max;
i++)
{
if(!memcmp(strA->cstr+i, strB->cstr, max-i))
if(!memcmp(strA->data+i, strB->data, max-i))
return i;
}
return SIZE_MAX;
}
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),

View file

@ -1,6 +1,6 @@
/*
* Copyright (c) 2012, Jonathan Schleifer <js@webkeks.org>
* All rights reserved.
* Copyright (c) 2012, Jos Kuijpers <jos@kuijpersvof.nl>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@ -39,5 +39,6 @@ extern size_t cfw_string_len(CFWString*);
extern bool cfw_string_set(CFWString*, const char*);
extern cfw_unichar cfw_string_char(CFWString*, size_t);
extern size_t cfw_string_find(CFWString*, CFWString*, cfw_range_t);
extern bool cfw_string_append(CFWString*, CFWString*);
#endif

9
src/corefw.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef __COREFW_H__
#define __COREFW_H__
#include "cfwclass.h"
#include "cfwobject.h"
#include "cfwstring.h"
#include "cfwarray.h"
#endif

View file

@ -38,13 +38,25 @@ main()
size_t i;
s[0] = cfw_new(cfw_string, "Hallo");
s[1] = cfw_new(cfw_string, "Welt");
s[1] = cfw_new(cfw_string, " Welt");
s[2] = cfw_new(cfw_string, "!");
a = cfw_new(cfw_array, s[0], s[1], s[2], NULL);
cfw_unref(s[0]);
cfw_unref(s[1]);
cfw_unref(s[2]);
s[0] = cfw_new(cfw_string, NULL);
for (i = 0; i < cfw_array_size(a); i++)
puts(cfw_string_c(cfw_array_get(a, i)));
cfw_string_append(s[0], cfw_array_get(a, i));
cfw_unref(a);
puts(cfw_string_c(s[0]));
cfw_unref(s[0]);
return 0;
}