Skip to content

Commit 2983ed2

Browse files
committed
Use FileStream not FILE
1 parent 06141dd commit 2983ed2

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

source/slang/slang-options.cpp

+15-18
Original file line numberDiff line numberDiff line change
@@ -2962,38 +2962,34 @@ SlangResult OptionsParser::_parse(int argc, char const* const* argv)
29622962
// Coerce Slang to load from the given file, without letting it automatically
29632963
// choose .slang-module files over .slang files.
29642964
// First try to load as source string, and fall back to loading as an IR Blob.
2965-
// Avoid guessing based on filename or inspect the file contents.
2966-
FILE* file;
2967-
fopen_s(&file, fileName.value.getBuffer(), "rb");
2968-
if (!file)
2965+
// Avoid guessing based on filename or inspecting the file contents.
2966+
FileStream file;
2967+
if (SLANG_FAILED(file.init(fileName.value, FileMode::Open, FileAccess::Read, FileShare::None)))
29692968
{
29702969
m_sink->diagnose(arg.loc, Diagnostics::cannotOpenFile, fileName.value);
29712970
return SLANG_FAIL;
29722971
}
2973-
fseek(file, 0, SEEK_END);
2974-
size_t size = ftell(file);
2975-
fseek(file, 0, SEEK_SET);
2976-
std::vector<char> buffer(size + 1);
2977-
size_t result = fread(buffer.data(), 1, size, file);
2978-
if (result != size)
2979-
{
2980-
m_sink->diagnoseRaw(Severity::Error, "Failed to read file");
2981-
return SLANG_FAIL;
2982-
}
2983-
buffer[size] = 0;
2984-
fclose(file);
29852972

2973+
List<uint8_t> buffer;
2974+
file.seek(SeekOrigin::End, 0);
2975+
const Int64 size = file.getPosition();
2976+
buffer.setCount(size + 1);
2977+
file.seek(SeekOrigin::Start, 0);
2978+
SLANG_RETURN_ON_FAIL(file.readExactly(buffer.getBuffer(), (size_t)size));
2979+
buffer[size] = 0;
2980+
file.close();
2981+
29862982
ComPtr<slang::IModule> module;
29872983
module = session->loadModuleFromSourceString(
29882984
"module",
29892985
"path",
2990-
buffer.data(),
2986+
(const char*)buffer.getBuffer(),
29912987
diagnostics.writeRef());
29922988
if (!module)
29932989
{
29942990
// Load buffer as an IR blob
29952991
ComPtr<slang::IBlob> blob;
2996-
blob = RawBlob::create(buffer.data(), size);
2992+
blob = RawBlob::create(buffer.getBuffer(), size);
29972993

29982994
module = session->loadModuleFromIRBlob(
29992995
"module",
@@ -3012,6 +3008,7 @@ SlangResult OptionsParser::_parse(int argc, char const* const* argv)
30123008
}
30133009
else
30143010
{
3011+
// success, print out the disassembly in a way that slang-test can read
30153012
m_sink->diagnoseRaw(
30163013
Severity::Note,
30173014
(const char*)disassemblyBlob->getBufferPointer());

0 commit comments

Comments
 (0)