From: Michael Rasmussen Date: Sun, 3 Nov 2024 00:55:36 +0000 (+0100) Subject: init X-Git-Url: http://git.datanom.net/clib.git/commitdiff_plain/392d65ba6c9a90a052b8d7213156a89e9201b38d?ds=sidebyside init Signed-off-by: Michael Rasmussen --- diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..e69de29 diff --git a/ChangeLog b/ChangeLog new file mode 100644 index 0000000..e69de29 diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..f0667b3 --- /dev/null +++ b/Makefile.am @@ -0,0 +1,14 @@ +AUTOMAKE_OPTIONS = gnu + +ACLOCAL_AMFLAGS = -I m4 + +SUBDIRS = \ + src + +EXTRA_DIST = \ + autogen.sh \ + AUTHORS \ + ChangeLog \ + NEWS \ + README + diff --git a/NEWS b/NEWS new file mode 100644 index 0000000..e69de29 diff --git a/README b/README new file mode 100644 index 0000000..e69de29 diff --git a/autogen.sh b/autogen.sh new file mode 100755 index 0000000..a10227f --- /dev/null +++ b/autogen.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +if [ ! -e m4 ]; then + mkdir m4 +fi + +if [ ! -e auxdir ]; then + mkdir auxdir +fi + +aclocal -I m4 \ + && libtoolize --force --copy \ + && autoheader \ + && automake --add-missing --gnu --copy \ + && autoconf \ + && ./configure --enable-maintainer-mode $@ diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..a67dc56 --- /dev/null +++ b/configure.ac @@ -0,0 +1,82 @@ +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_PREREQ([2.72]) +AC_INIT([C library],[0.1.0],[mir@datanom.net]) +AC_COPYRIGHT([Copyright (c) 2024 Michael Rasmussen.]) +AC_CONFIG_MACRO_DIR([m4]) +AC_CONFIG_AUX_DIR(auxdir) +AC_CANONICAL_TARGET + +AC_CONFIG_SRCDIR([src/stack.c]) +AM_INIT_AUTOMAKE([gnu no-dist-gzip dist-bzip2 1.10]) +AM_MAINTAINER_MODE + +CFLAGS="-Wall" + +if test "x$USE_MAINTAINER_MODE" = "xyes"; then + CFLAGS="${CFLAGS} -g -DDEBUG" + AC_DEFINE_UNQUOTED(DEBUG, [1], [Enable debug mode.]) +else + CFLAGS="${CFLAGS} -O -O2" +fi + +GLIB_REQUIRED=2.50.0 + +AC_SUBST(GLIB_REQUIRED) + +AC_CONFIG_HEADERS(config.h) + +# Checks for programs. +AC_PROG_CC +AC_PROG_INSTALL +AC_PROG_LN_S +AC_PROG_MAKE_SET +LT_INIT + +if test -n "$lt_prog_compiler_pic"; then + CFLAGS="$CFLAGS $lt_prog_compiler_pic" +fi + +AC_SYS_LARGEFILE + +PKG_PROG_PKG_CONFIG +AC_LIB_PREFIX + +# Checks for libraries. +PKG_CHECK_MODULES([GLIB], + [glib-2.0 >= $GLIB_REQUIRED + gobject-2.0 >= $GOBJECT_REQUIRED + gmodule-2.0] +) +AC_SUBST(GLIB_CFLAGS) +AC_SUBST(GLIB_LIBS) + +# Checks for header files. +AC_CHECK_HEADER([assert.h], + AC_DEFINE([HAVE_ASSERT_H], [1], [Define to 1 if you have the header file.])) + +# Checks for typedefs, structures, and compiler characteristics. + +# Checks for library functions. + +AC_CONFIG_FILES([ + Makefile + src/Makefile +]) + +AC_OUTPUT +echo "" +echo "$PACKAGE $VERSION" +echo " +Configuration: + + Source code: ${ac_pwd} + Compiler: ${CC} $($CC -dumpversion) + CFLAGS: ${CFLAGS} +" +echo "" +echo "The binary will be installed in $prefix/bin" +echo "" +echo "Configure finished, type 'make' to build." + diff --git a/demo/demo.c b/demo/demo.c new file mode 100644 index 0000000..8e5de8c --- /dev/null +++ b/demo/demo.c @@ -0,0 +1,28 @@ +#include +#include +#include "queue.h" +#include "stack.h" + +int main() { +/* + Stack* stack = stack_init(); + if (stack_empty(stack)) + printf("Stack is empty\n"); + + int* i = malloc(sizeof(int*)); + *i = 10; + stack_push(stack, i); + int* j = malloc(sizeof(int*)); + *j = 5; + stack_push(stack, j); + int* val = stack_pop(stack); + printf("Stack back: %i\n", *val); + free(val); + void** elems = stack_clear(&stack); + for (int i = 0; elems[i]; i++) + free(elems[i]); + free(elems); +*/ + return 0; +} + diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..d6f9a18 --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,20 @@ +AUTOMAKE_OPTIONS = gnu + +AM_CPPFLAGS = \ + -I${top_srcdir} \ + -I${top_builddir} \ + @GLIB_CFLAGS@ + +noinst_LIBRARIES = libclib.a + +libclib_a_SOURCES = \ + queue.c \ + queue.h \ + stack.c \ + stack.h + +libclib_a_LIBADD = \ + $(LIBOBJS) \ + $(ALLOCA) \ + @LIBS@ + diff --git a/src/queue.c b/src/queue.c new file mode 100644 index 0000000..bcfc65a --- /dev/null +++ b/src/queue.c @@ -0,0 +1,99 @@ +#include +#include "queue.h" + +typedef struct _Queue { + void** elems; + int size; + QueueFreeFunc free; +} Queue; + +static void free_elem(Queue* queue) { + if (queue->free) { + for (int i = 0; i < queue->size; i++) + queue->free(queue->elems[i]); + } + free(queue->elems); +} + +Queue* queue_new() { + Queue* q = malloc(sizeof(Queue)); + q->size = 0; + q->elems = NULL; + q->free = NULL; + return q; +} + +Queue* queue_new_full(QueueFreeFunc free){ + Queue* q = malloc(sizeof(Queue)); + q->size = 0; + q->elems = NULL; + q->free = free; + return q; +} + +void queue_destroy(Queue* queue) { + free_elem(queue); + free(queue); +} + +/* +static void extend(Queue* queue, void* elem) { + while (int i = queue->size - 1; i > 0; i--) { + queue->elems[i] = queue->elems[i - 1] + } + queue->elems[0] elem; +} +*/ +void* queue_dequeue(Queue* queue) { + if (queue_empty(queue)) + return NULL; + queue->size -= 1; + void* elem = queue->elems[queue->size]; + queue->elems[queue->size] = NULL; + queue->elems = realloc(queue->elems, queue->size * sizeof(void*)); + + return elem; +} + +void queue_enqueue(Queue* queue, void* elem) { + if (queue == NULL) + return; + + queue->size += 1; + queue->elems = realloc(queue->elems, queue->size * sizeof(void*)); + for (int i = queue->size - 1; i > 0; i--) { + queue->elems[i] = queue->elems[i - 1]; + } + queue->elems[0] = elem; +} + +void* queue_front(Queue* queue) { + if (queue_empty(queue)) + return NULL; + return queue->elems[0]; +} + +void* queue_back(Queue* queue) { + if (queue_empty(queue)) + return NULL; + return queue->elems[queue->size - 1]; +} + +bool queue_empty(Queue* queue) { + return (queue == NULL || queue->size == 0); +} + +void queue_clear(Queue* queue) { + if (queue == NULL) + return; + + free_elem(queue); + queue->size = 0; + queue->elems = NULL; +} + +int queue_size(Queue* queue) { + if (queue_empty(queue)) + return 0; + return queue->size; +} diff --git a/src/queue.h b/src/queue.h new file mode 100644 index 0000000..bed27d0 --- /dev/null +++ b/src/queue.h @@ -0,0 +1,21 @@ + +#ifndef _QUEUE_H_ +#define _QUEUE_H_ + +#include + +typedef struct _Queue Queue; +typedef void (*QueueFreeFunc)(void*); + +Queue* queue_new(); +Queue* queue_new_full(QueueFreeFunc free); +void queue_destroy(Queue*); +void* queue_dequeue(Queue*); +void queue_enqueue(Queue*, void*); +void* queue_front(Queue*); +void* queue_back(Queue*); +void queue_clear(Queue*); +bool queue_empty(Queue*); +int queue_size(Queue*); + +#endif diff --git a/src/stack.c b/src/stack.c new file mode 100644 index 0000000..8035865 --- /dev/null +++ b/src/stack.c @@ -0,0 +1,169 @@ +#include +#include "stack.h" + +#define INIT_SIZE 100 + +struct _Stack { + int size; + void** elems; + StackFreeFunc free; +}; + +static void free_elem(Stack* stack) { + if (stack->free) { + for (int i = 0; i < stack->size; i++) + stack->free(stack->elems[i]); + } + free(stack->elems); +} + +static Stack* stack_new_common() { + Stack* s = malloc(sizeof(Stack)); + s->elems = malloc(sizeof(void*) * INIT_SIZE); + s->size = 0; + s->free = NULL; + return s; +} + +Stack* stack_new() { + return stack_new_common(); +} + +Stack* stack_new_full(StackFreeFunc free) { + Stack* s = stack_new_common(); + s->free = free; + return s; +} + +void stack_destroy(Stack* stack) { + free_elem(stack); + free(stack); +} + +void* stack_pop(Stack* stack) { + if (stack_empty(stack)) + return NULL; + stack->size -= 1; + void* elem = stack->elems[stack->size]; + stack->elems[stack->size] = NULL; + + return elem; +} + +void stack_push(Stack* stack, void* elem) { + if (stack == NULL) + return; + + stack->size += 1; + if (stack->size == INIT_SIZE) { + stack->elems = realloc(stack->elems, (INIT_SIZE + stack->size) * sizeof(void*)); + } + + stack->elems[stack->size - 1] = elem; +} + +void* stack_peek(Stack* stack) { + if (stack_empty(stack)) + return NULL; + return stack->elems[stack->size - 1]; +} + +bool stack_empty(Stack* stack) { + return (stack == NULL || stack->size == 0); +} + +void stack_clear(Stack* stack) { + if (stack == NULL) + return; + + free_elem(stack); + stack->size = 0; +} + +int stack_size(Stack* stack) { + if (stack_empty(stack)) + return 0; + return stack->size; +} + +/* +typedef struct _Stack { + void** elems; + int size; + StackFreeFunc free; +} Stack; + +static void free_elem(Stack* stack) { + if (stack->free) { + for (int i = 0; i < stack->size; i++) + stack->free(stack->elems[i]); + } + free(stack->elems); +} + +Stack* stack_new() { + Stack* s = malloc(sizeof(Stack)); + s->size = 0; + s->elems = NULL; + s->free = NULL; + return s; +} + +Stack* stack_new_full(StackFreeFunc free) { + Stack* s = malloc(sizeof(Stack)); + s->size = 0; + s->elems = NULL; + s->free = free; + return s; +} + +void stack_destroy(Stack* stack) { + free_elem(stack); + free(stack); +} + +void* stack_pop(Stack* stack) { + if (stack_empty(stack)) + return NULL; + stack->size -= 1; + void* elem = stack->elems[stack->size]; + stack->elems[stack->size] = NULL; + stack->elems = realloc(stack->elems, stack->size * sizeof(void*)); + + return elem; +} + +void stack_push(Stack* stack, void* elem) { + if (stack == NULL) + return; + + stack->size += 1; + stack->elems = realloc(stack->elems, stack->size * sizeof(void*)); + stack->elems[stack->size - 1] = elem; +} + +void* stack_peek(Stack* stack) { + if (stack_empty(stack)) + return NULL; + return stack->elems[stack->size - 1]; +} + +bool stack_empty(Stack* stack) { + return (stack == NULL || stack->size == 0); +} + +void stack_clear(Stack* stack) { + if (stack == NULL) + return; + + free_elem(stack); + stack->size = 0; + stack->elems = NULL; +} + +int stack_size(Stack* stack) { + if (stack_empty(stack)) + return 0; + return stack->size; +} +*/ diff --git a/src/stack.h b/src/stack.h new file mode 100644 index 0000000..6775ef1 --- /dev/null +++ b/src/stack.h @@ -0,0 +1,19 @@ +#ifndef _STACK_H_ +#define _STACK_H_ + +#include + +typedef struct _Stack Stack; +typedef void (*StackFreeFunc)(void*); + +Stack* stack_new(); +Stack* stack_new_full(StackFreeFunc free); +void stack_destroy(Stack*); +void* stack_pop(Stack*); +void stack_push(Stack*, void*); +void* stack_peek(Stack*); +bool stack_empty(Stack*); +void stack_clear(Stack*); +int stack_size(Stack*); + +#endif