From 2c26c6f795b1bbcdc5165f97ba617f6faf0e8f3b Mon Sep 17 00:00:00 2001 From: Geoffray Levasseur Date: Tue, 3 Nov 2026 18:22:44 +0100 Subject: [PATCH] added stringtable widget --- widget/CMakeLists.txt | 4 +- widget/include/Cevo/CevoStringTables.h | 64 +++++++++++ widget/include/Cevo/CevoWidgets.h | 1 + widget/src/CevoStringTables.cpp | 146 +++++++++++++++++++++++++ 4 files changed, 214 insertions(+), 1 deletion(-) create mode 100644 widget/include/Cevo/CevoStringTables.h create mode 100644 widget/src/CevoStringTables.cpp diff --git a/widget/CMakeLists.txt b/widget/CMakeLists.txt index 603af38..bf49c53 100644 --- a/widget/CMakeLists.txt +++ b/widget/CMakeLists.txt @@ -82,7 +82,7 @@ set_target_properties(dpiwidgets PROPERTIES # (../../cevo/Packages/CevoComponents), built on top of Dpi:: above. Provides # a Cevo:: namespace (Area, ButtonBase/A/B/C/G/N, EOTButton, ListBoxEx, # DrawDlg/BaseMessgDlg, BufferedDrawDlg/FramedDlg, GraphicSet, Texture, -# ScreenTools' drawing helpers). Directories, StringTables, AsyncProcess2 and +# ScreenTools' drawing helpers, StringTables). Directories, AsyncProcess2 and # Sound are not ported: they are plain platform/data utilities (QProcess # already covers AsyncProcess2's job; Sound is superseded by ../media). @@ -101,6 +101,7 @@ set(CEVO_WIDGETS_HEADERS include/Cevo/CevoListBoxEx.h include/Cevo/CevoPVScrollBar.h include/Cevo/CevoScreenTools.h + include/Cevo/CevoStringTables.h include/Cevo/CevoTexture.h include/Cevo/CevoWidgets.h ) @@ -120,6 +121,7 @@ set(CEVO_WIDGETS_SOURCES src/CevoListBoxEx.cpp src/CevoPVScrollBar.cpp src/CevoScreenTools.cpp + src/CevoStringTables.cpp src/CevoTexture.cpp ) diff --git a/widget/include/Cevo/CevoStringTables.h b/widget/include/Cevo/CevoStringTables.h new file mode 100644 index 0000000..734760c --- /dev/null +++ b/widget/include/Cevo/CevoStringTables.h @@ -0,0 +1,64 @@ +// 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. + +#pragma once + +#include +#include + +namespace Cevo { + +// TStringTable equivalent: a "#KEY text" / "#KEY" + following lines text file +// (Language.txt, Help.txt, ...) with key lookup, indexed-line lookup, +// handle-based access and full-text search. +class StringTable { +public: + // Returns false (and leaves the table empty) if the file can't be read. + bool loadFromFile(const QString &fileName); + + // Index of the "#item" label line, -1 if there is none. + int getHandle(const QString &item) const; + // index < 0: the item's (multi-line joined) text; otherwise its + // index'th line. Empty result for an unknown handle/line, "*" for a + // blank line. + QString lookupByHandle(int handle, int index = -1) const; + // Like lookupByHandle(getHandle(item), index), but unknown items give + // "[item]" / "[item index]" instead of an empty string. + QString lookup(const QString &item, int index = -1) const; + QString operator[](const QString &item) const { return lookup(item); } + // Continues a case-insensitive content search after (handle, index); + // start with handle = -1. + bool search(const QString &content, int &handle, int &index) const; + +private: + static bool isLabel(const QString &text, const QString &name); + + QStringList m_lines; +}; + +} // namespace Cevo diff --git a/widget/include/Cevo/CevoWidgets.h b/widget/include/Cevo/CevoWidgets.h index e8c01ef..656e8b1 100644 --- a/widget/include/Cevo/CevoWidgets.h +++ b/widget/include/Cevo/CevoWidgets.h @@ -44,4 +44,5 @@ #include "Cevo/CevoGraphicSet.h" #include "Cevo/CevoListBoxEx.h" #include "Cevo/CevoScreenTools.h" +#include "Cevo/CevoStringTables.h" #include "Cevo/CevoTexture.h" diff --git a/widget/src/CevoStringTables.cpp b/widget/src/CevoStringTables.cpp new file mode 100644 index 0000000..fd67475 --- /dev/null +++ b/widget/src/CevoStringTables.cpp @@ -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 + +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