Note that there are some explanatory texts on larger screens.

plurals
  1. POFile relocations - bad practice?
    primarykey
    data
    text
    <p>I'm working with a binary file format that deals with data relocations, ie. 'pointers' to data that is located in another part of the file, and a double pointer is written at the end of the file (integer that holds the offset of the data pointer).</p> <p>I have a <code>std::queue&lt;unsigned int*&gt;</code> that holds the data pointers. Here is an example of how I use it (warning, ugly pointer code):</p> <pre><code>std::queue&lt;unsigned int*&gt; relocations; struct TestData { unsigned int Length; char* Text; unsigned int Format; }; void MakeData() { unsigned char* chunkOneData = new unsigned char[sizeof(TestData)]; TestData* mainData = reinterpret_cast&lt;TestData*&gt;(chunkOneData); mainData-&gt;Length = this-&gt;Text.length(); mainData-&gt;Format = this-&gt;TextFormat; unsigned char* chunkFiveData = new unsigned char[this-&gt;Text.length() + 1]; strcpy(reinterpret_cast&lt;char*&gt;(chunkFiveData), this-&gt;Text.c_str()); mainData-&gt;Text = reinterpret_cast&lt;char*&gt;(chunkFiveData); relocations.push(reinterpret_cast&lt;unsigned int*&gt;(&amp;mainData-&gt;Text)); dataChunks[0].Add(chunkOneData, sizeof(TestData)); dataChunks[4].Add(chunkFiveData, this-&gt;Text.length() + 1); } </code></pre> <p>And here is the code that is 'making' the file relocations:</p> <pre><code>std::vector&lt;unsigned int&gt; relocationTable; std::queue&lt;unsigned int*&gt; relocations; unsigned int GetDataOffset(unsigned int *relocation) { for (int i = 0; i &lt; 6; i++) { for (int j = 0; j &lt; dataChunks[i].size(); j++) { if ((relocation &gt;= (unsigned int*)dataChunks[i][j].data) &amp;&amp; (relocation &lt; (unsigned int*)dataChunks[i][j].data + dataChunks[i][j].size)) { return ((unsigned int)relocation - (unsigned int)dataChunks[i][j].data + dataChunks[i][j].relativeOffset); } } } return 0xFFFFFFFF; } void MakeRelocationTable() { while (!relocations.empty()) { unsigned int* relocation = relocations.front(); relocations.pop(); unsigned int* relocatedData = *((unsigned int**)relocation); if (relocatedData != NULL) { unsigned int dataOffset = GetDataOffset(relocatedData); if (dataOffset = 0xFFFFFFFF) dataOffset = 0; *relocation = dataOffset; unsigned int pointerOffset = GetDataOffset(relocation); relocationTable.push_back(pointerOffset); } } } </code></pre> <p>Now, tell me if you disagree, but that is some hideous code IMO. I got a headache just writing it. But it seems like it is the 'easiest' (however you define easy) approach to this.</p> <p>Is there anyway I can clean this code up? Or are there other ways I can do this? Thank you for your help,</p> <p>Alex</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload