examples

Toy examples in single C files.
git clone git://henryandlizzy.uk/examples
Log | Files | Refs

commit 78e7d57d4b5f79651731d3a16ecf4e5a37b9d70b
parent 5b8b2bf1476f25adf89fdd439144cfee3ee7186b
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Sat,  6 Aug 2022 00:33:23 +0100

Fix warnings and stricten to -Wextra

Diffstat:
Mmakefile | 5+++--
Msrc/alsa-simple.c | 2+-
Msrc/cie-1931.cpp | 4++--
Msrc/cobs.c | 4++--
Msrc/coro-timer-dispatch.cpp | 1+
Msrc/dsp.c | 4++--
Msrc/flat-set.cpp | 1+
Msrc/gl-asteroids.cpp | 6+++---
Msrc/guess-number.c | 2+-
Msrc/io_uring.cpp | 9++++-----
Msrc/pulse-async-client.cpp | 14+++-----------
Msrc/pulse-simple-client.c | 2+-
Msrc/regex.c | 2+-
Msrc/sorts.cpp | 1+
Msrc/token-threaded-forth.cpp | 6++----
15 files changed, 28 insertions(+), 35 deletions(-)

diff --git a/makefile b/makefile @@ -1,5 +1,6 @@ -CXXFLAGS := -std=c++20 - +WFLAGS := -Wall -Wextra -Werror +CFLAGS := $(WFLAGS) +CXXFLAGS := -std=c++20 $(WFLAGS) c_sources := $(wildcard src/*.c) cpp_sources := $(wildcard src/*.cpp) diff --git a/src/alsa-simple.c b/src/alsa-simple.c @@ -6,7 +6,7 @@ void maybe_die(int err, char const* name) if (err >= 0) return; - printf("Playback open error: %s\n", snd_strerror(err)); + printf("%s: %s\n", name, snd_strerror(err)); exit(1); } diff --git a/src/cie-1931.cpp b/src/cie-1931.cpp @@ -41,8 +41,8 @@ struct XYZ } }; -struct XYZ xyzFromWavelength(double x) { - struct XYZ color; +struct XYZ xyzFromWavelength(double) { + struct XYZ color; // TODO return color; } diff --git a/src/cobs.c b/src/cobs.c @@ -38,7 +38,7 @@ size_t cobs_encode(void* buf, void const* data, size_t len) *code_out = out - code_out; *out++ = 0; - assert(out - (unsigned char*)buf <= cobs_encoded_max_size(len)); + assert(out - (unsigned char*)buf <= (ssize_t)cobs_encoded_max_size(len)); return out - (unsigned char*)buf; } @@ -75,7 +75,7 @@ size_t cobs_decode(void* buf, void const* data, size_t len) struct { size_t de_siz; - unsigned char* decoded, *encoded; + char* decoded, *encoded; } const tests[] = { {1, "\0", "\x01\x01"}, {1, "A", "\x02" "A"}, diff --git a/src/coro-timer-dispatch.cpp b/src/coro-timer-dispatch.cpp @@ -1,6 +1,7 @@ #include <coroutine> #include <iostream> #include <list> +#include <algorithm> #include <unistd.h> static int jiffies; diff --git a/src/dsp.c b/src/dsp.c @@ -59,7 +59,7 @@ float* fmalloc_or_die(unsigned n) if (!p) die("malloc"); - for (int i = 0; i < n; ++i) + for (unsigned i = 0; i < n; ++i) p[i] = 0; return p; } @@ -106,7 +106,7 @@ void graph(float signal, float out) putchar('\n'); } -int main(int argc, char const* argv[]) +int main() { in_len = 2; out_len = 2; diff --git a/src/flat-set.cpp b/src/flat-set.cpp @@ -1,6 +1,7 @@ #include <vector> #include <iostream> #include <iterator> +#include <algorithm> template <typename T> struct flat_set diff --git a/src/gl-asteroids.cpp b/src/gl-asteroids.cpp @@ -54,7 +54,7 @@ void sound_routine(std::stop_token token) } } - snd_pcm_sframes_t frames = snd_pcm_writei(handle, buf.data(), buf.size()); + snd_pcm_writei(handle, buf.data(), buf.size()); } snd_pcm_drain(handle); snd_pcm_close(handle); @@ -392,7 +392,7 @@ visual const& random_rock_visual(void) return *rock_types[rtype]; } -int main(int argc, char* argv[]) +int main(int, char* argv[]) { signed score = 0; float firerate = 2, bullet_speed = 1; @@ -408,7 +408,7 @@ int main(int argc, char* argv[]) std::array<short, 2 * 512> square; for (unsigned i = 0; i < square.size(); ++i) - square[i] = (i & 0x80 - 0x40) * 50; + square[i] = ((i & 0x80) - 0x40) * 50; std::array<short, 3 * 512> saw; for (unsigned i = 0; i < saw.size(); ++i) diff --git a/src/guess-number.c b/src/guess-number.c @@ -35,6 +35,6 @@ int main() time_t end = time(NULL); - printf("Congratultions, your score is %u\n", score * (end - start)); + printf("Congratultions, your score is %lu\n", score * (end - start)); return 0; } diff --git a/src/io_uring.cpp b/src/io_uring.cpp @@ -109,23 +109,22 @@ awaitable uring::read(int fd, std::span<char> buf, unsigned flags) { auto sqe = get_sqe(); io_uring_prep_read(sqe, fd, buf.data(), buf.size(), flags); - return {*this, *sqe}; + return {*this, *sqe, {}}; } awaitable uring::write(int fd, std::span<char const> buf, unsigned flags) { auto sqe = get_sqe(); io_uring_prep_write(sqe, fd, buf.data(), buf.size(), flags); - return {*this, *sqe}; + return {*this, *sqe, {}}; } task read_routine(uring& u) { char b[128]; std::span<char> buf(b); - int fd = 1; - unsigned flags = 0; int n = co_await u.read(0, buf, 0); - co_await u.write(1, {b, n}, 0); + if (n > 0) + co_await u.write(1, {b, (std::size_t)n}, 0); co_return; } diff --git a/src/pulse-async-client.cpp b/src/pulse-async-client.cpp @@ -46,12 +46,8 @@ void wavegen(std::span<short> buf) void pulse_state_cb(pa_context* c, void*) { std::cout << "pulse state = " << pa_context_get_state(c) << '\n'; - switch (pa_context_get_state(c)) - { - case PA_CONTEXT_READY: + if (pa_context_get_state(c) == PA_CONTEXT_READY) ready = true; - break; - } } void write_cb(pa_stream* s, size_t, void*) @@ -65,7 +61,7 @@ void write_cb(pa_stream* s, size_t, void*) pa_stream_write(s, buf, len, nullptr, 0, PA_SEEK_RELATIVE); } -void underflow_cb(pa_stream* s, void*) +void underflow_cb(pa_stream*, void*) { std::cout << __func__ << '\n'; } @@ -121,8 +117,6 @@ int main(void) pos = sample.cbegin(); } - int latency = 20000; - mainloop mainloop{}; auto api = mainloop.get_api(); @@ -144,12 +138,10 @@ int main(void) }; auto playstream = pa_stream_new(ctx, "playback", &ss, nullptr); assert(playstream); - + pa_stream_set_write_callback(playstream, write_cb, nullptr); pa_stream_set_underflow_callback(playstream, underflow_cb, nullptr); - uint32_t len = pa_usec_to_bytes(latency, &ss); - uint32_t minreq = pa_usec_to_bytes(0, &ss); pa_buffer_attr bufattr = { .maxlength = (uint32_t)-1, diff --git a/src/pulse-simple-client.c b/src/pulse-simple-client.c @@ -3,7 +3,7 @@ #include <math.h> -int main(int argc, char* argv[]) +int main(int, char* argv[]) { pa_sample_spec ss = { .format = PA_SAMPLE_S16NE, diff --git a/src/regex.c b/src/regex.c @@ -18,7 +18,7 @@ char const* const tests[] = -int main(int argc, char* argv[]) +int main() { //char const re[] = R"~(^(?:(\d+)|([^=]+))=(?:(\d+)|([^=]+))$)~"; char const re[] = "^((\\d+)|([^=]+))=((\\d+)|([^=]+))$"; diff --git a/src/sorts.cpp b/src/sorts.cpp @@ -6,6 +6,7 @@ #include <span> #include <numeric> #include <string_view> +#include <algorithm> template <typename T> void bubble_sort(std::span<T> const& elements) diff --git a/src/token-threaded-forth.cpp b/src/token-threaded-forth.cpp @@ -28,7 +28,7 @@ int main() &&negate, &&increment, }; - char const code[] = + unsigned char const code[] = { LITERAL(6), DUP, @@ -47,11 +47,9 @@ int main() EXIT, }; std::intptr_t stack[1024]; - char const* return_stack[1024]; - char const** ret = return_stack; std::intptr_t* top = stack; - char const* instruction = code; + unsigned char const* instruction = code; next: goto *tokens[*instruction++];