colourpicker

Simple Colour Picker written in C
git clone anongit@rnpnr.xyz:colourpicker.git
Log | Files | Refs | Feed | Submodules | README | LICENSE

Commit: ba8927b4546c26a0d9a350f387ff683708896be1
Parent: dff4c4c9a71fea12db94dfe3f7d7262637b51e95
Author: Randy Palamar
Date:   Wed, 31 Jul 2024 21:57:07 -0600

drop non-standard getline from gen_incs

Diffstat:
Mbuild.sh | 10+++++-----
Mgen_incs.c | 53++++++++++++++++++++++++++++++++++++++++-------------
2 files changed, 45 insertions(+), 18 deletions(-)

diff --git a/build.sh b/build.sh @@ -31,6 +31,11 @@ fi [ ! -s "config.h" ] && cp config.def.h config.h +if [ ! -e "shader_inc.h" ] || [ "hsv_lerp.glsl" -nt "shader_inc.h" ]; then + ${cc} $cflags -o gen_incs gen_incs.c $ldflags -flto -s + ./gen_incs +fi + if [ "$debug" ]; then # Hot Reloading/Debugging cflags="$cflags -O0 -ggdb -D_DEBUG -Wno-unused-function" @@ -43,9 +48,4 @@ if [ "$debug" ]; then ${cc} $libcflags colourpicker.c -o libcolourpicker.so $libldflags fi -if [ "hsv_lerp.glsl" -nt "shader_inc.h" ]; then - ${cc} $cflags -D_XOPEN_SOURCE=600 -o gen_incs gen_incs.c -s $ldflags - ./gen_incs -fi - ${cc} $cflags -o colourpicker main.c $ldflags diff --git a/gen_incs.c b/gen_incs.c @@ -1,12 +1,31 @@ #include <raylib.h> -#include <stdlib.h> +#include <stddef.h> +#include <stdint.h> #include <stdio.h> +#include <stdlib.h> #include "config.h" #define ISSPACE(a) ((a) == ' ' || (a) == '\t') +typedef struct {int8_t *data; ptrdiff_t len;} s8; + +static s8 +get_line(s8 *s) +{ + s8 res = {.data = s->data}; + while (s->len && s->data[0] != '\n') { + s->data++; + s->len--; + res.len++; + } + /* NOTE: skip over trailing \n */ + s->data++; + s->len--; + return res; +} + int main(void) { @@ -26,25 +45,33 @@ main(void) return 1; } - char *line = NULL; - size_t len = 0; - ssize_t read; + fseek(shader_file, 0, SEEK_END); + s8 shader_data = {.len = ftell(shader_file)}; + rewind(shader_file); + shader_data.data = malloc(shader_data.len); + if (!shader_data.data) { + fputs("Failed to allocate space for reading shader file!\n", stdout); + return 1; + } + fread(shader_data.data, shader_data.len, 1, shader_file); + + s8 s = shader_data; /* NOTE: skip over license notice */ - getline(&line, &len, shader_file); + s8 line = get_line(&s); fputs("static char *g_hsv_shader_text =\n\t", out_file); - while ((read = getline(&line, &len, shader_file)) != -1) { - char *s; - for (s = line; *s; s++, read--) { - if (!ISSPACE(*s)) - break; + do { + line = get_line(&s); + while (line.len && ISSPACE(*line.data)) { + line.data++; + line.len--; } - if (read > 1) { + if (line.len) { fputc('"', out_file); - fwrite(s, read - 1, 1, out_file); + fwrite(line.data, line.len, 1, out_file); fputs("\\n\"\n\t", out_file); } - } + } while (s.len > 0); fputs(";\n", out_file); fclose(out_file); fclose(shader_file);