Forráskód Böngészése

resource: do not fail to read empty files

Daniele Bartolini 5 éve
szülő
commit
ca203c68e0

+ 4 - 0
docs/changelog.rst

@@ -5,6 +5,10 @@ Changelog
 ------
 ------
 *DD MMM YYYY*
 *DD MMM YYYY*
 
 
+**Data Compiler**
+
+* Fixed an issue that caused the compiler to crash when reading empty source files
+
 **Runtime**
 **Runtime**
 
 
 * Removed support for multiple components per Unit.
 * Removed support for multiple components per Unit.

+ 17 - 14
src/resource/compile_options.cpp

@@ -87,13 +87,20 @@ bool CompileOptions::resource_exists(const char* type, const char* name)
 	return file_exists(path.c_str());
 	return file_exists(path.c_str());
 }
 }
 
 
-Buffer CompileOptions::read_temporary(const char* path)
+Buffer CompileOptions::read_all(File* file)
 {
 {
-	File* file = _data_filesystem.open(path, FileOpenMode::READ);
-	u32 size = file->size();
+	const u32 size = file->size();
 	Buffer buf(default_allocator());
 	Buffer buf(default_allocator());
 	array::resize(buf, size);
 	array::resize(buf, size);
-	file->read(array::begin(buf), size);
+	if (size != 0)
+		file->read(array::begin(buf), size);
+	return buf;
+}
+
+Buffer CompileOptions::read_temporary(const char* path)
+{
+	File* file = _data_filesystem.open(path, FileOpenMode::READ);
+	Buffer buf = read_all(file);
 	_data_filesystem.close(*file);
 	_data_filesystem.close(*file);
 	return buf;
 	return buf;
 }
 }
@@ -110,12 +117,6 @@ void CompileOptions::write_temporary(const char* path, const Buffer& data)
 	write_temporary(path, array::begin(data), array::size(data));
 	write_temporary(path, array::begin(data), array::size(data));
 }
 }
 
 
-///
-Buffer CompileOptions::read()
-{
-	return read(_source_path.c_str());
-}
-
 Buffer CompileOptions::read(const char* path)
 Buffer CompileOptions::read(const char* path)
 {
 {
 	fake_read(path);
 	fake_read(path);
@@ -128,14 +129,16 @@ Buffer CompileOptions::read(const char* path)
 	source_filesystem.set_prefix(source_dir.c_str());
 	source_filesystem.set_prefix(source_dir.c_str());
 
 
 	File* file = source_filesystem.open(path, FileOpenMode::READ);
 	File* file = source_filesystem.open(path, FileOpenMode::READ);
-	const u32 size = file->size();
-	Buffer buf(default_allocator());
-	array::resize(buf, size);
-	file->read(array::begin(buf), size);
+	Buffer buf = read_all(file);
 	source_filesystem.close(*file);
 	source_filesystem.close(*file);
 	return buf;
 	return buf;
 }
 }
 
 
+Buffer CompileOptions::read()
+{
+	return read(_source_path.c_str());
+}
+
 void CompileOptions::fake_read(const char* path)
 void CompileOptions::fake_read(const char* path)
 {
 {
 	TempAllocator256 ta;
 	TempAllocator256 ta;

+ 7 - 4
src/resource/compile_options.h

@@ -108,6 +108,9 @@ struct CompileOptions
 	///
 	///
 	bool resource_exists(const char* type, const char* name);
 	bool resource_exists(const char* type, const char* name);
 
 
+	///
+	Buffer read_all(File* file);
+
 	///
 	///
 	Buffer read_temporary(const char* path);
 	Buffer read_temporary(const char* path);
 
 
@@ -117,14 +120,14 @@ struct CompileOptions
 	///
 	///
 	void write_temporary(const char* path, const Buffer& data);
 	void write_temporary(const char* path, const Buffer& data);
 
 
-	/// Reads the source data and returns it.
-	/// It also registers the source path as a dependency.
-	Buffer read();
-
 	/// Reads the data at @a path and returns it.
 	/// Reads the data at @a path and returns it.
 	/// It also registers @a path as a dependency.
 	/// It also registers @a path as a dependency.
 	Buffer read(const char* path);
 	Buffer read(const char* path);
 
 
+	/// Reads the source data and returns it.
+	/// It also registers the source path as a dependency.
+	Buffer read();
+
 	/// Registers @a path as dependency without reading anything.
 	/// Registers @a path as dependency without reading anything.
 	void fake_read(const char* path);
 	void fake_read(const char* path);