提交 6577a325 编写于 作者: J jim price

Support gzipped scene files

上级 4763b251
......@@ -137,6 +137,11 @@ std::unique_ptr<Tokenizer> Tokenizer::CreateFromFile(
return std::make_unique<Tokenizer>(std::move(str), std::move(errorCallback));
}
if (HasExtension(filename, ".gz")) {
std::string str = ReadDecompressedFileContents(filename);
return std::make_unique<Tokenizer>(std::move(str), std::move(errorCallback));
}
#ifdef PBRT_HAVE_MMAP
int fd = open(filename.c_str(), O_RDONLY);
if (fd == -1) {
......@@ -197,7 +202,7 @@ std::unique_ptr<Tokenizer> Tokenizer::CreateFromFile(
return std::make_unique<Tokenizer>(ptr, len, filename, std::move(errorCallback));
#else
std::string str = ReadFileContents(filename);
return std::make_unique_ptr<Tokenizer>(std::move(str), std::move(errorCallback));
return std::make_unique<Tokenizer>(std::move(str), std::move(errorCallback));
#endif
}
......
......@@ -8,6 +8,8 @@
#include <pbrt/util/error.h>
#include <pbrt/util/string.h>
#include <zlib/zlib.h>
#include <filesystem/path.h>
#include <algorithm>
#include <cctype>
......@@ -126,6 +128,28 @@ std::string ReadFileContents(std::string filename) {
(std::istreambuf_iterator<char>()));
}
std::string ReadDecompressedFileContents(std::string filename) {
std::string contents;
gzFile f = gzopen(filename.c_str(), "rb");
if (!f) {
Error("%s: unable to open file", filename);
return {};
}
char buffer[4096];
int bytesRead = 0;
do {
bytesRead = gzread(f, buffer, 4096);
contents.append(buffer, bytesRead);
} while ( !gzeof(f) );
gzclose(f);
return contents;
}
FILE *FOpenRead(std::string filename) {
#ifdef PBRT_IS_WINDOWS
return _wfopen(WStringFromUTF8(filename).c_str(), L"rb");
......
......@@ -15,6 +15,7 @@
namespace pbrt {
// File and Filename Function Declarations
std::string ReadDecompressedFileContents(std::string filename);
std::string ReadFileContents(std::string filename);
bool WriteFileContents(std::string filename, const std::string &contents);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册