diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-07-23 19:39:13 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-07-23 19:39:26 +0200 |
| commit | 421300c27123ca647772dc6d9bd0cabf7f5f9c99 (patch) | |
| tree | d916b62056b2440aef374b21373d16272bb709ee | |
| parent | 53e9e371a16ae2d167f1fc2577dff2c2426eed68 (diff) | |
refactor: src & pragma once
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | CMakeLists.txt | 23 | ||||
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | base_test.c | 2 | ||||
| -rw-r--r-- | generate.fish | 2 | ||||
| -rw-r--r-- | include/json.h (renamed from json.h) | 10 | ||||
| -rw-r--r-- | include/log.h (renamed from log.h) | 0 | ||||
| -rw-r--r-- | include/memory.h (renamed from memory.h) | 5 | ||||
| -rw-r--r-- | include/str.h (renamed from string.h) | 8 | ||||
| -rw-r--r-- | include/testing.h (renamed from testing.h) | 0 | ||||
| -rw-r--r-- | include/types.h (renamed from types.h) | 0 | ||||
| -rw-r--r-- | include/vec.h (renamed from vec.h) | 8 | ||||
| -rw-r--r-- | main.c | 6 | ||||
| -rw-r--r-- | src/json.c (renamed from json.c) | 8 | ||||
| -rw-r--r-- | src/log.c (renamed from log.c) | 7 | ||||
| -rw-r--r-- | src/memory.c (renamed from memory.c) | 4 | ||||
| -rw-r--r-- | src/str.c (renamed from string.c) | 5 | ||||
| -rw-r--r-- | src/vec.c (renamed from vec.c) | 6 | ||||
| -rw-r--r-- | tests/test_json.c (renamed from test_json.c) | 4 | ||||
| -rw-r--r-- | tests/test_string.c (renamed from test_string.c) | 4 | ||||
| -rw-r--r-- | tests/test_vec.c (renamed from test_vec.c) | 4 |
22 files changed, 43 insertions, 68 deletions
@@ -4,3 +4,4 @@ Debug/ Release/ test.c +out.json diff --git a/CMakeLists.txt b/CMakeLists.txt index c022fc3..a5fd18e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,26 +2,13 @@ cmake_minimum_required(VERSION 3.30.5) project(kagamic VERSION 0.1 LANGUAGES C) set(CMAKE_C_STANDARD 17) -add_executable(main - main.c - string.h string.c - memory.h memory.c - vec.h vec.c - json.h json.c - log.h log.c -) +file(GLOB SOURCES "src/*.c") +file(GLOB TEST_SOURCES "test/*.c") + +add_executable(main main.c ${SOURCES}) target_link_libraries(main m) target_compile_options(main PRIVATE -Wall -pedantic -Wextra) - -add_executable(test - test.c - testing.h - string.h string.c - memory.h memory.c - vec.h vec.c - json.h json.c - log.h log.c -) +add_executable(test test.c ${SOURCES} ${TEST_SOURCES}) target_link_libraries(test m) target_compile_options(test PRIVATE -Wall -pedantic -Wextra) @@ -16,4 +16,4 @@ test: ./Debug/test; format: - clang-format -i *.c *.h + clang-format -i src/*.c include/*.h tests/*.c *.c @@ -7,8 +7,6 @@ - Benchmark 100MB file - json.loads: 526 ms ± 77 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) - self: 348 ms (mean of 7 runs, 1 loop each). 2079 MB/s -- More modern include pragma (once) -- File structure - Static functions for private? - `valgrind --leak-check=full ./your_program` - assert with error msg? diff --git a/base_test.c b/base_test.c index 6d095f8..a06aab4 100644 --- a/base_test.c +++ b/base_test.c @@ -1,4 +1,4 @@ -#include "types.h" +#include "include/types.h" #define test(func) \ switch (func()) { \ diff --git a/generate.fish b/generate.fish index b0e5308..96f8542 100644 --- a/generate.fish +++ b/generate.fish @@ -1,5 +1,5 @@ echo "" > test.c; -set fs (ls test_*.c); +set fs (ls tests/test_*.c); for f in $fs; echo "#include \"$f\"" >> test.c; end; @@ -1,8 +1,7 @@ -#ifndef JSON_H -#define JSON_H -#include "memory.h" -#include "string.h" -#include "vec.h" +#pragma once +#include "../include/memory.h" +#include "../include/str.h" +#include "../include/vec.h" typedef struct { Vec keys; // Vec<Str> @@ -33,4 +32,3 @@ JSON *JSONObject_get(JSONObject obj, Str key); i32 JSON_fprint(FILE *w, JSON json); JSON JSON_parse(Arena *a, byte *src); JSON JSON_Int(Arena *a, i32 val); -#endif diff --git a/memory.h b/include/memory.h index 1a33452..d202e97 100644 --- a/memory.h +++ b/include/memory.h @@ -1,8 +1,8 @@ +#pragma once #include <stddef.h> -#ifndef MEMORY_H #include "types.h" -#define MEMORY_H + typedef struct { size cap; byte *beg; @@ -14,4 +14,3 @@ Arena Arena_new(size cap); void *alloc(Arena *a, size count, size tsize, size align); size Arena_get_used(Arena a); void Arena_reset(Arena *a); -#endif @@ -1,12 +1,11 @@ +#pragma once #include <stddef.h> #include <stdint.h> #include <stdio.h> -#include "memory.h" -#include "types.h" +#include "../include/memory.h" +#include "../include/types.h" -#ifndef STRING_H -#define STRING_H #define S(s) \ (Str) { s, lengthof(s) } typedef struct { @@ -17,4 +16,3 @@ i32 Str_equals(Str a, Str b); u64 Str_hash64(Str s); Str Str_copy(Arena *a, byte *data, size len); i32 Str_fprint(FILE *w, Str s); -#endif diff --git a/testing.h b/include/testing.h index 9e0c022..9e0c022 100644 --- a/testing.h +++ b/include/testing.h diff --git a/types.h b/include/types.h index bacedf8..bacedf8 100644 --- a/types.h +++ b/include/types.h @@ -1,11 +1,10 @@ +#pragma once #include <stddef.h> #include <stdint.h> -#include "memory.h" -#include "types.h" +#include "../include/memory.h" +#include "../include/types.h" -#ifndef VEC_H -#define VEC_H typedef struct { void *data; size len; @@ -18,4 +17,3 @@ Vec _Vec_new(Arena *a, size tsize, size cap, size align); void Vec_push(Arena *a, Vec *v, void *data); #define Vec_get(v, T, p) (T *)_Vec_get(v, p) void *_Vec_get(Vec v, size pos); -#endif @@ -3,9 +3,9 @@ #include <sys/stat.h> #include <time.h> -#include "json.h" -#include "memory.h" -#include "types.h" +#include "include/json.h" +#include "include/memory.h" +#include "include/types.h" // Load entire file into memory byte *load_file(const char *filename, size *file_size) { @@ -1,5 +1,3 @@ -#include "json.h" - #include <assert.h> #include <ctype.h> #include <stddef.h> @@ -7,9 +5,9 @@ #include <stdio.h> #include <string.h> -#include "json.h" -#include "string.h" -#include "types.h" +#include "../include/json.h" +#include "../include/str.h" +#include "../include/types.h" JSONObject *JSONObject_new(Arena *a) { JSONObject *object = new(a, JSONObject, 1); @@ -1,13 +1,12 @@ -#include "log.h" - #include <stdio.h> -#include "types.h" +#include "../include/log.h" +#include "../include/types.h" void info(Logger l, const byte *name, const byte *str, ...) { if (l.level >= INFO) { printf("[%s] ", name); - printf(str); + printf("%s", str); printf("\n"); } } @@ -3,8 +3,8 @@ #include <stdlib.h> #include <string.h> -#include "memory.h" -#include "types.h" +#include "../include/memory.h" +#include "../include/types.h" Arena Arena_new(size cap) { Arena a = {0}; @@ -1,10 +1,9 @@ -#include "string.h" - #include <stdint.h> #include <stdio.h> #include <string.h> -#include "memory.h" +#include "../include/memory.h" +#include "../include/str.h" // Returns 1 if strings are equal, 0 otherwise i32 Str_equals(Str a, Str b) { @@ -1,8 +1,8 @@ #include <string.h> -#include "assert.h" -#include "types.h" -#include "vec.h" +#include "../include/assert.h" +#include "../include/types.h" +#include "../include/vec.h" Vec _Vec_new(Arena *a, size tsize, size cap, size align) { assert(cap > 0); diff --git a/test_json.c b/tests/test_json.c index a37996c..4ef3d5d 100644 --- a/test_json.c +++ b/tests/test_json.c @@ -1,5 +1,5 @@ -#include "json.h" -#include "testing.h" +#include "../include/json.h" +#include "../include/testing.h" i32 test_json_integer() { Arena perm = Arena_new(128e3); diff --git a/test_string.c b/tests/test_string.c index 0d6dd71..1baab6a 100644 --- a/test_string.c +++ b/tests/test_string.c @@ -1,5 +1,5 @@ -#include "string.h" -#include "testing.h" +#include "../include/string.h" +#include "../include/testing.h" i32 test_string_literal_equality() { t_assert(Str_equals(S("Bonjour"), S("Bonjour"))); diff --git a/test_vec.c b/tests/test_vec.c index 180ba41..68cb0ce 100644 --- a/test_vec.c +++ b/tests/test_vec.c @@ -1,5 +1,5 @@ -#include "testing.h" -#include "vec.h" +#include "../include/testing.h" +#include "../include/vec.h" i32 test_int_vec() { Arena perm = Arena_new(128e3); |
