mirror of
https://gitlab.com/mbunkus/mkvtoolnix.git
synced 2025-01-09 03:31:41 +00:00
GUI: utility function for detecting BOMs and non-ASCII chars in a file
This commit is contained in:
parent
387bf33e02
commit
7acced8112
@ -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;
|
||||
}
|
||||
|
||||
}}}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user