added stringtable widget
This commit is contained in:
1 parent
e59988ab1d
commit
2c26c6f795
4 files changed
+214
-1
No files matched your search
@@ -0,0 +1,146 @@
|
||||
// This file is part of c-evo-evo.
|
||||
//
|
||||
// This software is governed by the CeCILL license under French law and
|
||||
// abiding by the rules of distribution of free software. You can use,
|
||||
// modify and/or redistribute the software under the terms of the CeCILL
|
||||
// license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
// "http://www.cecill.info".
|
||||
//
|
||||
// As a counterpart to the access to the source code and rights to copy,
|
||||
// modify and redistribute granted by the license, users are provided only
|
||||
// with a limited warranty and the software's author, the holder of the
|
||||
// economic rights, and the successive licensors have only limited
|
||||
// liability.
|
||||
//
|
||||
// In this respect, the user's attention is drawn to the risks associated
|
||||
// with loading, using, modifying and/or developing or reproducing the
|
||||
// software by the user in light of its specific status of free software,
|
||||
// that may mean that it is complicated to manipulate, and that also
|
||||
// therefore means that it is reserved for developers and experienced
|
||||
// professionals having in-depth computer knowledge. Users are therefore
|
||||
// encouraged to load and test the software's suitability as regards their
|
||||
// requirements in conditions enabling the security of their systems and/or
|
||||
// data to be ensured and, more generally, to use and operate it in the
|
||||
// same conditions as regards security.
|
||||
//
|
||||
// The fact that you are presently reading this means that you have had
|
||||
// knowledge of the CeCILL license and that you accept its terms.
|
||||
|
||||
#include "Cevo/CevoStringTables.h"
|
||||
|
||||
#include <QFile>
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
namespace {
|
||||
|
||||
QString trimmedBlanks(const QString &text)
|
||||
{
|
||||
int start = 0;
|
||||
while (start < text.length() && (text.at(start) == QLatin1Char(' ') || text.at(start) == QLatin1Char('\t')))
|
||||
++start;
|
||||
int end = text.length();
|
||||
while (end > start && (text.at(end - 1) == QLatin1Char(' ') || text.at(end - 1) == QLatin1Char('\t')))
|
||||
--end;
|
||||
return text.mid(start, end - start);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool StringTable::isLabel(const QString &text, const QString &name)
|
||||
{
|
||||
return text.startsWith(QLatin1Char('#')) && text.mid(1, name.length()) == name &&
|
||||
(text.length() == name.length() + 1 || text.mid(name.length() + 1, 1) == QLatin1String(" "));
|
||||
}
|
||||
|
||||
bool StringTable::loadFromFile(const QString &fileName)
|
||||
{
|
||||
m_lines.clear();
|
||||
QFile file(fileName);
|
||||
if (!file.open(QIODevice::ReadOnly))
|
||||
return false;
|
||||
QByteArray bytes = file.readAll();
|
||||
if (bytes.startsWith("\xEF\xBB\xBF")) // UTF-8 byte order mark
|
||||
bytes.remove(0, 3);
|
||||
m_lines = QString::fromUtf8(bytes).split(QLatin1Char('\n'));
|
||||
if (!m_lines.isEmpty() && m_lines.last().isEmpty())
|
||||
m_lines.removeLast();
|
||||
for (QString &line : m_lines) {
|
||||
if (line.endsWith(QLatin1Char('\r')))
|
||||
line.chop(1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int StringTable::getHandle(const QString &item) const
|
||||
{
|
||||
for (int i = 0; i < m_lines.size(); ++i) {
|
||||
if (isLabel(m_lines.at(i), item))
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
QString StringTable::lookupByHandle(int handle, int index) const
|
||||
{
|
||||
QString result;
|
||||
if (index < 0) {
|
||||
if (handle < 0)
|
||||
return QString();
|
||||
const QString &label = m_lines.at(handle);
|
||||
const int space = label.indexOf(QLatin1Char(' '));
|
||||
result = space < 0 ? QString() : label.mid(space + 1);
|
||||
while (handle + 1 < m_lines.size() && !m_lines.at(handle + 1).startsWith(QLatin1Char('#'))) {
|
||||
++handle;
|
||||
const QString &line = m_lines.at(handle);
|
||||
if (!line.isEmpty() && line.at(0) != QLatin1Char('\'')) { // ' starts a comment line
|
||||
if (!result.isEmpty() && !result.endsWith(QLatin1Char('\\')))
|
||||
result += QLatin1Char(' ');
|
||||
result += line;
|
||||
}
|
||||
}
|
||||
} else if (handle + index + 1 >= m_lines.size()) {
|
||||
return QString();
|
||||
} else {
|
||||
result = m_lines.at(handle + index + 1);
|
||||
}
|
||||
result = trimmedBlanks(result);
|
||||
if (result.isEmpty())
|
||||
result = QStringLiteral("*");
|
||||
return result;
|
||||
}
|
||||
|
||||
QString StringTable::lookup(const QString &item, int index) const
|
||||
{
|
||||
const int handle = getHandle(item);
|
||||
QString result = handle >= 0 ? lookupByHandle(handle, index) : QString();
|
||||
if (result.isEmpty())
|
||||
result = index < 0 ? QStringLiteral("[%1]").arg(item) : QStringLiteral("[%1 %2]").arg(item).arg(index);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool StringTable::search(const QString &content, int &handle, int &index) const
|
||||
{
|
||||
const QString upperContent = content.toUpper();
|
||||
int h = handle;
|
||||
int i = h < 0 ? 0 : index + 1;
|
||||
for (;;) {
|
||||
if (h + i + 1 >= m_lines.size())
|
||||
return false;
|
||||
if (m_lines.at(h + i + 1).startsWith(QLatin1Char('#'))) {
|
||||
h = h + i + 1;
|
||||
i = -1;
|
||||
}
|
||||
const QString &line = m_lines.at(h + i + 1);
|
||||
const bool special = !line.isEmpty() && (line.at(0) == QLatin1Char('#') || line.at(0) == QLatin1Char(':') ||
|
||||
line.at(0) == QLatin1Char(';'));
|
||||
if (h >= 0 && !special && line.toUpper().contains(upperContent)) {
|
||||
index = i;
|
||||
handle = h;
|
||||
return true;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Cevo
|
||||
Reference in new issue
Block a user