cli: Move most classes to shared lib for reuse
This commit is contained in:
45
shared/encodingdetector.cpp
Normal file
45
shared/encodingdetector.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <QDataStream>
|
||||
#include "encodingdetector.h"
|
||||
#include <looqsgeneralexception.h>
|
||||
EncodingDetector::EncodingDetector()
|
||||
{
|
||||
}
|
||||
|
||||
QString EncodingDetector::detectEncoding(const QByteArray &data) const
|
||||
{
|
||||
uchardet_t detector = uchardet_new();
|
||||
if(uchardet_handle_data(detector, data.data(), data.size()) != 0)
|
||||
{
|
||||
uchardet_delete(detector);
|
||||
throw LooqsGeneralException("Decoder failed");
|
||||
}
|
||||
uchardet_data_end(detector);
|
||||
QString encoding = uchardet_get_charset(detector);
|
||||
uchardet_delete(detector);
|
||||
return encoding;
|
||||
}
|
||||
QString EncodingDetector::detectEncoding(QDataStream &s) const
|
||||
{
|
||||
uchardet_t detector = uchardet_new();
|
||||
|
||||
char buffer[4096];
|
||||
int n;
|
||||
while((n = s.readRawData(buffer, sizeof(buffer))) > 0)
|
||||
{
|
||||
if(uchardet_handle_data(detector, buffer, n) != 0)
|
||||
{
|
||||
uchardet_delete(detector);
|
||||
|
||||
throw LooqsGeneralException("Decoder failed");
|
||||
}
|
||||
}
|
||||
if(n == -1)
|
||||
{
|
||||
uchardet_delete(detector);
|
||||
throw LooqsGeneralException("Read failed");
|
||||
}
|
||||
uchardet_data_end(detector);
|
||||
QString encoding = uchardet_get_charset(detector);
|
||||
uchardet_delete(detector);
|
||||
return encoding;
|
||||
}
|
||||
Reference in New Issue
Block a user