From f2b0bf6e3f45f4f2a474732f29528299825bf7e9 Mon Sep 17 00:00:00 2001 From: KWSys Upstream Date: Thu, 7 Jan 2016 13:00:32 -0500 Subject: [PATCH] KWSys 2016-01-07 (2418443e) Code extracted from: http://public.kitware.com/KWSys.git at commit 2418443ed01e5caeb47908064fd4dad2eff4736f (master). Upstream Shortlog ----------------- Clinton Stimpson (1): 2418443e FStream: Fix reading BOM on single-character files --- FStream.cxx | 2 ++ testFStream.cxx | 79 +++++++++---------------------------------------- 2 files changed, 16 insertions(+), 65 deletions(-) diff --git a/FStream.cxx b/FStream.cxx index 018652c84..5a30997d6 100644 --- a/FStream.cxx +++ b/FStream.cxx @@ -34,6 +34,7 @@ BOM ReadBOM(std::istream& in) in.read(reinterpret_cast(bom), 2); if(!in.good()) { + in.clear(); in.seekg(orig); return BOM_None; } @@ -68,6 +69,7 @@ BOM ReadBOM(std::istream& in) in.seekg(p); return BOM_UTF16LE; } + in.clear(); in.seekg(orig); return BOM_None; } diff --git a/testFStream.cxx b/testFStream.cxx index ac5220ac0..5e537258f 100644 --- a/testFStream.cxx +++ b/testFStream.cxx @@ -41,8 +41,13 @@ static int testNoFile() return 0; } -static kwsys::FStream::BOM expected_bom[5] = +static const int num_test_files = 7; +static const int max_test_file_size = 45; + +static kwsys::FStream::BOM expected_bom[num_test_files] = { + kwsys::FStream::BOM_None, + kwsys::FStream::BOM_None, kwsys::FStream::BOM_UTF8, kwsys::FStream::BOM_UTF16LE, kwsys::FStream::BOM_UTF16BE, @@ -50,8 +55,10 @@ static kwsys::FStream::BOM expected_bom[5] = kwsys::FStream::BOM_UTF32BE }; -static unsigned char expected_bom_data[5][5] = +static unsigned char expected_bom_data[num_test_files][5] = { + {0}, + {0}, {3, 0xEF, 0xBB, 0xBF}, {2, 0xFF, 0xFE}, {2, 0xFE, 0xFF}, @@ -59,8 +66,10 @@ static unsigned char expected_bom_data[5][5] = {4, 0x00, 0x00, 0xFE, 0xFF}, }; -static unsigned char file_data[5][45] = +static unsigned char file_data[num_test_files][max_test_file_size] = { + {1, 'H'}, + {11, 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'}, {11, 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'}, {22, 0x48, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0x6F, 0x00, 0x20, 0x00, 0x57, 0x00, 0x6F, 0x00, 0x72, 0x00, 0x6C, 0x00, 0x64, 0x00}, @@ -80,7 +89,7 @@ static unsigned char file_data[5][45] = static int testBOM() { // test various encodings in binary mode - for(int i=0; i<5; i++) + for(int i=0; i(expected_bom_data[0]+1), - *expected_bom_data[0]); - out << "Hello World"; - } - - kwsys::ifstream in("bom.txt"); - kwsys::FStream::BOM bom = kwsys::FStream::ReadBOM(in); - if(bom != kwsys::FStream::BOM_UTF8) - { - std::cout << "Unexpected BOM for utf-8 case" << std::endl; - return 1; - } - char data[45]; - in.read(data, file_data[0][0]); - if(!in.good()) - { - std::cout << "Unable to read data for utf-8 case" << std::endl; - return 1; - } - - if(memcmp(data, file_data[0]+1, file_data[0][0]) != 0) - { - std::cout << "Incorrect read data for utf-8 case" << std::endl; - return 1; - } - } - return 0; }