convert c pointers to optional unknown length ptrs

This commit is contained in:
Luna 2019-08-11 11:12:21 -03:00
parent ca7a655ebb
commit 6b78bb16d8

View file

@ -12,17 +12,12 @@ const c = @cImport({
const warn = std.debug.warn; const warn = std.debug.warn;
const allocator = std.heap.c_allocator; const allocator = std.heap.c_allocator;
fn alloc_str(size: c_ulong) [*c]u8 { fn sliceify(ptr: [*]u8) []u8 {
return @ptrCast([*c]u8, @alignCast(@alignOf(u8), c.malloc(c_ulong(size)))); return ptr[0..c.strlen(ptr)];
} }
fn free_str(ptr: [*c]u8) void { export fn journal_open(topic_cptr: ?[*]u8) os.fd_t {
c.free(@ptrCast(?*c_void, ptr)); const topic = sliceify(topic_cptr.?);
}
export fn journal_open(topic_opt: [*c]u8) os.fd_t {
const topic_len = c.strlen(topic_opt);
const topic: []u8 = topic_opt[0..topic_len];
const journal_dir = std.fs.path.resolve(allocator, [_][]const u8{ const journal_dir = std.fs.path.resolve(allocator, [_][]const u8{
std.os.getenvC(c"HOME").?, std.os.getenvC(c"HOME").?,
@ -61,10 +56,11 @@ export fn journal_open(topic_opt: [*c]u8) os.fd_t {
return fd; return fd;
} }
export fn journal_write(journal: os.fd_t, message: [*c]u8) void { export fn journal_write(journal: os.fd_t, message_cptr: ?[*]u8) void {
const msglen = c.strlen(message); std.os.write(
journal,
std.os.write(journal, message[0..msglen]) catch |err| { sliceify(message_cptr.?),
) catch |err| {
std.debug.warn("Error while writing to file: {}\n", err); std.debug.warn("Error while writing to file: {}\n", err);
return; return;
}; };
@ -76,24 +72,25 @@ export fn journal_close(journal: os.fd_t) void {
export fn journal_write_topic( export fn journal_write_topic(
journal: os.fd_t, journal: os.fd_t,
topic_opt: [*c]u8, topic_cptr: ?[*]u8,
message_opt: [*c]u8, message_cptr: ?[*]u8,
) void { ) void {
var topic = topic_opt[0..c.strlen(topic_opt)]; var topic = sliceify(topic_cptr.?);
var message = message_opt[0..c.strlen(message_opt)]; var message = sliceify(message_cptr.?);
var tstamp_total = allocator.alloc(u8, 128);
defer allocator.free(tstamp_total);
var tstamp_cstr: [*c]u8 = alloc_str(128);
var rawtime: c.time_t = undefined; var rawtime: c.time_t = undefined;
_ = c.time(&rawtime); _ = c.time(&rawtime);
var cur_time: [*c]const c.struct_tm = c.gmtime(&rawtime); var cur_time: [*c]const c.struct_tm = c.gmtime(&rawtime);
_ = c.strftime(tstamp_cstr, usize(128), c"%c", cur_time); _ = c.strftime(tstamp_cstr.ptr, usize(128), c"%c", cur_time);
var tstamp = tstamp_cstr[0..c.strlen(tstamp_cstr)]; var tstamp = sliceify(tstamp_total);
std.debug.warn("tstamp: {}\ntopic: {}\nmsg: {}\n", tstamp, topic, message); std.debug.warn("tstamp: {}\ntopic: {}\nmsg: {}\n", tstamp, topic, message);
// catch unreachable is intended.
var fmt_msg = std.fmt.allocPrint( var fmt_msg = std.fmt.allocPrint(
allocator, allocator,
"[{}] [{}]: {}\n", "[{}] [{}]: {}\n",
@ -105,12 +102,11 @@ export fn journal_write_topic(
return; return;
}; };
// catch unreachable is intended.
var fmt_msg_cstr = std.cstr.addNullByte( var fmt_msg_cstr = std.cstr.addNullByte(
allocator, allocator,
fmt_msg, fmt_msg,
) catch unreachable; ) catch unreachable;
free_str(tstamp_cstr);
journal_write(journal, fmt_msg_cstr.ptr); journal_write(journal, fmt_msg_cstr.ptr);
} }