objwriter.cxx

changeset 50
2e333a3ca49a
parent 49
8e2f7a031410
child 68
588cc27e84bb
equal deleted inserted replaced
49:8e2f7a031410 50:2e333a3ca49a
99 g_GotMainLoop = false; 99 g_GotMainLoop = false;
100 } 100 }
101 101
102 // Write string table 102 // Write string table
103 void ObjWriter::WriteStringTable () { 103 void ObjWriter::WriteStringTable () {
104 // If we added strings here, we need to write a list of them.
105 unsigned int stringcount = CountStringTable (); 104 unsigned int stringcount = CountStringTable ();
106 if (stringcount) { 105 if (!stringcount)
107 Write<long> (DH_STRINGLIST); 106 return;
108 Write<long> (stringcount); 107
109 108 // Write header
110 for (unsigned int a = 0; a < stringcount; a++) 109 Write<long> (DH_STRINGLIST);
111 WriteString (g_StringTable[a]); 110 Write<long> (stringcount);
112 } 111
112 // Write all strings
113 for (unsigned int a = 0; a < stringcount; a++)
114 WriteString (g_StringTable[a]);
113 } 115 }
114 116
115 // Write main buffer to file 117 // Write main buffer to file
116 void ObjWriter::WriteToFile () { 118 void ObjWriter::WriteToFile () {
117 fp = fopen (filepath, "w"); 119 fp = fopen (filepath, "w");
122 for (unsigned int u = 0; u < MAX_MARKS; u++) { 124 for (unsigned int u = 0; u < MAX_MARKS; u++) {
123 ScriptMarkReference* ref = MainBuffer->refs[u]; 125 ScriptMarkReference* ref = MainBuffer->refs[u];
124 if (!ref) 126 if (!ref)
125 continue; 127 continue;
126 128
127 for (unsigned int v = 0; v < sizeof (word); v++) { 129 // Substitute the placeholder with the mark position
128 union_t<word> uni; 130 union_t<word> uni;
129 uni.val = static_cast<word> (MainBuffer->marks[ref->num]->pos); 131 uni.val = static_cast<word> (MainBuffer->marks[ref->num]->pos);
132 for (unsigned int v = 0; v < sizeof (word); v++)
130 memset (MainBuffer->buffer + ref->pos + v, uni.b[v], 1); 133 memset (MainBuffer->buffer + ref->pos + v, uni.b[v], 1);
131 }
132 134
133 /* 135 /*
134 printf ("reference %u at %d resolved to %u at %d\n", 136 printf ("reference %u at %d resolved to %u at %d\n",
135 u, ref->pos, ref->num, MainBuffer->marks[ref->num]->pos); 137 u, ref->pos, ref->num, MainBuffer->marks[ref->num]->pos);
136 */ 138 */
137 numWrittenReferences++; 139 numWrittenReferences++;
138 } 140 }
139 141
140 // Then, dump the main buffer to the file 142 // Then, dump the main buffer to the file
141 for (unsigned int x = 0; x < MainBuffer->writesize; x++) 143 for (unsigned int x = 0; x < MainBuffer->writesize; x++)
142 WriteDataToFile<unsigned char> (*(MainBuffer->buffer+x)); 144 WriteDataToFile<byte> (*(MainBuffer->buffer+x));
143 145
144 printf ("-- %u byte%s written to %s\n", numWrittenBytes, PLURAL (numWrittenBytes), filepath.chars()); 146 printf ("-- %u byte%s written to %s\n", numWrittenBytes, PLURAL (numWrittenBytes), filepath.chars());
145 fclose (fp); 147 fclose (fp);
146 } 148 }
147 149

mercurial