diff options
| author | Lauro Oyen <15063951+laurooyen@users.noreply.github.com> | 2024-12-02 20:46:43 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-12-02 11:46:43 -0800 |
| commit | eaa8dcfcc9deabb906cc09bf31fc17ab6f343ff4 (patch) | |
| tree | 8e0f4658de3efb5e7696e8588c55471f9d65ba18 /tools/slang-cpp-parser/file-util.cpp | |
| parent | 7aaf7009e2c6055a714ba4a93ab3dd73d2d2cdb7 (diff) | |
Move c++ parsing code from slang-cpp-extractor to static library (#5675)
* Move c++ parsing code from slang-cpp-extractor to static library
* Format code
* Remove relative includes
---------
Co-authored-by: slangbot <ellieh+slangbot@nvidia.com>
Co-authored-by: Yong He <yonghe@outlook.com>
Diffstat (limited to 'tools/slang-cpp-parser/file-util.cpp')
| -rw-r--r-- | tools/slang-cpp-parser/file-util.cpp | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/tools/slang-cpp-parser/file-util.cpp b/tools/slang-cpp-parser/file-util.cpp new file mode 100644 index 000000000..9e7c60755 --- /dev/null +++ b/tools/slang-cpp-parser/file-util.cpp @@ -0,0 +1,101 @@ +#include "file-util.h" + +#include "core/slang-io.h" + +namespace CppParse +{ +using namespace Slang; + +namespace +{ // anonymous +struct DiagnosticReporter +{ + SlangResult report(SlangResult res) + { + if (SLANG_FAILED(res)) + { + if (m_sink) + { + if (res == SLANG_E_CANNOT_OPEN) + { + m_sink->diagnose(SourceLoc(), CPPDiagnostics::cannotOpenFile, m_filename); + } + else + { + m_sink->diagnose(SourceLoc(), CPPDiagnostics::errorAccessingFile, m_filename); + } + } + } + return res; + } + + DiagnosticReporter(const String& filename, DiagnosticSink* sink) + : m_filename(filename), m_sink(sink) + { + } + + DiagnosticSink* m_sink; + String m_filename; +}; + +} // namespace + +/* static */ SlangResult FileUtil::readAllText( + const Slang::String& fileName, + DiagnosticSink* sink, + String& outRead) +{ + DiagnosticReporter reporter(fileName, sink); + + RefPtr<FileStream> stream = new FileStream; + SLANG_RETURN_ON_FAIL(reporter.report( + stream->init(fileName, FileMode::Open, FileAccess::Read, FileShare::ReadWrite))); + + StreamReader reader; + SLANG_RETURN_ON_FAIL(reporter.report(reader.init(stream))); + SLANG_RETURN_ON_FAIL(reporter.report(reader.readToEnd(outRead))); + + return SLANG_OK; +} + +/* static */ SlangResult FileUtil::writeAllText( + const Slang::String& fileName, + DiagnosticSink* sink, + const UnownedStringSlice& text) +{ + // TODO(JS): + // There is an optimization/behavior here,that checks if the contents has changed. It only + // writes if it hasn't That might not be what you want (both because of extra work of read, the + // file modified stamp or other reasons, file is write only etc) NOTE! That this also does the + // work of the comparison after it is decoded, but the *bytes* might actually be different. + + if (File::exists(fileName)) + { + String existingText; + if (SLANG_SUCCEEDED(readAllText(fileName, nullptr, existingText))) + { + if (existingText == text) + return SLANG_OK; + } + } + + DiagnosticReporter reporter(fileName, sink); + + RefPtr<FileStream> stream = new FileStream; + SLANG_RETURN_ON_FAIL(reporter.report(stream->init(fileName, FileMode::Create))); + + StreamWriter writer; + SLANG_RETURN_ON_FAIL(reporter.report(writer.init(stream))); + SLANG_RETURN_ON_FAIL(reporter.report(writer.write(text))) + return SLANG_OK; +} + +/* static */ void FileUtil::indent(Index indentCount, StringBuilder& out) +{ + for (Index i = 0; i < indentCount; ++i) + { + out << CPP_EXTRACT_INDENT_STRING; + } +} + +} // namespace CppParse |
