Commit: 66d57d53b801c54c88f4093bf4af465f34e0b9e9
Parent: aab6bcdfb4852018775831a6663901dafe01adcd
Author: Randy Palamar
Date: Mon, 7 Oct 2024 16:35:14 -0600
remove remaining stdlib.h usage
Diffstat:
3 files changed, 35 insertions(+), 10 deletions(-)
diff --git a/beamformer.h b/beamformer.h
@@ -47,7 +47,7 @@ typedef struct {
} BPModifiableValue;
typedef struct {
- char buf[64];
+ u8 buf[64];
BPModifiableValue store;
i32 buf_len;
i32 cursor;
diff --git a/ui.c b/ui.c
@@ -189,12 +189,12 @@ bmv_sprint(BPModifiableValue *bmv, Stream *s)
static void
do_text_input(BeamformerCtx *ctx, i32 max_disp_chars, Rect r, Color colour)
{
- v2 ts = measure_text(ctx->font, (s8){.len = ctx->is.buf_len, .data = (u8 *)ctx->is.buf});
+ v2 ts = measure_text(ctx->font, (s8){.len = ctx->is.buf_len, .data = ctx->is.buf});
v2 pos = {.x = r.pos.x, .y = r.pos.y + (r.size.y - ts.y) / 2};
i32 buf_delta = ctx->is.buf_len - max_disp_chars;
if (buf_delta < 0) buf_delta = 0;
- s8 buf = {.len = ctx->is.buf_len - buf_delta, .data = (u8 *)ctx->is.buf + buf_delta};
+ s8 buf = {.len = ctx->is.buf_len - buf_delta, .data = ctx->is.buf + buf_delta};
{
/* NOTE: drop a char if the subtext still doesn't fit */
v2 nts = measure_text(ctx->font, buf);
@@ -297,7 +297,7 @@ static void
set_text_input_idx(BeamformerCtx *ctx, BPModifiableValue bmv, Rect r, v2 mouse)
{
if (ctx->is.store.value && !bmv_equal(&ctx->is.store, &bmv)) {
- f32 new_val = strtof(ctx->is.buf, NULL);
+ f32 new_val = parse_f64((s8){.len = ctx->is.buf_len, .data =ctx->is.buf});
bmv_store_value(ctx, &ctx->is.store, new_val, 0);
}
@@ -311,7 +311,6 @@ set_text_input_idx(BeamformerCtx *ctx, BPModifiableValue bmv, Rect r, v2 mouse)
bmv_sprint(&bmv, &s);
ASSERT(!s.errors);
ctx->is.buf_len = s.widx;
- ctx->is.buf[ctx->is.buf_len] = 0;
ASSERT(CheckCollisionPointRec(mouse.rl, r.rl));
ctx->is.cursor_hover_p = CLAMP01((mouse.x - r.pos.x) / r.size.w);
@@ -363,8 +362,7 @@ do_text_input_listing(s8 prefix, s8 suffix, BPModifiableValue bmv, BeamformerCtx
b32 bmv_active = bmv_equal(&bmv, &ctx->is.store);
if (bmv_active) {
- txt_s = measure_text(ctx->font, (s8){.len = ctx->is.buf_len,
- .data = (u8 *)ctx->is.buf});
+ txt_s = measure_text(ctx->font, (s8){.len = ctx->is.buf_len, .data = ctx->is.buf});
} else {
bmv_sprint(&bmv, &buf);
txt_s = measure_text(ctx->font, stream_to_s8(buf));
diff --git a/util.c b/util.c
@@ -1,7 +1,5 @@
/* See LICENSE for license details. */
-#include <stdarg.h>
#include <stdio.h>
-#include <stdlib.h>
static void *
mem_clear(u8 *p, u8 c, size len)
@@ -11,7 +9,7 @@ mem_clear(u8 *p, u8 c, size len)
}
static void
-mem_move(char *src, char *dest, size n)
+mem_move(u8 *src, u8 *dest, size n)
{
if (dest < src) while (n) { *dest++ = *src++; n--; }
else while (n) { n--; dest[n] = src[n]; }
@@ -167,6 +165,35 @@ normalize_v3(v3 a)
return result;
}
+static f64
+parse_f64(s8 s)
+{
+ f64 integral = 0, fractional = 0, sign = 1;
+
+ if (s.len && *s.data == '-') {
+ sign = -1;
+ s.data++;
+ s.len--;
+ }
+
+ while (s.len && *s.data != '.') {
+ integral *= 10;
+ integral += *s.data - '0';
+ s.data++;
+ s.len--;
+ }
+
+ if (*s.data == '.') { s.data++; s.len--; }
+
+ while (s.len) {
+ ASSERT(s.data[s.len - 1] != '.');
+ fractional /= 10;
+ fractional += (f64)(s.data[--s.len] - '0') / 10.0;
+ }
+ f64 result = sign * (integral + fractional);
+ return result;
+}
+
static void
fill_hadamard(i32 *m, u32 dim)
{