#include #include #include namespace willowc { std::optional SourceManager::addFile(std::string_view _path) { std::error_code ec; std::filesystem::path uncanonical_path{_path}; auto path = std::filesystem::weakly_canonical(uncanonical_path, ec); if (ec) { return false; } std::string display_path = path.make_preferred(); if (!std::filesystem::exists(path, ec) || ec) return std::nullopt; if (!std::filesystem::is_regular_file(path, ec) || ec) return std::nullopt; std::size_t filesize = std::filesystem::file_size(path, ec); if (ec) return std::nullopt; std::ifstream f{display_path, std::ios::binary}; if (!f) return std::nullopt; auto buf = std::make_unique(filesize); f.read(buf.get(), filesize); const FileID id = file_table.size(); file_table.push_back(File{std::move(display_path), std::move(buf)}); return id; } } // namespace willowc