Mike's Code Snippets

Set.zip (5k)

ANSI C

This is a fast ADT that uses a hash table with external chaining and a two way linked list to give O(1) lookups, insertions, deletions and traversal. Traversal is usually disordered, but a generic heapsort function (giving O(n log n) performance) allows the data to be sorted. Additionally to all this, memory allocation is done when the set is initially created, and a free list is used to avoid memory allocation calls after initialisation.

This generic implementation only supports fixed length keys (although you can add an external hashing function to a produce fixed length keys from variable length keys to fix this is some cases, see the IniReader), but can be used with any data.

HashFile.zip (4k)

ANSI C

This is a completely generic disk based hash table ADT. The hash table uses internal chaining, and double hashing. An attempt has been made to optimise the probe sequence on a read-miss, by setting the probe distance to 2<h'(key)<40 so that a probe sequence shouldn't cause huge seeks, or cause the IO system to wait for the disk to revolve by requesting the immediate next record. This could be optimised if required according to the application/profiling.

This hash table ADT does not support deletion or updates, but does allow additional data to be added.

IniReader.zip (6k)

ANSI C

This is a very simple and generic .ini file reader. This reader is fully configurable from an array that must be defined in the source code. The array holds elements that state the key pattern, how to parse it (using sscanf e.g. "%d") and the address of the place to put the key value. The reader can also be made to load key values into arrays.

Internally the reader is very simple and uses the above 'Set' code to archive non-linear key lookups, meaning that the reader will be fast for even very large INI files.

WavLib.zip (12k)

x86 byte order, ANSI C

This library provides a simple ADT for reading, creating and writing Microsoft PCM encoded .WAV files. Cue point information can also be optionally read or written. It is simple, and doesn't have many features, but should be easy to extend to whatever advanced functionality you need.

If you want to know about the .WAV file format, the document wave.pdf will probably tell you most of what you need to know.

DbMalloc.zip (5k)

optional POSIX threads, ANSI C

This is a very small and simple debugging malloc library. It is a drop in replacement for malloc, calloc, realloc, strdup and free, and uses macros to provide line number and file information in its output. It can be compiled with or without POSIX mutexes wrapped around the functions, making the library optionally thread-safe.

Internally this library uses a linked list, so will give poor performance in large applications. This library would however serve as the basis of a more comprehensive library, and the source does lend it's self to specific customisation.

ASELoader.tgz (128k)

C, OpenGL, with Linux Makefile
Can be build with MS Visual Studio

ASE files are ASCII, human readable files that contain data needed to construct a 3D models with textures and material properties. The easiest way to create ASE files is to use 3D Studio Max, which can export to this format.

This snippet parses ASE files and contains routines to render and manipulate models in OpenGL. The loader will also load textures (which must be stored in TGA format) and apply them according to texture co-ordinates. Material properties such as colour and diffuse reflection are also loaded and may be (optionally) applied.

This snippet comes as a complete program that loads an ASE file of a truck (Thanks to Tom Blackford) and displays it slowly spinning in the centre of the screen.

This demo includes a Makefile to build it on Linux (using Mesa), which may need editing depending on the location of the Mesa libraries on your system. The demo can also be compiled with MS Visual Studio, although it may need small changes to make this work (although most code should be fine if WIN32 is #define'd).

JGraph.zip (10k)

Java, Swing. Includes Javadocs

This Java class extends the Swing JPanel component to offer a very simple graph that is able to plot multiple series with a legend and labelled axes. The graph is auto scaling, and is ideal for plotting quick graphs to visualise output of small data runs. The zip file includes javadocs that explain how to use the class (Browsable here.)
Base64.zip (2k)

ANSI C

A very simple base 64 encoder and decoder that utilises lookup tables to ensure that it is fast. The encoder and decoder have been written with reference to RFC1521, section 7, and hopefully conform completely to this standard.

The download package includes a test file that verifies that the encoder works correctly while also showing the usage of the functions (of which there are only 2, encode and decode!).

Demonise (2k)

Linux C

A very simple program to aid running programs as daemons. Principally this was designed to run Java VM processes as daemons during system initialisation, but it can be used to 'background' any process. The program will fork() a child which then reopens its standard file handles (stdin, stderr and stdout) to /dev/null before replacing it's image with that of some program specified on the command line. The parent process prints the child process's PID to stdout making it simple for a controlling script to collect and record in a lock file if needed. Additionally the program can also switch to another user prior to spawning the child process by calling setuid().
MySql ERD (24k)

Java, MySQL JDBC

This is a program that uses the JDBC with MySQL specific queries to produce an Entity Relationship Diagram (ERD) of database tables and key relations. The output is in the DOT language, and must be processed by DOT to produce a viewable image as a gif, png or ps file. Relationships between tables are extracted from foreign keys, so the target database must use InnoDb tables for this support. The Java code requires the MySQL Connector/J JDBC driver to be installed. An example of the output from a complex database with many foreign keys can be found in the download package, or here, noting that it has been converted from PS to PDF format to make it easier to view.

Additionally this package contains a small and generic command line option parser.

Other information....

webmaster@mcternan.uk
www.mcternan.co.uk

This page provides a number of downloads that contain example code and makefiles to compile small example programs. In all cases the code of interest will be separated into its own file and accompanying header file. Most programmers should be able to understand the functionality of a module from the header file alone, but if this is not the case, all source code is included and is written neatly and commented.

I don't claim any of this code to be the best available for a given task, although I hope that some of it will be useful to others, by way of example or just to give people ideas as to how to do things. I always welcome comments or questions.

Links...

www.mcternan.co.uk

Cygwin , Win32 ports of GNU applications, including gcc and gdb. Excellent and free, absolutely superb!
PFE , a brilliant, free and completely customisable programmers file editor.
Textpad , not free, but very cheap and nicely configurable.
FSDEXT2 , transparently mount your Linux ext2fs partitions on Windows 95. (Works on 98 also for me).