15 Sep 2008 elanthis   » (Journeyer)

Make Array

Here’s a useful little tool that I whipped up for another project. It takes a binary file and products C code containing the file in an array. Yes, there are other tools for this, but each has some problem or another (such as not making the array const, which is bad for memory usage; or not NUL-terminating the array for when you’re referencing a text file and want to use standard C string utilities for searching it).

The tool is a grand total of 54 lines includes blanks and white-space. Perfect size to just drop into your project’s source tree. No reason for a package or anything like that.


Using it is simple. Call the utility with the input filename and the name of the array to generate. The array will be defined as a const char[]. It’ll also define name_len as a const size_t with the length of the input file. The array is NUL-terminated (the NUL is not part of the _len constant). Output is send to stdout, so pipe/redirect it as needed. Useful if you want to put a bunch of such arrays into a single file.

Another version I may end up making for a similar purpose is one that just generates C string constants, one for each line, use the C constant string concatenation feature for making the input. For text files it’s (sometimes) nice to be able to see the text in your generated code, e.g. for debugging purposes.

Syndicated 2008-09-15 21:02:36 from Sean Middleditch

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!