Skip to content

Instantly share code, notes, and snippets.

View johnbartholomew's full-sized avatar

John Bartholomew johnbartholomew

View GitHub Profile
@johnbartholomew
johnbartholomew / str2long.c
Created March 6, 2013 12:20
str2long function with correct range checks.
/* Written for
*
* A Quick Coding Contest: Convert String to Integer Without Overflow
*
* http://blog.regehr.org/archives/909
*
* This has not yet been run through clang's integer behaviour sanitizer,
* because I don't have a recent enough clang build.
*/
#if 0 /* example use: */
#define EMIT_FIXUP_DEFINITIONS 1
/* if EMIT_FIXUP_DEFINITIONS is 0 or undefined then
* only the struct declaration will be emitted
* typically you want to only emit fixup function
* definitions in a single translation unit.
*/
#define DECLARE_STRUCT_NAME BITMAPFILEHEADER
-- Gets a list of stations in nearby systems that match some criteria.
--
-- Example:
--
-- local orbital_ports = Game.system:GetNearbyStationPaths(
-- 30, nil, function (station) return station.type == 'STARPORT_ORBITAL' end, true)
--
-- for i = 1, #orbital_ports do
-- local path = orbital_ports[i]
-- print(path, ' -- ', path:GetSystemBody().name, ' in system ', path:GetStarSystem().name)
@johnbartholomew
johnbartholomew / gist:5434034
Last active December 16, 2015 12:19
Pioneer approximate code style conventions
Classes, namespaces and methods are LikeThis.
Non-public member variables are m_likeThis.
Local variables and local constants are likeThis.
Public member variables are likeThis, but you should think about making them non-public and providing an accessor instead.
Accessors are called GetFooBar() and SetFooBar(), but you should think about whether you really want to provide SetFooBar() or if you want to provide nicer verb methods (e.g., Missile provides Arm() and Disarm(), rather than SetArmed()).
// build with: g++ -std=c++98 -Wall -ocheck test.cpp
#include <iostream>
#include <string>
#include <sstream>
int parse_int(const char *x) {
std::istringstream ss(x);
int y;
ss >> y;
@johnbartholomew
johnbartholomew / TestJourney.lua
Created May 14, 2013 14:20
Pioneer autopilot test script
RepeatJourney = true
local journeys = {}
Event.Register('onGameStart', function ()
journeys = {}
end)
Event.Register('onShipDocked', function (ship, station)
local journey = journeys[ship]
@johnbartholomew
johnbartholomew / ClangWarnGlobals.cpp
Created May 28, 2013 18:24
A Clang plugin to generate warnings for non-const global variables.
// -Wglobals Clang plugin
//
// Based on example plugins and searching Clang's API documentation.
// BEWARE! This is my first ever attempt at a Clang plugin.
// Makefile
#if 0
warnglobals.so: WarnGlobals.cpp
clang++ -std=c++98 -shared -fPIC $$(llvm-config --cflags --libs support mc) -o "$@" $^ -L/usr/lib/llvm/ -lclang
// ---- text.vert.glsl
#version 150
uniform vec2 text_offset;
uniform vec2 rcp_screen_size;
in vec2 v_screen_pos;
in vec2 v_uv;
out vec2 f_uv;
@johnbartholomew
johnbartholomew / gist:6811245
Last active December 24, 2015 14:19
Creating statically linkable C "packages"
#define PACKAGE_PRIVATE __attribute__((__visibility__("hidden")))
#define PACKAGE_PUBLIC __attribute__((__visibility__("default")))
/* -------- a.c -------- */
static int the_thing = 42;
PACKAGE_PRIVATE int compute_thing(void) {
return the_thing;
}

Some definitions:

  • A "safe" transformation is a transformation of a path A into a path B such that any file system operation on path A will produce the same behaviour if given path B.

  • "normalize" means remove redundant directory separators (including trailing separators) and replace non-canonical separators with canonical separators (on Windows, backslash is the canonical separator, but forward slashes are also recognised as separators. On POSIX, forward slash is the only separator character and is canonical). Normalization is trivially "safe"; it doesn't add or remove any path components,