convert c pointers to optional unknown length ptrs
This commit is contained in:
parent
ca7a655ebb
commit
6b78bb16d8
1 changed files with 19 additions and 23 deletions
|
@ -12,17 +12,12 @@ const c = @cImport({
|
|||
const warn = std.debug.warn;
|
||||
const allocator = std.heap.c_allocator;
|
||||
|
||||
fn alloc_str(size: c_ulong) [*c]u8 {
|
||||
return @ptrCast([*c]u8, @alignCast(@alignOf(u8), c.malloc(c_ulong(size))));
|
||||
fn sliceify(ptr: [*]u8) []u8 {
|
||||
return ptr[0..c.strlen(ptr)];
|
||||
}
|
||||
|
||||
fn free_str(ptr: [*c]u8) void {
|
||||
c.free(@ptrCast(?*c_void, ptr));
|
||||
}
|
||||
|
||||
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];
|
||||
export fn journal_open(topic_cptr: ?[*]u8) os.fd_t {
|
||||
const topic = sliceify(topic_cptr.?);
|
||||
|
||||
const journal_dir = std.fs.path.resolve(allocator, [_][]const u8{
|
||||
std.os.getenvC(c"HOME").?,
|
||||
|
@ -61,10 +56,11 @@ export fn journal_open(topic_opt: [*c]u8) os.fd_t {
|
|||
return fd;
|
||||
}
|
||||
|
||||
export fn journal_write(journal: os.fd_t, message: [*c]u8) void {
|
||||
const msglen = c.strlen(message);
|
||||
|
||||
std.os.write(journal, message[0..msglen]) catch |err| {
|
||||
export fn journal_write(journal: os.fd_t, message_cptr: ?[*]u8) void {
|
||||
std.os.write(
|
||||
journal,
|
||||
sliceify(message_cptr.?),
|
||||
) catch |err| {
|
||||
std.debug.warn("Error while writing to file: {}\n", err);
|
||||
return;
|
||||
};
|
||||
|
@ -76,24 +72,25 @@ export fn journal_close(journal: os.fd_t) void {
|
|||
|
||||
export fn journal_write_topic(
|
||||
journal: os.fd_t,
|
||||
topic_opt: [*c]u8,
|
||||
message_opt: [*c]u8,
|
||||
topic_cptr: ?[*]u8,
|
||||
message_cptr: ?[*]u8,
|
||||
) void {
|
||||
var topic = topic_opt[0..c.strlen(topic_opt)];
|
||||
var message = message_opt[0..c.strlen(message_opt)];
|
||||
var topic = sliceify(topic_cptr.?);
|
||||
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;
|
||||
|
||||
_ = c.time(&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);
|
||||
|
||||
// catch unreachable is intended.
|
||||
var fmt_msg = std.fmt.allocPrint(
|
||||
allocator,
|
||||
"[{}] [{}]: {}\n",
|
||||
|
@ -105,12 +102,11 @@ export fn journal_write_topic(
|
|||
return;
|
||||
};
|
||||
|
||||
// catch unreachable is intended.
|
||||
var fmt_msg_cstr = std.cstr.addNullByte(
|
||||
allocator,
|
||||
fmt_msg,
|
||||
) catch unreachable;
|
||||
|
||||
free_str(tstamp_cstr);
|
||||
|
||||
journal_write(journal, fmt_msg_cstr.ptr);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue