57 OnEnterBuffer = new DataBuffer; |
57 OnEnterBuffer = new DataBuffer; |
58 numWrittenBytes = 0; |
58 numWrittenBytes = 0; |
59 filepath = path; |
59 filepath = path; |
60 } |
60 } |
61 |
61 |
62 ObjWriter::~ObjWriter () { |
|
63 delete MainBuffer; |
|
64 delete OnEnterBuffer; |
|
65 |
|
66 // This crashes for some reason o_O |
|
67 // Should make no difference anyway, since ObjWriter |
|
68 // is only deleted at the end of the program anyway |
|
69 // delete MainLoopBuffer; |
|
70 } |
|
71 |
|
72 void ObjWriter::WriteString (char* s) { |
62 void ObjWriter::WriteString (char* s) { |
73 Write<long> (strlen (s)); |
63 Write<long> (strlen (s)); |
74 for (unsigned int u = 0; u < strlen (s); u++) |
64 for (unsigned int u = 0; u < strlen (s); u++) |
75 Write<char> (s[u]); |
65 Write<char> (s[u]); |
76 } |
66 } |
121 |
111 |
122 for (unsigned int a = 0; a < stringcount; a++) |
112 for (unsigned int a = 0; a < stringcount; a++) |
123 WriteString (g_StringTable[a]); |
113 WriteString (g_StringTable[a]); |
124 } |
114 } |
125 |
115 |
126 printf ("%u string%s written\n", stringcount, PLURAL (stringcount)); |
116 printf ("%u / %u string%s written\n", stringcount, MAX_LIST_STRINGS, PLURAL (stringcount)); |
127 } |
117 } |
128 |
118 |
129 // Write main buffer to file |
119 // Write main buffer to file |
130 void ObjWriter::WriteToFile () { |
120 void ObjWriter::WriteToFile () { |
131 fp = fopen (filepath, "w"); |
121 fp = fopen (filepath, "w"); |
137 for (unsigned int x = 0; x < MainBuffer->writesize; x++) { |
127 for (unsigned int x = 0; x < MainBuffer->writesize; x++) { |
138 // Check if this position is a reference |
128 // Check if this position is a reference |
139 for (unsigned int r = 0; r < MAX_MARKS; r++) { |
129 for (unsigned int r = 0; r < MAX_MARKS; r++) { |
140 if (MainBuffer->refs[r] && MainBuffer->refs[r]->pos == x) { |
130 if (MainBuffer->refs[r] && MainBuffer->refs[r]->pos == x) { |
141 word ref = static_cast<word> (MainBuffer->marks[MainBuffer->refs[r]->num]->pos); |
131 word ref = static_cast<word> (MainBuffer->marks[MainBuffer->refs[r]->num]->pos); |
|
132 printf ("insert position %ld at script pos %u\n", |
|
133 ref, x); |
142 WriteDataToFile<word> (ref); |
134 WriteDataToFile<word> (ref); |
|
135 |
|
136 // This reference is now used up |
|
137 delete MainBuffer->refs[r]; |
|
138 MainBuffer->refs[r] = NULL; |
|
139 |
|
140 // All other references need their positions bumped up as the |
|
141 // bytecode gained 4 more bytes with the written reference |
|
142 for (unsigned int s = 0; s < MAX_MARKS; s++) |
|
143 if (MainBuffer->refs[s]) |
|
144 MainBuffer->refs[s]->pos += sizeof (word); |
143 } |
145 } |
144 } |
146 } |
145 |
147 |
146 WriteDataToFile<unsigned char> (*(MainBuffer->buffer+x)); |
148 WriteDataToFile<unsigned char> (*(MainBuffer->buffer+x)); |
147 } |
149 } |