#pragma once #include "CodegenConfig.hpp" #include "CodegenMacros.hpp" #include "CodegenOutput.inl" #include #include #include #include #include namespace Utils { std::string ReadFileAsString(const std::filesystem::path& path) { auto file = Utils::OpenCstdioFile(path, Utils::Read); if (!file) throw std::runtime_error("Failed to open source file."); DEFER { fclose(file); }; fseek(file, 0, SEEK_END); auto fileSize = ftell(file); rewind(file); std::string result(fileSize, '\0'); fread(result.data(), fileSize, 1, file); return result; } bool WriteOutputFile(const CodegenOutput& output, std::string_view dir, std::string_view filename, std::string_view additionalSuffix = {}) { char path[2048]; snprintf(path, sizeof(path), "%.*s/%.*s%.*s", PRINTF_STRING_VIEW(dir), PRINTF_STRING_VIEW(filename), PRINTF_STRING_VIEW(additionalSuffix)); auto outputFile = Utils::OpenCstdioFile(path, Utils::WriteTruncate); if (!outputFile) { printf("[ERROR] unable to open output file %s\n", path); return false; } DEFER { fclose(outputFile); }; DEBUG_PRINTF("Writing output %s\n", path); output.Write(outputFile); return true; } void ProduceGeneratedHeaderFileHeader(CodegenOutput& output) { output.AddOutputThing(CodegenOutputThing{ .text = &R"""( // This file is generated. Any changes will be overidden when building. #pragma once #include )"""[1], }); } void ProduceGeneratedSourceFileHeader(CodegenOutput& output) { output.AddOutputThing(CodegenOutputThing{ .text = &R"""( // This file is generated. Any changes will be overidden when building. #include "GeneratedCode.hpp" #include #include using namespace std::literals; )"""[1], }); } } // namespace Utils