Browse Source

Improved reader/writer error handling.

Branimir Karadžić 10 years ago
parent
commit
016bfc4290

+ 2 - 2
examples/13-stencil/stencil.cpp

@@ -682,7 +682,7 @@ struct Mesh
 #define BGFX_CHUNK_MAGIC_PRI BX_MAKEFOURCC('P', 'R', 'I', 0x0)
 #define BGFX_CHUNK_MAGIC_PRI BX_MAKEFOURCC('P', 'R', 'I', 0x0)
 
 
 		bx::CrtFileReader reader;
 		bx::CrtFileReader reader;
-		reader.open(_filePath);
+		bx::open(&reader, _filePath);
 
 
 		Group group;
 		Group group;
 
 
@@ -763,7 +763,7 @@ struct Mesh
 			}
 			}
 		}
 		}
 
 
-		reader.close();
+		bx::close(&reader);
 	}
 	}
 
 
 	void unload()
 	void unload()

+ 2 - 2
examples/14-shadowvolumes/shadowvolumes.cpp

@@ -1030,7 +1030,7 @@ struct Mesh
 #define BGFX_CHUNK_MAGIC_PRI BX_MAKEFOURCC('P', 'R', 'I', 0x0)
 #define BGFX_CHUNK_MAGIC_PRI BX_MAKEFOURCC('P', 'R', 'I', 0x0)
 
 
 		bx::CrtFileReader reader;
 		bx::CrtFileReader reader;
-		reader.open(_filePath);
+		bx::open(&reader, _filePath);
 
 
 		Group group;
 		Group group;
 
 
@@ -1114,7 +1114,7 @@ struct Mesh
 			}
 			}
 		}
 		}
 
 
-		reader.close();
+		bx::close(&reader);
 
 
 		for (GroupArray::iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
 		for (GroupArray::iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
 		{
 		{

+ 2 - 2
examples/16-shadowmaps/shadowmaps.cpp

@@ -891,7 +891,7 @@ struct Mesh
 #define BGFX_CHUNK_MAGIC_PRI BX_MAKEFOURCC('P', 'R', 'I', 0x0)
 #define BGFX_CHUNK_MAGIC_PRI BX_MAKEFOURCC('P', 'R', 'I', 0x0)
 
 
 		bx::CrtFileReader reader;
 		bx::CrtFileReader reader;
-		reader.open(_filePath);
+		bx::open(&reader, _filePath);
 
 
 		Group group;
 		Group group;
 
 
@@ -971,7 +971,7 @@ struct Mesh
 			}
 			}
 		}
 		}
 
 
-		reader.close();
+		bx::close(&reader);
 	}
 	}
 
 
 	void unload()
 	void unload()

+ 3 - 3
examples/common/aviwriter.h

