Chris@1855: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@1855: Chris@1855: /* Chris@1855: Sonic Visualiser Chris@1855: An audio file viewer and annotation editor. Chris@1855: Centre for Digital Music, Queen Mary, University of London. Chris@1855: Chris@1855: This program is free software; you can redistribute it and/or Chris@1855: modify it under the terms of the GNU General Public License as Chris@1855: published by the Free Software Foundation; either version 2 of the Chris@1855: License, or (at your option) any later version. See the file Chris@1855: COPYING included with this distribution for more information. Chris@1855: */ Chris@1855: Chris@1855: #include "TextTest.h" Chris@1855: Chris@1855: #include "base/Debug.h" Chris@1855: #include "base/StringBits.h" Chris@1855: Chris@1855: #include Chris@1855: #include Chris@1855: Chris@1855: bool Chris@1855: TextTest::isApparentTextDocument(FileSource source) Chris@1855: { Chris@1855: // Return true if the document can be opened and contains some Chris@1855: // sort of text, either UTF-8 (so it could be Turtle) or another Chris@1855: // encoding that is recognised as XML Chris@1855: Chris@1855: if (!source.isAvailable()) { Chris@1855: SVDEBUG << "NOTE: TextTest::isApparentTextDocument: Failed to retrieve document from " << source.getLocation() << endl; Chris@1855: return false; Chris@1855: } Chris@1855: Chris@1855: QFile file(source.getLocalFilename()); Chris@1855: if (!file.open(QFile::ReadOnly)) { Chris@1855: SVDEBUG << "NOTE: TextTest::isApparentTextDocument: Failed to open local file from " << source.getLocalFilename() << endl; Chris@1855: return false; Chris@1855: } Chris@1855: Chris@1855: QByteArray bytes = file.read(200); Chris@1855: Chris@1859: if (StringBits::isValidUtf8(std::string(bytes.data()), true)) { Chris@1855: SVDEBUG << "NOTE: TextTest::isApparentTextDocument: Document appears to be UTF-8" << endl; Chris@1855: return true; // good enough to be worth trying to parse Chris@1855: } Chris@1855: Chris@1855: QXmlInputSource xmlSource; Chris@1855: xmlSource.setData(bytes); // guesses text encoding Chris@1855: Chris@1855: if (xmlSource.data().startsWith("