diff --git a/src/mkvtoolnix-gui/util/util.cpp b/src/mkvtoolnix-gui/util/util.cpp index 90c28e932..d4afd087f 100644 --- a/src/mkvtoolnix-gui/util/util.cpp +++ b/src/mkvtoolnix-gui/util/util.cpp @@ -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(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; +} + }}} diff --git a/src/mkvtoolnix-gui/util/util.h b/src/mkvtoolnix-gui/util/util.h index 5c319d8c7..3ee192cf3 100644 --- a/src/mkvtoolnix-gui/util/util.h +++ b/src/mkvtoolnix-gui/util/util.h @@ -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