Files
QTeletextMaker/pagebase.cpp

128 lines
3.3 KiB
C++
Raw Normal View History

2020-09-11 12:06:06 +01:00
/*
* Copyright (C) 2020 Gavin MacGregor
*
* This file is part of QTeletextMaker.
*
* QTeletextMaker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QTeletextMaker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QTeletextMaker. If not, see <https://www.gnu.org/licenses/>.
*/
#include <QByteArray>
#include "pagebase.h"
PageBase::PageBase()
{
// We use nullptrs to keep track of allocated packets, so initialise them this way
for (int i=0; i<90; i++)
2020-09-11 12:06:06 +01:00
m_packets[i] = nullptr;
for (int i=PageBase::C4ErasePage; i<=PageBase::C11SerialMagazine; i++)
m_controlBits[i] = false;
2020-09-11 12:06:06 +01:00
}
2020-09-16 20:49:52 +01:00
PageBase::PageBase(const PageBase &other)
{
for (int i=PageBase::C4ErasePage; i<=PageBase::C11SerialMagazine; i++)
2020-09-16 20:49:52 +01:00
setControlBit(i, other.controlBit(i));
for (int i=0; i<90; i++)
if (other.packetNeededArrayIndex(i))
setPacketArrayIndex(i, other.packetArrayIndex(i));
else
m_packets[i] = nullptr;
2020-09-16 20:49:52 +01:00
}
2020-09-11 12:06:06 +01:00
PageBase::~PageBase()
{
for (int i=0; i<90; i++)
2020-09-11 12:06:06 +01:00
if (m_packets[i] != nullptr)
delete m_packets[i];
}
QByteArray PageBase::packet(int packetNumber, int designationCode) const
{
2020-09-16 20:49:52 +01:00
int arrayIndex = packetNumber;
if (packetNumber >= 26)
2020-09-16 20:49:52 +01:00
arrayIndex += (packetNumber - 26) * 16 + designationCode;
return packetArrayIndex(arrayIndex);
}
QByteArray PageBase::packetArrayIndex(int arrayIndex) const
{
if (m_packets[arrayIndex] == nullptr)
return QByteArray(); // Blank result
2020-09-16 20:49:52 +01:00
return *m_packets[arrayIndex];
}
bool PageBase::packetNeeded(int packetNumber, int designationCode) const
{
2020-09-16 20:49:52 +01:00
int arrayIndex = packetNumber;
if (packetNumber >= 26)
2020-09-16 20:49:52 +01:00
arrayIndex += (packetNumber - 26) * 16 + designationCode;
return packetNeededArrayIndex(arrayIndex);
}
bool PageBase::packetNeededArrayIndex(int arrayIndex) const
{
return m_packets[arrayIndex] != nullptr;
}
2020-09-11 12:06:06 +01:00
bool PageBase::setPacket(int packetNumber, QByteArray packetContents)
{
return setPacket(packetNumber, 0, packetContents);
}
bool PageBase::setPacket(int packetNumber, int designationCode, QByteArray packetContents)
{
2020-09-16 20:49:52 +01:00
int arrayIndex = packetNumber;
2020-09-11 12:06:06 +01:00
if (packetNumber >= 26)
2020-09-16 20:49:52 +01:00
arrayIndex += (packetNumber - 26) * 16 + designationCode;
return setPacketArrayIndex(arrayIndex, packetContents);
}
bool PageBase::setPacketArrayIndex(int arrayIndex, QByteArray packetContents)
{
if (m_packets[arrayIndex] == nullptr)
m_packets[arrayIndex] = new QByteArray(40, 0x00);
*m_packets[arrayIndex] = packetContents;
return true;
}
bool PageBase::deletePacket(int packetNumber, int designationCode)
{
2020-09-16 20:49:52 +01:00
int arrayIndex = packetNumber;
if (packetNumber >= 26)
2020-09-16 20:49:52 +01:00
arrayIndex += (packetNumber - 26) * 16 + designationCode;
return deletePacketArrayIndex(arrayIndex);
}
bool PageBase::deletePacketArrayIndex(int arrayIndex)
{
if (m_packets[arrayIndex] != nullptr) {
delete m_packets[arrayIndex];
m_packets[arrayIndex] = nullptr;
}
2020-09-11 12:06:06 +01:00
return true;
}
bool PageBase::setControlBit(int bitNumber, bool active)
{
m_controlBits[bitNumber] = active;
return true;
}