| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* BEGIN software license | ||
| 2 | * | ||
| 3 | * MsXpertSuite - mass spectrometry software suite | ||
| 4 | * ----------------------------------------------- | ||
| 5 | * Copyright (C) 2009--2020 Filippo Rusconi | ||
| 6 | * | ||
| 7 | * http://www.msxpertsuite.org | ||
| 8 | * | ||
| 9 | * This file is part of the MsXpertSuite project. | ||
| 10 | * | ||
| 11 | * The MsXpertSuite project is the successor of the massXpert project. This | ||
| 12 | * project now includes various independent modules: | ||
| 13 | * | ||
| 14 | * - massXpert, model polymer chemistries and simulate mass spectrometric data; | ||
| 15 | * - mineXpert, a powerful TIC chromatogram/mass spectrum viewer/miner; | ||
| 16 | * | ||
| 17 | * This program is free software: you can redistribute it and/or modify | ||
| 18 | * it under the terms of the GNU General Public License as published by | ||
| 19 | * the Free Software Foundation, either version 3 of the License, or | ||
| 20 | * (at your option) any later version. | ||
| 21 | * | ||
| 22 | * This program is distributed in the hope that it will be useful, | ||
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 25 | * GNU General Public License for more details. | ||
| 26 | * | ||
| 27 | * You should have received a copy of the GNU General Public License | ||
| 28 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 29 | * | ||
| 30 | * END software license | ||
| 31 | */ | ||
| 32 | |||
| 33 | |||
| 34 | /////////////////////// Std lib includes | ||
| 35 | |||
| 36 | |||
| 37 | /////////////////////// Qt includes | ||
| 38 | #include <QDebug> | ||
| 39 | #include <QFileInfo> | ||
| 40 | |||
| 41 | |||
| 42 | /////////////////////// IsoSpec | ||
| 43 | |||
| 44 | |||
| 45 | /////////////////////// Local includes | ||
| 46 | #include "MassDataCborBaseHandler.hpp" | ||
| 47 | |||
| 48 | |||
| 49 | namespace MsXpS | ||
| 50 | { | ||
| 51 | |||
| 52 | namespace libXpertMass | ||
| 53 | { | ||
| 54 | |||
| 55 | |||
| 56 | // FIXME TODO DOCUMENTATION | ||
| 57 | |||
| 58 | |||
| 59 | ✗ | MassDataCborBaseHandler::MassDataCborBaseHandler(QObject *parent_p) | |
| 60 | ✗ | : QObject(parent_p) | |
| 61 | { | ||
| 62 | ✗ | } | |
| 63 | |||
| 64 | |||
| 65 | ✗ | MassDataCborBaseHandler::~MassDataCborBaseHandler() | |
| 66 | { | ||
| 67 | ✗ | } | |
| 68 | |||
| 69 | void | ||
| 70 | ✗ | MassDataCborBaseHandler::setInputFileName(const QString &file_name) | |
| 71 | { | ||
| 72 | ✗ | m_inputFileName = file_name; | |
| 73 | ✗ | } | |
| 74 | |||
| 75 | |||
| 76 | void | ||
| 77 | ✗ | MassDataCborBaseHandler::setOutputFileName(const QString &file_name) | |
| 78 | { | ||
| 79 | ✗ | m_outputFileName = file_name; | |
| 80 | ✗ | } | |
| 81 | |||
| 82 | |||
| 83 | void | ||
| 84 | ✗ | MassDataCborBaseHandler::setMassDataType(MassDataType mass_data_type) | |
| 85 | { | ||
| 86 | ✗ | m_massDataType = mass_data_type; | |
| 87 | ✗ | } | |
| 88 | |||
| 89 | |||
| 90 | MassDataType | ||
| 91 | ✗ | MassDataCborBaseHandler::getMassDataType() const | |
| 92 | { | ||
| 93 | ✗ | return m_massDataType; | |
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | bool | ||
| 98 | ✗ | MassDataCborBaseHandler::readContext( | |
| 99 | [[maybe_unused]] QCborStreamReaderSPtr &reader_sp) | ||
| 100 | { | ||
| 101 | ✗ | qDebug() << "The base class handler function does nothing."; | |
| 102 | ✗ | return false; | |
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | MassDataType | ||
| 107 | ✗ | MassDataCborBaseHandler::readMassDataType(QCborStreamReaderSPtr &reader_sp) | |
| 108 | { | ||
| 109 | ✗ | QString current_key; | |
| 110 | |||
| 111 | // The file format starts with a quint64 that indicates the type of mass | ||
| 112 | // data (MassDataType enum to quint64). | ||
| 113 | |||
| 114 | ✗ | while(!reader_sp->lastError() && reader_sp->hasNext()) | |
| 115 | { | ||
| 116 | // The first iteration in the CBOR data structure is in a map. | ||
| 117 | |||
| 118 | ✗ | QCborStreamReader::Type type = reader_sp->type(); | |
| 119 | // qDebug() << "Type is:" << type; | ||
| 120 | |||
| 121 | ✗ | if(type != QCborStreamReader::Map) | |
| 122 | ✗ | qFatal( | |
| 123 | "The byte array does not have the expected CBOR structure for " | ||
| 124 | "mass data."); | ||
| 125 | |||
| 126 | ✗ | reader_sp->enterContainer(); | |
| 127 | |||
| 128 | ✗ | while(reader_sp->lastError() == QCborError::NoError && | |
| 129 | ✗ | reader_sp->hasNext()) | |
| 130 | { | ||
| 131 | ✗ | QCborStreamReader::Type type = reader_sp->type(); | |
| 132 | // qDebug() << "Type is:" << type; | ||
| 133 | |||
| 134 | // The very first item of the map should be a pair | ||
| 135 | // | ||
| 136 | // "DATA_TYPE / quint64 | ||
| 137 | |||
| 138 | ✗ | if(type == QCborStreamReader::String) | |
| 139 | { | ||
| 140 | // Read a CBOR string, concatenating all | ||
| 141 | // the chunks into a single string. | ||
| 142 | ✗ | QString str; | |
| 143 | ✗ | auto chunk = reader_sp->readString(); | |
| 144 | ✗ | while(chunk.status == QCborStreamReader::Ok) | |
| 145 | { | ||
| 146 | ✗ | str += chunk.data; | |
| 147 | ✗ | chunk = reader_sp->readString(); | |
| 148 | } | ||
| 149 | |||
| 150 | ✗ | if(chunk.status == QCborStreamReader::Error) | |
| 151 | { | ||
| 152 | // handle error condition | ||
| 153 | ✗ | qDebug() << "There was an error reading string chunk."; | |
| 154 | ✗ | str.clear(); | |
| 155 | } | ||
| 156 | |||
| 157 | // qDebug() << "The string that was read:" << str; | ||
| 158 | |||
| 159 | // If the current key is empty, this string value is a new key | ||
| 160 | // or tag. Otherwise, it's a value. | ||
| 161 | |||
| 162 | ✗ | if(current_key.isEmpty()) | |
| 163 | { | ||
| 164 | // qDebug() << "Setting current_key:" << str; | ||
| 165 | ✗ | current_key = str; | |
| 166 | } | ||
| 167 | else | ||
| 168 | { | ||
| 169 | // At this point we can reset current_key. | ||
| 170 | ✗ | current_key = QString(); | |
| 171 | } | ||
| 172 | ✗ | } | |
| 173 | ✗ | else if(type == QCborStreamReader::UnsignedInteger) | |
| 174 | { | ||
| 175 | ✗ | if(current_key != "DATA_TYPE") | |
| 176 | ✗ | qFatal( | |
| 177 | "The byte array does not have the expected CBOR " | ||
| 178 | "structure for " | ||
| 179 | "mass data."); | ||
| 180 | |||
| 181 | //quint64 data_type = reader_sp->toUnsignedInteger(); | ||
| 182 | //qDebug() << "The mass data type:" << data_type; | ||
| 183 | |||
| 184 | ✗ | return static_cast<MassDataType>(reader_sp->toUnsignedInteger()); | |
| 185 | } | ||
| 186 | else | ||
| 187 | ✗ | qFatal( | |
| 188 | "The byte array does not have the expected CBOR " | ||
| 189 | "structure for " | ||
| 190 | "mass data."); | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | ✗ | return MassDataType::UNSET; | |
| 195 | ✗ | } | |
| 196 | |||
| 197 | |||
| 198 | MassDataType | ||
| 199 | ✗ | MassDataCborBaseHandler::readMassDataType(const QString &input_file_name) | |
| 200 | { | ||
| 201 | ✗ | QFileInfo file_info(input_file_name); | |
| 202 | |||
| 203 | ✗ | if(!file_info.exists()) | |
| 204 | { | ||
| 205 | ✗ | qDebug() << "File not found."; | |
| 206 | ✗ | return MassDataType::UNSET; | |
| 207 | } | ||
| 208 | |||
| 209 | ✗ | QFile file(input_file_name); | |
| 210 | |||
| 211 | ✗ | bool res = file.open(QIODevice::ReadOnly); | |
| 212 | |||
| 213 | ✗ | if(!res) | |
| 214 | { | ||
| 215 | ✗ | qDebug() << "Failed to open the file for read."; | |
| 216 | ✗ | return MassDataType::UNSET; | |
| 217 | } | ||
| 218 | |||
| 219 | ✗ | QCborStreamReaderSPtr reader_sp = std::make_shared<QCborStreamReader>(&file); | |
| 220 | |||
| 221 | ✗ | return readMassDataType(reader_sp); | |
| 222 | ✗ | } | |
| 223 | |||
| 224 | |||
| 225 | MassDataType | ||
| 226 | ✗ | MassDataCborBaseHandler::readMassDataType(const QByteArray &byte_array) | |
| 227 | { | ||
| 228 | QCborStreamReaderSPtr reader_sp = | ||
| 229 | ✗ | std::make_shared<QCborStreamReader>(byte_array); | |
| 230 | |||
| 231 | ✗ | return readMassDataType(reader_sp); | |
| 232 | ✗ | } | |
| 233 | |||
| 234 | |||
| 235 | bool | ||
| 236 | ✗ | MassDataCborBaseHandler::readFile( | |
| 237 | [[maybe_unused]] const QString &input_file_name) | ||
| 238 | { | ||
| 239 | ✗ | qDebug() << "The base class handler function does nothing."; | |
| 240 | ✗ | return false; | |
| 241 | } | ||
| 242 | |||
| 243 | |||
| 244 | bool | ||
| 245 | ✗ | MassDataCborBaseHandler::readByteArray( | |
| 246 | [[maybe_unused]] const QByteArray &byte_array) | ||
| 247 | { | ||
| 248 | ✗ | qDebug() << "The base class handler function does nothing."; | |
| 249 | ✗ | return false; | |
| 250 | } | ||
| 251 | |||
| 252 | |||
| 253 | bool | ||
| 254 | ✗ | MassDataCborBaseHandler::writeFile( | |
| 255 | [[maybe_unused]] const QString &output_file_name) | ||
| 256 | { | ||
| 257 | ✗ | qDebug() << "The base class handler function does nothing."; | |
| 258 | ✗ | return false; | |
| 259 | } | ||
| 260 | |||
| 261 | |||
| 262 | void | ||
| 263 | ✗ | MassDataCborBaseHandler::writeByteArray([[maybe_unused]] QByteArray &byte_array) | |
| 264 | { | ||
| 265 | ✗ | qDebug() << "The base class handler function does nothing."; | |
| 266 | ✗ | } | |
| 267 | |||
| 268 | |||
| 269 | void | ||
| 270 | ✗ | MassDataCborBaseHandler::setTitle(const QString &title) | |
| 271 | { | ||
| 272 | ✗ | m_title = title; | |
| 273 | ✗ | } | |
| 274 | |||
| 275 | |||
| 276 | QString | ||
| 277 | ✗ | MassDataCborBaseHandler::getTitle() const | |
| 278 | { | ||
| 279 | ✗ | return m_title; | |
| 280 | } | ||
| 281 | |||
| 282 | |||
| 283 | } // namespace libXpertMass | ||
| 284 | |||
| 285 | } // namespace MsXpS | ||
| 286 | |||
| 287 |