Created
May 24, 2022 15:54
-
-
Save samhattangady/5ea5bce00f2edc44cb02eeadcab98b79 to your computer and use it in GitHub Desktop.
zig script to gather data into json.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gather is a script that runs to collect all the data into one single file | |
// we want to create a single json file where all the contents are dumped in | |
// as strings, with the object key being the filepath. | |
const std = @import("std"); | |
const JSON_SERIALIZER_MAX_DEPTH = 32; | |
pub const JsonWriter = std.io.Writer(*JsonStream, JsonStreamError, JsonStream.write); | |
pub const JsonStreamError = error{JsonWriteError}; | |
pub const JsonSerializer = std.json.WriteStream(JsonWriter, JSON_SERIALIZER_MAX_DEPTH); | |
pub const JsonStream = struct { | |
const Self = @This(); | |
buffer: std.ArrayList(u8), | |
pub fn new(allocator: std.mem.Allocator) Self { | |
return Self{ | |
.buffer = std.ArrayList(u8).init(allocator), | |
}; | |
} | |
pub fn deinit(self: *Self) void { | |
self.buffer.deinit(); | |
} | |
pub fn writer(self: *Self) JsonWriter { | |
return .{ .context = self }; | |
} | |
pub fn write(self: *Self, bytes: []const u8) JsonStreamError!usize { | |
self.buffer.appendSlice(bytes) catch unreachable; | |
return bytes.len; | |
} | |
pub fn save_data_to_file(self: *Self, filepath: []const u8) !void { | |
const file = try std.fs.cwd().createFile(filepath, .{}); | |
defer file.close(); | |
_ = try file.writeAll(self.buffer.items); | |
} | |
pub fn serializer(self: *Self) JsonSerializer { | |
return std.json.writeStream(self.writer(), JSON_SERIALIZER_MAX_DEPTH); | |
} | |
}; | |
pub fn main() anyerror!void { | |
var gpa = std.heap.GeneralPurposeAllocator(.{ .safety = true }){}; | |
defer { | |
_ = gpa.deinit(); | |
} | |
var allocator = gpa.allocator(); | |
const dirs = [_][]const u8{ "data/levels/", "data/shaperies/" }; // all directories that we care about go here. | |
var static = JsonStream.new(allocator); | |
defer static.deinit(); | |
var js = static.serializer(); | |
js.whitespace = std.json.StringifyOptions.Whitespace{ .indent = .{ .Space = 2 } }; | |
try js.beginObject(); | |
for (dirs) |dir_path| { | |
var dir = try std.fs.cwd().openDir(dir_path, .{ .iterate = true }); | |
var walker = try dir.walk(allocator); | |
defer walker.deinit(); | |
while (true) { | |
var entry = walker.next() catch unreachable; | |
if (entry == null) break; | |
var path = std.fmt.allocPrint(allocator, "{s}{s}", .{ dir_path, entry.?.basename }) catch unreachable; | |
defer allocator.free(path); | |
std.debug.print("path = {s}\n", .{path}); | |
const file = try std.fs.cwd().openFile(path, .{}); | |
defer file.close(); | |
const file_size = try file.getEndPos(); | |
const data = try file.readToEndAlloc(allocator, file_size); | |
defer allocator.free(data); | |
try js.objectField(path); | |
try js.emitString(data); | |
} | |
} | |
try js.endObject(); | |
static.save_data_to_file("data/static.dump") catch unreachable; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment