Sean Barrett and others show us the way to create and use relatively small include files for library code. I recommend that you read the FAQ on his stb repository at GitHub.
This repository holds my own bits of code that either aren’t part of the C standard or don’t work quite the way I want them to.
Troy Brumley, blametroi@gmail.com, May 2024.
So let it be written, So let it be done.
The headers to use are in the release
subdirectory. They are built from the makefile
with my version of the library packer program, found in the buildhdr
subdirectory. The source for each header is a pair of files in the inc
and src
subdirctories. txbabort.h is build from inc/abort.h and src/abort.c, and so on.
I keep all the release headers in my $HOME/.local/include/
directory and add that to my $CPATH
in varioius makefiles
.
Include the appropriate headers in your project. Each header has a preprocessor variable to control when the executable code will be included. #define
this in only one source file per linked output.
It’s all public domain as far as I’m concerned, but you can use this software either as public domain under the unlicense or under the terms of the MIT license.
(h/t to Sean Barrett at nothings.org for the most clear description of usable licensing I’ve ever seen.)
Functions are in what I think are sensible file groupings.
Filename | Description | Implementation Trigger |
---|---|---|
txbmisc.h | missing functions such as min/max | TXBMISC_IMPLEMENTATION |
txbpmute.h | iterative permutation generator | TXBPMUTE_IMPLEMENTATION |
txbstr.h | string utilities (tokenize) | TXBSTR_IMPLEMENTATION |
txblistd.h | doubly linked list management | TXBLISTD_IMPLEMENTATION |
txbabort.h | abort and abort_if | TXBABORT_IMPLEMENTATION |
txbpat.h | big enough subset of regular expressions | TXBPAT_IMPLEMENTATION |
The file testlibs.c
demonstrates the use of some of the library functions. The file unittest.c
has tests for doubly linked lists and other one off functions.
Signature | Description |
---|---|
int rand_between(int, int) | non cryptographically secure random integer in a range |
void shuffle(void **, int) | shuffle an array of pointers or long integers |
long *factors_of(long) | returns an array of the factors of a long integer |
?max(?,?) and ?min(?,?) | min and max for various numeric types |
bool is_even(long) | |
bool is_odd(long) | |
bool is_digit(char) | various character classification tests, i’m a |
bool is_word_char(char) | us programmer so the definitions are from my perspective |
bool is_whitespace(char) | |
bool is_punctuation(char) | |
bool is_control(char) | |
bool is_bracketing(char) | |
Signature | Description |
---|---|
int permute_next(int, int**) | permutes the values in an integer array of a given size |
str is char *
Signature | Description |
---|---|
char** split_string(str, str) | splits a string on runs of any of the separators |
list is listd_control_t * item is listd_item_t
Signature | Description |
---|---|
bool reset_listd_control(list) | resets/clears the list control block |
void free_all_items(list) | remove and free memory for all items on a list |
item make_item(list, void) | allocate and initialize an unlinked item and attach payload |
bool free_item(list, item**) | free an unlinked item and its payload |
item find_item(list, void) | does an item with a matching payload key exist in the list |
int count_items(list) | how many items are on the list |
bool add_item(list, item*) | and unlinked item from make_item to the list |
item remove_item(list, void) | unlink item with a matching payload key from the list |
item next_item(list, item*) | iterate forward through the items on the list |
item prev_item(list, item*) | as next_item, but backward |
a reasonable subset of regular expression pattern matching. no grouping via ()
or selection via |
. {,}
quantifiers are not implemented, but *
, ?
, and +
are. there are some entry point declarations not explicitly exposed in the header file for debugging and testing, but they are not marked static in the implementation and can be declared as needed.
pat is cpat_t * str is char *
Signature | Description |
---|---|
str convert_glob(str) | convert a filename glob to a regular expression |
pat compile_pattern(str) | compile a regular expression for later match processing |
bool match(str, pat) | does a string satisfy a pattern |
bool glob_match(str, pat) | does a string file name match using glob rules? |
str decompile_pattern(pat) | return a string representation of the pattern |
str pattern_source(pat) | return a read only copy of the original pattern source |
glob_match
and globbing are a bit of a pain. the implementation is good enough for my current needs, but it may need more work as time goes on.
This file defines two macros, abort(msg)
and abort_if(cond, msg)
which call the functions do_abort
and do_abort_if
to report the error message and then end the process via exit(EXIT_FAILURE)
.
- improve error message support with snprintf
- implement decompile_pattern
- implement {} quantifiers
- general refactoring
- (low priority) capture groups ()
- (low priority) or |
- create c version of the pack header code