Skip to content

Commit

Permalink
Merge pull request #2 from matterhorn103/read-utf16
Browse files Browse the repository at this point in the history
Adopt simpler approach to file reading on Qt6
  • Loading branch information
ghutchis authored Jan 4, 2025
2 parents eedf399 + 0df0fe8 commit e0a4f72
Showing 1 changed file with 59 additions and 46 deletions.
105 changes: 59 additions & 46 deletions avogadro/backgroundfileformat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,57 +42,70 @@ void BackgroundFileFormat::read()
m_error = tr("No file name set in BackgroundFileFormat!");

if (m_error.isEmpty()) {
// sometimes we hit UTF-16 files, so we need to convert them to UTF-8
// first check whether the file is UTF-16
QFile file(m_fileName);
QTextStream in(&file);
QString text;
bool isUTF16 = false;
if (file.open(QIODevice::ReadOnly)) {
QByteArray data = file.read(2);
// look for a byte-order mark
if ((data.size() == 2 && data[0] == '\xff' && data[1] == '\xfe') ||
(data.size() == 2 && data[0] == '\xfe' && data[1] == '\xff')) {
// UTF-16, read the file and let QString handle decoding
isUTF16 = true;
file.close();
file.open(QIODevice::ReadOnly | QIODevice::Text);
in.setCodec("UTF-16");
#if QT_VERSION >= 0x060000
QFile file(m_fileName);
if (file.open(QFile::ReadOnly | QIODevice::Text)) {

Check notice

Code scanning / Flawfinder (reported by Codacy)

Check when opening files - can an attacker redirect it (via symlinks), force the opening of special file type (e.g., device files), move things around to create a race condition, control its ancestors, or change its contents? (CWE-362). Note

Check when opening files - can an attacker redirect it (via symlinks), force the opening of special file type (e.g., device files), move things around to create a race condition, control its ancestors, or change its contents? (CWE-362).
QTextStream in(&file);
QString text;
text = in.readAll();
file.close();

Check notice

Code scanning / Flawfinder (reported by Codacy)

Check when opening files - can an attacker redirect it (via symlinks), force the opening of special file type (e.g., device files), move things around to create a race condition, control its ancestors, or change its contents? (CWE-362). Note

Check when opening files - can an attacker redirect it (via symlinks), force the opening of special file type (e.g., device files), move things around to create a race condition, control its ancestors, or change its contents? (CWE-362).
}
}

if (!isUTF16)
m_success =
m_format->readFile(m_fileName.toLocal8Bit().data(), *m_molecule);
else {
// write it to a temporary file and we'll read it back in
// some formats (like the generic output) need a file
// not just a string bugger

// first, we need the *name* of the file, not the full path
// because we're going to save a copy in a temp directory
QTemporaryDir tempDir;
QString tempFileName = tempDir.filePath(QFileInfo(m_fileName).fileName());
QFile tempFile(tempFileName);
if (tempFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&tempFile);
// set it to UTF-8
out.setCodec("UTF-8");
out << text;
out.flush();
tempFile.close();
m_success =
m_format->readFile(tempFileName.toLocal8Bit().data(), *m_molecule);
tempFile.remove();
} else // try just reading the string
m_success =
m_format->readString(text.toLocal8Bit().data(), *m_molecule);
}
}

if (!m_success)
m_error = QString::fromStdString(m_format->error());
#else
// sometimes we hit UTF-16 files, so we need to convert them to UTF-8
// first check whether the file is UTF-16
QFile file(m_fileName);

Check notice

Code scanning / Flawfinder (reported by Codacy)

Check when opening files - can an attacker redirect it (via symlinks), force the opening of special file type (e.g., device files), move things around to create a race condition, control its ancestors, or change its contents? (CWE-362). Note

Check when opening files - can an attacker redirect it (via symlinks), force the opening of special file type (e.g., device files), move things around to create a race condition, control its ancestors, or change its contents? (CWE-362).
QTextStream in(&file);
QString text;
bool isUTF16 = false;
if (file.open(QIODevice::ReadOnly)) {
QByteArray data = file.read(2);
// look for a byte-order mark
if ((data.size() == 2 && data[0] == '\xff' && data[1] == '\xfe') ||
(data.size() == 2 && data[0] == '\xfe' && data[1] == '\xff')) {
// UTF-16, read the file and let QString handle decoding
isUTF16 = true;
file.close();
file.open(QIODevice::ReadOnly | QIODevice::Text);
in.setCodec("UTF-16");
text = in.readAll();
file.close();
}
}

if (!isUTF16)
m_success =
m_format->readFile(m_fileName.toLocal8Bit().data(), *m_molecule);
else {
// write it to a temporary file and we'll read it back in
// some formats (like the generic output) need a file
// not just a string bugger

// first, we need the *name* of the file, not the full path
// because we're going to save a copy in a temp directory
QTemporaryDir tempDir;
QString tempFileName = tempDir.filePath(QFileInfo(m_fileName).fileName());
QFile tempFile(tempFileName);
if (tempFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&tempFile);
// set it to UTF-8
out.setCodec("UTF-8");
out << text;
out.flush();
tempFile.close();
m_success =
m_format->readFile(tempFileName.toLocal8Bit().data(), *m_molecule);
tempFile.remove();
} else // try just reading the string
m_success =
m_format->readString(text.toLocal8Bit().data(), *m_molecule);
}

if (!m_success)
m_error = QString::fromStdString(m_format->error());
#endif
}

emit finished();
Expand Down

0 comments on commit e0a4f72

Please sign in to comment.