Skip to content

Commit

Permalink
add files
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhongjia committed May 4, 2024
1 parent 0831fdd commit 0225470
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
26 changes: 26 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const lib = b.addStaticLibrary(.{
.name = "znvim",
.root_source_file = b.path("src/znvim.zig"),
.target = target,
.optimize = optimize,
});

b.installArtifact(lib);

const lib_unit_tests = b.addTest(.{
.root_source_file = b.path("src/znvim.zig"),
.target = target,
.optimize = optimize,
});

const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);

const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
}
14 changes: 14 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.{
.name = "znvim",
.version = "0.0.0",
.minimum_zig_version = "0.12.0",
.dependencies = .{
.@"zig-msgpack" = .{
.url = "https://github.com/zigcc/zig-msgpack/archive/refs/tags/0.0.4.tar.gz",
.hash = "12204ec362b0784a04825021b1bab2fe62d062db5f07052207363a835af7613bb12e",
},
},
.paths = .{
"",
},
}
43 changes: 43 additions & 0 deletions src/named_pipe.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//! this is for window's named pipe
//! more info:
//! https://learn.microsoft.com/en-us/windows/win32/api/namedpipeapi/

const std = @import("std");
const windows = std.os.windows;
const LPCWSTR = windows.LPCWSTR;
const DWORD = windows.DWORD;
const BOOL = windows.BOOL;
const WINAPI = windows.WINAPI;

/// Waits until either a time-out interval elapses or an instance of the specified named pipe is available for connection
/// (that is, the pipe's server process has a pending ConnectNamedPipe operation on the pipe).
extern "kernel32" fn WaitNamedPipeW(
lpNamedPipeName: LPCWSTR,
nTimeOut: DWORD,
) callconv(WINAPI) BOOL;

/// this function will try to connect named pipe on windows
/// no need to free the mem
pub fn connectNamedPipe(path: []const u8, allocator: std.mem.Allocator) !std.fs.File {
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
const arena_allocator = arena.allocator();

const utf16_path = try std.unicode.utf8ToUtf16LeWithNull(arena_allocator, path);
const handle = windows.kernel32.CreateFileW(
utf16_path,
windows.GENERIC_READ | windows.GENERIC_WRITE,
0,
null,
windows.OPEN_EXISTING,
0,
null,
);
if (handle == windows.INVALID_HANDLE_VALUE) {
return windows.unexpectedError(windows.kernel32.GetLastError());
}

return std.fs.File{
.handle = handle,
};
}
Empty file added src/rpc.zig
Empty file.
Empty file added src/znvim.zig
Empty file.

0 comments on commit 0225470

Please sign in to comment.