2013-12-22 03:58:02 +04:00
|
|
|
/*============================================================================
|
|
|
|
KWSys - Kitware System Library
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
|
|
|
|
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
|
|
|
|
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
|
|
|
#include "kwsysPrivate.h"
|
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
# pragma warning (disable:4786)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include KWSYS_HEADER(FStream.hxx)
|
2014-03-03 17:32:13 +04:00
|
|
|
#include <string.h>
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
# include <mem.h> /* memcmp */
|
|
|
|
#endif
|
2013-12-22 03:58:02 +04:00
|
|
|
|
|
|
|
// Work-around CMake dependency scanning limitation. This must
|
|
|
|
// duplicate the above list of headers.
|
|
|
|
#if 0
|
|
|
|
# include "FStream.hxx.in"
|
|
|
|
#endif
|
|
|
|
|
2015-08-28 21:50:49 +03:00
|
|
|
#include <iostream>
|
2013-12-22 03:58:02 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
static int testNoFile()
|
|
|
|
{
|
|
|
|
kwsys::ifstream in_file("NoSuchFile.txt");
|
|
|
|
if(in_file)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-03 17:32:13 +04:00
|
|
|
static kwsys::FStream::BOM expected_bom[5] =
|
|
|
|
{
|
|
|
|
kwsys::FStream::BOM_UTF8,
|
|
|
|
kwsys::FStream::BOM_UTF16LE,
|
|
|
|
kwsys::FStream::BOM_UTF16BE,
|
|
|
|
kwsys::FStream::BOM_UTF32LE,
|
|
|
|
kwsys::FStream::BOM_UTF32BE
|
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned char expected_bom_data[5][5] =
|
|
|
|
{
|
|
|
|
{3, 0xEF, 0xBB, 0xBF},
|
|
|
|
{2, 0xFF, 0xFE},
|
|
|
|
{2, 0xFE, 0xFF},
|
|
|
|
{4, 0xFF, 0xFE, 0x00, 0x00},
|
|
|
|
{4, 0x00, 0x00, 0xFE, 0xFF},
|
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned char file_data[5][45] =
|
|
|
|
{
|
|
|
|
{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},
|
|
|
|
{22, 0x00, 0x48, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0x6F, 0x00, 0x20,
|
|
|
|
0x00, 0x57, 0x00, 0x6F, 0x00, 0x72, 0x00, 0x6C, 0x00, 0x64},
|
|
|
|
{44, 0x48, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00,
|
|
|
|
0x6C, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
|
|
|
|
0x57, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00,
|
|
|
|
0x6C, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00},
|
|
|
|
{44, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x6C,
|
|
|
|
0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00, 0x20,
|
|
|
|
0x00, 0x00, 0x00, 0x57, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00, 0x72,
|
|
|
|
0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x64},
|
|
|
|
};
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
static int testBOM()
|
|
|
|
{
|
|
|
|
// test various encodings in binary mode
|
|
|
|
for(int i=0; i<5; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
kwsys::ofstream out("bom.txt", kwsys::ofstream::binary);
|
|
|
|
out.write(reinterpret_cast<const char*>(expected_bom_data[i]+1),
|
|
|
|
*expected_bom_data[i]);
|
|
|
|
out.write(reinterpret_cast<const char*>(file_data[i]+1),
|
|
|
|
file_data[i][0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
kwsys::ifstream in("bom.txt", kwsys::ofstream::binary);
|
|
|
|
kwsys::FStream::BOM bom = kwsys::FStream::ReadBOM(in);
|
|
|
|
if(bom != expected_bom[i])
|
|
|
|
{
|
2015-08-28 21:50:49 +03:00
|
|
|
std::cout << "Unexpected BOM " << i << std::endl;
|
2014-03-03 17:32:13 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
char data[45];
|
|
|
|
in.read(data, file_data[i][0]);
|
|
|
|
if(!in.good())
|
|
|
|
{
|
2015-08-28 21:50:49 +03:00
|
|
|
std::cout << "Unable to read data " << i << std::endl;
|
2014-03-03 17:32:13 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(memcmp(data, file_data[i]+1, file_data[i][0]) != 0)
|
|
|
|
{
|
2015-08-28 21:50:49 +03:00
|
|
|
std::cout << "Incorrect read data " << i << std::endl;
|
2014-03-03 17:32:13 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// test text file without bom
|
|
|
|
{
|
|
|
|
{
|
|
|
|
kwsys::ofstream out("bom.txt");
|
|
|
|
out << "Hello World";
|
|
|
|
}
|
|
|
|
|
|
|
|
kwsys::ifstream in("bom.txt");
|
|
|
|
kwsys::FStream::BOM bom = kwsys::FStream::ReadBOM(in);
|
|
|
|
if(bom != kwsys::FStream::BOM_None)
|
|
|
|
{
|
2015-08-28 21:50:49 +03:00
|
|
|
std::cout << "Unexpected BOM for none case" << std::endl;
|
2014-03-03 17:32:13 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
char data[45];
|
|
|
|
in.read(data, file_data[0][0]);
|
|
|
|
if(!in.good())
|
|
|
|
{
|
2015-08-28 21:50:49 +03:00
|
|
|
std::cout << "Unable to read data for none case" << std::endl;
|
2014-03-03 17:32:13 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(memcmp(data, file_data[0]+1, file_data[0][0]) != 0)
|
|
|
|
{
|
2015-08-28 21:50:49 +03:00
|
|
|
std::cout << "Incorrect read data for none case" << std::endl;
|
2014-03-03 17:32:13 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test text file with utf-8 bom
|
|
|
|
{
|
|
|
|
{
|
|
|
|
kwsys::ofstream out("bom.txt");
|
|
|
|
out.write(reinterpret_cast<const char*>(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)
|
|
|
|
{
|
2015-08-28 21:50:49 +03:00
|
|
|
std::cout << "Unexpected BOM for utf-8 case" << std::endl;
|
2014-03-03 17:32:13 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
char data[45];
|
|
|
|
in.read(data, file_data[0][0]);
|
|
|
|
if(!in.good())
|
|
|
|
{
|
2015-08-28 21:50:49 +03:00
|
|
|
std::cout << "Unable to read data for utf-8 case" << std::endl;
|
2014-03-03 17:32:13 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(memcmp(data, file_data[0]+1, file_data[0][0]) != 0)
|
|
|
|
{
|
2015-08-28 21:50:49 +03:00
|
|
|
std::cout << "Incorrect read data for utf-8 case" << std::endl;
|
2014-03-03 17:32:13 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-22 03:58:02 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
int testFStream(int, char*[])
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ret |= testNoFile();
|
2014-03-03 17:32:13 +04:00
|
|
|
ret |= testBOM();
|
2013-12-22 03:58:02 +04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|