@@ -26,7 +26,7 @@ struct AviWriter
 
 
 	bool open(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _fps, bool _yflip)
 	bool open(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _fps, bool _yflip)
 	{
 	{
-		if (0 != m_writer->open(_filePath) )
+		if (!bx::open(m_writer, _filePath) )
 		{
 		{
 			return false;
 			return false;
 		}
 		}
@@ -36,7 +36,7 @@ struct AviWriter
 		m_numFrames = 0;
 		m_numFrames = 0;
 		m_width = _width;
 		m_width = _width;
 		m_height = _height;
 		m_height = _height;
-		
+
 		// Bgfx returns _yflip true for OpenGL since bottom left corner is 0, 0. In D3D top left corner
 		// Bgfx returns _yflip true for OpenGL since bottom left corner is 0, 0. In D3D top left corner
 		// is 0, 0. DIB expect OpenGL style coordinates, so this is inverted logic for AVI writer.
 		// is 0, 0. DIB expect OpenGL style coordinates, so this is inverted logic for AVI writer.
 		m_yflip = !_yflip;
 		m_yflip = !_yflip;
@@ -163,7 +163,7 @@ struct AviWriter
 			m_writer->seek(m_lengthOffset, bx::Whence::Begin);
 			m_writer->seek(m_lengthOffset, bx::Whence::Begin);
 			bx::write(m_writer, m_numFrames);
 			bx::write(m_writer, m_numFrames);
 
 
-			m_writer->close();
+			bx::close(m_writer);
 
 
 			delete [] m_frame;
 			delete [] m_frame;
 			m_frame = NULL;
 			m_frame = NULL;

+ 12 - 7
examples/common/bgfx_utils.cpp

@@ -24,7 +24,7 @@ namespace stl = tinystl;
 
 
 void* load(bx::FileReaderI* _reader, bx::AllocatorI* _allocator, const char* _filePath, uint32_t* _size)
 void* load(bx::FileReaderI* _reader, bx::AllocatorI* _allocator, const char* _filePath, uint32_t* _size)
 {
 {
-	if (0 == bx::open(_reader, _filePath) )
+	if (bx::open(_reader, _filePath) )
 	{
 	{
 		uint32_t size = (uint32_t)bx::getSize(_reader);
 		uint32_t size = (uint32_t)bx::getSize(_reader);
 		void* data = BX_ALLOC(_allocator, size);
 		void* data = BX_ALLOC(_allocator, size);
@@ -45,6 +45,7 @@ void* load(bx::FileReaderI* _reader, bx::AllocatorI* _allocator, const char* _fi
 	{
 	{
 		*_size = 0;
 		*_size = 0;
 	}
 	}
+
 	return NULL;
 	return NULL;
 }
 }
 
 
@@ -60,7 +61,7 @@ void unload(void* _ptr)
 
 
 static const bgfx::Memory* loadMem(bx::FileReaderI* _reader, const char* _filePath)
 static const bgfx::Memory* loadMem(bx::FileReaderI* _reader, const char* _filePath)
 {
 {
-	if (0 == bx::open(_reader, _filePath) )
+	if (bx::open(_reader, _filePath) )
 	{
 	{
 		uint32_t size = (uint32_t)bx::getSize(_reader);
 		uint32_t size = (uint32_t)bx::getSize(_reader);
 		const bgfx::Memory* mem = bgfx::alloc(size+1);
 		const bgfx::Memory* mem = bgfx::alloc(size+1);
@@ -76,7 +77,7 @@ static const bgfx::Memory* loadMem(bx::FileReaderI* _reader, const char* _filePa
 
 
 static void* loadMem(bx::FileReaderI* _reader, bx::AllocatorI* _allocator, const char* _filePath, uint32_t* _size)
 static void* loadMem(bx::FileReaderI* _reader, bx::AllocatorI* _allocator, const char* _filePath, uint32_t* _size)
 {
 {
-	if (0 == bx::open(_reader, _filePath) )
+	if (bx::open(_reader, _filePath) )
 	{
 	{
 		uint32_t size = (uint32_t)bx::getSize(_reader);
 		uint32_t size = (uint32_t)bx::getSize(_reader);
 		void* data = BX_ALLOC(_allocator, size);
 		void* data = BX_ALLOC(_allocator, size);
@@ -596,10 +597,14 @@ Mesh* meshLoad(bx::ReaderSeekerI* _reader)
 Mesh* meshLoad(const char* _filePath)
 Mesh* meshLoad(const char* _filePath)
 {
 {
 	bx::FileReaderI* reader = entry::getFileReader();
 	bx::FileReaderI* reader = entry::getFileReader();
-	bx::open(reader, _filePath);
-	Mesh* mesh = meshLoad(reader);
-	bx::close(reader);
-	return mesh;
+	if (bx::open(reader, _filePath) )
+	{
+		Mesh* mesh = meshLoad(reader);
+		bx::close(reader);
+		return mesh;
+	}
+
+	return NULL;
 }
 }
 
 
 void meshUnload(Mesh* _mesh)
 void meshUnload(Mesh* _mesh)

+ 1 - 1
examples/common/entry/entry_sdl.cpp

@@ -426,7 +426,7 @@ namespace entry
 			setWindowSize(defaultWindow, m_width, m_height, true);
 			setWindowSize(defaultWindow, m_width, m_height, true);
 
 
 			bx::FileReaderI* reader = getFileReader();
 			bx::FileReaderI* reader = getFileReader();
-			if (0 == bx::open(reader, "gamecontrollerdb.txt") )
+			if (bx::open(reader, "gamecontrollerdb.txt") )
 			{
 			{
 				bx::AllocatorI* allocator = getAllocator();
 				bx::AllocatorI* allocator = getAllocator();
 				uint32_t size = (uint32_t)bx::getSize(reader);
 				uint32_t size = (uint32_t)bx::getSize(reader);

+ 2 - 2
src/bgfx.cpp

@@ -112,10 +112,10 @@ namespace bgfx
 			strcat(filePath, ".tga");
 			strcat(filePath, ".tga");
 
 
 			bx::CrtFileWriter writer;
 			bx::CrtFileWriter writer;
-			if (0 == writer.open(filePath) )
+			if (bx::open(&writer, filePath) )
 			{
 			{
 				imageWriteTga(&writer, _width, _height, _pitch, _data, false, _yflip);
 				imageWriteTga(&writer, _width, _height, _pitch, _data, false, _yflip);
-				writer.close();
+				bx::close(&writer);
 			}
 			}
 #endif // BX_CONFIG_CRT_FILE_READER_WRITER
 #endif // BX_CONFIG_CRT_FILE_READER_WRITER
 		}
 		}

+ 3 - 3
tools/geometryc/geometryc.cpp

@@ -811,7 +811,7 @@ int main(int _argc, const char* _argv[])
 	PrimitiveArray primitives;
 	PrimitiveArray primitives;
 
 
 	bx::CrtFileWriter writer;
 	bx::CrtFileWriter writer;
-	if (0 != writer.open(outFilePath) )
+	if (bx::open(&writer, outFilePath) )
 	{
 	{
 		printf("Unable to open output file '%s'.", outFilePath);
 		printf("Unable to open output file '%s'.", outFilePath);
 		exit(EXIT_FAILURE);
 		exit(EXIT_FAILURE);
@@ -1000,8 +1000,8 @@ int main(int _argc, const char* _argv[])
 			);
 			);
 	}
 	}
 
 
-	printf("size: %d\n", uint32_t(writer.seek() ) );
-	writer.close();
+	printf("size: %d\n", uint32_t(bx::seek(&writer) ) );
+	bx::close(&writer);
 
 
 	delete [] indexData;
 	delete [] indexData;
 	delete [] vertexData;
 	delete [] vertexData;

+ 29 - 28
tools/shaderc/shaderc.cpp

@@ -158,13 +158,13 @@ namespace bgfx
 		{
 		{
 		}
 		}
 
 
-		virtual int32_t close() BX_OVERRIDE
+		virtual void close() BX_OVERRIDE
 		{
 		{
 			generate();
 			generate();
 			return bx::CrtFileWriter::close();
 			return bx::CrtFileWriter::close();
 		}
 		}
 
 
-		virtual int32_t write(const void* _data, int32_t _size) BX_OVERRIDE
+		virtual int32_t write(const void* _data, int32_t _size, bx::Error*) BX_OVERRIDE
 		{
 		{
 			const char* data = (const char*)_data;
 			const char* data = (const char*)_data;
 			m_buffer.insert(m_buffer.end(), data, data+_size);
 			m_buffer.insert(m_buffer.end(), data, data+_size);
@@ -174,9 +174,9 @@ namespace bgfx
 	private:
 	private:
 		void generate()
 		void generate()
 		{
 		{
-	#define HEX_DUMP_WIDTH 16
-	#define HEX_DUMP_SPACE_WIDTH 96
-	#define HEX_DUMP_FORMAT "%-" BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "." BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "s"
+#define HEX_DUMP_WIDTH 16
+#define HEX_DUMP_SPACE_WIDTH 96
+#define HEX_DUMP_FORMAT "%-" BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "." BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "s"
 			const uint8_t* data = &m_buffer[0];
 			const uint8_t* data = &m_buffer[0];
 			uint32_t size = (uint32_t)m_buffer.size();
 			uint32_t size = (uint32_t)m_buffer.size();
 
 
@@ -214,9 +214,9 @@ namespace bgfx
 			}
 			}
 
 
 			outf("};\n");
 			outf("};\n");
-	#undef HEX_DUMP_WIDTH
-	#undef HEX_DUMP_SPACE_WIDTH
-	#undef HEX_DUMP_FORMAT
+#undef HEX_DUMP_WIDTH
+#undef HEX_DUMP_SPACE_WIDTH
+#undef HEX_DUMP_FORMAT
 		}
 		}
 
 
 		int32_t outf(const char* _format, ...)
 		int32_t outf(const char* _format, ...)
@@ -234,7 +234,8 @@ namespace bgfx
 				len = bx::vsnprintf(out, len, _format, argList);
 				len = bx::vsnprintf(out, len, _format, argList);
 			}
 			}
 
 
-			int32_t size = bx::CrtFileWriter::write(out, len);
+			bx::Error err;
+			int32_t size = bx::CrtFileWriter::write(out, len, &err);
 
 
 			va_end(argList);
 			va_end(argList);
 
 
@@ -351,10 +352,10 @@ namespace bgfx
 	void writeFile(const char* _filePath, const void* _data, int32_t _size)
 	void writeFile(const char* _filePath, const void* _data, int32_t _size)
 	{
 	{
 		bx::CrtFileWriter out;
 		bx::CrtFileWriter out;
-		if (0 == out.open(_filePath) )
+		if (bx::open(&out, _filePath) )
 		{
 		{
-			out.write(_data, _size);
-			out.close();
+			bx::write(&out, _data, _size);
+			bx::close(&out);
 		}
 		}
 	}
 	}
 
 
@@ -1102,7 +1103,7 @@ namespace bgfx
 					writer = new bx::CrtFileWriter;
 					writer = new bx::CrtFileWriter;
 				}
 				}
 
 
-				if (0 != writer->open(outFilePath) )
+				if (!bx::open(writer, outFilePath) )
 				{
 				{
 					fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
 					fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
 					return EXIT_FAILURE;
 					return EXIT_FAILURE;
@@ -1140,7 +1141,7 @@ namespace bgfx
 					compiled = compileHLSLShader(cmdLine, d3d, input, writer);
 					compiled = compileHLSLShader(cmdLine, d3d, input, writer);
 				}
 				}
 
 
-				writer->close();
+				bx::close(writer);
 				delete writer;
 				delete writer;
 			}
 			}
 			else if ('c' == shaderType) // Compute
 			else if ('c' == shaderType) // Compute
@@ -1235,14 +1236,14 @@ namespace bgfx
 						{
 						{
 							bx::CrtFileWriter writer;
 							bx::CrtFileWriter writer;
 
 
-							if (0 != writer.open(outFilePath) )
+							if (!bx::open(&writer, outFilePath) )
 							{
 							{
 								fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
 								fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
 								return EXIT_FAILURE;
 								return EXIT_FAILURE;
 							}
 							}
 
 
-							writer.write(preprocessor.m_preprocessed.c_str(), (int32_t)preprocessor.m_preprocessed.size() );
-							writer.close();
+							bx::write(&writer, preprocessor.m_preprocessed.c_str(), (int32_t)preprocessor.m_preprocessed.size() );
+							bx::close(&writer);
 
 
 							return EXIT_SUCCESS;
 							return EXIT_SUCCESS;
 						}
 						}
@@ -1259,7 +1260,7 @@ namespace bgfx
 								writer = new bx::CrtFileWriter;
 								writer = new bx::CrtFileWriter;
 							}
 							}
 
 
-							if (0 != writer->open(outFilePath) )
+							if (!bx::open(writer, outFilePath) )
 							{
 							{
 								fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
 								fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
 								return EXIT_FAILURE;
 								return EXIT_FAILURE;
@@ -1301,7 +1302,7 @@ namespace bgfx
 								compiled = compileHLSLShader(cmdLine, d3d, preprocessor.m_preprocessed, writer);
 								compiled = compileHLSLShader(cmdLine, d3d, preprocessor.m_preprocessed, writer);
 							}
 							}
 
 
-							writer->close();
+							bx::close(writer);
 							delete writer;
 							delete writer;
 						}
 						}
 
 
@@ -1312,10 +1313,10 @@ namespace bgfx
 								std::string ofp = outFilePath;
 								std::string ofp = outFilePath;
 								ofp += ".d";
 								ofp += ".d";
 								bx::CrtFileWriter writer;
 								bx::CrtFileWriter writer;
-								if (0 == writer.open(ofp.c_str() ) )
+								if (bx::open(&writer, ofp.c_str() ) )
 								{
 								{
 									writef(&writer, "%s : %s\n", outFilePath, preprocessor.m_depends.c_str() );
 									writef(&writer, "%s : %s\n", outFilePath, preprocessor.m_depends.c_str() );
-									writer.close();
+									bx::close(&writer);
 								}
 								}
 							}
 							}
 						}
 						}
