Make ReadEntireFileToString return a std::string.

Note: this makes the method unavailable for export from angle_util,
which is probably why it was designed the way it was in the first
place. However, we import the test utils source file as a static
lib into each test executable and test shared module, so it works.

Bug: angleproject:7404
Change-Id: Ia957268882c2b8529643660d7d4f34d142c0dc43
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3708602
Commit-Queue: Roman Lavrov <romanl@google.com>
Reviewed-by: Roman Lavrov <romanl@google.com>
This commit is contained in:
Jamie Madill 2022-06-16 11:33:14 -04:00 коммит произвёл Angle LUCI CQ
Родитель 5f1d0742fa
Коммит ca96cba9c5
5 изменённых файлов: 17 добавлений и 37 удалений

Просмотреть файл

@ -1196,15 +1196,8 @@ TestSuite::TestSuite(int *argc, char **argv, std::function<void()> registerTests
exit(EXIT_FAILURE);
}
uint32_t fileSize = 0;
if (!GetFileSize(mFilterFile.c_str(), &fileSize))
{
printf("Error getting filter file size: %s\n", mFilterFile.c_str());
exit(EXIT_FAILURE);
}
std::vector<char> fileContents(fileSize + 1, 0);
if (!ReadEntireFileToString(mFilterFile.c_str(), fileContents.data(), fileSize))
std::string fileContents;
if (!ReadEntireFileToString(mFilterFile.c_str(), &fileContents))
{
printf("Error loading filter file: %s\n", mFilterFile.c_str());
exit(EXIT_FAILURE);

Просмотреть файл

@ -16,17 +16,6 @@
namespace
{
bool ReadEntireFile(const std::string &filePath, std::string *contentsOut)
{
constexpr uint32_t kMaxBufferSize = 2000;
char buffer[kMaxBufferSize] = {};
if (!angle::ReadEntireFileToString(filePath.c_str(), buffer, kMaxBufferSize) ||
strlen(buffer) == 0)
return false;
*contentsOut = buffer;
return true;
}
GLuint CompileProgramInternal(const char *vsSource,
const char *tcsSource,
const char *tesSource,
@ -200,7 +189,7 @@ GLuint CompileShader(GLenum type, const char *source)
GLuint CompileShaderFromFile(GLenum type, const std::string &sourcePath)
{
std::string source;
if (!ReadEntireFile(sourcePath, &source))
if (!angle::ReadEntireFileToString(sourcePath.c_str(), &source))
{
std::cerr << "Error reading shader file: " << sourcePath << "\n";
return 0;
@ -318,14 +307,14 @@ GLuint CompileProgramWithTESS(const char *vsSource,
GLuint CompileProgramFromFiles(const std::string &vsPath, const std::string &fsPath)
{
std::string vsSource;
if (!ReadEntireFile(vsPath, &vsSource))
if (!angle::ReadEntireFileToString(vsPath.c_str(), &vsSource))
{
std::cerr << "Error reading shader: " << vsPath << "\n";
return 0;
}
std::string fsSource;
if (!ReadEntireFile(fsPath, &fsSource))
if (!angle::ReadEntireFileToString(fsPath.c_str(), &fsSource))
{
std::cerr << "Error reading shader: " << fsPath << "\n";
return 0;

Просмотреть файл

@ -37,7 +37,7 @@ bool GetFileSize(const char *filePath, uint32_t *sizeOut)
return true;
}
bool ReadEntireFileToString(const char *filePath, char *contentsOut, uint32_t maxLen)
bool ReadEntireFileToString(const char *filePath, std::string *contentsOut)
{
std::ifstream stream(filePath);
if (!stream)
@ -45,15 +45,12 @@ bool ReadEntireFileToString(const char *filePath, char *contentsOut, uint32_t ma
return false;
}
std::string contents;
stream.seekg(0, std::ios::end);
contents.reserve(static_cast<unsigned int>(stream.tellg()));
contentsOut->reserve(static_cast<unsigned int>(stream.tellg()));
stream.seekg(0, std::ios::beg);
contents.assign((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
contentsOut->assign((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
strncpy(contentsOut, contents.c_str(), maxLen);
return true;
}

Просмотреть файл

@ -51,8 +51,9 @@ bool CreateTemporaryFileInDir(const char *dir, char *tempFileNameOut, uint32_t m
// Deletes a file or directory.
bool DeleteSystemFile(const char *path);
// Reads a file contents into a string.
bool ReadEntireFileToString(const char *filePath, char *contentsOut, uint32_t maxLen);
// Reads a file contents into a string. Note: this method cannot be exported across a shared module
// boundary because it does memory allocation.
bool ReadEntireFileToString(const char *filePath, std::string *contentsOut);
// Compute a file's size.
bool GetFileSize(const char *filePath, uint32_t *sizeOut);

Просмотреть файл

@ -93,9 +93,9 @@ TEST(TestUtils, MAYBE_CreateAndDeleteTemporaryFile)
EXPECT_GE(retval, 0);
// Test ReadEntireFileToString
char actualString[kMaxPath];
EXPECT_TRUE(ReadEntireFileToString(path, actualString, kMaxPath));
EXPECT_EQ(strcmp(actualString, kOutputString), 0);
std::string actualString;
EXPECT_TRUE(ReadEntireFileToString(path, &actualString));
EXPECT_EQ(strcmp(actualString.c_str(), kOutputString), 0);
// Delete the temporary file.
EXPECT_TRUE(angle::DeleteSystemFile(path));
@ -121,9 +121,9 @@ TEST(TestUtils, MAYBE_CreateAndDeleteFileInTempDir)
EXPECT_GE(retval, 0);
// Test ReadEntireFileToString
char actualString[kMaxPath];
EXPECT_TRUE(ReadEntireFileToString(path, actualString, kMaxPath));
EXPECT_EQ(strcmp(actualString, kOutputString), 0);
std::string actualString;
EXPECT_TRUE(ReadEntireFileToString(path, &actualString));
EXPECT_EQ(strcmp(actualString.c_str(), kOutputString), 0);
// Delete the temporary file.
EXPECT_TRUE(angle::DeleteSystemFile(path));