work on bytearray.
This commit is contained in:
parent
5f7b6b015f
commit
e69d037cad
|
@ -15,5 +15,6 @@ pub const QVariantTypes = enum(u32) {
|
|||
int_1 = 2,
|
||||
int_2 = 3,
|
||||
string = 10,
|
||||
|
||||
stringlist = 11,
|
||||
short = 133,
|
||||
};
|
|
@ -13,6 +13,21 @@ pub fn get_byte(reader: anytype) !u8 {
|
|||
return try reader.readByte();
|
||||
}
|
||||
|
||||
pub fn get_bytearray(reader: anytype, allocator: *std.mem.Allocator) !std.ArrayList(u8) {
|
||||
var byteList = std.ArrayList(u8).init(allocator);
|
||||
var length = try get_int(reader);
|
||||
|
||||
|
||||
var index: usize = 0;
|
||||
while (true) {
|
||||
if (index == length) return byteList;
|
||||
|
||||
const byte = try reader.readByte();
|
||||
try byteList.append(byte);
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_string(reader: anytype, allocator: *std.mem.Allocator) ![]u8 {
|
||||
return try reader.readUntilDelimiterAlloc(
|
||||
allocator,
|
||||
|
@ -42,23 +57,31 @@ pub fn get_qvariant(reader: anytype) !u32 {
|
|||
pub fn get_variant_t(reader: anytype, type_id: u32, allocator: *std.mem.Allocator) !qvar.QVariant {
|
||||
switch (type_id) {
|
||||
@enumToInt(qvar.QVariantTypes.byte) => {
|
||||
var byte = try get_byte(reader);
|
||||
var byte = try get_byte(reader);
|
||||
return qvar.QVariant{ .byte = byte };
|
||||
},
|
||||
@enumToInt(qvar.QVariantTypes.int_1), @enumToInt(qvar.QVariantTypes.int_2) => {
|
||||
var int = try get_int(reader);
|
||||
var int = try get_int(reader);
|
||||
return qvar.QVariant{ .int = int };
|
||||
},
|
||||
@enumToInt(qvar.QVariantTypes.string) => {
|
||||
var str = try get_string(reader, allocator);
|
||||
var str = try get_string(reader, allocator);
|
||||
return qvar.QVariant{ .string = str };
|
||||
},
|
||||
@enumToInt(qvar.QVariantTypes.stringlist) => {
|
||||
var strlist = try get_stringlist(reader, allocator);
|
||||
return qvar.QVariant{ .stringlist = strlist };
|
||||
},
|
||||
@enumToInt(qvar.QVariantTypes.short) => {
|
||||
var short = try get_short(reader);
|
||||
return qvar.QVariant{ .short = short };
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
return qvar.QVariant{ .empty = true };
|
||||
return qvar.QVariant{ .empty = true };
|
||||
}
|
||||
|
||||
pub fn get_variant(reader: anytype, allocator: *std.mem.Allocator) !qvar.QVariant {
|
||||
var type_id = try get_qvariant(reader);
|
||||
return try get_variant_t(reader, type_id, allocator);
|
||||
var type_id = try get_qvariant(reader);
|
||||
return try get_variant_t(reader, type_id, allocator);
|
||||
}
|
||||
|
|
|
@ -57,6 +57,27 @@ test "read/write string" {
|
|||
try expect(std.mem.eql(u8, val, "Hello World!"));
|
||||
}
|
||||
|
||||
test "read/write bytearray" {
|
||||
var byteList = std.ArrayList(u8).init(global_allocator);
|
||||
defer byteList.deinit();
|
||||
|
||||
var arr = std.ArrayList(u8).init(global_allocator);
|
||||
defer arr.deinit();
|
||||
try arr.append(13);
|
||||
try arr.append(12);
|
||||
|
||||
try write.add_bytearray(byteList.writer(), arr);
|
||||
|
||||
var fBS = std.io.fixedBufferStream(byteList.items);
|
||||
var val = try read.get_bytearray(fBS.reader(), global_allocator);
|
||||
val.deinit();
|
||||
|
||||
try expect(arr.items.len == val.items.len);
|
||||
for (arr.items) |item, index| {
|
||||
try std.io.getStdErr().writer().print("{d} vs {d} \n", .{ item, val.items[index] });
|
||||
try std.testing.expect(item == val.items[index]);
|
||||
}}
|
||||
|
||||
test "read/write stringlist" {
|
||||
var byteList = std.ArrayList(u8).init(global_allocator);
|
||||
defer byteList.deinit();
|
||||
|
|
|
@ -12,6 +12,11 @@ pub fn add_byte(writer: anytype, byte: u8) !void {
|
|||
try writer.writeByte(byte);
|
||||
}
|
||||
|
||||
pub fn add_bytearray(writer: anytype, array: std.ArrayList(u8)) !void {
|
||||
try add_int(writer, @intCast(u32, array.items.len));
|
||||
try writer.writeAll(array.items);
|
||||
}
|
||||
|
||||
pub fn add_string(writer: anytype, str: []const u8) !void {
|
||||
try writer.writeAll(str);
|
||||
try writer.writeByte('\x00');
|
||||
|
|
Loading…
Reference in a new issue