From da0d295d6c8b6fb03245dea0583437c198890349 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Thu, 22 Apr 2021 09:32:25 -0400 Subject: C++ extractor improvements (#1803) * #include an absolute path didn't work - because paths were taken to always be relative. * Split of NodeTree. Split out FileUtil. Split out MacroWriter. * Rename slang-cpp-extractor-main.cpp -> cpp-extractor-main.cpp * First pass at extractor unit-tests * Initial parsing of enum. * Ability to disable/enable parsing of scope types. * Initial support for typedef. * Added operator== != to ArrayVIew. Added test for splitting to unit tests. * Improve comment in StringUtil. * Fix comment. * Fix typo. --- tools/slang-cpp-extractor/file-util.cpp | 72 +++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 tools/slang-cpp-extractor/file-util.cpp (limited to 'tools/slang-cpp-extractor/file-util.cpp') diff --git a/tools/slang-cpp-extractor/file-util.cpp b/tools/slang-cpp-extractor/file-util.cpp new file mode 100644 index 000000000..1d37df650 --- /dev/null +++ b/tools/slang-cpp-extractor/file-util.cpp @@ -0,0 +1,72 @@ +#include "file-util.h" + +#include "../../source/core/slang-io.h" + +namespace CppExtract { +using namespace Slang; + +/* static */SlangResult FileUtil::readAllText(const Slang::String& fileName, DiagnosticSink* sink, String& outRead) +{ + try + { + StreamReader reader(new FileStream(fileName, FileMode::Open, FileAccess::Read, FileShare::ReadWrite)); + outRead = reader.ReadToEnd(); + } + catch (const IOException&) + { + if (sink) + { + sink->diagnose(SourceLoc(), CPPDiagnostics::cannotOpenFile, fileName); + } + return SLANG_FAIL; + } + catch (...) + { + if (sink) + { + sink->diagnose(SourceLoc(), CPPDiagnostics::cannotOpenFile, fileName); + } + return SLANG_FAIL; + } + + return SLANG_OK; +} + +/* static */SlangResult FileUtil::writeAllText(const Slang::String& fileName, DiagnosticSink* sink, const UnownedStringSlice& text) +{ + try + { + if (File::exists(fileName)) + { + String existingText; + + if (readAllText(fileName, nullptr, existingText) == SLANG_OK) + { + if (existingText == text) + return SLANG_OK; + } + } + StreamWriter writer(new FileStream(fileName, FileMode::Create)); + writer.Write(text); + } + catch (const IOException&) + { + if (sink) + { + sink->diagnose(SourceLoc(), CPPDiagnostics::cannotOpenFile, fileName); + } + return SLANG_FAIL; + } + + return SLANG_OK; +} + +/* static */ void FileUtil::indent(Index indentCount, StringBuilder& out) +{ + for (Index i = 0; i < indentCount; ++i) + { + out << CPP_EXTRACT_INDENT_STRING; + } +} + +} // namespace CppExtract -- cgit v1.2.3