@@ -157,25 +157,26 @@ bool label_extract(LabelRepository *labels, MemoryMap *map, Section *section) {
157157 return ret ;
158158}
159159
160- static int data_extract_binary (FILE * out , Section * section , MemoryMap * map , LabelRepository * repository ) {
160+ static bool data_extract_binary (Output * output , Section * section , MemoryMap * map , LabelRepository * repository ) {
161161 uint16_t logical ;
162162 int32_t i ;
163- for (i = 0 , logical = section -> logical ; i < section -> size ; i ++ , logical ++ ) {
163+ bool ret = true;
164+ for (i = 0 , logical = section -> logical ; ret && (i < section -> size ); i ++ , logical ++ ) {
164165 uint8_t data = memory_map_read (map , logical );
165- fwrite ( & data , 1 , 1 , out );
166+ ret = output_raw ( output , data );
166167 }
167- return 1 ;
168+ return ret ;
168169}
169170
170- static int data_extract_hex (FILE * out , Section * section , MemoryMap * map , LabelRepository * repository ,
171+ static int data_extract_hex (Output * output , Section * section , MemoryMap * map , LabelRepository * repository ,
171172 CommentRepository * comments , int extra_infos ) {
172173 const int32_t element_size = section -> data .element_size ;
173174 const int32_t elements_per_line = section -> data .elements_per_line ;
174175
175176 int32_t i , j ;
176177 uint16_t logical ;
177178
178- size_t line_offset = ftell (out );
179+ // size_t line_offset = ftell(out);
179180 uint8_t line_page = section -> page ;
180181 uint16_t line_logical = section -> logical ;
181182
@@ -186,37 +187,51 @@ static int data_extract_hex(FILE *out, Section *section, MemoryMap *map, LabelRe
186187 uint8_t data [2 ] = {0 };
187188 int32_t top = 0 ;
188189
190+ bool ret = false;
191+
189192 for (i = 0 , j = 0 , logical = section -> logical ; i < section -> size ; i ++ , logical ++ ) {
190193 uint8_t page = memory_map_page (map , logical );
191194 Label label = {0 };
192195 bool has_label = label_repository_find (repository , logical , page , & label );
193196 if (has_label ) {
194197 // flush any bytes left in the buffer.
195198 if (top && (top < element_size )) {
196- fprintf (out , "\n%s.db $%02x" , g_spacing , data [0 ]);
199+ ret = output_newline (output );
200+ ret = output_fill_n (output , ' ' , 4U );
201+ ret = output_string (output , ".db" );
202+ ret = output_char (output , ' ' );
203+ ret = output_8h (output , data [0 ]);
204+
197205 for (int32_t l = 1 ; l < top ; l ++ ) { // useless as top is always equal to 1
198- fprintf (out , ",$%02x" , data [l ]);
206+ ret = output_char (output , ',' );
207+ ret = output_8h (output , data [l ]);
199208 }
200209 top = 0 ;
201210 }
202211 if (i ) {
203- fputc ( '\n' , out );
212+ ret = output_newline ( output );
204213 }
205- print_label ( out , & label );
214+ ret = output_label ( output , & label );
206215 j = 0 ;
207216 }
208217
209218 Comment dummy ;
210219 if (comment_repository_find (comments , logical , page , & dummy )) {
211220 if (has_comment ) {
212221 if (top && (top < element_size )) {
213- fprintf (out , "\n%s.db $%02x" , g_spacing , data [0 ]);
222+ ret = output_newline (output );
223+ ret = output_fill_n (output , ' ' , 4U );
224+ ret = output_string (output , ".db" );
225+ ret = output_char (output , ' ' );
226+ ret = output_8h (output , data [0 ]);
227+
214228 for (int32_t l = 1 ; l < top ; l ++ ) { // useless as top is always equal to 1
215- fprintf (out , ",$%02x" , data [l ]);
229+ ret = output_char (output , ',' );
230+ ret = output_8h (output , data [l ]);
216231 }
217232 top = 0 ;
218233 }
219- print_inline_comment ( out , ( int )( ftell ( out ) - line_offset ) , comment .text );
234+ ret = output_inline_comment ( output , comment .text );
220235 }
221236 comment = dummy ;
222237 has_comment = true;
@@ -228,59 +243,56 @@ static int data_extract_hex(FILE *out, Section *section, MemoryMap *map, LabelRe
228243 if (top >= element_size ) {
229244 char sep ;
230245 if (j == 0 ) {
231- fputc ( '\n' , out );
246+ ret = output_newline ( output );
232247
233- line_offset = ftell (out );
234248 line_logical = logical - top + 1 ;
235249 line_page = page ;
236250
237251 const char * data_decl = (top > 1 ) ? ".dw" : ".db" ;
238252
239- fprintf (out , "%s%s" , g_spacing , data_decl );
253+ ret = output_fill_n (output , ' ' , 4U );
254+ ret = output_string (output , data_decl );
255+
240256 sep = ' ' ;
241257 } else {
242258 sep = ',' ;
243259 }
244- fputc (sep , out );
245- fputc ('$' , out );
246- if (top > 1 ) {
247- while (top -- ) {
248- fprintf (out , "%02x" , data [top ]);
249- }
250- } else {
251- fprintf (out , "%02x" , data [0 ]);
260+ ret = output_char (output , sep );
261+ while (top > 1 ) {
262+ top -- ;
263+ ret = output_8h (output , data [top ]);
252264 }
253- top = 0 ;
254265 j ++ ;
255266
256267 if (j == elements_per_line ) {
257268 j = 0 ;
258- int n = (int )(ftell (out ) - line_offset );
259269 if (has_comment ) {
260- print_inline_comment ( out , n , comment .text );
270+ ret = output_inline_comment ( output , comment .text );
261271 has_comment = false;
262272 } else if (extra_infos ) {
263- print_statement_address ( out , n , line_logical , line_page );
273+ ret = output_address_comment ( output , line_page , line_logical );
264274 }
265275 }
266276 }
267277 }
268278 // flush remaining bytes
269279 if (top ) {
270- int n = (int )(ftell (out ) - line_offset );
271280 if (has_comment ) {
272- print_inline_comment ( out , n , comment .text );
281+ ret = output_inline_comment ( output , comment .text );
273282 has_comment = false;
274283 } else if (extra_infos ) {
275- print_statement_address ( out , n , line_logical , line_page );
284+ ret = output_address_comment ( output , line_page , line_logical );
276285 }
277- fprintf (out , "\n%s.db $%02x" , g_spacing , data [0 ]);
286+ ret = output_fill_to (output , ' ' , 10U ); // [todo]
287+ ret = output_string (output , ".db" );
288+ ret = output_8h (output , data [0 ]);
278289 for (int32_t j = 1 ; j < top ; j ++ ) { // useless as top is always equal to 1
279- fprintf (out , ",$%02x" , data [j ]);
290+ ret = output_char (output , ',' );
291+ ret = output_8h (output , data [j ]);
280292 }
281293 }
282- fputc ( '\n' , out );
283- return 1 ;
294+ ret = output_newline ( output );
295+ return ret ;
284296}
285297
286298static int data_extract_string (FILE * out , Section * section , MemoryMap * map , LabelRepository * repository ,
@@ -305,7 +317,7 @@ static int data_extract_string(FILE *out, Section *section, MemoryMap *map, Labe
305317 Label label = {0 };
306318 bool has_label = label_repository_find (repository , logical , page , & label );
307319 if (has_label ) {
308- if (c ) { // close string if neededs
320+ if (c ) { // close string if needed
309321 fputc ('"' , out );
310322 c = 0 ;
311323 }
@@ -319,7 +331,7 @@ static int data_extract_string(FILE *out, Section *section, MemoryMap *map, Labe
319331 Comment dummy = {0 };
320332 if (comment_repository_find (comments , logical , page , & dummy )) {
321333 if (j ) {
322- if (c ) { // close string if neededs
334+ if (c ) { // close string if needed
323335 fputc ('"' , out );
324336 c = 0 ;
325337 }
@@ -335,7 +347,7 @@ static int data_extract_string(FILE *out, Section *section, MemoryMap *map, Labe
335347 // display directives
336348 if (j == 0 ) {
337349 fputc ('\n' , out );
338- line_offset = ftell (out ); // record star of line
350+ line_offset = ftell (out ); // record start of line
339351 line_logical = logical ;
340352 line_page = page ;
341353 fprintf (out , "%s.db " , g_spacing );
@@ -501,17 +513,17 @@ static int data_extract_jump_table(FILE *out, Section *section, MemoryMap *map,
501513}
502514
503515/* Process data section. The result will be output has a binary file or an asm file containing hex values or strings. */
504- bool data_extract (FILE * out , Section * section , MemoryMap * map , LabelRepository * repository ,
516+ bool data_extract (Output * output , Section * section , MemoryMap * map , LabelRepository * repository ,
505517 CommentRepository * comments , int extra_infos ) {
506518 switch (section -> data .type ) {
507519 case DATA_TYPE_BINARY :
508- return data_extract_binary (out , section , map , repository );
520+ return data_extract_binary (output , section , map , repository );
509521 case DATA_TYPE_HEX :
510- return data_extract_hex (out , section , map , repository , comments , extra_infos );
522+ return data_extract_hex (output , section , map , repository , comments , extra_infos );
511523 case DATA_TYPE_STRING :
512- return data_extract_string (out , section , map , repository , comments , extra_infos );
524+ return true; // [todo] data_extract_string(out, section, map, repository, comments, extra_infos);
513525 case DATA_TYPE_JUMP_TABLE :
514- return data_extract_jump_table (out , section , map , repository , comments , extra_infos );
526+ return true; // [todo] data_extract_jump_table(out, section, map, repository, comments, extra_infos);
515527 default :
516528 return false;
517529 }
0 commit comments