From 66c434a79969e700c4257144c1ba28fe9d69b26e Mon Sep 17 00:00:00 2001 From: cdricms <36056008+cdricms@users.noreply.github.com> Date: Wed, 27 Dec 2023 16:27:46 +0100 Subject: [PATCH] Makefile for Windows + Unix; and some new function for LinkedList --- Makefile | 18 ++++++++++++++---- src/common.c | 29 +++++++++++++++++++++++++++-- src/common.h | 4 +++- src/engine.h | 8 ++++++++ src/main.c | 9 +++++---- 5 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 src/engine.h diff --git a/Makefile b/Makefile index ebf8400..30ce404 100644 --- a/Makefile +++ b/Makefile @@ -7,16 +7,26 @@ TARGET = main SOURCES = $(wildcard $(SRCDIR)/*.c) OBJECTS = $(patsubst $(SRCDIR)/%.c, $(BUILDDIR)/%.o, $(SOURCES)) -all: $(BUILDDIR)/$(TARGET) +ifeq ($(OS),Windows_NT) + EXECUTABLE = $(BUILDDIR)/$(TARGET).exe + RM = del /Q + MKDIR = mkdir +else + EXECUTABLE = $(BUILDDIR)/$(TARGET) + RM = rm -f + MKDIR = mkdir -p +endif -$(BUILDDIR)/$(TARGET): $(OBJECTS) +all: $(EXECUTABLE) + +$(EXECUTABLE): $(OBJECTS) $(CC) $(CFLAGS) -o $@ $^ $(BUILDDIR)/%.o: $(SRCDIR)/%.c - @mkdir -p $(BUILDDIR) + $(MKDIR) $(BUILDDIR) $(CC) $(CFLAGS) -c -o $@ $< clean: - rm -rf $(BUILDDIR)/* + $(RM) $(BUILDDIR)/* .PHONY: clean diff --git a/src/common.c b/src/common.c index a87b90a..8a8dbcc 100644 --- a/src/common.c +++ b/src/common.c @@ -92,8 +92,33 @@ bool linkedList_removeAt(LinkedList **head, const uint index) { return true; } -void linkedList_print(LinkedList *head, void (*callback)(void *)) { - LinkedList *current_node = head; +uint linkedList_length(const LinkedList *head) { + uint current_index = 0; + const LinkedList *current_node = head; + while (current_node != NULL) { + current_node = current_node->next; + current_index++; + } + + return current_index; +} + +bool linkedList_removeAll(LinkedList *head) { + uint length = linkedList_length(head); + LinkedList *lastNode = linkedList_getAt(head, length - 1); + if (lastNode == NULL) + return false; + while (lastNode->prev != NULL) { + lastNode = lastNode->prev; + free(lastNode->next); + } + free(lastNode); + + return true; +} + +void linkedList_print(const LinkedList *head, void (*callback)(void *)) { + const LinkedList *current_node = head; while (current_node != NULL) { callback(current_node->value); printf("->"); diff --git a/src/common.h b/src/common.h index fddaec6..59f5ea7 100644 --- a/src/common.h +++ b/src/common.h @@ -20,6 +20,8 @@ LinkedList *linkedList_getAt(LinkedList *head, const uint index); bool linkedList_insertAt(LinkedList *head, void *value, const uint index); bool linkedList_removeAt(LinkedList **head, const uint index); bool linkedList_append(LinkedList *head, void *value); -void linkedList_print(LinkedList *head, void (*callback)(void *)); +uint linkedList_length(const LinkedList *head); +bool linkedList_removeAll(LinkedList *head); +void linkedList_print(const LinkedList *head, void (*callback)(void *)); #endif // !__COMMON_HEADER diff --git a/src/engine.h b/src/engine.h new file mode 100644 index 0000000..f68b454 --- /dev/null +++ b/src/engine.h @@ -0,0 +1,8 @@ +#ifndef __ENGINE_HEADER +#define __ENGINE_HEADER + +#include "board.h" +#include "common.h" +#include "fen.h" + +#endif // !__ENGINE_HEADER diff --git a/src/main.c b/src/main.c index a281ded..fdd9681 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,4 @@ -#include "board.h" -#include "common.h" -#include "fen.h" +#include "engine.h" void printFEN(void *value) { printf("%s", (char *)value); } void printString(void *value) { printf("%s", *(char **)value); } @@ -12,9 +10,12 @@ int main() { board.history = linkedList_init(&fen); Coordinates *coords = piece_getMoves(board.squares[3].piece, board.squares[3].coords); + char a[] = "hello"; + linkedList_append(board.history, &a); + printf("%d\n", linkedList_length(board.history)); linkedList_print(board.history, printFEN); free(coords); - free(board.history); + linkedList_removeAll(board.history); // char *a = "hello"; // LinkedList *head = linkedList_init(&a); // char *b = "world";