Miscsrc: My Collection of Miscellaneous Source Code
My C library of Miscellaneous Source code.
The source code is available on github.
You'll probably already have similar code in your personal library already, because it doesn't contain anything that you won't find in a first year computer science textbook. Regardless I've decided to make these available anyway.
I have decided that I like the unlicense as my open source license of choice for these types of things, so that is the license under which I've released it.
Currently it has these modules, each consisting of a C source and a header:
- csv - Parser for CSV files. It tries to conform to rfc4180. I also based my implementation on the details found in this tutorial.
- eval - Mathematical expression evaluator. Call it with a string like "2 + 2" and it will return 4. It uses a recursive descent parser internally
- getarg - a getopt() replacement for platforms that doesn't have one (like, Windows). Up until 2007 I used to work for a company that still used Microsoft Visual C++ 6.0 (long story) and it was a fun excercise to reimplement it.
- hash - A generic hash table that maps char * strings to void * objects.
I have to admit that the hash function used is rather naive - a real man's hash function needs to be more heavy duty. See this article by Bob Jenkins and this article by Paul Hsieh.
Also it uses simple chaining to resolve collisions.
It If the table fills to more than half of its capacity, it doubles the number of buckets and rehashes every entry in the table. Therefore the table size must be a power of two.
There are more sophisticated ways to implement hash tables, but I value this module's simplicity.
- ini - A reader/writer for .ini files.
- list - Functions for manipulating linked lists. Normally when I program in C i just put a "next pointer" in whatever data structure I want to put in a list, but this module provides a bunch of additional functions for using the list like a stack or a queue and iterating over a list and so on.
- regex - A very simple regular expression evaluator based on an article simply titled Regular Expressions by none other than Brian W. Kernighan and Rob Pike in the DrDobbs' Journal of April 1999. You can read the article online.
What I like about the evaluator in the original article is its simplicity. It is truely beautiful.
The regex evaluator in the original article only supported '*', '.', '^' and '$' as special characters. In my version, it has been extended to include '+', '?' and '' operators found in other regex libraries. It also has sub() and gsub() functions for text substitution.
- simil - compares how similar two strings are using the Ratcliff-Obershelp algorithm from the article Pattern Matching: the Gestalt Approach by John W. Ratclif in the July 1988 issue of DrDobbs. Actually, in the November 1988 letters section of DrDobb's Journal, two readers published versions of the algorithm implemented in C (the original version was in assembly). My version is based on the one presented in Joe Preston's letter if you have access to that issue of DrDobbs' Journal.
- utils - Contains some utility functions like my_readfile() to read an entire file into memory and my_strdup() because strdup() is not actually part of ANSI C.
Miscsrc was partly inspired by Bob Stout's SNIPPETS.org archive. The snippets.org domain belongs to someone else today. A version of the snippets archive dated July 1997 is available at http://www.brokersys.com/snippets/
. Some of the individual files are available on this site