yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Lauro OyenMove c++ parsing code from slang-cpp-extractor to static library (#5675)eaa8dcfcc

master
2.7 KiB101 linesraw
1#include "file-util.h"
2
3#include "core/slang-io.h"
4
5namespace CppParse
6{
7using namespace Slang;
8
9namespace
10{ // anonymous
11struct DiagnosticReporter
12{
13    SlangResult report(SlangResult res)
14    {
15        if (SLANG_FAILED(res))
16        {
17            if (m_sink)
18            {
19                if (res == SLANG_E_CANNOT_OPEN)
20                {
21                    m_sink->diagnose(SourceLoc(), CPPDiagnostics::cannotOpenFile, m_filename);
22                }
23                else
24                {
25                    m_sink->diagnose(SourceLoc(), CPPDiagnostics::errorAccessingFile, m_filename);
26                }
27            }
28        }
29        return res;
30    }
31
32    DiagnosticReporter(const String& filename, DiagnosticSink* sink)
33        : m_filename(filename), m_sink(sink)
34    {
35    }
36
37    DiagnosticSink* m_sink;
38    String m_filename;
39};
40
41} // namespace
42
43/* static */ SlangResult FileUtil::readAllText(
44    const Slang::String& fileName,
45    DiagnosticSink* sink,
46    String& outRead)
47{
48    DiagnosticReporter reporter(fileName, sink);
49
50    RefPtr<FileStream> stream = new FileStream;
51    SLANG_RETURN_ON_FAIL(reporter.report(
52        stream->init(fileName, FileMode::Open, FileAccess::Read, FileShare::ReadWrite)));
53
54    StreamReader reader;
55    SLANG_RETURN_ON_FAIL(reporter.report(reader.init(stream)));
56    SLANG_RETURN_ON_FAIL(reporter.report(reader.readToEnd(outRead)));
57
58    return SLANG_OK;
59}
60
61/* static */ SlangResult FileUtil::writeAllText(
62    const Slang::String& fileName,
63    DiagnosticSink* sink,
64    const UnownedStringSlice& text)
65{
66    // TODO(JS):
67    // There is an optimization/behavior here,that checks if the contents has changed. It only
68    // writes if it hasn't That might not be what you want (both because of extra work of read, the
69    // file modified stamp or other reasons, file is write only etc) NOTE! That this also does the
70    // work of the comparison after it is decoded, but the *bytes* might actually be different.
71
72    if (File::exists(fileName))
73    {
74        String existingText;
75        if (SLANG_SUCCEEDED(readAllText(fileName, nullptr, existingText)))
76        {
77            if (existingText == text)
78                return SLANG_OK;
79        }
80    }
81
82    DiagnosticReporter reporter(fileName, sink);
83
84    RefPtr<FileStream> stream = new FileStream;
85    SLANG_RETURN_ON_FAIL(reporter.report(stream->init(fileName, FileMode::Create)));
86
87    StreamWriter writer;
88    SLANG_RETURN_ON_FAIL(reporter.report(writer.init(stream)));
89    SLANG_RETURN_ON_FAIL(reporter.report(writer.write(text)))
90    return SLANG_OK;
91}
92
93/* static */ void FileUtil::indent(Index indentCount, StringBuilder& out)
94{
95    for (Index i = 0; i < indentCount; ++i)
96    {
97        out << CPP_EXTRACT_INDENT_STRING;
98    }
99}
100
101} // namespace CppParse