sw-battle-test-c

Interesting test task in C instead of C++
git clone git://bsandro.tech/sw-battle-test-c
Log | Files | Refs | README

commit d37c72dd5a988c4215f5264d071bcf78e05f9c8f
parent 1681025a03cec6232e0554cc33af3ddb7410ca59
Author: bsandro <email@bsandro.tech>
Date:   Tue, 29 Sep 2026 23:35:31 +0300

rudimentary parser + ditching variadics for commands

Diffstat:
M.gitignore | 1+
MMakefile | 8++++----
Mcommands/CREATE_MAP.c | 4++--
Mcore/command.h | 3++-
Acore/parser.c | 14++++++++++++++
Acore/parser.h | 3+++
Mmain.c | 38++++++++++++++++++++++++++++++++------
7 files changed, 58 insertions(+), 13 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,3 +1,4 @@ *.o sw-battle-test-c _* +*.swp diff --git a/Makefile b/Makefile @@ -1,16 +1,16 @@ NAME=$(shell basename ${PWD}) -SDL_CFLAGS=$(shell pkg-config --cflags sdl2 SDL2_image) -SDL_LDFLAGS=$(shell pkg-config --libs sdl2 SDL2_image) +#SDL_CFLAGS=$(shell pkg-config --cflags sdl2 SDL2_image) +#SDL_LDFLAGS=$(shell pkg-config --libs sdl2 SDL2_image) SRC:=$(shell find . -type f -iname '*.c') DEPS:=$(shell find . -type f -iname '*.h') OBJ:=$(SRC:.c=.o) -CFLAGS=-std=c11 -Wpedantic -Wall -Wextra -Wno-unused -ffast-math -I. $(SDL_CFLAGS) +CFLAGS=-std=gnu11 -Wpedantic -Wall -Wextra -Wno-unused -ffast-math -I. #$(SDL_CFLAGS) ifeq (${DEBUG},1) CFLAGS+=-O0 -g else CFLAGS+=-O3 -mtune=native -march=native endif -LDFLAGS=-lc -lm -flto $(SDL_LDFLAGS) +LDFLAGS=-lc -lm -flto #$(SDL_LDFLAGS) #CC=gcc all: $(NAME) diff --git a/commands/CREATE_MAP.c b/commands/CREATE_MAP.c @@ -1,5 +1,5 @@ #include "core/command.h" -void cmd_create_map(World *w, ...) { - puts("create map"); +void cmd_create_map(World *w, int arg1, int arg2) { + printf("create map %d %d\n", arg1, arg2); } diff --git a/core/command.h b/core/command.h @@ -3,7 +3,8 @@ #include <stdio.h> #include "core/world.h" -typedef void Command_Func(World *w, ...); +//typedef void Command_Func(World *w, ...); +typedef void Command_Func(); typedef struct Command { const char *name; diff --git a/core/parser.c b/core/parser.c @@ -0,0 +1,14 @@ +#include <stdio.h> +#include <string.h> +#include "core/parser.h" + +void try_parse_call(char *line) { + //printf("\n=======\nline: %s\n=======\n", line); + char *saveptr = NULL; + for (char *src=line;;src=NULL) { + char *token=strtok_r(src, " \r\n", &saveptr); + if (token==NULL) break; + printf(" -> %s", token); + } + printf("\n---------\n"); +} diff --git a/core/parser.h b/core/parser.h @@ -0,0 +1,3 @@ +#pragma once + +void try_parse_call(char *line); diff --git a/main.c b/main.c @@ -1,11 +1,14 @@ #include <stdio.h> +#include <stdlib.h> #include <string.h> #include "core/world.h" +#include "core/parser.h" #include "commands/_registry.h" - #include "systems/actions.h" #include "systems/death.h" +#define MAX_COMMAND_SIZE 32 + int main(int argc, char **argv) { World w; Map_Init(&w.map, 3, 4); @@ -16,15 +19,38 @@ int main(int argc, char **argv) { puts("world init ok"); if (argc>1) { + FILE *f = fopen(argv[1], "r"); + // @todo check if f is NULL + char *line = NULL; + size_t linesize = 0; + ssize_t slen; + while ((slen=getline(&line, &linesize, f))!=-1) { + if (slen==1) continue; + //printf("[%zd] %s", slen, line); + try_parse_call(line); +/* + printf("\n=======\nline: %s\n=======\n", line); + char *saveptr = NULL; + for (char *src=line;;src=NULL) { + char *token=strtok_r(src, " \r\n", &saveptr); + if (token==NULL) break; + printf(" -> %s", token); + } + printf("\n---------\n"); +*/ + } + free(line); + fclose(f); for (int i=0;i<COMMANDS_COUNT;++i) { - if (strcmp(COMMANDS[i].name, argv[1])==0) { - COMMANDS[i].func(&w, "ehlo"); - goto fin; + if (strncmp(COMMANDS[i].name, "CREATE_MAP", MAX_COMMAND_SIZE)==0) { + COMMANDS[i].func(&w, 10, 20); + //goto fin; + break; } } } - puts("unknown command"); - return 1; + //puts("unknown command"); + return 0; fin: while (World_Run_Systems(&w)) {