BibleTime
cswordldkey.cpp
Go to the documentation of this file.
1/*********
2*
3* In the name of the Father, and of the Son, and of the Holy Spirit.
4*
5* This file is part of BibleTime's source code, https://bibletime.info/
6*
7* Copyright 1999-2026 by the BibleTime developers.
8* The BibleTime source code is licensed under the GNU General Public License
9* version 2.0.
10*
11**********/
12
13#include "cswordldkey.h"
14
15#include <QByteArray>
16#include "../../util/btassert.h"
17#include "../../util/cp1252.h"
18#include "../drivers/cswordmoduleinfo.h"
19
20// Sword includes:
21#ifdef __GNUC__
22#pragma GCC diagnostic push
23#pragma GCC diagnostic ignored "-Wextra-semi"
24#pragma GCC diagnostic ignored "-Wsuggest-override"
25#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
26#endif
27#ifdef __clang__
28#pragma clang diagnostic push
29#pragma clang diagnostic ignored "-Wsuggest-destructor-override"
30#endif
31#include <swkey.h>
32#include <swmodule.h>
33#ifdef __clang__
34#pragma clang diagnostic pop
35#endif
36#ifdef __GNUC__
37#pragma GCC diagnostic pop
38#endif
39
40
42 : CSwordKey(module)
43{ m_key = " "; }
44
45/** No descriptions */
47 : CSwordKey(k)
48 , m_key(k.m_key.getText())
49{}
50
51/** No descriptions */
52CSwordLDKey::CSwordLDKey(const sword::SWKey *k, const CSwordModuleInfo *module)
53 : CSwordKey(module)
54 , m_key(*k)
55{}
56
57sword::SWKey const & CSwordLDKey::asSwordKey() const noexcept { return m_key; }
58
59/** Clones this object by copying the members. */
61 return new CSwordLDKey(*this);
62}
63
64/** Sets the module of this key. */
66 BT_ASSERT(newModule);
67 if (m_module == newModule) return;
69
70 const QString oldKey = key();
71 m_module = newModule;
72 setKey(oldKey);
73}
74
75QString CSwordLDKey::key() const {
76 //return QString::fromUtf8((const char*)*this);
78
79 if (m_module->isUnicode()) {
80 return QString::fromUtf8(m_key.getText());
81 } else {
82 bool error;
83 auto converted = util::cp1252::toUnicode(m_key.getText(), error);
84 BT_ASSERT(!error);
85 return converted;
86 }
87}
88
89const char * CSwordLDKey::rawKey() const {
90 return m_key.getText();
91}
92
93bool CSwordLDKey::setKey(const QString &newKey) {
95
96 if (m_module->isUnicode()) {
97 return setKey(newKey.toUtf8().constData());
98 }
99 else {
100 bool error;
101 auto converted = util::cp1252::fromUnicode(newKey, error);
102 return error ? false : setKey(converted.constData());
103 }
104}
105
106
107/** Sets the key of this instance */
108bool CSwordLDKey::setKey(const char *newKey) {
109 BT_ASSERT(newKey);
110
111 if (newKey) {
112 m_key = newKey; //set the key
113 m_module->swordModule().setKey(&m_key);
114 m_module->snap();
115 }
116
117 return !m_key.popError();
118}
119
120/** Uses the parameter to returns the next entry afer this key. */
122 auto & m = m_module->swordModule();
123 m.setKey(&m_key); // use this key as base for the next one!
124 // m.getKey()->setText( (const char*)key().utf8() );
125
126 m.setSkipConsecutiveLinks(true);
127 m++;
128 m.setSkipConsecutiveLinks(false);
129
130 setKey(m.getKeyText());
131 m_key.setText(m.getKeyText());
132
133 return this;
134}
135
136/** Uses the parameter to returns the next entry afer this key. */
138 auto & m = m_module->swordModule();
139 m.setKey(&m_key); // use this key as base for the next one!
140 // m.getKey()->setText( (const char*)key().utf8() );
141
142 m.setSkipConsecutiveLinks(true);
143 m--;
144 m.setSkipConsecutiveLinks(false);
145
146 m_key.setText(m.getKeyText());
147
148 return this;
149}
150
#define BT_ASSERT(...)
Definition btassert.h:17
const CSwordModuleInfo * m_module
Definition cswordkey.h:112
CSwordLDKey * NextEntry()
CSwordLDKey * PreviousEntry()
CSwordLDKey * copy() const final override
CSwordLDKey(const CSwordModuleInfo *module)
sword::SWKey m_key
void setModule(const CSwordModuleInfo *module) final override
bool setKey(const QString &newKey) final override
sword::SWKey const & asSwordKey() const noexcept final override
QString key() const final override
const char * rawKey() const final override
bool isUnicode() const noexcept
virtual bool snap() const
ModuleType type() const
sword::SWModule & swordModule() const
QByteArray fromUnicode(QString const &str, bool &error)
Definition cp1252.cpp:145
QString toUnicode(QByteArray const &data, bool &error)
Definition cp1252.cpp:142