summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2024-10-25 22:43:39 +0800
committerruki <[email protected]>2024-10-25 22:43:39 +0800
commit70a00eb8873410b1b6ba69bd05d11d94d1011ac7 (patch)
treebff320bdf9f7daf848e7d8eba45c86cbcbd839fc
parent4dfd87d6ad16abf3e3d055654058cada99f21155 (diff)
imple bin2c
-rw-r--r--core/src/xmake/utils/bin2c.c42
1 files changed, 41 insertions, 1 deletions
diff --git a/core/src/xmake/utils/bin2c.c b/core/src/xmake/utils/bin2c.c
index 1e0704070..3c4b9e424 100644
--- a/core/src/xmake/utils/bin2c.c
+++ b/core/src/xmake/utils/bin2c.c
@@ -35,7 +35,47 @@
*/
static tb_bool_t xm_utils_bin2c_dump(tb_stream_ref_t istream, tb_stream_ref_t ostream, tb_int_t linewidth, tb_bool_t nozeroend)
{
- return tb_true;
+ tb_bool_t first = tb_true;
+ tb_hong_t i = 0;
+ tb_char_t line[8192];
+ tb_byte_t data[512];
+ tb_size_t linesize = 0;
+ tb_size_t need = 0;
+ tb_assert_and_check_return_val(linewidth < sizeof(data), tb_false);
+ while (!tb_stream_beof(istream))
+ {
+ linesize = 0;
+ need = (tb_size_t)tb_min(tb_stream_left(istream), linewidth);
+ if (need)
+ {
+ if (!tb_stream_bread(istream, data, need))
+ break;
+
+ for (i = 0; i < need; i++)
+ {
+ tb_assert_and_check_break(linesize < sizeof(line));
+ if (first)
+ {
+ first = tb_false;
+ line[linesize++] = ' ';
+ }
+ else
+ {
+ line[linesize++] = ',';
+ }
+
+ tb_assert_and_check_break(linesize + 5 < sizeof(line));
+
+ linesize += tb_snprintf(line + linesize, sizeof(line) - linesize, " 0x%02X", data[i]);
+ }
+ tb_assert_and_check_break(i == need && linesize && linesize < sizeof(line));
+
+ if (tb_stream_bwrit_line(ostream, line, linesize) < 0)
+ break;
+ }
+ }
+
+ return tb_stream_beof(istream);
}
/* //////////////////////////////////////////////////////////////////////////////////////