[Mammoth/Teton] Add an OpenFile interface and use it to load a font.

This commit is contained in:
Drew Galbraith 2023-11-22 16:42:42 -08:00
parent 4fd17a59ea
commit 86ce0a68a3
6 changed files with 84 additions and 21 deletions

45
lib/mammoth/file/file.cpp Normal file
View file

@ -0,0 +1,45 @@
#include "file/file.h"
#include <victoriafalls/victoriafalls.yunq.client.h>
#include <yellowstone/yellowstone.yunq.client.h>
#include <zglobal.h>
#include "util/debug.h"
namespace mmth {
namespace {
VFSClient* gVfsClient = nullptr;
} // namespace
File File::Open(glcr::StringView path) {
if (gVfsClient == 0) {
YellowstoneClient client(gInitEndpointCap);
GetEndpointRequest yreq;
yreq.set_endpoint_name("victoriafalls");
Endpoint yresp;
check(client.GetEndpoint(yreq, yresp));
gVfsClient = new VFSClient(yresp.endpoint());
}
OpenFileRequest req;
req.set_path(path);
OpenFileResponse resp;
check(gVfsClient->OpenFile(req, resp));
return File(OwnedMemoryRegion::FromCapability(resp.memory()));
}
glcr::StringView File::as_str() {
return glcr::StringView((char*)raw_ptr(), file_data_.size());
}
void* File::raw_ptr() { return reinterpret_cast<void*>(file_data_.vaddr()); }
uint8_t* File::byte_ptr() {
return reinterpret_cast<uint8_t*>(file_data_.vaddr());
}
} // namespace mmth

25
lib/mammoth/file/file.h Normal file
View file

@ -0,0 +1,25 @@
#pragma once
#include <glacier/memory/move.h>
#include <glacier/string/string_view.h>
#include "mammoth/util/memory_region.h"
namespace mmth {
class File {
public:
static File Open(glcr::StringView path);
glcr::StringView as_str();
void* raw_ptr();
uint8_t* byte_ptr();
private:
OwnedMemoryRegion file_data_;
File(OwnedMemoryRegion&& file) : file_data_(glcr::Move(file)) {}
};
} // namespace mmth