map to array fizzbuzz technique

This commit is contained in:
Kitteh 2021-06-03 16:05:20 +01:00
parent 4d36de6c5d
commit 04385070f1
4 changed files with 29 additions and 5 deletions

View file

@ -4,6 +4,8 @@ const write = @import("./qtshit/write.zig");
const QVariantType = @import("./qtshit/types/QVariant.zig").QVariant;
const prettyPrintQVariant = @import("./qtshit/utils/prettyPrintQVariant.zig").prettyPrintQVariant;
const freeQVariant = @import("./qtshit/utils/freeQVariant.zig").freeQVariant;
const QVariantMapToQVariantList = @import("./qtshit/utils/QVariantMapToQVariantList.zig").QVariantMapToQVariantList;
fn dumpDebug(name: []const u8, list: std.ArrayList(u8)) !void {
std.debug.print("dumpDebug list len {d}\n", .{list.items.len});

View file

@ -24,6 +24,12 @@ pub fn main() !void {
try client.quassel_init_packet();
try client.quassel_login(username, password);
while (true) {
try client.read_quassel_packet();
client.read_quassel_packet() catch |err| {
if (err == error.DecodeError) {
std.debug.print("Decode Error.\n", .{});
} else {
return err;
}
};
}
}

View file

@ -17,7 +17,7 @@ const readUserType = @import("./usertypes/readUserType.zig").readUserType;
const QVariant = @import("../types/QVariant.zig").QVariant;
const QVariantTypes = @import("../types/QVariantTypes.zig").QVariantTypes;
pub fn readQVariantT(reader: anytype, type_id: u32, allocator: *std.mem.Allocator) (@TypeOf(reader).Error || error{EndOfStream} || error{OutOfMemory})!QVariant {
pub fn readQVariantT(reader: anytype, type_id: u32, allocator: *std.mem.Allocator) (@TypeOf(reader).Error || error{EndOfStream} || error{OutOfMemory} || error{DecodeError})!QVariant {
switch (type_id) {
@enumToInt(QVariantTypes.Byte) => {
var byte = try readByte(reader);
@ -65,9 +65,7 @@ pub fn readQVariantT(reader: anytype, type_id: u32, allocator: *std.mem.Allocato
},
else => {
std.debug.print("Unknown Type ID: {d}\n", .{type_id});
return QVariant{ .String = "UnknownType" };
//@panic("Unknown Type See Above.");
return error.DecodeError;
},
}
return QVariant{ .empty = true };

View file

@ -0,0 +1,18 @@
const std = @import("std");
const QVariant = @import("../types/QVariant.zig").QVariant;
const range = @import("./RangeIter.zig").range;
pub fn QVariantMapToQVariantList(varMap: std.StringHashMap(QVariant), allocator: *std.mem.Allocator) ![]QVariant {
var variantList = try allocator.alloc(QVariant, @intCast(usize, varMap.count() * 2));
var varMapIter = varMap.iterator();
var iter = range(u32, 0, varMap.count() * 2);
while (iter.next()) |i| {
if (i % 2 == 1) continue;
var item = varMapIter.next().?;
variantList[i] = QVariant{.String = item.key};
variantList[i + 1] = item.value;
}
return variantList;
}