Hey, I didn't read the whole thing, just ne555's last comment about the register length being disrupted by variable string length.
If you are trying to get rid of that, I would do the following:
1 2 3 4 5
|
struct SerializableString
{
char *theString;
unsigned int stringOffset;
}
|
I would then create an array of SerializableString and fill it in a loop. For the example code, let's imagine that arrStrings is a vector<std::string> (or std::wstring) that contains all the strings from all registers. Or better yet, a vector<char *> so we don't waste RAM duplicating the strings. Let's imagine that the original strings live safely elsewhere in string objects (this is the same reasoning for using char * in SerializableString):
1 2 3 4 5 6 7 8 9 10
|
//Since I know the length of the array, I'll just use a C array here:
SerializableString *arrIndexes = new SerializableString[arrStrings.size()];
//The byte counter. Remember that 1 char = 2 bytes if using unicode!
unsigned int totalBytes = 0;
for (vector<char *>::iterator it = arrStrings.begin(); it < arrStrings.end(); it++)
{
arrIndexes[it - arrStrings.begin()].theString = *it;
arrIndexes[it - arrStrings.begin()].stringOffset = totalBytes;
totalBytes += strlen(*it) + 1; //Must account for the null char.
}
|
Now, instead of saving the string with the individual register, you just save the string offset stored in arrIndexes. Note that arrIndexes contains a reference to the string, but maybe it is not needed. I added it just in case.
Save at the very beginning of the file the total number of records. Then offset 0 is the next byte after the last record, which should be of a constant size because any and all strings are saved after them. Oh, and yes, save the strings including their null chars at the end, one after another. :-)
Sorry if this is irrelevant to the post. The topic caught my eye but in the end I didn't fully read it.
And now that I'm done writing, I see that arrStrings is unnecessary. Instead, use a vector<SerializableString>. After you have constructed it (added all strings), calculate the offsets.