From eaa8dcfcc9deabb906cc09bf31fc17ab6f343ff4 Mon Sep 17 00:00:00 2001 From: Lauro Oyen <15063951+laurooyen@users.noreply.github.com> Date: Mon, 2 Dec 2024 20:46:43 +0100 Subject: 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 Co-authored-by: Yong He --- tools/slang-cpp-extractor/file-util.cpp | 101 -------------------------------- 1 file changed, 101 deletions(-) delete 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 deleted file mode 100644 index 2980a22ce..000000000 --- a/tools/slang-cpp-extractor/file-util.cpp +++ /dev/null @@ -1,101 +0,0 @@ -#include "file-util.h" - -#include "../../source/core/slang-io.h" - -namespace CppExtract -{ -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 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 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 CppExtract -- cgit v1.2.3