mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-17 01:06:08 +03:00
macos: url from path
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
const std = @import("std");
|
||||
const Allocator = std.mem.Allocator;
|
||||
const foundation = @import("../foundation.zig");
|
||||
const c = @import("c.zig");
|
||||
|
||||
pub const URL = opaque {
|
||||
pub fn createWithString(str: *foundation.String, base: ?*URL) Allocator.Error!*URL {
|
||||
@ -11,6 +12,22 @@ pub const URL = opaque {
|
||||
) 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(
|
||||
str: *foundation.String,
|
||||
escape: *foundation.String,
|
||||
@ -43,6 +60,11 @@ pub const URL = opaque {
|
||||
) ?*foundation.String;
|
||||
};
|
||||
|
||||
pub const URLPathStyle = enum(c_int) {
|
||||
posix = c.kCFURLPOSIXPathStyle,
|
||||
windows = c.kCFURLWindowsPathStyle,
|
||||
};
|
||||
|
||||
test {
|
||||
const testing = std.testing;
|
||||
|
||||
@ -61,3 +83,22 @@ test {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user