Open sourcing nlutils: Nitrogen Logic's C utility library

libnlutils on GitHub

If you’ve read my previous post, you know that I’m opening up some of the source code from my old startup, Nitrogen Logic. Here’s the first major piece of the Nitrogen Logic source code to go open source. It’s a core utility library used by all of Nitrogen Logic’s C projects, called nlutils.

Because of C’s very limited standard library compared to more modern and full-featured languages, it’s incredibly common for developers to write their own core data structures and helper functions that they use over and over. These include better string manipulation functions, linked lists, associative arrays, etc. – things that we take for granted in languages like Ruby.

nlutils is the set of such tools for Nitrogen Logic’s C projects. Where possible the code adheres to C99 and POSIX.1-2008. The header files are thoroughly documented, so browse around the include/ directory to see what nlutils provides. After meticulously reviewing the code and tests, I’ve pushed it to GitHub under the GNU AGPLv3 license for all to enjoy.

What’s included?

The main purpose of nlutils is to augment the C standard library with additional data structures and functions I needed when writing other C projects, such as the Depth Camera Controller’s Kinect interface daemon and the Automation Controller’s automation logic system. There are also a few things related to the web UI of the controllers themselves.

Some of the C functions:

  • Arithmetic functions for struct timespec (why these aren’t present in POSIX is a bit of a mystery to me).
  • Thread management functions that provide a slightly simpler interface than POSIX threads and allow renaming threads.
  • A variant datatype that can store int, float, string, or raw data values
  • Process management and process I/O helpers, including functions for reading the entire output of a process into a string and variations on a popen3() function for controlling process I/O.
  • An asynchronous wrapper for the curl command for making process-isolated HTTP requests.
  • String splitting, comparison, conversion, and escaping functions.

What’s it made out of?

Nlutils is mostly C, with some tools written as Bash scripts. There’s also a bit of Ruby, some HTML, and the build scripts that tie everything together. I cleaned up all of the bit rot and made sure everything works on Ubuntu 18.04.

nlutils $ cloc --script-lang=Ruby,ruby1.9.1 [^R]* v 1.74  T=0.12 s (801.3 files/s, 129860.9 lines/s)
Language                     files          blank        comment           code
C                               40           1489           1482           8238
Bourne Shell                    19            287            271           1101
CSS                              3            123             62            542
C/C++ Header                    17            229            884            450
CMake                           11             53             16            153
HTML                             3              0              0             72
Ruby                             2             13              6             51
make                             1             11              0             25
SUM:                            96           2205           2721          10632

The library also has a test suite that runs under Valgrind by default. Tests are run automatically on Travis CI when a new commit is pushed.

nlutils $ make test

All tests pass

It’s on GitHub

So check out nlutils on GitHub, and keep an eye here for future releases of more Nitrogen Logic source code.