I have had the file INITDATA.BAR for a long time for internal use. Only today did I decide to release it to the public.
How many times has a software developer wanted to get data incorporated into storage in a local executable…only to discover that you have to use the native data-initialization syntax, very much unlike the format in which the data is stored?
You can be very patient and hand-enter the data (you’d never do it by hand unless there is only a small amount of it).
Take this example. I want to enter the following bytes as static constant data, to be used in the body of a program later: 40H 7FH 23H A8H. In C-based languages, you’d initialize an array like this:
static const char mydata[] = { 0x40, 0x7F, 0x23, 0xA8 };
But chances are, the data isn’t going to be written out as “0×40, 0x7F, 0×23, 0xA8″ or anything even remotely like it. Furthermore, you might want the data written out as short integers, signed or unsigned, in decimal instead of hexadecimal, etc.
Four bytes? Just hand-translate it. Over a hundred? Maybe not. Different base? Ugh. Wrong endian order? Super-ugh.
A simple solution, if not straightforward, is to load the data as a separate file from the disk, like this:
FILE *afile = fopen("mydata.dat", "rb");
if (afile) {
fread(mydata, sizeof(mydata), 1, afile);
fclose(afile);
}
Ah, but what about…
- Is pathname always relative? Should it be absolute? How do I reconcile with installation folder, etc?
- What if file can’t be loaded?
- Is file same size as declared variable? How do I resize it if source data changes? Should I dynamically allocate memory for it?
- What if I forget to go through all the proper value checks and closes because as a programmer I’m kind of lazy?
- Does this separate load step REALLY need to be done at all?
That last question is the clincher. The answer is NO.
The C++ initialized data converter is the answer. It reads a binary data file and spits out C++ initialized data. Or, alternatively, it can read initialized data and spit out binary data. It’s a two-way converter with many F8-mode selection functions.
If you’ve ever looked at how many files are in the BARfly installation folder, you’d know there are surprisingly few. BARfly uses BAR to run its architecture…but where are all the BAR-based architectural components? They’re not BAR files, and they’re not in the master registry.
The answer: INITDATA.BAR. I’ve long since made initialized data out of such components. The BARfly.exe executable has literally swallowed them.
Engineers of all stripes love the idea of making a machine work with fewer moving parts. Would you rather carry around a large package or several smaller ones? Same load. One’s a lot easier to carry.
Tags: C++, data, file, initialized