Index: sources/utility/format.cpp =================================================================== diff -u -rbb8f39a014644c70b832dd2a784f62fa9f6b6106 -r2ef03b2ce51b4dc507f66e9671953a8e0824bde9 --- sources/utility/format.cpp (.../format.cpp) (revision bb8f39a014644c70b832dd2a784f62fa9f6b6106) +++ sources/utility/format.cpp (.../format.cpp) (revision 2ef03b2ce51b4dc507f66e9671953a8e0824bde9) @@ -1,20 +1,35 @@ /*! * - * Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. - * copyright - * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, - * IN PART OR IN WHOLE, - * WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. + * Copyright (c) 2019-2024 Diality Inc. - All Rights Reserved. + * \copyright + * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN + * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * - * file format.cpp - * date 12/16/2019 - * author Behrouz NematiPour + * \file format.cpp + * \author (last) Behrouz NematiPour + * \date (last) 28-May-2023 + * \author (original) Behrouz NematiPour + * \date (original) 16-Dec-2019 * */ #include "format.h" +// Qt +#include + +// Project +#include "types.h" + Format::Format() { } +/*! + * \brief Format::toHexString + * \details converts the unsigned int 16 bit value vValue to QString. + * \param vValue - value to convert. + * \param vWith0x - adds 0x to the output. + * \param vLen - length of the output string. + * \return QString + */ QString Format::toHexString(quint16 vValue, bool vWith0x, quint8 vLen) { if ( vWith0x ) { return "0x" + QString("%1").arg(vValue,0,16).rightJustified(vLen, '0').toUpper(); @@ -23,31 +38,188 @@ } } +/*! + * \brief Format::toHexByteArray + * \details Returns a hex encoded copy of the byte array. + * The hex encoding uses the numbers 0-9 and the letters a-f. + * Also converts to uppercase. + * \param vData - Data to convert + * \param separator - separator to put in string output in the middle of each byte. + * \return + */ QByteArray Format::toHexByteArray(const QByteArray &vData, char separator) { return vData.toHex(separator).toUpper(); } +/*! + * \brief Format::toHexString + * \details Converts the vData of QByteArray to Hex representation of QString + * \param vData - Data to convert + * \param separator - separator to put in string output in the middle of each byte. + * \return QString + */ QString Format::toHexString(const QByteArray &vData, char separator) { QString string = toHexByteArray(vData, separator); return string; } +/*! + * \brief Format::fromVariant + * \details This static method converts the defined types into QByteArray + * \param vData - The value + * \return The QByteAttay of the value vData if cannot be converted 0x00 will be returned + * \note Regarding the QVariant type conversion, if cannot be converted 0 will be returned + * This rule has been used and also to be consistent followed the same rule. + * \note This method converts both float and double to F32 and returns its QByteArray representation. + */ QByteArray Format::fromVariant(const QVariant &vData) { QByteArray mData; - if(vData.type() == QVariant::String) { + + switch (static_cast(vData.type())) { + case QMetaType::QString: // string + { mData += vData.toByteArray(); return mData; } - if(vData.type() == QVariant::ByteArray) { + case QMetaType::QByteArray: // byte array + { mData += vData.toByteArray(); return mData; } - mData += vData.toUInt(); + case QMetaType::QVariantList: // list + { + QVariantList list = vData.toList(); + for(const auto &item: qAsConst(list)) { + mData += fromVariant(item); + } + return mData; + } + case QMetaType::Bool: // bool + { + mData += vData.toUInt(); + return mData; + } + + case QMetaType::Float: + case QMetaType::Double: // F32 + { + Types::F32 f32; + f32.value = vData.toFloat(); + Types::setValue(f32, mData); + return mData; + } + + case QMetaType::UInt: // U32 + { + Types::U32 u32; + u32.value = vData.toUInt(); + Types::setValue(u32, mData); + return mData; + } + + case QMetaType::Int: // S32 + { + Types::S32 s32; + s32.value = vData.toInt(); + Types::setValue(s32, mData); + return mData; + } + + case QMetaType::Short: // S16 + { + Types::S16 s16; + s16.value = vData.toInt(); + Types::setValue(s16, mData); + return mData; + } + + case QMetaType::UShort: // U16 + { + Types::U16 u16; + u16.value = vData.toInt(); + Types::setValue(u16, mData); + return mData; + } + + case QMetaType::Char: // S08 + { + Types::S08 s08; + s08.value = vData.toInt(); + Types::setValue(s08, mData); + return mData; + } + + case QMetaType::UChar: // U08 + { + Types::U08 u08; + u08.value = vData.toInt(); + Types::setValue(u08, mData); + return mData; + } + + default: + break; + } + + mData += '\0'; return mData; } +/*! + * \brief Format::toStringList + * \details Converts the list of of character base items in List of string items. + * \param vList - list of the character base items + * \param vRemoveDuplicate - remove duplicate items if true + * \param vPrefix - add the prefix vPrefix to each item + * \return The QStringList conversion of the character base items vList. + */ +QStringList Format::toStringList(const QList vList, bool vRemoveDuplicate, QString vPrefix) +{ + QStringList list; + for (const auto &listItem : vList) { + auto item = vPrefix + listItem; + if ( vRemoveDuplicate ) { + if ( ! list.contains(item) ) { + list += item; + } + } + else + list += item; + } + return list; +} + +/*! + * \brief Format::fromEpoch + * \details Converts the epoch (in seconds) to the date string format, formatted by vDateTimeFormat + * \param vEpoch - epoch + * \param vFormat - returned date time format + * \return the date time representation of the epoch in QString by vFormat. + */ +QString Format::fromEpoch(qint64 vEpoch, QString vFormat) +{ + if ( ! vEpoch ) return ""; + QDateTime dateTime = QDateTime::fromSecsSinceEpoch(vEpoch); + return dateTime.toString(vFormat); +} + +/*! + * \brief Format::fromVariantList + * \param vData - QVariantList data to be converted to the QSteingList + * \return the QStringList conversion of the vData + */ +QStringList Format::fromVariantList(const QVariantList &vData) +{ + QStringList data; + + // convert the data to string list + for (auto datum : vData) { + data += datum.toString(); + } + return data; +}