diff options
| author | Stefan Weigl-Bosker <stefan@s00.xyz> | 2026-02-03 14:59:53 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-02-03 14:59:53 -0500 |
| commit | add95b14f74e6dbe04a6efe98ff0f20424930b73 (patch) | |
| tree | 13ce413ee4190a4c8f8743c7740aaa8d04353f14 /willow/tools/willowc/lib/sourcemanager.cpp | |
| parent | c5b2905c5a64433f8519531a77d3acc42d881f17 (diff) | |
| download | compiler-dev/stefan.tar.gz | |
[willow]: initial frontend work, unit tests (#8)dev/stefan
Diffstat (limited to 'willow/tools/willowc/lib/sourcemanager.cpp')
| -rw-r--r-- | willow/tools/willowc/lib/sourcemanager.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/willow/tools/willowc/lib/sourcemanager.cpp b/willow/tools/willowc/lib/sourcemanager.cpp new file mode 100644 index 0000000..e2a8e72 --- /dev/null +++ b/willow/tools/willowc/lib/sourcemanager.cpp @@ -0,0 +1,41 @@ +#include <filesystem> + +#include <fstream> +#include <sourcemanager.hpp> + +namespace willowc { + +std::optional<FileID> 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<char[]>(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 |