From 52a601bc170d473d0dfdbd7e66c950736e058219 Mon Sep 17 00:00:00 2001 From: Kitteh Date: Wed, 2 Jun 2021 13:18:12 +0100 Subject: [PATCH] test --- src/client.zig | 4 ++-- src/qtshit/qvariant.zig | 24 +----------------------- src/qtshit/read.zig | 1 - src/qtshit/write.zig | 39 +++++++++++++++------------------------ 4 files changed, 18 insertions(+), 50 deletions(-) diff --git a/src/client.zig b/src/client.zig index 8aaddcf..f6c9268 100644 --- a/src/client.zig +++ b/src/client.zig @@ -50,7 +50,7 @@ pub const Client = struct { try map.put("ClientVersion", .{ .string = "0.1 (quasselclient)" }); try map.put("ClientDate", .{ .string = "0" }); - try write.add_qvariantmap(list.writer(), map); + try write.add_qvariantmap(list.writer(), s.allocator, map); try dumpDebug("ClientInit.bin", list); try s.stream.writer().writeAll(list.items); @@ -62,7 +62,7 @@ pub const Client = struct { try map.put("User", .{ .string = username }); try map.put("Password", .{ .string = password }); - try write.add_qvariantmap(s.stream.writer(), map); + try write.add_qvariantmap(s.stream.writer(), s.allocator, map); } }; diff --git a/src/qtshit/qvariant.zig b/src/qtshit/qvariant.zig index d76e91c..1112a50 100644 --- a/src/qtshit/qvariant.zig +++ b/src/qtshit/qvariant.zig @@ -50,26 +50,4 @@ pub fn qvariant_type_id(variant: QVariant) !u32 { return 0; }, } -} - -pub fn qvariant_size(variant: QVariant) !u32 { - var size: u32 = 0; - - switch (variant) { - .int => { - size += 4; - }, - .byte => { - size += 1; - }, - .string => |*str| { - size += @intCast(u32, str.len); - // Null Terminator - size += 1; - }, - else => { - size += 0; - }, - } - return size; -} +} \ No newline at end of file diff --git a/src/qtshit/read.zig b/src/qtshit/read.zig index bfee870..44cf4cf 100644 --- a/src/qtshit/read.zig +++ b/src/qtshit/read.zig @@ -49,7 +49,6 @@ pub fn get_stringlist(reader: anytype, allocator: *std.mem.Allocator) !std.Array pub fn get_qvariant(reader: anytype) !u32 { var type_id = try get_int(reader); // TODO: make nop variants work??? - _ = try get_byte(reader); return type_id; } diff --git a/src/qtshit/write.zig b/src/qtshit/write.zig index fbe3ff6..a9f8f5c 100644 --- a/src/qtshit/write.zig +++ b/src/qtshit/write.zig @@ -33,10 +33,9 @@ pub fn add_stringlist(writer: anytype, strList: std.ArrayList([]const u8)) !void // Call this and Then write your type pub fn add_qvariant_with_id(writer: anytype, type_id: u32) !void { try add_int(writer, type_id); - try add_byte(writer, 0); } -pub fn add_qvariant(writer: anytype, variant: qvar.QVariant) !void { +pub fn add_qvariant(writer: anytype, allocator: *std.mem.Allocator, variant: qvar.QVariant) !void { try add_qvariant_with_id(writer, try qvar.qvariant_type_id(variant)); switch (variant) { .byte => |out| { @@ -46,7 +45,7 @@ pub fn add_qvariant(writer: anytype, variant: qvar.QVariant) !void { try add_int(writer, out); }, .qmap => |out| { - try add_qvariantmap(writer, out); + try add_qvariantmap(writer, allocator, out); }, .string => |out| { try add_string(writer, out); @@ -66,31 +65,23 @@ pub fn add_qvariant(writer: anytype, variant: qvar.QVariant) !void { } } -pub fn add_qvariantmap(writer: anytype, map: std.StringHashMap(qvar.QVariant)) @TypeOf(writer).Error!void { - var size: u32 = 0; - - var sizeIterator = map.iterator(); - - while (sizeIterator.next()) |entry| { - size += try qvar.qvariant_size(.{ .string = entry.key }); - size += try qvar.qvariant_size(entry.value); - size += 4; - } - - try add_int(writer, size + 4 + 1 + 4); - try add_int(writer, 8); - try add_byte(writer, 1); - try add_int(writer, map.count()); - +pub fn add_qvariantmap(writer: anytype, allocator: *std.mem.Allocator, map: std.StringHashMap(qvar.QVariant)) (@TypeOf(writer).Error || std.os.WriteError || error{OutOfMemory})!void { + var data = std.ArrayList(u8).init(allocator); var writeIterator = map.iterator(); - - while (writeIterator.next()) |entry| { - try add_string(writer, entry.key); - try add_qvariant(writer, entry.value); + try add_string(data.writer(), entry.key); + try add_qvariant(data.writer(), allocator, entry.value); } - std.debug.print("Size {d} !\n", .{size}); + // lengths o the mesage + try add_int(writer, @intCast(u32, data.items.len) + 4 + 1 + 4); + // qvariantmap type + try add_int(writer, 8); // 4 + // validity? + try add_byte(writer, 0); // 1 + // elements + try add_int(writer, map.count()); // 4 + try writer.writeAll(data.items); return; }