build: more idiomatic hexing
This commit is contained in:
parent
1be0389343
commit
17a86b351a
1 changed files with 8 additions and 23 deletions
31
build.zig
31
build.zig
|
@ -145,38 +145,23 @@ pub fn build(b: *std.build.Builder) !void {
|
||||||
const input_path = b.fmt("examples/server/public/{s}", .{asset});
|
const input_path = b.fmt("examples/server/public/{s}", .{asset});
|
||||||
const output_path = b.fmt("examples/server/{s}.hpp", .{asset});
|
const output_path = b.fmt("examples/server/{s}.hpp", .{asset});
|
||||||
|
|
||||||
const input_file = try std.fs.cwd().openFile(input_path, .{});
|
const input = try std.fs.cwd().readFileAlloc(b.allocator, input_path, std.math.maxInt(usize));
|
||||||
defer input_file.close();
|
|
||||||
const input = try input_file.readToEndAlloc(b.allocator, std.math.maxInt(usize));
|
|
||||||
defer b.allocator.free(input);
|
defer b.allocator.free(input);
|
||||||
|
|
||||||
const hex = try b.allocator.alloc(u8, input.len * 6);
|
var buf = std.ArrayList(u8).init(b.allocator);
|
||||||
defer b.allocator.free(hex);
|
defer buf.deinit();
|
||||||
var i: usize = 0;
|
|
||||||
for (input) |byte| {
|
for (input) |byte| {
|
||||||
const high = byte >> 4;
|
try std.fmt.format(buf.writer(), "0x{X:0>2}, ", .{byte});
|
||||||
const low = byte & 0xF;
|
|
||||||
hex[i] = '0';
|
|
||||||
hex[i + 1] = 'x';
|
|
||||||
hex[i + 2] = if (high < 10) high + '0' else high - 10 + 'A';
|
|
||||||
hex[i + 3] = if (low < 10) low + '0' else low - 10 + 'A';
|
|
||||||
hex[i + 4] = ',';
|
|
||||||
hex[i + 5] = ' ';
|
|
||||||
i += 6;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const output_file = try std.fs.cwd().createFile(output_path, .{});
|
var name = try std.mem.replaceOwned(u8, b.allocator, asset, "-", "_");
|
||||||
defer output_file.close();
|
|
||||||
|
|
||||||
const name = try b.allocator.alloc(u8, asset.len);
|
|
||||||
defer b.allocator.free(name);
|
defer b.allocator.free(name);
|
||||||
std.mem.copy(u8, name, asset);
|
|
||||||
std.mem.replaceScalar(u8, name, '-', '_');
|
|
||||||
std.mem.replaceScalar(u8, name, '.', '_');
|
std.mem.replaceScalar(u8, name, '.', '_');
|
||||||
|
|
||||||
try output_file.writeAll(b.fmt(
|
try std.fs.cwd().writeFile(output_path, b.fmt(
|
||||||
"unsigned char {s}[] = {{{s}}};\nunsigned int {s}_len = {d};\n",
|
"unsigned char {s}[] = {{{s}}};\nunsigned int {s}_len = {d};\n",
|
||||||
.{ name, hex, name, input.len },
|
.{ name, buf.items, name, input.len },
|
||||||
));
|
));
|
||||||
|
|
||||||
std.debug.print("Dumped hex of \"{s}\" ({s}) to {s}\n", .{ input_path, name, output_path });
|
std.debug.print("Dumped hex of \"{s}\" ({s}) to {s}\n", .{ input_path, name, output_path });
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue