From 3920682ff5a1c135197b3509e989512fc96725d7 Mon Sep 17 00:00:00 2001 From: Kitteh Date: Wed, 2 Jun 2021 21:48:56 +0100 Subject: [PATCH] Move write things into one ile. --- src/qtshit/read.zig | 2 +- src/qtshit/write.zig | 136 ++--------------------- src/qtshit/write/writeByte.zig | 3 + src/qtshit/write/writeFrame.zig | 16 +++ src/qtshit/write/writeInt.zig | 3 + src/qtshit/write/writeQByteArray.zig | 6 + src/qtshit/write/writeQStringList.zig | 10 ++ src/qtshit/write/writeQVariant.zig | 54 +++++++++ src/qtshit/write/writeQVariantHeader.zig | 7 ++ src/qtshit/write/writeQVariantList.zig | 12 ++ src/qtshit/write/writeQVariantMap.zig | 22 ++++ src/qtshit/write/writeShort.zig | 3 + src/qtshit/write/writeSignedByte.zig | 3 + src/qtshit/write/writeString.zig | 13 +++ src/qtshit/write/writeUInt.zig | 3 + 15 files changed, 168 insertions(+), 125 deletions(-) create mode 100644 src/qtshit/write/writeByte.zig create mode 100644 src/qtshit/write/writeFrame.zig create mode 100644 src/qtshit/write/writeInt.zig create mode 100644 src/qtshit/write/writeQByteArray.zig create mode 100644 src/qtshit/write/writeQStringList.zig create mode 100644 src/qtshit/write/writeQVariant.zig create mode 100644 src/qtshit/write/writeQVariantHeader.zig create mode 100644 src/qtshit/write/writeQVariantList.zig create mode 100644 src/qtshit/write/writeQVariantMap.zig create mode 100644 src/qtshit/write/writeShort.zig create mode 100644 src/qtshit/write/writeSignedByte.zig create mode 100644 src/qtshit/write/writeString.zig create mode 100644 src/qtshit/write/writeUInt.zig diff --git a/src/qtshit/read.zig b/src/qtshit/read.zig index ee521ba..9f25412 100644 --- a/src/qtshit/read.zig +++ b/src/qtshit/read.zig @@ -9,4 +9,4 @@ pub const readQVariantList = @import("./read/readQVariantList.zig").readQVariant pub const readQVariantT = @import("./read/readQVariantT.zig").readQVariantT; pub const readQVariant = @import("./read/readQVariant.zig").readQVariant; pub const readQVariantMap = @import("./read/readQVariantMap.zig").readQVariantMap; -pub const readQStringList = @import("./readQStringList.zig").readQStringList; +pub const readQStringList = @import("./readQStringList.zig").readQStringList; \ No newline at end of file diff --git a/src/qtshit/write.zig b/src/qtshit/write.zig index 9216350..4375a35 100644 --- a/src/qtshit/write.zig +++ b/src/qtshit/write.zig @@ -1,124 +1,12 @@ -const std = @import("std"); -const QVariantType = @import("./types/QVariant.zig").QVariant; -const QVariantTypeID = @import("./utils/QVariantTypeID.zig").QVariantTypeID; - -const AllKnownErrors = (std.os.WriteError || error{OutOfMemory}); - -pub fn writeInt(writer: anytype, number: i32) !void { - try writer.writeIntBig(i32, number); -} - -pub fn writeUInt(writer: anytype, number: u32) !void { - try writer.writeIntBig(u32, number); -} - -pub fn writeShort(writer: anytype, number: u16) !void { - try writer.writeIntBig(u16, number); -} - -pub fn writeByte(writer: anytype, byte: u8) !void { - try writer.writeByte(byte); -} - -pub fn writeQByteArray(writer: anytype, array: []u8) !void { - try writeUInt(writer, @intCast(u32, array.len)); - try writer.writeAll(array); -} - -pub fn writeString(writer: anytype, allocator: *std.mem.Allocator, str: []const u8) !void { - var ut16Str = try allocator.alloc(u16, str.len); - defer allocator.free(ut16Str); - for (str) |character, index| { - ut16Str[index] = std.mem.nativeToBig(u16, @as(u16, character)); - } - - try writeInt(writer, @intCast(i32, ut16Str.len * 2)); - try writer.writeAll(std.mem.sliceAsBytes(ut16Str)); -} - -pub fn writeQStringList(writer: anytype, allocator: *std.mem.Allocator, strList: [][]const u8) !void { - try writeUInt(writer, @intCast(u32, strList.len)); - for (strList) |string| { - try writeString(writer, allocator, string); - } -} - -pub fn writeQVariantList(writer: anytype, allocator: *std.mem.Allocator, varList: []QVariantType) AllKnownErrors!void { - try writeUInt(writer, @intCast(u32, varList.len)); - for (varList) |v| { - try writeQVariant(writer, allocator, v); - } -} - -pub fn writeQVariantMap(writer: anytype, allocator: *std.mem.Allocator, map: std.StringHashMap(QVariantType)) (@TypeOf(writer).Error || std.os.WriteError || error{OutOfMemory})!void { - var data = std.ArrayList(u8).init(allocator); - defer data.deinit(); - - var writeIterator = map.iterator(); - while (writeIterator.next()) |entry| { - try writeString(data.writer(), allocator, entry.key); - try writeQVariant(data.writer(), allocator, entry.value); - } - - try writeUInt(writer, map.count()); - try writer.writeAll(data.items); - - return; -} - -// Call this and Then write your type -pub fn writeQVariantHeader(writer: anytype, type_id: u32) !void { - try writeUInt(writer, type_id); - try writeByte(writer, 0); -} - -pub fn writeQVariant(writer: anytype, allocator: *std.mem.Allocator, variant: QVariantType) !void { - try writeQVariantHeader(writer, try QVariantTypeID(variant)); - switch (variant) { - .Byte => |out| { - try writeByte(writer, out); - }, - .Int => |out| { - try writeInt(writer, out); - }, - .UInt => |out| { - try writeUInt(writer, out); - }, - .QVariantMap => |out| { - try writeQVariantMap(writer, allocator, out); - }, - .QVariantList => |out| { - try writeQVariantList(writer, allocator, out); - }, - .String => |out| { - try writeString(writer, allocator, out); - }, - .QStringList => |out| { - try writeQStringList(writer, allocator, out); - }, - .QByteArray => |out| { - try writeQByteArray(writer, out); - }, - .Short => |out| { - try writeShort(writer, out); - }, - .UserType => { - @panic("Can't write UserTypes"); - }, - //else => { - // @panic("Unsupported!"); - //}, - } -} - -pub fn writeFrame(writer: anytype, allocator: *std.mem.Allocator, map: std.StringHashMap(QVariantType)) (@TypeOf(writer).Error || std.os.WriteError || error{OutOfMemory})!void { - var data = std.ArrayList(u8).init(allocator); - try writeQVariant(data.writer(), allocator, .{ - .QVariantMap = map, - }); - - try writeUInt(writer, @intCast(u32, data.items.len)); - - try writer.writeAll(data.items); - return; -} +pub const writeInt = @import("./write/writeInt.zig").writeInt; +pub const writeUInt = @import("./write/writeUInt.zig").writeUInt; +pub const writeShort = @import("./write/writeShort.zig").writeShort; +pub const writeByte = @import("./write/writeByte.zig").writeByte; +pub const writeSignedByte = @import("./write/writeSignedByte.zig").writeSignedByte; +pub const writeQByteArray = @import("./write/writeQByteArray.zig").writeQByteArray; +pub const writeString = @import("./write/writeString.zig").writeString; +pub const writeQVariantList = @import("./write/writeQVariantList.zig").writeQVariantList; +pub const writeQVariantHeader = @import("./write/writeQVariantHeader.zig").writeQVariantHeader; +pub const writeQVariant = @import("./write/writeQVariant.zig").writeQVariant; +pub const writeQVariantMap = @import("./write/writeQVariantMap.zig").writeQVariantMap; +pub const writeFrame = @import("./write/writeFrame.zig").writeFrame; \ No newline at end of file diff --git a/src/qtshit/write/writeByte.zig b/src/qtshit/write/writeByte.zig new file mode 100644 index 0000000..8db7c99 --- /dev/null +++ b/src/qtshit/write/writeByte.zig @@ -0,0 +1,3 @@ +pub fn writeByte(writer: anytype, byte: u8) !void { + try writer.writeByte(byte); +} \ No newline at end of file diff --git a/src/qtshit/write/writeFrame.zig b/src/qtshit/write/writeFrame.zig new file mode 100644 index 0000000..be81a5e --- /dev/null +++ b/src/qtshit/write/writeFrame.zig @@ -0,0 +1,16 @@ +const std = @import("std"); +const QVariantType = @import("../types/QVariant.zig").QVariant; + +const writeUInt = @import("./writeUInt.zig").writeUInt; +const writeQVariant = @import("./writeQVariant.zig").writeQVariant; + +pub fn writeFrame(writer: anytype, allocator: *std.mem.Allocator, map: std.StringHashMap(QVariantType)) (@TypeOf(writer).Error || std.os.WriteError || error{OutOfMemory})!void { + var data = std.ArrayList(u8).init(allocator); + try writeQVariant(data.writer(), allocator, .{ + .QVariantMap = map, + }); + + try writeUInt(writer, @intCast(u32, data.items.len)); + + try writer.writeAll(data.items); +} diff --git a/src/qtshit/write/writeInt.zig b/src/qtshit/write/writeInt.zig new file mode 100644 index 0000000..eb150b1 --- /dev/null +++ b/src/qtshit/write/writeInt.zig @@ -0,0 +1,3 @@ +pub fn writeInt(writer: anytype, number: i32) !void { + try writer.writeIntBig(i32, number); +} \ No newline at end of file diff --git a/src/qtshit/write/writeQByteArray.zig b/src/qtshit/write/writeQByteArray.zig new file mode 100644 index 0000000..337b10b --- /dev/null +++ b/src/qtshit/write/writeQByteArray.zig @@ -0,0 +1,6 @@ +const writeUInt = @import("./writeUInt.zig").writeUInt; + +pub fn writeQByteArray(writer: anytype, array: []u8) !void { + try writeUInt(writer, @intCast(u32, array.len)); + try writer.writeAll(array); +} \ No newline at end of file diff --git a/src/qtshit/write/writeQStringList.zig b/src/qtshit/write/writeQStringList.zig new file mode 100644 index 0000000..ed46cb5 --- /dev/null +++ b/src/qtshit/write/writeQStringList.zig @@ -0,0 +1,10 @@ +const std = @import("std"); +const writeUInt = @import("./writeUInt.zig").writeUInt; +const writeString = @import("./writeString.zig").writeString; + +pub fn writeQStringList(writer: anytype, allocator: *std.mem.Allocator, strList: [][]const u8) !void { + try writeUInt(writer, @intCast(u32, strList.len)); + for (strList) |string| { + try writeString(writer, allocator, string); + } +} \ No newline at end of file diff --git a/src/qtshit/write/writeQVariant.zig b/src/qtshit/write/writeQVariant.zig new file mode 100644 index 0000000..b949fc3 --- /dev/null +++ b/src/qtshit/write/writeQVariant.zig @@ -0,0 +1,54 @@ +const std = @import("std"); +const QVariantType = @import("../types/QVariant.zig").QVariant; +const QVariantTypeID = @import("../utils/QVariantTypeID.zig").QVariantTypeID; + +const writeInt = @import("./writeInt.zig").writeInt; +const writeUInt = @import("./writeUInt.zig").writeUInt; +const writeShort = @import("./writeShort.zig").writeShort; +const writeByte = @import("./writeByte.zig").writeByte; +const writeSignedByte = @import("./writeSignedByte.zig").writeSignedByte; +const writeQByteArray = @import("./writeQByteArray.zig").writeQByteArray; +const writeString = @import("./writeString.zig").writeString; +const writeQVariantList = @import("./writeQVariantList.zig").writeQVariantList; +const writeQVariantHeader = @import("./writeQVariantHeader.zig").writeQVariantHeader; +const writeQVariantMap = @import("./writeQVariantMap.zig").writeQVariantMap; +const writeQStringList = @import("./writeQStringList.zig").writeQStringList; + +pub fn writeQVariant(writer: anytype, allocator: *std.mem.Allocator, variant: QVariantType) !void { + try writeQVariantHeader(writer, try QVariantTypeID(variant)); + switch (variant) { + .Byte => |out| { + try writeByte(writer, out); + }, + .Int => |out| { + try writeInt(writer, out); + }, + .UInt => |out| { + try writeUInt(writer, out); + }, + .QVariantMap => |out| { + try writeQVariantMap(writer, allocator, out); + }, + .QVariantList => |out| { + try writeQVariantList(writer, allocator, out); + }, + .String => |out| { + try writeString(writer, allocator, out); + }, + .QStringList => |out| { + try writeQStringList(writer, allocator, out); + }, + .QByteArray => |out| { + try writeQByteArray(writer, out); + }, + .Short => |out| { + try writeShort(writer, out); + }, + .UserType => { + @panic("Can't write UserTypes"); + }, + //else => { + // @panic("Unsupported!"); + //}, + } +} \ No newline at end of file diff --git a/src/qtshit/write/writeQVariantHeader.zig b/src/qtshit/write/writeQVariantHeader.zig new file mode 100644 index 0000000..1856a28 --- /dev/null +++ b/src/qtshit/write/writeQVariantHeader.zig @@ -0,0 +1,7 @@ +const writeUInt = @import("./writeUInt.zig").writeUInt; +const writeByte = @import("./writeByte.zig").writeByte; + +pub fn writeQVariantHeader(writer: anytype, type_id: u32) !void { + try writeUInt(writer, type_id); + try writeByte(writer, 0); +} diff --git a/src/qtshit/write/writeQVariantList.zig b/src/qtshit/write/writeQVariantList.zig new file mode 100644 index 0000000..4bddfc1 --- /dev/null +++ b/src/qtshit/write/writeQVariantList.zig @@ -0,0 +1,12 @@ +const std = @import("std"); +const QVariantType = @import("../types/QVariant.zig").QVariant; +const writeUInt = @import("./writeUInt.zig").writeUInt; +const writeQVariant = @import("./writeQVariant.zig").writeQVariant; + +const AllKnownErrors = (std.os.WriteError || error{OutOfMemory}); +pub fn writeQVariantList(writer: anytype, allocator: *std.mem.Allocator, varList: []QVariantType) AllKnownErrors!void { + try writeUInt(writer, @intCast(u32, varList.len)); + for (varList) |v| { + try writeQVariant(writer, allocator, v); + } +} \ No newline at end of file diff --git a/src/qtshit/write/writeQVariantMap.zig b/src/qtshit/write/writeQVariantMap.zig new file mode 100644 index 0000000..7ac1f4f --- /dev/null +++ b/src/qtshit/write/writeQVariantMap.zig @@ -0,0 +1,22 @@ +const std = @import("std"); +const QVariantType = @import("../types/QVariant.zig").QVariant; +const writeString = @import("./writeString.zig").writeString; +const writeUInt = @import("./writeUInt.zig").writeUInt; +const writeQVariant = @import("./writeQVariant.zig").writeQVariant; + + +pub fn writeQVariantMap(writer: anytype, allocator: *std.mem.Allocator, map: std.StringHashMap(QVariantType)) (@TypeOf(writer).Error || std.os.WriteError || error{OutOfMemory})!void { + var data = std.ArrayList(u8).init(allocator); + defer data.deinit(); + + var writeIterator = map.iterator(); + while (writeIterator.next()) |entry| { + try writeString(data.writer(), allocator, entry.key); + try writeQVariant(data.writer(), allocator, entry.value); + } + + try writeUInt(writer, map.count()); + try writer.writeAll(data.items); + + return; +} \ No newline at end of file diff --git a/src/qtshit/write/writeShort.zig b/src/qtshit/write/writeShort.zig new file mode 100644 index 0000000..2a7f0d2 --- /dev/null +++ b/src/qtshit/write/writeShort.zig @@ -0,0 +1,3 @@ +pub fn writeShort(writer: anytype, number: u16) !void { + try writer.writeIntBig(u16, number); +} \ No newline at end of file diff --git a/src/qtshit/write/writeSignedByte.zig b/src/qtshit/write/writeSignedByte.zig new file mode 100644 index 0000000..1314026 --- /dev/null +++ b/src/qtshit/write/writeSignedByte.zig @@ -0,0 +1,3 @@ +pub fn writeSignedByte(writer: anytype, byte: i8) !void { + try writer.writeIntBig(i8, number); +} \ No newline at end of file diff --git a/src/qtshit/write/writeString.zig b/src/qtshit/write/writeString.zig new file mode 100644 index 0000000..e47d34b --- /dev/null +++ b/src/qtshit/write/writeString.zig @@ -0,0 +1,13 @@ +const std = @import("std"); +const writeInt = @import("./writeInt.zig").writeInt; + +pub fn writeString(writer: anytype, allocator: *std.mem.Allocator, str: []const u8) !void { + var ut16Str = try allocator.alloc(u16, str.len); + defer allocator.free(ut16Str); + for (str) |character, index| { + ut16Str[index] = std.mem.nativeToBig(u16, @as(u16, character)); + } + + try writeInt(writer, @intCast(i32, ut16Str.len * 2)); + try writer.writeAll(std.mem.sliceAsBytes(ut16Str)); +} \ No newline at end of file diff --git a/src/qtshit/write/writeUInt.zig b/src/qtshit/write/writeUInt.zig new file mode 100644 index 0000000..cd3d8d5 --- /dev/null +++ b/src/qtshit/write/writeUInt.zig @@ -0,0 +1,3 @@ +pub fn writeUInt(writer: anytype, number: u32) !void { + try writer.writeIntBig(u32, number); +} \ No newline at end of file