Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .codex/environments/environment.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# THIS IS AUTOGENERATED. DO NOT EDIT MANUALLY
version = 1
name = "mise"

[setup]
script = '''
set -euo pipefail

cd "${CODEX_WORKTREE_PATH:-$PWD}"

mise trust
'''
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CPPCHECK := $(shell if command -v cppcheck >/dev/null 2>&1; then echo cppcheck;
CLANG_TIDY_EXTRA_ARGS := $(shell if [ "$$(uname)" = "Darwin" ]; then echo "--extra-arg=--sysroot=$$(xcrun --show-sdk-path)"; fi)

SRC_FILES := src/*.c include/*.h
TEST_FILES := tests/unit/*.c
TEST_FILES := $(wildcard tests/unit/*.c tests/api/*.c)
CMD_FILES := $(wildcard cmd/*/*.c cmd/*/*.h)
ALL_FILES := $(SRC_FILES) $(TEST_FILES) $(CMD_FILES)
LINT_FILES := $(shell find src tests -type f -name '*.c')
Expand Down
2 changes: 1 addition & 1 deletion include/tl_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
*
* @return void
*/
void tl_init_app(int argc, char *argv[]);
void tl_app_init(int argc, char *argv[]);

#endif // TL_APP_H
18 changes: 9 additions & 9 deletions include/tl_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
* @note Available for certain ARM architectures.
*/
#if defined(__ARM_NEON) || defined(__ARM_NEON__)
#define TL_NEON_AVAILABLE 1
#define TL_HAS_NEON 1
#else
#define TL_NEON_AVAILABLE 0
#define TL_HAS_NEON 0
#endif

/**
Expand All @@ -23,39 +23,39 @@
* @note Available for all ARM architectures.
*/
#ifdef __ARM_ARCH
#define TL_CMSIS_DSP_AVAILABLE 1
#define TL_HAS_CMSIS_DSP 1
#else
#define TL_CMSIS_DSP_AVAILABLE 0
#define TL_HAS_CMSIS_DSP 0
#endif

/**
* @brief Returns the debug level.
*
* @return The debug level.
*/
int tl_get_debug_level();
int tl_config_get_debug_level();

/**
* @brief Sets the debug level.
*
* @param level The debug level.
*
* @return True or false.
* @return void
*/
bool tl_set_debug_level(int level);
void tl_config_set_debug_level(int level);

/**
* @brief Returns whether ARM NEON is available.
*
* @return True or false.
*/
bool tl_neon_available();
bool tl_config_has_neon();

/**
* @brief Returns whether ARM CMSIS-DSP is available.
*
* @return True or false.
*/
bool tl_cmsis_dsp_available();
bool tl_config_has_cmsis_dsp();

#endif // TL_CONFIG_H
8 changes: 4 additions & 4 deletions include/tl_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
*
* @return void
*/
#define TL_DEBUG_PRINT(level, fmt, ...) \
do { \
if (tl_get_debug_level() >= level) \
fprintf(stderr, fmt, ##__VA_ARGS__); \
#define TL_DEBUG_PRINT(level, fmt, ...) \
do { \
if (tl_config_get_debug_level() >= level) \
fprintf(stderr, fmt, ##__VA_ARGS__); \
} while (0)

#endif // TL_DEBUG_H
13 changes: 6 additions & 7 deletions include/tl_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ typedef enum {
TL_ERROR_INVALID_SIZE = 36, // invalid size
TL_ERROR_INVALID_TYPE = 37, // invalid type
TL_ERROR_INVALID_VALUE = 38, // invalid value
} TLErrorCode;
} TlErrorCode;

/**
* @brief Represents an error.
Expand All @@ -40,21 +40,20 @@ typedef enum {
* @param message The error message.
*/
typedef struct {
TLErrorCode code;
TlErrorCode code;
size_t message_size;
const char *message;
} TLError;
} TlError;

/**
* @brief Sets the error with the given code and message.
*
* @param error The error to set.
* @param code The error code.
* @param message The error message.
* @param ... The arguments for the formatted message.
* @param ... The error message followed by the formatted message arguments.
*
* @return void
* @return TL_ERROR_NONE on success, or an error code on failure.
*/
void tl_error_set(TLError *error, TLErrorCode code, const char *message, ...);
int tl_error_set_message(TlError *error, TlErrorCode code, ...);

#endif // TL_ERROR_H
37 changes: 19 additions & 18 deletions include/tl_flag.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ typedef enum {
*
* @return The argv index, or TL_ARG_NOT_FOUND when not found.
*/
size_t tl_arg_index(int argc, char *argv[], const char *name);
size_t tl_flag_get_arg_index(int argc, char *argv[], const char *name);

/**
* @brief Returns the first argv index matching the given name after an index.
Expand All @@ -93,13 +93,13 @@ size_t tl_arg_index(int argc, char *argv[], const char *name);
*
* @return The argv index, or TL_ARG_NOT_FOUND when not found.
*/
size_t tl_arg_index_after(int argc, char *argv[], const char *name, size_t index);
size_t tl_flag_get_arg_index_after(int argc, char *argv[], const char *name, size_t index);

/**
* @brief Parses the given command line arguments with options.
*
* Default parsing is used when options is NULL or both option lists are NULL.
* With default parsing this behaves like tl_parse_args. Strict mode is enabled
* With default parsing this behaves like tl_flag_parse_args. Strict mode is enabled
* when either option list is non-NULL.
*
* @param argc The number of command line arguments.
Expand All @@ -108,7 +108,8 @@ size_t tl_arg_index_after(int argc, char *argv[], const char *name, size_t index
*
* @return TL_PARSE_OK on success, or a negative parse error.
*/
TlParseResult tl_parse_args_ex(int argc, char *argv[], const TlParseOptions *options);
TlParseResult tl_flag_parse_args_with_options(int argc, char *argv[],
const TlParseOptions *options);

/**
* @brief Parses an explicit argv range.
Expand All @@ -125,8 +126,8 @@ TlParseResult tl_parse_args_ex(int argc, char *argv[], const TlParseOptions *opt
*
* @return TL_PARSE_OK on success, or a negative parse error.
*/
TlParseResult tl_parse_args_range(int argc, char *argv[], size_t start_index, size_t end_index,
const TlParseOptions *options);
TlParseResult tl_flag_parse_args_range(int argc, char *argv[], size_t start_index, size_t end_index,
const TlParseOptions *options);

/**
* @brief Parses the given command line arguments.
Expand All @@ -143,7 +144,7 @@ TlParseResult tl_parse_args_range(int argc, char *argv[], size_t start_index, si
*
* @return TL_PARSE_OK on success, or a negative parse error.
*/
TlParseResult tl_parse_args(int argc, char *argv[]);
TlParseResult tl_flag_parse_args(int argc, char *argv[]);

/**
* @brief Parses a raw command line string.
Expand All @@ -156,17 +157,17 @@ TlParseResult tl_parse_args(int argc, char *argv[]);
*
* @return TL_PARSE_OK on success, or a negative parse error.
*/
TlParseResult tl_parse_line(const char *line);
TlParseResult tl_flag_parse_line(const char *line);

/**
* @brief Releases memory held by the argument parser.
*
* Safe to call when nothing has been parsed. Called implicitly by
* tl_parse_args and tl_parse_line.
* tl_flag_parse_args and tl_flag_parse_line.
*
* @return void
*/
void tl_free_args(void);
void tl_flag_free_args(void);

/**
* @brief Looks up a specific flag.
Expand All @@ -175,19 +176,19 @@ void tl_free_args(void);
*
* @return true if the flag is found, false otherwise.
*/
bool tl_lookup_flag(const char *flag);
bool tl_flag_has_flag(const char *flag);

/**
* @brief Returns the value of a specific flag.
*
* Returns the value of the first occurrence of flag. For repeated flags
* use tl_count_flag and tl_get_flag_at.
* use tl_flag_count_flag and tl_flag_get_value_at.
*
* @param flag The flag to get.
*
* @return The value of the flag, or NULL if not found or no value.
*/
const char *tl_get_flag(const char *flag);
const char *tl_flag_get_value(const char *flag);

/**
* @brief Returns the number of times a flag was given.
Expand All @@ -196,7 +197,7 @@ const char *tl_get_flag(const char *flag);
*
* @return The occurrence count (0 if not given).
*/
size_t tl_count_flag(const char *flag);
size_t tl_flag_count_flag(const char *flag);

/**
* @brief Returns the value of a repeated flag at a given index.
Expand All @@ -208,7 +209,7 @@ size_t tl_count_flag(const char *flag);
*
* @return The value, or NULL if out of range or no value at that index.
*/
const char *tl_get_flag_at(const char *flag, size_t index);
const char *tl_flag_get_value_at(const char *flag, size_t index);

/**
* @brief Looks up a specific positional argument by value.
Expand All @@ -217,7 +218,7 @@ const char *tl_get_flag_at(const char *flag, size_t index);
*
* @return true if the positional is found, false otherwise.
*/
bool tl_lookup_positional(const char *value);
bool tl_flag_has_positional(const char *value);

/**
* @brief Returns the number of positional arguments.
Expand All @@ -227,7 +228,7 @@ bool tl_lookup_positional(const char *value);
*
* @return The positional argument count.
*/
size_t tl_count_positional(void);
size_t tl_flag_count_positional(void);

/**
* @brief Returns the positional argument at the given index.
Expand All @@ -236,6 +237,6 @@ size_t tl_count_positional(void);
*
* @return The positional value, or NULL if out of range.
*/
const char *tl_get_positional(size_t index);
const char *tl_flag_get_positional(size_t index);

#endif // TL_FLAG_H
2 changes: 1 addition & 1 deletion include/tl_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
*
* @return The difference between the two timespec structures in nanoseconds.
*/
long long tl_timespec_diff_ns(const struct timespec *start, const struct timespec *end);
long long tl_test_get_timespec_diff_ns(const struct timespec *start, const struct timespec *end);

#endif // TL_TEST_H
8 changes: 4 additions & 4 deletions src/tl_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#include "tl_flag.h"
#include <stdlib.h>

void tl_init_app(int argc, char *argv[]) {
tl_parse_args(argc, argv);
if (tl_get_flag("--debug-level")) {
tl_set_debug_level((int)strtol(tl_get_flag("--debug-level"), NULL, 10));
void tl_app_init(int argc, char *argv[]) {
tl_flag_parse_args(argc, argv);
if (tl_flag_get_value("--debug-level")) {
tl_config_set_debug_level((int)strtol(tl_flag_get_value("--debug-level"), NULL, 10));
}
}
13 changes: 6 additions & 7 deletions src/tl_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,24 @@
*/
static int debug_level = 0;

int tl_get_debug_level() {
int tl_config_get_debug_level() {
return debug_level;
}

bool tl_set_debug_level(int level) {
void tl_config_set_debug_level(int level) {
debug_level = level;
return true;
}

bool tl_neon_available() {
#if TL_NEON_AVAILABLE
bool tl_config_has_neon() {
#if TL_HAS_NEON
return true;
#else
return false;
#endif
}

bool tl_cmsis_dsp_available() {
#if TL_CMSIS_DSP_AVAILABLE
bool tl_config_has_cmsis_dsp() {
#if TL_HAS_CMSIS_DSP
return true;
#else
return false;
Expand Down
30 changes: 18 additions & 12 deletions src/tl_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#include <stdlib.h>
#include <string.h>

void tl_error_set(TLError *error, TLErrorCode code, const char *message, ...) {
// Ignore if error is NULL
int tl_error_set_message(TlError *error, TlErrorCode code, ...) {
if (!error) {
return;
return TL_ERROR_INVALID_ARGUMENT;
}

// Set error code and message
// Set error code
error->code = code;

// Clear previous error message
if (error->message) {
free((void *)error->message);
Expand All @@ -22,24 +22,30 @@ void tl_error_set(TLError *error, TLErrorCode code, const char *message, ...) {
}

// Set message if provided
va_list args;
va_start(args, code);
const char *message = va_arg(args, const char *);
if (message) {
va_list args;
va_start(args, message);
int size = vsnprintf(NULL, 0, message, args);
va_end(args);
va_list format_args;
va_copy(format_args, args);
int size = vsnprintf(NULL, 0, message, format_args);
va_end(format_args);

if (size < 0) {
return; // ignore if vsnprintf fails
va_end(args);
return TL_ERROR_INTERNAL;
}

error->message = malloc(size + 1); // include null terminator by adding 1
if (!error->message) {
return; // ignore if malloc fails
va_end(args);
return TL_ERROR_MEMORY_ALLOCATION;
}
error->message_size = size + 1;

va_start(args, message);
vsnprintf((char *)error->message, error->message_size, message, args);
va_end(args);
}
va_end(args);

return TL_ERROR_NONE;
}
Loading