@@ -1642,7 +1643,7 @@ namespace bgfx
 						{
 						{
 							bx::CrtFileWriter writer;
 							bx::CrtFileWriter writer;
 
 
-							if (0 != writer.open(outFilePath) )
+							if (!bx::open(&writer, outFilePath) )
 							{
 							{
 								fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
 								fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
 								return EXIT_FAILURE;
 								return EXIT_FAILURE;
@@ -1659,8 +1660,8 @@ namespace bgfx
 										);
 										);
 								}
 								}
 							}
 							}
-							writer.write(preprocessor.m_preprocessed.c_str(), (int32_t)preprocessor.m_preprocessed.size() );
-							writer.close();
+							bx::write(&writer, preprocessor.m_preprocessed.c_str(), (int32_t)preprocessor.m_preprocessed.size() );
+							bx::close(&writer);
 
 
 							return EXIT_SUCCESS;
 							return EXIT_SUCCESS;
 						}
 						}
@@ -1677,7 +1678,7 @@ namespace bgfx
 								writer = new bx::CrtFileWriter;
 								writer = new bx::CrtFileWriter;
 							}
 							}
 
 
-							if (0 != writer->open(outFilePath) )
+							if (!bx::open(writer, outFilePath) )
 							{
 							{
 								fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
 								fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
 								return EXIT_FAILURE;
 								return EXIT_FAILURE;
@@ -1796,7 +1797,7 @@ namespace bgfx
 										);
 										);
 							}
 							}
 
 
