summaryrefslogtreecommitdiffstats
path: root/tools/slang-cpp-extractor/file-util.cpp
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2021-10-04 14:15:51 -0400
committerGitHub <noreply@github.com>2021-10-04 14:15:51 -0400
commit97bb82ebcdf8f1391b9d93b5a8d7b1dfc4e88e52 (patch)
treef120ba282cbea96d23ed179737984a4610d3b520 /tools/slang-cpp-extractor/file-util.cpp
parentb3dfe383c6d31ff3dbd76dcfb32de8d536382f3e (diff)
Removing exceptions from core/compiler-core (#1953)
* #include an absolute path didn't work - because paths were taken to always be relative. * Refactor Stream. Working on all tests. * Split out CharEncode. * Make method names lower camel. m_prefix in Writer/Reader * Tidy up around CharEncode interface. * Small improvements around encode/decode. * Better use of types. * Remove readLine from TextReader. * Remove exceptions from Stream/Text handling. * Fix some typos. * Fix tabbing. * Fix missing override. * Remove remaining exception throw/catch via using signal mechanism. * Remove exceptions that are not used anymore. * Document the Stream interface. * Remove index for decoding 'get byte' function. * Fix CharReader -> ByteReader.
Diffstat (limited to 'tools/slang-cpp-extractor/file-util.cpp')
-rw-r--r--tools/slang-cpp-extractor/file-util.cpp90
1 files changed, 55 insertions, 35 deletions
diff --git a/tools/slang-cpp-extractor/file-util.cpp b/tools/slang-cpp-extractor/file-util.cpp
index 1d37df650..e73ba7f55 100644
--- a/tools/slang-cpp-extractor/file-util.cpp
+++ b/tools/slang-cpp-extractor/file-util.cpp
@@ -5,60 +5,80 @@
namespace CppExtract {
using namespace Slang;
-/* static */SlangResult FileUtil::readAllText(const Slang::String& fileName, DiagnosticSink* sink, String& outRead)
+namespace { // anonymous
+struct DiagnosticReporter
{
- try
- {
- StreamReader reader(new FileStream(fileName, FileMode::Open, FileAccess::Read, FileShare::ReadWrite));
- outRead = reader.ReadToEnd();
- }
- catch (const IOException&)
+ SlangResult report(SlangResult res)
{
- if (sink)
+ if (SLANG_FAILED(res))
{
- sink->diagnose(SourceLoc(), CPPDiagnostics::cannotOpenFile, fileName);
+ 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 SLANG_FAIL;
+ return res;
}
- catch (...)
+
+ DiagnosticReporter(const String& filename, DiagnosticSink* sink) :
+ m_filename(filename),
+ m_sink(sink)
{
- if (sink)
- {
- sink->diagnose(SourceLoc(), CPPDiagnostics::cannotOpenFile, fileName);
- }
- return SLANG_FAIL;
}
+ DiagnosticSink* m_sink;
+ String m_filename;
+};
+
+} // anonymous
+
+/* 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)
{
- try
- {
- if (File::exists(fileName))
- {
- String existingText;
+ // 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 (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 (File::exists(fileName))
{
- if (sink)
+ String existingText;
+ if (SLANG_SUCCEEDED(readAllText(fileName, nullptr, existingText)))
{
- sink->diagnose(SourceLoc(), CPPDiagnostics::cannotOpenFile, fileName);
+ if (existingText == text)
+ return SLANG_OK;
}
- return SLANG_FAIL;
}
- 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)