macos: url from path

This commit is contained in:
Mitchell Hashimoto
2022-10-08 10:13:30 -07:00
parent a21dda2b08
commit 175ff0f777

View File

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const foundation = @import("../foundation.zig"); const foundation = @import("../foundation.zig");
const c = @import("c.zig");
pub const URL = opaque { pub const URL = opaque {
pub fn createWithString(str: *foundation.String, base: ?*URL) Allocator.Error!*URL { pub fn createWithString(str: *foundation.String, base: ?*URL) Allocator.Error!*URL {
@ -11,6 +12,22 @@ pub const URL = opaque {
) orelse error.OutOfMemory; ) orelse error.OutOfMemory;
} }
pub fn createWithFileSystemPath(
path: *foundation.String,
style: URLPathStyle,
dir: bool,
) Allocator.Error!*URL {
return @intToPtr(
?*URL,
@ptrToInt(c.CFURLCreateWithFileSystemPath(
null,
@ptrCast(c.CFStringRef, path),
@enumToInt(style),
if (dir) 1 else 0,
)),
) orelse error.OutOfMemory;
}
pub fn createStringByReplacingPercentEscapes( pub fn createStringByReplacingPercentEscapes(
str: *foundation.String, str: *foundation.String,
escape: *foundation.String, escape: *foundation.String,
@ -43,6 +60,11 @@ pub const URL = opaque {
) ?*foundation.String; ) ?*foundation.String;
}; };
pub const URLPathStyle = enum(c_int) {
posix = c.kCFURLPOSIXPathStyle,
windows = c.kCFURLWindowsPathStyle,
};
test { test {
const testing = std.testing; const testing = std.testing;
@ -61,3 +83,22 @@ test {
try testing.expectEqualStrings("/foo", cstr); try testing.expectEqualStrings("/foo", cstr);
} }
} }
test "path" {
const testing = std.testing;
const str = try foundation.String.createWithBytes("foo/bar.ttf", .utf8, false);
defer str.release();
const url = try URL.createWithFileSystemPath(str, .posix, false);
defer url.release();
{
const path = url.copyPath().?;
defer path.release();
var buf: [128]u8 = undefined;
const cstr = path.cstring(&buf, .utf8).?;
try testing.expectEqualStrings("foo/bar.ttf", cstr);
}
}