From dae27de2429b5b5ba46ee51f67fa495c8fddc592 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Tue, 21 Jun 2016 20:56:39 -0700 Subject: [PATCH] Fixed issue #814. --- tools/shaderc/shaderc.cpp | 47 +++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/tools/shaderc/shaderc.cpp b/tools/shaderc/shaderc.cpp index 52178a18c..c26f60cc9 100644 --- a/tools/shaderc/shaderc.cpp +++ b/tools/shaderc/shaderc.cpp @@ -20,15 +20,6 @@ namespace bgfx #define BGFX_CHUNK_MAGIC_FSH BX_MAKEFOURCC('F', 'S', 'H', 0x4) #define BGFX_CHUNK_MAGIC_VSH BX_MAKEFOURCC('V', 'S', 'H', 0x4) - long int fsize(FILE* _file) - { - long int pos = ftell(_file); - fseek(_file, 0L, SEEK_END); - long int size = ftell(_file); - fseek(_file, pos, SEEK_SET); - return size; - } - static const char* s_ARB_shader_texture_lod[] = { "texture2DLod", @@ -298,14 +289,23 @@ namespace bgfx File(const char* _filePath) : m_data(NULL) { - FILE* file = fopen(_filePath, "r"); - if (NULL != file) + bx::CrtFileReader reader; + if (bx::open(&reader, _filePath) ) { - m_size = fsize(file); + m_size = bx::getSize(&reader); m_data = new char[m_size+1]; - m_size = (uint32_t)fread(m_data, 1, m_size, file); + m_size = bx::read(&reader, m_data, m_size); + bx::close(&reader); + + if (m_data[0] == '\xef' + && m_data[1] == '\xbb' + && m_data[2] == '\xbf') + { + memmove(m_data, &m_data[3], m_size-3); + m_size -= 3; + } + m_data[m_size] = '\0'; - fclose(file); } } @@ -953,8 +953,8 @@ namespace bgfx bool compiled = false; - FILE* file = fopen(filePath, "r"); - if (NULL == file) + bx::CrtFileReader reader; + if (!bx::open(&reader, filePath) ) { fprintf(stderr, "Unable to open file '%s'.\n", filePath); } @@ -1064,14 +1064,23 @@ namespace bgfx char* input; { const size_t padding = 1024; - uint32_t size = (uint32_t)fsize(file); + uint32_t size = (uint32_t)bx::getSize(&reader); data = new char[size+padding+1]; - size = (uint32_t)fread(data, 1, size, file); + size = (uint32_t)bx::read(&reader, data, size); + + if (data[0] == '\xef' + && data[1] == '\xbb' + && data[2] == '\xbf') + { + memmove(data, &data[3], size-3); + size -= 3; + } + // Compiler generates "error X3000: syntax error: unexpected end of file" // if input doesn't have empty line at EOF. data[size] = '\n'; memset(&data[size+1], 0, padding); - fclose(file); + bx::close(&reader); if (!raw) {