GUI: utility function for detecting BOMs and non-ASCII chars in a file

This commit is contained in:
Moritz Bunkus 2015-08-23 12:23:56 +02:00
parent 387bf33e02
commit 7acced8112
2 changed files with 39 additions and 0 deletions

View File

@ -414,4 +414,34 @@ preventScrollingWithoutFocus(QObject *parent) {
install(child);
}
BomAsciiCheckResult
checkForBomAndNonAscii(QString const &fileName) {
QFile file{fileName};
if (!file.open(QIODevice::ReadOnly))
return {};
auto content = file.readAll();
file.close();
auto result = BomAsciiCheckResult{};
auto dataSize = content.size();
auto dataPtr = reinterpret_cast<unsigned char const *>(content.constData());
auto dataEnd = dataPtr + dataSize;
mm_text_io_c::detect_byte_order_marker(dataPtr, dataSize, result.byteOrder, result.bomLength);
dataPtr += result.bomLength;
while (dataPtr < dataEnd) {
if (*dataPtr > 127) {
result.containsNonAscii = true;
break;
}
++dataPtr;
}
return result;
}
}}}

View File

@ -98,6 +98,15 @@ QString displayableDate(QDateTime const &date);
QString itemFlagsToString(Qt::ItemFlags const &flags);
// File stuff
struct BomAsciiCheckResult {
byte_order_e byteOrder{BO_NONE};
unsigned int bomLength{};
bool containsNonAscii{};
};
BomAsciiCheckResult checkForBomAndNonAscii(QString const &fileName);
}}}
#endif // MTX_MKVTOOLNIX_GUI_UTIL_UTIL_H