@@ -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,15 +243,16 @@ 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 = ',' ;
@@ -280,7 +296,7 @@ static int data_extract_hex(FILE *out, Section *section, MemoryMap *map, LabelRe
280296 }
281297 }
282298 fputc ('\n' , out );
283- return 1 ;
299+ return ret ;
284300}
285301
286302static int data_extract_string (FILE * out , Section * section , MemoryMap * map , LabelRepository * repository ,
@@ -305,7 +321,7 @@ static int data_extract_string(FILE *out, Section *section, MemoryMap *map, Labe
305321 Label label = {0 };
306322 bool has_label = label_repository_find (repository , logical , page , & label );
307323 if (has_label ) {
308- if (c ) { // close string if neededs
324+ if (c ) { // close string if needed
309325 fputc ('"' , out );
310326 c = 0 ;
311327 }
@@ -319,7 +335,7 @@ static int data_extract_string(FILE *out, Section *section, MemoryMap *map, Labe
319335 Comment dummy = {0 };
320336 if (comment_repository_find (comments , logical , page , & dummy )) {
321337 if (j ) {
322- if (c ) { // close string if neededs
338+ if (c ) { // close string if needed
323339 fputc ('"' , out );
324340 c = 0 ;
325341 }
@@ -335,7 +351,7 @@ static int data_extract_string(FILE *out, Section *section, MemoryMap *map, Labe
335351 // display directives
336352 if (j == 0 ) {
337353 fputc ('\n' , out );
338- line_offset = ftell (out ); // record star of line
354+ line_offset = ftell (out ); // record start of line
339355 line_logical = logical ;
340356 line_page = page ;
341357 fprintf (out , "%s.db " , g_spacing );
@@ -501,17 +517,17 @@ static int data_extract_jump_table(FILE *out, Section *section, MemoryMap *map,
501517}
502518
503519/* 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 ,
520+ bool data_extract (Output * output , Section * section , MemoryMap * map , LabelRepository * repository ,
505521 CommentRepository * comments , int extra_infos ) {
506522 switch (section -> data .type ) {
507523 case DATA_TYPE_BINARY :
508- return data_extract_binary (out , section , map , repository );
524+ return data_extract_binary (output , section , map , repository );
509525 case DATA_TYPE_HEX :
510- return data_extract_hex (out , section , map , repository , comments , extra_infos );
526+ return data_extract_hex (output , section , map , repository , comments , extra_infos );
511527 case DATA_TYPE_STRING :
512- return data_extract_string (out , section , map , repository , comments , extra_infos );
528+ return true; // [todo] data_extract_string(out, section, map, repository, comments, extra_infos);
513529 case DATA_TYPE_JUMP_TABLE :
514- return data_extract_jump_table (out , section , map , repository , comments , extra_infos );
530+ return true; // [todo] data_extract_jump_table(out, section, map, repository, comments, extra_infos);
515531 default :
516532 return false;
517533 }
0 commit comments