Skip to content

Instantly share code, notes, and snippets.

@mofosyne
mofosyne / augmented_enum_macro_error_code.c
Last active November 16, 2024 14:25
Self Describing Error Code Enum Macro (Or other status tracking variables) V2
///usr/bin/env ccache gcc -Wall -Wextra -Werror -O3 -std=gnu17 "$0" -o /tmp/a -lm && /tmp/a "$@"; exit
#include <stdint.h>
#include <stdio.h>
/*
Self Describing Error Code Enum Macro (Or other status tracking variables) V2
Author: Brian Khuu (2024)
This is an idea I got for easier management of error codes and other enums.
The benefit of this approach is that it provides a way of having self
@mofosyne
mofosyne / self_describing_error_code_macros.c
Created November 13, 2024 13:55
Self Describing Error Code Enum Macro (Or other status tracking variables) In C
/*
Self Describing Error Code Enum Macro (Or other status tracking variables) In C
Author: Brian Khuu (2024)
This is an idea I got for easier management of error codes and other enums.
The benefit of this approach is that it provides a way of having self
documenting enumerated values which would be useful for debug loggers.
Ergo, your debug log could print the meaning of an error or status message.
*/
@mofosyne
mofosyne / bounded_and_clamped_value_macro.c
Last active November 12, 2024 11:17
Bounded And Clamped Value Macros (Useful for guarding against invalid integer ranges)
#!/usr/bin/tcc -run
// Bounded Value Macros (Useful for guarding against invalid integer ranges)
#define clamp_upper(value, max) ((value) < (max) ? (value) : (max))
#define clamp_lower(value, min) ((value) > (min) ? (value) : (min))
#define clamp_range(value, min, max) clamp_lower(min, clamp_upper(value, max))
#define is_above_bound(value, max) ((value) > (max))
#define is_below_bound(value, min) ((value) < (min))
#define is_within_bound(value, min, max) ((value) >= (min) && (value) <= (max))
@mofosyne
mofosyne / gnu_make_4.3_Implicit_Rules_And_Variables.make
Last active October 27, 2024 11:42
GNU Make 4.3 Implicit Variables And Rules `make -p 2>/dev/null | sed '/^# environment$/,/^[^#]/d'` (`2>/dev/null` to suppress error message) (sed is to remove sensitive enviromental variables)
# GNU Make 4.3
# Built for x86_64-pc-linux-gnu
# Copyright (C) 1988-2020 Free Software Foundation, Inc.
# License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
# This is free software: you are free to change and redistribute it.
# There is NO WARRANTY, to the extent permitted by law.
# Make data base, printed on Sun Oct 27 22:42:28 2024
# Variables
@mofosyne
mofosyne / sexp_formatter.py
Last active September 22, 2024 15:49
KiCADv8 Style Prettyfy S-Expression Formatter (sexp formatter)
#!/usr/bin/env python3
# KiCADv8 Style Prettify S-Expression Formatter (sexp formatter)
# By Brian Khuu, 2024
# This script reformats KiCad-like S-expressions to match a specific formatting style.
# Note: This script modifies formatting only; it does not perform linting or validation.
# Context: Compact element settings are added to support KiCAD-specific handling for readability, e.g., PCB_PLUGIN::formatPolyPts.
import os
import argparse
from pathlib import Path
@mofosyne
mofosyne / digikey_api_v4_product_search.py
Created September 16, 2024 15:42
Digikey Basic Product Search Script (Targeting Digikey API v4)
#!/usr/bin/env python3
# digikey_api_v4_product_search.py
# Digikey Basic Product Search Script (Targeting Digikey API v4)
# By Brian Khuu 2024
# This is a digikey api v4 script that is focused only on just the flow needed to get product information.
# Recommend Reading:
# - https://developer.digikey.com/documentation
# - Section "OAuth 20: 2 Legged Flow" has good information on basic authnetic
@mofosyne
mofosyne / autodoc-justfile.py
Created August 10, 2024 06:36
Autogenerated Documentation For Justfiles
#!/usr/bin/env python3
# Autogenerated Documentation For Justfiles
# This was created to support this issue ticket https://github.com/casey/just/issues/2033#issuecomment-2278336973
import json
import subprocess
from typing import Any
# just --dump --dump-format json --unstable | jq > test.json
json_output = subprocess.run(
["just", "--dump", "--dump-format", "json", "--unstable"],
@mofosyne
mofosyne / escape_markdown_inline_code.py
Created July 30, 2024 11:53
For markdown inline code. Find the longest contiguous sequence of backticks in the string then wrap string with appropriate number of backticks required to escape it
def escape_markdown_inline_code(value_string):
# Find the longest contiguous sequence of backticks in the string then
# wrap string with appropriate number of backticks required to escape it
max_backticks = max((len(match.group(0)) for match in re.finditer(r'`+', value_string)), default=0)
inline_code_marker = '`' * (max_backticks + 1)
# If the string starts or ends with a backtick, add a space at the beginning and end
if value_string.startswith('`') or value_string.endswith('`'):
value_string = f" {value_string} "
@mofosyne
mofosyne / generate_markdown_toc.sh
Created April 25, 2024 14:08
Generate A Markdown TOC from a pdf via this bash function (Useful for Obsidian Note Taking)
# Usage: generate_markdown_toc "AS 3000-2018 Wiring Rules.pdf" 3
generate_markdown_toc() {
local pdf_file="$1"
local max_level="$2"
pdftk "$pdf_file" dump_data | awk -v max_level="$max_level" '/BookmarkTitle:/ {gsub("BookmarkTitle: ", ""); title=$0} /BookmarkPageNumber:/ {gsub("BookmarkPageNumber: ", ""); page=$0} /BookmarkLevel:/ {gsub("BookmarkLevel: ", ""); level=$0; if (level <= max_level) printf("%s- [ ] %s (Page %s)\n", sprintf("%" level*2 "s", ""), title, page)}'
}
@mofosyne
mofosyne / strtok_escaped.c
Last active April 22, 2024 11:37
alternative to strtok but with escaped character support for deliminating a string with a single char (e.g. csv or psv)
#include <stdio.h>
#include <string.h>
char *strtok_escaped(char *str, const char *delim) {
// Tokenise a string from a single char deliminator
// (strtok can deal with a deliminator string but for my purpose of splitting a psv table I only need one char)
// (There are better ways to optimise this, but just wanted something to work for now)
// https://gist.github.com/mofosyne/81c94740c0f33259606afa823562914c
static char *last_token_end = NULL;