mirror of
https://github.com/sigmasternchen/crap-libs
synced 2025-03-15 07:38:56 +00:00
first version
This commit is contained in:
parent
413730fadc
commit
2dd30f478e
9 changed files with 322 additions and 0 deletions
30
oop/Makefile
Normal file
30
oop/Makefile
Normal file
|
@ -0,0 +1,30 @@
|
|||
#
|
||||
# Makefile for crap-libs/oop
|
||||
#
|
||||
# Author: overflowerror
|
||||
#
|
||||
|
||||
CC = gcc
|
||||
DEFS = -D_XOPEN_SOURCE=500 -D_BSD_SOURCE
|
||||
CFLAGS = -Wall -Wextra -g -std=c99 -pedantic -DENDEBUG $(DEFS) -fPIC
|
||||
LDFLAGS = $(DEFS)
|
||||
LIBFLAGS = -shared $(DEFS)
|
||||
|
||||
.PHONY: all clean
|
||||
|
||||
all: example liboop.so
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
example: example.o oop.o
|
||||
$(CC) $(LDFLAGS) -o $@ $^
|
||||
|
||||
liboop.so: oop.o
|
||||
$(CC) $(LIBFLAGS) -o $@ $^
|
||||
|
||||
oop.o: oop.c oop.h
|
||||
example.o: example.c oop.h
|
||||
|
||||
clean:
|
||||
rm -f *.o example liboop.so
|
46
oop/example.c
Normal file
46
oop/example.c
Normal file
|
@ -0,0 +1,46 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "oop.h"
|
||||
|
||||
#define test construct(test)
|
||||
|
||||
class_t test_class;
|
||||
|
||||
__attribute__ ((constructor))
|
||||
static void add_test_class(void) {
|
||||
test_class.id = new_class("test", NO_SUPER_CLASS, true);
|
||||
}
|
||||
|
||||
typedef struct test {
|
||||
meta_object_t meta;
|
||||
void (*destruct)(struct test*);
|
||||
const char* string;
|
||||
void (*print)(struct test*);
|
||||
} test_t;
|
||||
|
||||
void test_method_print(test_t* this) {
|
||||
printf("%s\n", this->string);
|
||||
}
|
||||
void test_method_destruct(test_t* this) {
|
||||
free(this);
|
||||
}
|
||||
|
||||
test_t* test_method_construct(const char* string) {
|
||||
test_t* obj = malloc(sizeof(test_t));
|
||||
obj->string = string;
|
||||
obj->destruct = test_method_destruct;
|
||||
obj->print = test_method_print;
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
test_t* obj = new test("Hallo Welt");
|
||||
|
||||
obj->print(obj);
|
||||
|
||||
obj->destruct(obj);
|
||||
|
||||
return 0;
|
||||
}
|
47
oop/oop.c
Normal file
47
oop/oop.c
Normal file
|
@ -0,0 +1,47 @@
|
|||
#include "oop.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
class_id_t class_ids = 0;
|
||||
|
||||
meta_class_t classes[MAX_CLASSES];
|
||||
|
||||
class_id_t new_class(const char* name, class_id_t super, bool instanceable) {
|
||||
classes[class_ids] = (meta_class_t) {
|
||||
.name = name,
|
||||
.super = super,
|
||||
.instanceable = instanceable
|
||||
};
|
||||
return class_ids++;
|
||||
}
|
||||
|
||||
bool instance_of_id(void* object, class_id_t id) {
|
||||
if (sizeof (object) < sizeof (object_t))
|
||||
return false; // not an object
|
||||
object_t* obj = (object_t*) object;
|
||||
return obj->meta_obj.class.id == id;
|
||||
}
|
||||
|
||||
bool instance_of(void* object, class_t class) {
|
||||
return instance_of_id(object, class.id);
|
||||
}
|
||||
|
||||
const char* get_class_name_by_id(class_id_t id) {
|
||||
return classes[id].name;
|
||||
}
|
||||
|
||||
const char* get_class_name(class_t class) {
|
||||
return get_class_name_by_id(class.id);
|
||||
}
|
||||
|
||||
class_id_t id_from_name(const char* class_name) {
|
||||
for (int i = 0; i < class_ids; i++) {
|
||||
if (strcmp(class_name, classes[i].name) == 0)
|
||||
return i;
|
||||
}
|
||||
return NO_CLASS;
|
||||
}
|
||||
|
||||
bool class_exists(const char* class_name) {
|
||||
return id_from_name(class_name) != NO_CLASS;
|
||||
}
|
44
oop/oop.h
Normal file
44
oop/oop.h
Normal file
|
@ -0,0 +1,44 @@
|
|||
#ifndef OOP_H
|
||||
#define OOP_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#define construct(name) name ##_method_construct
|
||||
|
||||
#define call(obj, method) (obj)->method((obj))
|
||||
#define new
|
||||
|
||||
#define MAX_CLASSES 1024
|
||||
#define NO_SUPER_CLASS -1
|
||||
#define NO_CLASS -1
|
||||
|
||||
typedef int class_id_t;
|
||||
|
||||
typedef struct meta_class {
|
||||
const char* name;
|
||||
class_id_t super;
|
||||
bool instanceable;
|
||||
} meta_class_t;
|
||||
|
||||
typedef struct class {
|
||||
class_id_t id;
|
||||
} class_t;
|
||||
|
||||
typedef struct meta_object {
|
||||
class_t class;
|
||||
} meta_object_t;
|
||||
|
||||
typedef struct object {
|
||||
meta_object_t meta_obj;
|
||||
void (*destruct)(struct object*);
|
||||
} object_t;
|
||||
|
||||
class_id_t new_class(const char*, class_id_t, bool);
|
||||
bool instance_of_id(void*, class_id_t);
|
||||
bool instance_of(void*, class_t);
|
||||
const char* get_class_name(class_t);
|
||||
class_id_t id_from_name(const char*);
|
||||
bool class_exists(const char*);
|
||||
|
||||
#endif
|
34
strbuilder/Makefile
Normal file
34
strbuilder/Makefile
Normal file
|
@ -0,0 +1,34 @@
|
|||
#
|
||||
# Makefile for crap-libs/strbuilder
|
||||
#
|
||||
# Author: overflowerror
|
||||
#
|
||||
|
||||
CC = gcc
|
||||
DEFS = -D_XOPEN_SOURCE=500 -D_BSD_SOURCE -B "../oop/"
|
||||
CFLAGS = -Wall -Wextra -g -std=c99 -pedantic -DENDEBUG $(DEFS) -fPIC
|
||||
LDFLAGS = $(DEFS)
|
||||
LIBFLAGS = -shared $(DEFS)
|
||||
|
||||
.PHONY: all clean
|
||||
|
||||
all: example libstrbuilder.so
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
example: example.o strbuilder.o ../oop/liboop.so
|
||||
$(CC) $(LDFLAGS) -o $@ $^
|
||||
|
||||
../oop/liboop.so:
|
||||
cd ../oop/ && $(MAKE) liboop.so
|
||||
|
||||
libstrbuilder.so: strbuilder.o
|
||||
$(CC) $(LIBFLAGS) -o $@ $^
|
||||
|
||||
strbuilder.o: strbuilder.c strbuilder.h
|
||||
example.o: example.c strbuilder.h
|
||||
|
||||
clean:
|
||||
rm -f *.o example libstrbuilder.so
|
||||
cd ../oop/ && $(MAKE) clean
|
BIN
strbuilder/example
Executable file
BIN
strbuilder/example
Executable file
Binary file not shown.
20
strbuilder/example.c
Normal file
20
strbuilder/example.c
Normal file
|
@ -0,0 +1,20 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "../oop/oop.h"
|
||||
#include "strbuilder.h"
|
||||
|
||||
int main(void) {
|
||||
strbuilder_t* builder = new strbuilder("Hallo");
|
||||
|
||||
builder->add(builder, " Welt");
|
||||
builder->add(builder, "!");
|
||||
builder->build(builder);
|
||||
|
||||
const char* string = builder->get(builder);
|
||||
printf("%s\n", string);
|
||||
|
||||
builder->destruct(builder);
|
||||
|
||||
return 0;
|
||||
}
|
75
strbuilder/strbuilder.c
Normal file
75
strbuilder/strbuilder.c
Normal file
|
@ -0,0 +1,75 @@
|
|||
#include "strbuilder.h"
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
class_t strbuilder_class;
|
||||
|
||||
__attribute__ ((constructor))
|
||||
static void add_strbuilder_class(void) {
|
||||
strbuilder_class.id = new_class("strbuilder", NO_SUPER_CLASS, true);
|
||||
}
|
||||
|
||||
void strbuilder_method_clear(strbuilder_t* this) {
|
||||
for (int i = 0; i < this->nrstrings; i++) {
|
||||
free(this->strings[i]);
|
||||
}
|
||||
free(this->string);
|
||||
this->string = NULL;
|
||||
this->nrstrings = 0;
|
||||
}
|
||||
|
||||
void strbuilder_method_destruct(strbuilder_t* this) {
|
||||
this->clear(this);
|
||||
free(this);
|
||||
}
|
||||
|
||||
void strbuilder_method_add(strbuilder_t* this, const char* string) {
|
||||
this->strings = realloc(this->strings, ++this->nrstrings * sizeof(char*));
|
||||
this->strings[this->nrstrings - 1] = malloc(strlen(string) + 1);
|
||||
strcpy(this->strings[this->nrstrings - 1], string);
|
||||
}
|
||||
|
||||
size_t strbuilder_method_length(strbuilder_t* this) {
|
||||
size_t length = 0;
|
||||
if (this->string != NULL)
|
||||
length = strlen(this->string);
|
||||
for (int i = 0; i < this->nrstrings; i++) {
|
||||
length += strlen(this->strings[i]);
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
void strbuilder_method_build(strbuilder_t* this) {
|
||||
size_t length = this->length(this);
|
||||
bool empty = this->string == NULL;
|
||||
this->string = realloc(this->string, length + 1);
|
||||
if (empty)
|
||||
this->string[0] = '\0';
|
||||
for (int i = 0; i < this->nrstrings; i++) {
|
||||
strcat(this->string, this->strings[i]);
|
||||
free(this->strings[i]);
|
||||
}
|
||||
this->nrstrings = 0;
|
||||
}
|
||||
|
||||
const char* strbuilder_method_get(strbuilder_t* this) {
|
||||
return this->string;
|
||||
}
|
||||
|
||||
strbuilder_t* strbuilder_method_construct(const char* string) {
|
||||
strbuilder_t* obj = malloc(sizeof(strbuilder_t));
|
||||
obj->string = malloc(strlen(string) + 1);
|
||||
strcpy(obj->string, string);
|
||||
obj->strings = NULL;
|
||||
obj->nrstrings = 0;
|
||||
|
||||
obj->destruct = strbuilder_method_destruct;
|
||||
obj->add = strbuilder_method_add;
|
||||
obj->build = strbuilder_method_build;
|
||||
obj->get = strbuilder_method_get;
|
||||
obj->clear = strbuilder_method_clear;
|
||||
obj->length = strbuilder_method_length;
|
||||
|
||||
return obj;
|
||||
}
|
26
strbuilder/strbuilder.h
Normal file
26
strbuilder/strbuilder.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
#ifndef STRBUILDER_H
|
||||
#define STRBUILDER_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "../oop/oop.h"
|
||||
|
||||
#define strbuilder construct(strbuilder)
|
||||
|
||||
extern class_t strbuilder_class;
|
||||
|
||||
typedef struct strbuilder {
|
||||
meta_object_t meta;
|
||||
void (*destruct)(struct strbuilder*);
|
||||
void (*add)(struct strbuilder*, const char*);
|
||||
void (*build)(struct strbuilder*);
|
||||
const char* (*get)(struct strbuilder*);
|
||||
void (*clear)(struct strbuilder*);
|
||||
size_t (*length)(struct strbuilder*);
|
||||
char* string;
|
||||
char** strings;
|
||||
int nrstrings;
|
||||
} strbuilder_t;
|
||||
|
||||
strbuilder_t* strbuilder_method_construct(const char*);
|
||||
|
||||
#endif
|
Loading…
Reference in a new issue