@@ -58,7 +58,7 @@ static tb_bool_t xm_utils_bin2c_dump(tb_stream_ref_t istream,
5858 tb_stream_ref_t ostream ,
5959 tb_int_t linewidth ,
6060 tb_bool_t nozeroend ) {
61-
61+
6262 tb_bool_t first = tb_true ;
6363 tb_bool_t zero_pending = tb_false ;
6464 tb_byte_t data [XM_BIN2C_DATA_SIZE ];
@@ -68,7 +68,7 @@ static tb_bool_t xm_utils_bin2c_dump(tb_stream_ref_t istream,
6868 tb_size_t data_pos = 0 ;
6969 tb_size_t data_size = 0 ;
7070 tb_assert_and_check_return_val (linewidth > 0 && linewidth <= XM_BIN2C_LINEWIDTH_MAX , tb_false );
71-
71+
7272 while (!tb_stream_beof (istream ) || data_pos < data_size || zero_pending ) {
7373 // read a large chunk of data if buffer is empty
7474 if (data_pos >= data_size ) {
@@ -84,13 +84,13 @@ static tb_bool_t xm_utils_bin2c_dump(tb_stream_ref_t istream,
8484 if (!to_read ) {
8585 break ;
8686 }
87-
87+
8888 if (!tb_stream_bread (istream , data , to_read )) {
8989 break ;
9090 }
9191 data_size = to_read ;
9292 data_pos = 0 ;
93-
93+
9494 // check if we need to add zero terminator at the end
9595 if (!nozeroend && tb_stream_beof (istream )) {
9696 if (data_size < XM_BIN2C_DATA_SIZE ) {
@@ -103,7 +103,7 @@ static tb_bool_t xm_utils_bin2c_dump(tb_stream_ref_t istream,
103103 }
104104 }
105105 }
106-
106+
107107 // process bytes from buffer
108108 while (data_pos < data_size ) {
109109 // check if we need a new line
@@ -112,12 +112,12 @@ static tb_bool_t xm_utils_bin2c_dump(tb_stream_ref_t istream,
112112 if (tb_stream_bwrit_line (ostream , line , linesize ) < 0 ) {
113113 return tb_false ;
114114 }
115-
115+
116116 linesize = 0 ;
117117 bytes_in_line = 0 ;
118118 first = tb_false ;
119119 }
120-
120+
121121 // ensure we have enough space in line buffer (6 chars per byte: ", 0xXX")
122122 if (linesize + 6 > sizeof (line )) {
123123 // flush partial line if buffer is full
@@ -128,35 +128,31 @@ static tb_bool_t xm_utils_bin2c_dump(tb_stream_ref_t istream,
128128 linesize = 0 ;
129129 }
130130 }
131-
131+
132132 // add separator
133- if (bytes_in_line == 0 ) {
134- if (first ) {
135- line [linesize ++ ] = ' ' ;
136- first = tb_false ;
137- } else {
138- line [linesize ++ ] = ',' ;
139- }
133+ if (bytes_in_line == 0 && first ) {
134+ line [linesize ++ ] = ' ' ;
135+ first = tb_false ;
140136 } else {
141137 line [linesize ++ ] = ',' ;
142138 }
143-
139+
144140 // write hex value (inline for performance)
145141 xm_utils_bin2c_write_hex (line + linesize , data [data_pos ]);
146142 linesize += 5 ;
147143 bytes_in_line ++ ;
148144 data_pos ++ ;
149145 }
150146 }
151-
147+
152148 // flush remaining line
153149 if (linesize > 0 ) {
154150 // write line (tb_stream_bwrit_line will add newline automatically)
155151 if (tb_stream_bwrit_line (ostream , line , linesize ) < 0 ) {
156152 return tb_false ;
157153 }
158154 }
159-
155+
160156 return tb_stream_beof (istream );
161157}
162158
0 commit comments