-							writer->close();
+							bx::close(writer);
 							delete writer;
 							delete writer;
 						}
 						}
 
 
@@ -1807,10 +1808,10 @@ namespace bgfx
 								std::string ofp = outFilePath;
 								std::string ofp = outFilePath;
 								ofp += ".d";
 								ofp += ".d";
 								bx::CrtFileWriter writer;
 								bx::CrtFileWriter writer;
-								if (0 == writer.open(ofp.c_str() ) )
+								if (bx::open(&writer, ofp.c_str() ) )
 								{
 								{
 									writef(&writer, "%s : %s\n", outFilePath, preprocessor.m_depends.c_str() );
 									writef(&writer, "%s : %s\n", outFilePath, preprocessor.m_depends.c_str() );
-									writer.close();
+									bx::close(&writer);
 								}
 								}
 							}
 							}
 						}
 						}

+ 5 - 0
tools/shaderc/shaderc.h

@@ -6,6 +6,11 @@
 #ifndef SHADERC_H_HEADER_GUARD
 #ifndef SHADERC_H_HEADER_GUARD
 #define SHADERC_H_HEADER_GUARD
 #define SHADERC_H_HEADER_GUARD
 
 
+namespace bgfx
+{
+	extern bool g_verbose;
+}
+
 #define _BX_TRACE(_format, ...) \
 #define _BX_TRACE(_format, ...) \
 				BX_MACRO_BLOCK_BEGIN \
 				BX_MACRO_BLOCK_BEGIN \
 					if (bgfx::g_verbose) \
 					if (bgfx::g_verbose) \

+ 2 - 2
tools/texturec/texturec.cpp

@@ -380,7 +380,7 @@ int main(int _argc, const char* _argv[])
 	BX_UNUSED(sdf, edge);
 	BX_UNUSED(sdf, edge);
 
 
 	bx::CrtFileReader reader;
 	bx::CrtFileReader reader;
-	if (0 != bx::open(&reader, inputFileName) )
+	if (!bx::open(&reader, inputFileName) )
 	{
 	{
 		help("Failed to open input file.");
 		help("Failed to open input file.");
 		return EXIT_FAILURE;
 		return EXIT_FAILURE;
@@ -546,7 +546,7 @@ int main(int _argc, const char* _argv[])
 			if (NULL != output)
 			if (NULL != output)
 			{
 			{
 				bx::CrtFileWriter writer;
 				bx::CrtFileWriter writer;
-				if (0 == bx::open(&writer, outputFileName) )
+				if (!bx::open(&writer, outputFileName) )
 				{
 				{
 					if (NULL != bx::stristr(outputFileName, ".ktx") )
 					if (NULL != bx::stristr(outputFileName, ".ktx") )
 					{
 					{