Skip to content

Instantly share code, notes, and snippets.

@mooware
mooware / print-exception-text.cpp
Created February 12, 2016 22:57
An example program to show that FormatMessage() cannot properly return the text for an exception code.
#include <windows.h>
#include <stdio.h>
int main()
{
HMODULE hMod = LoadLibrary("ntdll.dll");
char buf[1024];
DWORD id = 0xc0000005; // code for access violation
@mooware
mooware / measure.cpp
Created March 3, 2016 01:08
Similar to the "time" or "timeit" commands, for Windows.
#include <Windows.h>
#include <Psapi.h>
#include <stdio.h>
#include <stdint.h>
uint64_t fileTimeToUsec(const FILETIME &ft)
{
return ((uint64_t(ft.dwHighDateTime) << 32) | ft.dwLowDateTime) / 10;
}
@mooware
mooware / tiny_string.h
Created September 6, 2016 01:31
An experimental "very short string optimization"
// a fun little experiment i came up with:
// similar to the classic "short string optimization", we could actually use
// the bytes of the pointer itself as the short string buffer.
// a "tagged pointer" is used to distinguish between the internal buffer
// and a heap-allocated string.
#include <cassert>
#include <cstdint>
#include <cstring>
@mooware
mooware / snake.ino
Created June 30, 2017 03:02
Snake for Arduino on a LED matrix
// snake on a MAX7219 led matrix
// NOTE: assuming the chip and pins are on the left side of the led matrix,
// row = 0, col = 0 is at the upper left corner
#include <LedControl.h>
// we're using a membrane keypad as controller
#include <Keypad.h>
@mooware
mooware / colormsvc.py
Created October 13, 2017 13:15
Colorization for MSVC warnings and errors
import sys, re
COMPILE_PATTERN = re.compile(r'^.+\(\d+\) ?: (fatal )?(error|warning|note)( C\d+)?: .+$')
LINK_PATTERN = re.compile(r'^.+ : (fatal )?(error|warning) LNK\d+: .+$')
OTHER_PATTERN = re.compile(r'^.+ : (general |Command line )?(error|warning) \w+ ?: .+$')
class WinColorStream:
# wincon.h
FOREGROUND_BLACK = 0x0000
FOREGROUND_BLUE = 0x0001
@mooware
mooware / pihut_xmas_tree.py
Last active December 1, 2018 21:27
Script for the Pi Hut 3D Xmas Tree
from gpiozero import LEDBoard
from time import sleep
import random
tree = LEDBoard(*range(2, 28), pwm=True)
# first led is the top one in the star
tree[0].value = 0.6
leds = range(1, len(tree))
@mooware
mooware / constexpr-char-lookup-table.cpp
Created September 28, 2019 01:49
Constexpr generation of character lookup tables
// generate character-based static lookup tables
// that can be used for character classification,
// e.g. like in cctype functions (isdigit, isalpha ...)
struct CharLookupTable
{
bool data[256];
template <unsigned int N>
constexpr CharLookupTable(const char (&str)[N])
@mooware
mooware / twitch_redirect.py
Last active February 2, 2020 00:58
Simple bottle.py application to play twitch.tv streams through HTML5 video
# uses bottle.py as web framework, streamlink for getting the stream URL
# and hls.js to play the HLS stream in an HTML5 video tag
from bottle import *
from streamlink import Streamlink
from urllib.request import urlopen, Request
import sys, re, json
def u(s):
if isinstance(s, str):
@mooware
mooware / jollymania.py
Created November 15, 2020 04:25
Download games2jolly games for Flashpoint
import os.path
import sqlite3
import urllib.request
def main():
db = sqlite3.connect(os.path.join('Data', 'flashpoint.sqlite'))
cur = db.execute("SELECT launchCommand FROM game WHERE launchCommand LIKE '%games2jolly%'")
urls = [s for (s,) in cur.fetchall()]
for index, url in enumerate(urls):
print('{}/{} {}'.format(index + 1, len(urls), url))
@mooware
mooware / srlmulti.py
Last active January 17, 2021 01:08
Simple bottle.py application to open a multitwitch (or similar site) for an SRL race
from bottle import *
from urllib.request import urlopen
import json
import re
DEFAULT_HOSTS = ['multitwitch.tv', 'multistre.am', 'kadgar.net/live']
SRL_API = "https://api.speedrunslive.com/races/"
SANITY_RE = re.compile('[0-9a-z]+')
RACE_STATE_COMPLETE = 4 # from the SRL API