Skip to content

Instantly share code, notes, and snippets.

View mlabbe's full-sized avatar

Michael Labbe mlabbe

View GitHub Profile
@mlabbe
mlabbe / strncpy_sucks.c
Created November 11, 2015 01:45
Friends don't let friends use strncpy()
size_t len = strlen(path);
assert(len < MAX_STRLEN);
len = MIN(len, MAX_STRLEN);
if (len < 1)
return;
strncpy(be->fs->path, path, len);
be->fs->path[len-1] = 0;
@mlabbe
mlabbe / pieballs.java
Created December 7, 2015 21:39
Pieballs!
// Source code to Pieballs in Processing 3
// by Michael Labbe @frogtoss
//
// Reverse engineered after watching a similar video by @scienceporn
//
// Download processing at http://processing.org
// Paste code in the window and run!
//
// Source is released into the public domain.
@mlabbe
mlabbe / AppleBundle.py
Created December 20, 2015 23:55
Apple Bundle Writer in Python 3
import os
import shutil
import os.path
class AppleBundle:
def __init__(self, app_name, exe_path, icon_path, version_tuple=('1','0','0')):
self.app_name = app_name
self.exe_path = exe_path
self.icon_path = icon_path
@mlabbe
mlabbe / bits.c
Created April 3, 2016 18:06
detect bits
/* detect target enviroment bits */
#if _WIN64
# define FTG_BITS 64
#elif _WIN32
# define FTG_BITS 32
#endif
#if __GNUC__
# if __x86_64__ || __ppc64__
# define FTG_BITS 64
@mlabbe
mlabbe / static.h
Created April 9, 2016 05:27
C89 static assert
// usage: FTG_STATIC_ASSERT(1==0)
// error message contains "assertion_at_line_xxx"
//
// usage: FTG_STATIC_ASSERT_MSG(1==0, one_is_not_zero)
// error message contains: "one_is_not_zero"
//
// tested on GCC, Clang, MSVC 98 and MSVC 2015
//
@mlabbe
mlabbe / endian_test.h
Created April 29, 2016 04:55
Endian detection in the preprocessor
// My goal is to perform endian detect in the preprocessor without
// breaking C89 compatibility. YES, I know how rare big endian is in 2016. I don't care.
#if (0xFF<<8) & 65280
# define LITTLE_ENDIAN
#else
# define BIG_ENDIAN
#endif
@mlabbe
mlabbe / dump-defines.sh
Created April 29, 2016 17:14
Dump all compiler defines
CC=/usr/bin/gcc
if [[ $# -gt 0 ]]
then
CC=$1
fi
touch ___temp.c
$CC -dM -E ___temp.c
rm ___temp.c
@mlabbe
mlabbe / strcatall.c
Created May 23, 2016 20:57
ftg strcatall
/* append num strings, returning heap-allocated string.
caller must free() returned ptr. */
char *
ftg_strcatall(size_t num, ...)
{
size_t i, alloc_size = 0;
va_list va1, va2;
char *buf, *p_buf;
va_start(va1, num);
@mlabbe
mlabbe / path_exists.c
Created May 28, 2016 06:11
Cross platform path exists test
/* returns true if the dir or file exists */
FTGDEF bool
ftg_path_exists(const char *path)
{
#ifdef FTG_POSIX_LIKE
return access(path, F_OK) != -1;
#elif defined(_WIN32)
ftg_wchar_t u8_path[MAX_PATH];
DWORD attrib;
@mlabbe
mlabbe / ftg_core.h
Last active October 5, 2016 23:40
ftg_core.h
/* ftg_core.h - v0.5 - Frogtoss Toolbox. Public domain-like license below.
ftg libraries are copyright (C) 2015 Frogtoss Games, Inc.
http://github.com/mlabbe/ftg_toolbox
ftg header files are single file header files intended to be useful
in C/C++. ftg_core contains generally useful functions
Special thanks to STB for the inspiration.