Move write things into one ile.
This commit is contained in:
parent
dada29e25a
commit
3920682ff5
|
@ -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;
|
|
@ -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;
|
3
src/qtshit/write/writeByte.zig
Normal file
3
src/qtshit/write/writeByte.zig
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub fn writeByte(writer: anytype, byte: u8) !void {
|
||||
try writer.writeByte(byte);
|
||||
}
|
16
src/qtshit/write/writeFrame.zig
Normal file
16
src/qtshit/write/writeFrame.zig
Normal 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);
|
||||
}
|
3
src/qtshit/write/writeInt.zig
Normal file
3
src/qtshit/write/writeInt.zig
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub fn writeInt(writer: anytype, number: i32) !void {
|
||||
try writer.writeIntBig(i32, number);
|
||||
}
|
6
src/qtshit/write/writeQByteArray.zig
Normal file
6
src/qtshit/write/writeQByteArray.zig
Normal 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);
|
||||
}
|
10
src/qtshit/write/writeQStringList.zig
Normal file
10
src/qtshit/write/writeQStringList.zig
Normal 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);
|
||||
}
|
||||
}
|
54
src/qtshit/write/writeQVariant.zig
Normal file
54
src/qtshit/write/writeQVariant.zig
Normal 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!");
|
||||
//},
|
||||
}
|
||||
}
|
7
src/qtshit/write/writeQVariantHeader.zig
Normal file
7
src/qtshit/write/writeQVariantHeader.zig
Normal 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);
|
||||
}
|
12
src/qtshit/write/writeQVariantList.zig
Normal file
12
src/qtshit/write/writeQVariantList.zig
Normal 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);
|
||||
}
|
||||
}
|
22
src/qtshit/write/writeQVariantMap.zig
Normal file
22
src/qtshit/write/writeQVariantMap.zig
Normal 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;
|
||||
}
|
3
src/qtshit/write/writeShort.zig
Normal file
3
src/qtshit/write/writeShort.zig
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub fn writeShort(writer: anytype, number: u16) !void {
|
||||
try writer.writeIntBig(u16, number);
|
||||
}
|
3
src/qtshit/write/writeSignedByte.zig
Normal file
3
src/qtshit/write/writeSignedByte.zig
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub fn writeSignedByte(writer: anytype, byte: i8) !void {
|
||||
try writer.writeIntBig(i8, number);
|
||||
}
|
13
src/qtshit/write/writeString.zig
Normal file
13
src/qtshit/write/writeString.zig
Normal 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));
|
||||
}
|
3
src/qtshit/write/writeUInt.zig
Normal file
3
src/qtshit/write/writeUInt.zig
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub fn writeUInt(writer: anytype, number: u32) !void {
|
||||
try writer.writeIntBig(u32, number);
|
||||
}
|
Loading…
Reference in a new issue