Move write things into one ile.

This commit is contained in:
Kitteh 2021-06-02 21:48:56 +01:00
parent dada29e25a
commit 3920682ff5
15 changed files with 168 additions and 125 deletions

View file

@ -1,124 +1,12 @@
const std = @import("std"); pub const writeInt = @import("./write/writeInt.zig").writeInt;
const QVariantType = @import("./types/QVariant.zig").QVariant; pub const writeUInt = @import("./write/writeUInt.zig").writeUInt;
const QVariantTypeID = @import("./utils/QVariantTypeID.zig").QVariantTypeID; pub const writeShort = @import("./write/writeShort.zig").writeShort;
pub const writeByte = @import("./write/writeByte.zig").writeByte;
const AllKnownErrors = (std.os.WriteError || error{OutOfMemory}); pub const writeSignedByte = @import("./write/writeSignedByte.zig").writeSignedByte;
pub const writeQByteArray = @import("./write/writeQByteArray.zig").writeQByteArray;
pub fn writeInt(writer: anytype, number: i32) !void { pub const writeString = @import("./write/writeString.zig").writeString;
try writer.writeIntBig(i32, number); 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 fn writeUInt(writer: anytype, number: u32) !void { pub const writeQVariantMap = @import("./write/writeQVariantMap.zig").writeQVariantMap;
try writer.writeIntBig(u32, number); pub const writeFrame = @import("./write/writeFrame.zig").writeFrame;
}
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;
}

View file

@ -0,0 +1,3 @@
pub fn writeByte(writer: anytype, byte: u8) !void {
try writer.writeByte(byte);
}

View file

@ -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);
}

View file

@ -0,0 +1,3 @@
pub fn writeInt(writer: anytype, number: i32) !void {
try writer.writeIntBig(i32, number);
}

View file

@ -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);
}

View file

@ -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);
}
}

View file

@ -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!");
//},
}
}

View file

@ -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);
}

View file

@ -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);
}
}

View file

@ -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;
}

View file

@ -0,0 +1,3 @@
pub fn writeShort(writer: anytype, number: u16) !void {
try writer.writeIntBig(u16, number);
}

View file

@ -0,0 +1,3 @@
pub fn writeSignedByte(writer: anytype, byte: i8) !void {
try writer.writeIntBig(i8, number);
}

View file

@ -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));
}

View file

@ -0,0 +1,3 @@
pub fn writeUInt(writer: anytype, number: u32) !void {
try writer.writeIntBig(u32, number);
}