Sample Code

Recently I had the chance to do a little project that had been on my mind for some time. I implemented an extensible version of the printf family of subroutines. By extensible I mean that it is possible to add idiosyncratic format specifiers. For example, working in networking, there was a frequent need to convert IP addresses to human readable form, which people would typically do with something like the following:

char buff[32]; xxx
printf("address: %s\n",buff);

With my version of printf (named printfe) this would be done as:

printfe("address: %=I\n",addr);

Extension formats are flagged by "%=", and the format specifier "%=I" indicates an IP address to be output. Examples of other possible extensions are included in the code: binary numbers, booleans, and MAC addresses. I also included the gnu extension of having the option of displaying numbers with grouping, that is, display 12345 as 12,345.

There is an extensible version of each standard output printf function (named by just adding an "e" to the end of the standard subroutine. There is also a C++ version called OSprintfe, which would be used as follows:

OSprintfe(os,"address: %=I%=Z",addr);

The extension "%=Z" being equivalent to the C++ "eol".

If you think this might be of use to you, or if you just want to see an example of how I design software, printfe is available on sourceforge at printfe