refactor(hmpl,hectic): warnings
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
{ stdenv, gcc, lib }:
|
||||
{ stdenv, gcc, lib, bash }:
|
||||
|
||||
stdenv.mkDerivation {
|
||||
pname = "hectic";
|
||||
@@ -6,22 +6,15 @@ stdenv.mkDerivation {
|
||||
src = ./.;
|
||||
doCheck = true;
|
||||
|
||||
nativeBuildInputs = [ gcc ];
|
||||
|
||||
buildPhase = ''
|
||||
mkdir -p target
|
||||
${gcc}/bin/cc -Wall -Wextra -g \
|
||||
-std=c99 \
|
||||
-pedantic -fsanitize=address \
|
||||
-c hectic.c -o target/hectic.o
|
||||
${gcc}/bin/ar rcs target/libhectic.a target/hectic.o
|
||||
ls
|
||||
${bash}/bin/sh ./make.sh build
|
||||
'';
|
||||
|
||||
checkPhase = ''
|
||||
mkdir -p target/test
|
||||
for test_file in test/*.c; do
|
||||
exe="target/test/$(basename ''${test_file%.c})"
|
||||
${gcc}/bin/cc -Wall -Wextra -g -pedantic -fsanitize=address -I. "$test_file" -Ltarget -lhectic -o "$exe"
|
||||
"$exe"
|
||||
done
|
||||
${bash}/bin/sh ./make.sh check
|
||||
'';
|
||||
|
||||
installPhase = ''
|
||||
@@ -31,7 +24,7 @@ stdenv.mkDerivation {
|
||||
'';
|
||||
|
||||
meta = {
|
||||
description = "libhectic";
|
||||
description = "hectic";
|
||||
license = lib.licenses.mit;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ void set_output_color_mode(ColorMode mode) {
|
||||
|
||||
const char* log_level_to_string(LogLevel level) {
|
||||
switch (level) {
|
||||
case LOG_LEVEL_TRACE: return "TRACE";
|
||||
case LOG_LEVEL_DEBUG: return "DEBUG";
|
||||
case LOG_LEVEL_LOG: return "LOG";
|
||||
case LOG_LEVEL_INFO: return "INFO";
|
||||
@@ -22,7 +23,9 @@ const char* log_level_to_string(LogLevel level) {
|
||||
|
||||
LogLevel log_level_from_string(const char *level_str) {
|
||||
if (!level_str) return LOG_LEVEL_INFO;
|
||||
if (strcmp(level_str, "DEBUG") == 0)
|
||||
if (strcmp(level_str, "TRACE") == 0)
|
||||
return LOG_LEVEL_TRACE;
|
||||
else if (strcmp(level_str, "DEBUG") == 0)
|
||||
return LOG_LEVEL_DEBUG;
|
||||
else if (strcmp(level_str, "LOG") == 0)
|
||||
return LOG_LEVEL_LOG;
|
||||
@@ -75,22 +78,11 @@ char* log_message(LogLevel level, char *file, int line, const char *format, ...)
|
||||
return timeStr;
|
||||
}
|
||||
|
||||
// -----------
|
||||
// -- utils --
|
||||
// -----------
|
||||
// ----------
|
||||
// -- misc --
|
||||
// ----------
|
||||
|
||||
void substr(const char *src, char *dest, size_t start, size_t len) {
|
||||
raise_debug("substring %s from %zu to %zu", src, start, len);
|
||||
size_t srclen = strlen(src);
|
||||
if (start >= srclen) {
|
||||
dest[0] = '\0';
|
||||
return;
|
||||
}
|
||||
if (start + len > srclen)
|
||||
len = srclen - start;
|
||||
strncpy(dest, src + start, len);
|
||||
dest[len] = '\0';
|
||||
}
|
||||
//void fomatBytes(size_t bytes, char )
|
||||
|
||||
// ----------
|
||||
// -- Json --
|
||||
@@ -223,7 +215,7 @@ static Json *json_parse_value__(const char **s, Arena *arena) {
|
||||
if (!item) return NULL;
|
||||
memset(item, 0, sizeof(Json));
|
||||
item->type = JSON_STRING;
|
||||
item->string = json_parse_string__(s, arena);
|
||||
item->JsonValue.string = json_parse_string__(s, arena);
|
||||
return item;
|
||||
} else if (strncmp(*s, "null", 4) == 0) {
|
||||
Json *item = arena_alloc(arena, sizeof(Json));
|
||||
@@ -237,7 +229,7 @@ static Json *json_parse_value__(const char **s, Arena *arena) {
|
||||
if (!item) return NULL;
|
||||
memset(item, 0, sizeof(Json));
|
||||
item->type = JSON_BOOL;
|
||||
item->boolean = 1;
|
||||
item->JsonValue.boolean = 1;
|
||||
*s += 4;
|
||||
return item;
|
||||
} else if (strncmp(*s, "false", 5) == 0) {
|
||||
@@ -245,7 +237,7 @@ static Json *json_parse_value__(const char **s, Arena *arena) {
|
||||
if (!item) return NULL;
|
||||
memset(item, 0, sizeof(Json));
|
||||
item->type = JSON_BOOL;
|
||||
item->boolean = 0;
|
||||
item->JsonValue.boolean = 0;
|
||||
*s += 5;
|
||||
return item;
|
||||
} else if ((**s == '-') || isdigit((unsigned char)**s)) {
|
||||
@@ -253,7 +245,7 @@ static Json *json_parse_value__(const char **s, Arena *arena) {
|
||||
if (!item) return NULL;
|
||||
memset(item, 0, sizeof(Json));
|
||||
item->type = JSON_NUMBER;
|
||||
item->number = json_parse_number__(s);
|
||||
item->JsonValue.number = json_parse_number__(s);
|
||||
return item;
|
||||
} else if (**s == '[') {
|
||||
return json_parse_array__(s, arena);
|
||||
@@ -267,14 +259,14 @@ Json *json_parse(Arena *arena, const char **s) {
|
||||
return json_parse_value__(s, arena);
|
||||
}
|
||||
|
||||
char *json_to_string(Arena *arena, Json *item) {
|
||||
char *json_to_string(Arena *arena, const Json * const item) {
|
||||
return json_to_string_with_opts(arena, item, JSON_NORAW);
|
||||
}
|
||||
|
||||
/* Minimal JSON printer with raw output option.
|
||||
When raw is non-zero and the item is a JSON_STRING, it is printed without quotes.
|
||||
*/
|
||||
char *json_to_string_with_opts(Arena *arena, Json *item, JsonRawOpt raw) {
|
||||
char *json_to_string_with_opts(Arena *arena, const Json * const item, JsonRawOpt raw) {
|
||||
char *out = arena_alloc(arena, 1024);
|
||||
if (!out)
|
||||
return NULL;
|
||||
@@ -304,13 +296,13 @@ char *json_to_string_with_opts(Arena *arena, Json *item, JsonRawOpt raw) {
|
||||
sprintf(ptr, "]");
|
||||
} else if (item->type == JSON_STRING) {
|
||||
if ((int)raw)
|
||||
sprintf(ptr, "%s", item->string);
|
||||
sprintf(ptr, "%s", item->JsonValue.string);
|
||||
else
|
||||
sprintf(ptr, "\"%s\"", item->string);
|
||||
sprintf(ptr, "\"%s\"", item->JsonValue.string);
|
||||
} else if (item->type == JSON_NUMBER) {
|
||||
sprintf(ptr, "%g", item->number);
|
||||
sprintf(ptr, "%g", item->JsonValue.number);
|
||||
} else if (item->type == JSON_BOOL) {
|
||||
sprintf(ptr, item->boolean ? "true" : "false");
|
||||
sprintf(ptr, item->JsonValue.boolean ? "true" : "false");
|
||||
} else if (item->type == JSON_NULL) {
|
||||
sprintf(ptr, "null");
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
#ifndef EPRINTF_HECTIC
|
||||
#define EPRINTF_HECTIC
|
||||
|
||||
// NOTE(yukkop): definitions and features from the POSIX.1-2008 standard
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
@@ -30,7 +33,7 @@ typedef enum {
|
||||
} ColorMode;
|
||||
|
||||
// Static color mode variable
|
||||
static ColorMode color_mode = COLOR_MODE_AUTO;
|
||||
static ColorMode color_mode __attribute__((unused)) = COLOR_MODE_AUTO;
|
||||
|
||||
// Function to set color mode
|
||||
void set_output_color_mode(ColorMode mode);
|
||||
@@ -62,6 +65,7 @@ void set_output_color_mode(ColorMode mode);
|
||||
// ------------
|
||||
|
||||
typedef enum {
|
||||
LOG_LEVEL_TRACE,
|
||||
LOG_LEVEL_DEBUG,
|
||||
LOG_LEVEL_LOG,
|
||||
LOG_LEVEL_INFO,
|
||||
@@ -78,6 +82,15 @@ LogLevel log_level_from_string(const char *level_str);
|
||||
|
||||
char* log_message(LogLevel level, char *file, int line, const char *format, ...);
|
||||
|
||||
#define raise_trace_with_opt(file, line, fmt, ...) log_message(LOG_LEVEL_TRACE, file, line, fmt, ##__VA_ARGS__)
|
||||
#define raise_debug_with_opt(file, line, fmt, ...) log_message(LOG_LEVEL_DEBUG, file, line, fmt, ##__VA_ARGS__)
|
||||
#define raise_log_with_opt(file, line, fmt, ...) log_message(LOG_LEVEL_LOG, file, line, fmt, ##__VA_ARGS__)
|
||||
#define raise_info_with_opt(file, line, fmt, ...) log_message(LOG_LEVEL_INFO, file, line, fmt, ##__VA_ARGS__)
|
||||
#define raise_notice_with_opt(file, line, fmt, ...) log_message(LOG_LEVEL_NOTICE, file, line, fmt, ##__VA_ARGS__)
|
||||
#define raise_warn_with_opt(file, line, fmt, ...) log_message(LOG_LEVEL_WARN, file, line, fmt, ##__VA_ARGS__)
|
||||
#define raise_exception_with_opt(file, line, fmt, ...) log_message(LOG_LEVEL_EXCEPTION, file, line, fmt, ##__VA_ARGS__)
|
||||
|
||||
#define raise_trace(fmt, ...) log_message(LOG_LEVEL_TRACE, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
||||
#define raise_debug(fmt, ...) log_message(LOG_LEVEL_DEBUG, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
||||
#define raise_log(fmt, ...) log_message(LOG_LEVEL_LOG, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
||||
#define raise_info(fmt, ...) log_message(LOG_LEVEL_INFO, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
||||
@@ -89,7 +102,7 @@ char* log_message(LogLevel level, char *file, int line, const char *format, ...)
|
||||
// -- arena --
|
||||
// -----------
|
||||
|
||||
#define ARENA_DEFAULT_SIZE 1024
|
||||
#define ARENA_DEFAULT_SIZE MEM_MiB
|
||||
|
||||
typedef struct {
|
||||
void *begin;
|
||||
@@ -100,6 +113,7 @@ typedef struct {
|
||||
// NOTE(yukkop): This macro is used to define procedures so that `__LINE__` and `__FILE__`
|
||||
// in `raise_debug` reflect the location where the macro is called, not where it's defined.
|
||||
#define arena_alloc_or_null(arena, size) __extension__ ({ \
|
||||
raise_trace("arena_alloc_or_null(%p, %zu)", (arena), (size)); \
|
||||
void *mem__ = NULL; \
|
||||
if ((arena)->begin == 0) { \
|
||||
*(arena) = arena_init(ARENA_DEFAULT_SIZE); \
|
||||
@@ -196,7 +210,17 @@ typedef struct {
|
||||
// -- misc --
|
||||
// ----------
|
||||
|
||||
void substr(const char *src, char *dest, size_t start, size_t len);
|
||||
#define MEM_b 1
|
||||
#define MEM_KiB 1024
|
||||
#define MEM_MiB (MEM_KiB * 1024)
|
||||
#define MEM_GiB (MEM_MiB * 1024)
|
||||
#define MEM_TiB (MEM_TiB * 1024)
|
||||
#define MEM_PiB (MEM_TiB * 1024)
|
||||
#define MEM_EiB (MEM_PiB * 1024)
|
||||
#define MEM_ZiB (MEM_EiB * 1024)
|
||||
#define MEM_YiB (MEM_ZiB * 1024)
|
||||
#define MEM_RiB (MEM_YiB * 1024)
|
||||
#define MEM_QiB (MEM_RiB * 1024)
|
||||
|
||||
// ----------
|
||||
// -- Json --
|
||||
@@ -226,14 +250,14 @@ typedef struct Json {
|
||||
double number;
|
||||
char *string;
|
||||
int boolean;
|
||||
};
|
||||
} JsonValue;
|
||||
} Json;
|
||||
|
||||
Json *json_parse(Arena *arena, const char **s);
|
||||
|
||||
char *json_to_string(Arena *arena, Json *item);
|
||||
char *json_to_string(Arena *arena, const Json * const item);
|
||||
|
||||
char *json_to_string_with_opts(Arena *arena, Json *item, JsonRawOpt raw);
|
||||
char *json_to_string_with_opts(Arena *arena, const Json * const item, JsonRawOpt raw);
|
||||
|
||||
/* Retrieve an object item by key (case-sensitive) */
|
||||
Json *json_get_object_item(Json *object, const char *key);
|
||||
|
||||
101
package/c/hectic/make.sh
Normal file
101
package/c/hectic/make.sh
Normal file
@@ -0,0 +1,101 @@
|
||||
#!/bin/sh
|
||||
# Usage: make.sh [build|check] [--norun] [--debug] [--color]
|
||||
# Options:
|
||||
# build Build the library and app (default if no mode is provided).
|
||||
# check Build tests; runs them unless --norun is specified.
|
||||
# --norun (check only) Build tests but do not run them.
|
||||
# --debug Build with -O0 (debug mode).
|
||||
# --color Pass -fdiagnostics-color=always to compiler.
|
||||
# help, --help Show this help message.
|
||||
|
||||
check_dependencies() {
|
||||
for dep in cc ar; do
|
||||
if ! command -v "$dep" >/dev/null 2>&1; then
|
||||
echo "Error: Required dependency '$dep' not found." >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
}
|
||||
check_dependencies
|
||||
|
||||
print_help() {
|
||||
cat <<EOF
|
||||
Usage: $0 [build|check] [--norun] [--debug] [--color]
|
||||
build Build the library and app (default).
|
||||
check Build tests; runs them unless --norun is specified.
|
||||
--norun (check only) Build tests but do not run them.
|
||||
--debug Build with debug flags (-O0).
|
||||
--color Force colored compiler diagnostics.
|
||||
help, --help Display this help message.
|
||||
EOF
|
||||
}
|
||||
|
||||
# Show help if requested
|
||||
case "$1" in
|
||||
help|--help)
|
||||
print_help
|
||||
exit 0
|
||||
;;
|
||||
esac
|
||||
|
||||
# Default flags
|
||||
RUN_TESTS=1
|
||||
OPTFLAGS="-O2"
|
||||
CFLAGS="-Wall -Wextra -Werror -pedantic -fsanitize=address"
|
||||
LDFLAGS="-lhectic"
|
||||
STD_FLAGS="-std=c99"
|
||||
COLOR_FLAG=""
|
||||
|
||||
MODE="${1:-build}"
|
||||
shift
|
||||
|
||||
# Process options
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
--norun)
|
||||
RUN_TESTS=0
|
||||
;;
|
||||
--debug)
|
||||
OPTFLAGS="-O0"
|
||||
;;
|
||||
--color)
|
||||
COLOR_FLAG="-fdiagnostics-color=always"
|
||||
;;
|
||||
*)
|
||||
echo "Unknown option: $1"
|
||||
print_help
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
if [ -n "$COLOR_FLAG" ]; then
|
||||
CFLAGS="$CFLAGS $COLOR_FLAG"
|
||||
fi
|
||||
|
||||
case "$MODE" in
|
||||
build)
|
||||
mkdir -p target
|
||||
echo "# Build library"
|
||||
# shellcheck disable=SC2086
|
||||
cc $CFLAGS $OPTFLAGS $STD_FLAGS -c hectic.c -lhectic -o target/hectic.o
|
||||
ar rcs target/libhectic.a target/hectic.o
|
||||
;;
|
||||
check)
|
||||
mkdir -p target/test
|
||||
export LOG_LEVEL=TRACE
|
||||
for test_file in test/*.c; do
|
||||
exe="target/test/$(basename "${test_file%.c}")"
|
||||
# shellcheck disable=SC2086
|
||||
cc $CFLAGS $OPTFLAGS -pedantic -I. "$test_file" -Ltarget -lhectic $LDFLAGS -o "$exe"
|
||||
if [ "$RUN_TESTS" -eq 1 ]; then
|
||||
"$exe"
|
||||
fi
|
||||
done
|
||||
;;
|
||||
*)
|
||||
print_help
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
@@ -59,7 +59,7 @@ void test_arena_strdup() {
|
||||
void test_arena_repstr() {
|
||||
Arena arena = arena_init(128);
|
||||
const char *original = "Hello, World!";
|
||||
// Replace substring starting at index 5, length 3 (", W") with " -"
|
||||
// Replace substr_cloneing starting at index 5, length 3 (", W") with " -"
|
||||
// According to the macro logic, the suffix is taken from original[5+3+1] onward.
|
||||
// That results in: "Hello" + " -" + "rld!" = "Hello -rld!"
|
||||
char *result = arena_repstr(&arena, original, 5, 3, " -");
|
||||
|
||||
Reference in New Issue
Block a user