diff options
| author | TazyFoundSoup <gardendistrict0x0@outlook.com> | 2026-05-22 09:43:59 +1000 |
|---|---|---|
| committer | TazyFoundSoup <gardendistrict0x0@outlook.com> | 2026-05-22 09:43:59 +1000 |
| commit | 77aa9a3259ddfd6c64534d63424d5b0fa3010900 (patch) | |
| tree | 3744ec77c3cbe164457d1e421fd4bf7dceff4c02 /src | |
| parent | 4d42b317f4970bd08f9b81fcc184fc015736ff58 (diff) | |
refactor: move benches to include directory
Diffstat (limited to 'src')
| -rw-r--r-- | src/bridges/stdio/stream_bridge.c | 2 | ||||
| -rw-r--r-- | src/dbg.c | 157 | ||||
| -rw-r--r-- | src/format/image/png.c | 16 |
3 files changed, 156 insertions, 19 deletions
diff --git a/src/bridges/stdio/stream_bridge.c b/src/bridges/stdio/stream_bridge.c index 17a3191..09aad14 100644 --- a/src/bridges/stdio/stream_bridge.c +++ b/src/bridges/stdio/stream_bridge.c @@ -9,7 +9,7 @@ size_t ff_file_read(void *ptr, size_t size, void *user) return fread(ptr, 1, size, f); } -size_t ff_file_write(const void *ptr, size_t size, const void *user) +size_t ff_file_write(const void *ptr, size_t size, void *user) { FILE *f = (FILE *)user; return fwrite(ptr, 1, size, f); @@ -1,26 +1,157 @@ #include <tinyff/stream.h> #include <tinyff/dbg.h> +#include <stdarg.h> +#include <stdint.h> + +static size_t ff_internal_itoa(int value, char* buf) +{ + char temp[12]; + size_t i = 0, j = 0; + + if (value == 0) { + buf[0] = '0'; + return 1; + } + + unsigned int uvalue; + + if (value < 0) { + buf[j++] = '-'; + uvalue = (unsigned int)(-(long)value); + } else { + uvalue = (unsigned int)value; + } + + while (uvalue > 0) { + temp[i++] = (uvalue % 10) + '0'; + uvalue /= 10; + } + + while (i > 0) { + buf[j++] = temp[--i]; + } + + return j; +} ff_result ff_set_debug_stream(ff_stream* stream, ff_ctx* ctx) { - ctx->ff_debug_stream = *stream; - ctx->ff_debug_enabled = (stream != NULL); + if (!ctx) return FF_RESULT_ERROR_NULL_PTR; + + if (stream) { + ctx->ff_debug_stream = *stream; + ctx->ff_debug_enabled = true; + } else { + ctx->ff_debug_enabled = false; + } - // TODO: Add banner back maybe - return FF_RESULT_OK; } -ff_result ff_dprintf(ff_ctx* ctx, const char *msg) +static inline void ff_debug_flush(ff_ctx* ctx, char* buffer, size_t* idx) +{ + if (*idx == 0) return; + + ctx->ff_debug_stream.write( + &ctx->ff_debug_stream, + *idx, + (uint8_t*)buffer + ); + + *idx = 0; +} + +ff_result ff_dprintf(ff_ctx* ctx, const char *format, ...) { - if (!ctx->ff_debug_enabled){ + if (!ctx || !ctx->ff_debug_enabled || !ctx->ff_debug_stream.write) { return FF_RESULT_WARN_DEBUG_DISABLED; } - - if (!ctx->ff_debug_stream.write){ - return FF_RESULT_WARN_DEBUG_DISABLED; + + va_list args; + va_start(args, format); + + char buffer[512]; + size_t buf_idx = 0; + const char* p = format; + + while (*p) { + + if (buf_idx >= sizeof(buffer) - 64) { + ff_debug_flush(ctx, buffer, &buf_idx); + } + + if (*p == '%') { + p++; + + if (!*p) break; + + if (*p == 's') { + const char* s = va_arg(args, const char*); + if (!s) s = "(null)"; + + while (*s) { + buffer[buf_idx++] = *s++; + + if (buf_idx >= sizeof(buffer)) { + ff_debug_flush(ctx, buffer, &buf_idx); + } + } + } + else if (*p == 'd') { + int d = va_arg(args, int); + buf_idx += ff_internal_itoa(d, &buffer[buf_idx]); + } + else if (*p == 'u') { + unsigned int u = va_arg(args, unsigned int); + buf_idx += ff_internal_itoa((int)u, &buffer[buf_idx]); + } + else if (*p == 'l' && *(p + 1) == 'd') { + long d = va_arg(args, long); + buf_idx += ff_internal_itoa((int)d, &buffer[buf_idx]); + p++; + } + else if (*p == 'f') { + double f = va_arg(args, double); + + if (f < 0) { + buffer[buf_idx++] = '-'; + f = -f; + } + + int whole = (int)f; + double frac = f - whole; + + buf_idx += ff_internal_itoa(whole, &buffer[buf_idx]); + + buffer[buf_idx++] = '.'; + + for (int i = 0; i < 3; i++) { + frac *= 10.0; + int digit = (int)frac; + buffer[buf_idx++] = '0' + digit; + frac -= digit; + } + } + else if (*p == 'c') { + buffer[buf_idx++] = (char)va_arg(args, int); + } + else if (*p == '%') { + buffer[buf_idx++] = '%'; + } + else { + buffer[buf_idx++] = '%'; + buffer[buf_idx++] = *p; + } + } + else { + buffer[buf_idx++] = *p; + } + + p++; } - - ctx->ff_debug_stream.write(msg, ff_strlen(msg), ctx->ff_debug_stream.user); - return FF_RESULT_OK; // result_ok means the result is ok -} + + ff_debug_flush(ctx, buffer, &buf_idx); + + va_end(args); + return FF_RESULT_OK; +}
\ No newline at end of file diff --git a/src/format/image/png.c b/src/format/image/png.c index 12080d6..d5959bc 100644 --- a/src/format/image/png.c +++ b/src/format/image/png.c @@ -483,10 +483,16 @@ ff_result ff_close_png(ff_ctx* ctx, ff_png_ctx *png_ctx) ff_result ff_write_chunk(ff_stream *stream, const char *type, uint8_t *buf, size_t len) { - stream->write(stream, 4, type); - stream->write(stream, 4, (uint32_t*)&len); - stream->write(stream, len, buf); - stream->write(stream, 4, (uint32_t*)0); // TODO: Implement actual CRC calculation (use ff_write_be32) + stream->write(type, 4, stream->user); + + uint32_t be_len = (uint32_t)len; + stream->write(&be_len, 4, stream->user); + + stream->write(buf, len, stream->user); + + uint32_t zero_crc = 0; + stream->write(&zero_crc, 4, stream->user); + return FF_RESULT_OK; } @@ -495,7 +501,7 @@ ff_result ff_encode_png(ff_ctx *ctx, ff_png_ctx *png_ctx, ff_stream *stream) FF_BENCH_START(ctx, "png"); FF_BENCH_MARK(ctx, "ff_encode_png"); - stream->write(stream, 8, PNG_SIGNATURE); + stream->write(stream, 8, (void *)PNG_SIGNATURE); // TODO: Actually write the PNG data // For now I'll void the parameters |
