Added Checking of filetypes from the xml. Added a static function fileTypeFromData to the DataFile class. This opens the given file and checks the xml for its file type, as oposed to relying on the file extension

This commit is contained in:
Dave French
2015-02-23 21:16:38 +00:00
parent bf38b15d63
commit 7037faedd3
3 changed files with 46 additions and 1 deletions

View File

@@ -114,6 +114,45 @@ DataFile::DataFile( Type type ) :
}
DataFile::Type DataFile::fileTypeFromData(const QString fileName)
{
QString errorMsg;
DataFile::Type t;
QDomDocument doc;
QFile inFile( fileName );
if( !inFile.open( QIODevice::ReadOnly ) )
{
return DataFile::Type::UnknownType;
}
QByteArray data = inFile.readAll();
inFile.close();
int line = -1, col = -1;
if( !doc.setContent( data, &errorMsg, &line, &col ) )
{
// parsing failed? then try to uncompress data
QByteArray uncompressed = qUncompress( data );
if( !uncompressed.isEmpty() )
{
if( doc.setContent( uncompressed, &errorMsg, &line, &col ) )
{
line = col = -1;
}
}
if( line >= 0 && col >= 0 )
{
return DataFile::Type::UnknownType;
}
}
QDomElement root = doc.documentElement();
t = type( root.attribute( "type" ) );
return t;
}