Skip to content

Instantly share code, notes, and snippets.

Avatar

Louis Warren louisswarren

View GitHub Profile
@louisswarren
louisswarren / Makefile
Last active March 10, 2023 23:17
Include files at compile time in C
View Makefile
.PHONY: test
test: main
./$<
main: main.o incl_hello.o incl_message.o
main.o: main.c include_file.h
incl_%.o: incl_%.txt
@# If you always want to use includes as strings, you can check that
@# they are null-terminated at compile-time:
@louisswarren
louisswarren / trim.sh
Created February 9, 2023 00:29
Trim audio files with fade
View trim.sh
#!/bin/sh
# Usage:
# trim FILE START END FADE_IN FADE_OUT
# 1 2 3 4 5
# Trim the file between START and END, with fade in and out, and
# 2 seconds of silence after
dur=`expr "${3%:*}" \* 60 + "${3#*:}" - "${2%:*}" \* 60 - "${2#*:}"`
fo=`expr "$dur" - $5`
@louisswarren
louisswarren / vet.sh
Created February 6, 2023 02:00
Vet which media files to copy
View vet.sh
#!/bin/sh
vet() {
echo "Playing $1"
mpv --no-audio-display "$1"
read x
if [ "$x" == "y" ]; then
cp -v "$1" "$2/"
else
echo "Skipping $1"
fi
@louisswarren
louisswarren / html.c
Created January 29, 2023 02:23
HTML generation in C
View html.c
#include <stdio.h>
static const char *closebuff[1024];
static const char tabs[] = "\t\t\t\t\t\t\t\t";
void
put(int *t, const char *msg)
{
const char *indent = tabs + sizeof(tabs) - 1 - *t;
printf("%s%s\n", indent < tabs ? tabs : indent, msg);
@louisswarren
louisswarren / fullwidth.c
Last active January 13, 2023 21:57
Fullwidth text is even more upper case than upper case
View fullwidth.c
#include <stdio.h>
#include <stdint.h>
int
main(void)
{
int c, d;
while ((c = getchar()) != EOF) {
d = c & 0x1f;
if ((c | 0x20) > 0x60 && d < 26) {
@louisswarren
louisswarren / spectrum.c
Last active December 30, 2022 08:00
Generate all hues in the colour spectrum
View spectrum.c
/*
* Idea: We want to cycle through all hues, with maximum saturation and value,
* using RGB. This gives 1530 = 3! * (256 - 3!) colours:
000-0fe: ff 00-fe 00
0ff-1fd: ff-01 ff 00
1fe-2fc: 00 ff 00-fe
2fd-3fb: 00 ff-01 ff
3fc-4fa: 00-fe 00 ff
4fb-5f9: ff 00 ff-01
@louisswarren
louisswarren / coding-style.sql
Created December 3, 2022 00:55
My coding style for SQL
View coding-style.sql
/* Idea:
* Use 8 spaces for indenting the body of clauses
* Use 4 spaces for all other indentation
* Set your editor to expand tab to 4 spaces
This is highly readible while minimising effort while writing.
Indentation that requires manual character-level alignment would be
extremely painful (for me).
*/
with
@louisswarren
louisswarren / tabulate.py
Last active December 9, 2022 22:34
Tabulation in python, in place of spreadsheets
View tabulate.py
class RowRef:
def __init__(self, colmap, rows, idx, default = None):
self.colmap = colmap
self.rows = rows
self.idx = idx
self.default = default
def __getitem__(self, colname):
if not 0 <= self.idx < len(self.rows):
return self.default
@louisswarren
louisswarren / .gitignore
Last active August 21, 2022 00:02
CPython testing
View .gitignore
*.o
*.o
primes.*.so
@louisswarren
louisswarren / private_ips.py
Last active July 9, 2022 00:18
All private IPv4 addresses, in all formats
View private_ips.py
IPV4_OCTET_TYPES = {
4: (24, 16, 8, 0,),
3: (24, 16, 0,),
2: (24, 0,),
1: ( 0,),
}
def num(ip):
octets = tuple(map(int, ip.split('.')))
bases = IPV4_OCTET_TYPES[len(octets)]