Commit 1f71575e authored by Martin Straka's avatar Martin Straka

Added new shared isds structures

parent 2973480c
......@@ -93,6 +93,10 @@ SOURCES += \
src/datovka_shared/io/sqlite/db.cpp \
src/datovka_shared/io/sqlite/db_single.cpp \
src/datovka_shared/io/sqlite/table.cpp \
src/datovka_shared/isds/box_interface.cpp \
src/datovka_shared/isds/internal_conversion.cpp \
src/datovka_shared/isds/message_interface.cpp \
src/datovka_shared/isds/type_conversion.cpp \
src/datovka_shared/localisation/localisation.cpp \
src/datovka_shared/records_management/conversion.cpp \
src/datovka_shared/records_management/io/records_management_connection.cpp \
......@@ -175,6 +179,11 @@ HEADERS += \
src/datovka_shared/io/sqlite/db.h \
src/datovka_shared/io/sqlite/db_single.h \
src/datovka_shared/io/sqlite/table.h \
src/datovka_shared/isds/box_interface.h \
src/datovka_shared/isds/internal_conversion.h \
src/datovka_shared/isds/message_interface.h \
src/datovka_shared/isds/type_conversion.h \
src/datovka_shared/isds/types.h \
src/datovka_shared/localisation/localisation.h \
src/datovka_shared/records_management/conversion.h \
src/datovka_shared/records_management/io/records_management_connection.h \
......
This diff is collapsed.
This diff is collapsed.
/*
* Copyright (C) 2014-2018 CZ.NIC
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*/
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <sys/time.h> /* struct timeval */
#include "src/datovka_shared/isds/internal_conversion.h"
QByteArray Isds::fromCData(const void *cData, size_t cSize)
{
if ((cData == NULL) || (cSize == 0)) {
return QByteArray();
}
return QByteArray((const char *)cData, cSize);
}
bool Isds::toCDataCopy(void **cDataPtr, size_t *cSize, const QByteArray &data)
{
if (Q_UNLIKELY((cDataPtr == Q_NULLPTR) || (cSize == NULL))) {
Q_ASSERT(0);
return false;
}
if (*cDataPtr != NULL) {
/* Delete already created. */
std::free(*cDataPtr); *cDataPtr = NULL;
*cSize = 0;
}
if (data.isNull() || (data.size() == 0)) {
return true;
}
/* Copy data content.*/
*cSize = data.size();
*cDataPtr = std::malloc(*cSize);
if (Q_UNLIKELY(*cDataPtr == NULL)) {
Q_ASSERT(0);
*cSize = 0;
return false;
}
std::memcpy(*cDataPtr, data.constData(), *cSize);
return true;
}
QString Isds::fromCStr(const char *cStr)
{
if (cStr == NULL) {
return QString();
}
return QString(cStr);
}
bool Isds::toCStrCopy(char **cStrPtr, const QString &str)
{
if (Q_UNLIKELY(cStrPtr == Q_NULLPTR)) {
Q_ASSERT(0);
return false;
}
if (*cStrPtr != NULL) {
/* Delete already allocated. */
std::free(*cStrPtr); *cStrPtr = NULL;
}
if (str.isNull()) {
return true;
}
/* Copy string content. */
QByteArray strBytes(str.toUtf8()); /* Must not be deleted before memcpy. */
const char *utfStr = strBytes.constData();
if (utfStr == Q_NULLPTR) {
return true;
}
size_t utfStrLen = std::strlen(utfStr);
*cStrPtr = (char *)std::malloc(utfStrLen + 1);
if (Q_UNLIKELY(*cStrPtr == NULL)) {
Q_ASSERT(0);
return false;
}
if (utfStrLen > 0) {
std::memcpy(*cStrPtr, utfStr, utfStrLen);
}
(*cStrPtr)[utfStrLen] = '\0';
return true;
}
QDate Isds::dateFromStructTM(struct tm *cDate)
{
if (cDate == NULL) {
return QDate();
}
int year = cDate->tm_year + 1900;
int month = cDate->tm_mon + 1;
int day = cDate->tm_mday;
return QDate(year, month, day);
}
bool Isds::toCDateCopy(struct tm **cDatePtr, const QDate &date)
{
if (Q_UNLIKELY(cDatePtr == Q_NULLPTR)) {
Q_ASSERT(0);
return false;
}
if (date.isNull()) {
if (*cDatePtr != NULL) {
/* Delete allocated. */
std::free(*cDatePtr); *cDatePtr = NULL;
}
return true;
}
if (*cDatePtr == NULL) {
*cDatePtr = (struct tm *)std::malloc(sizeof(**cDatePtr));
if (Q_UNLIKELY(*cDatePtr == NULL)) {
Q_ASSERT(0);
return false;
}
}
std::memset(*cDatePtr, 0, sizeof(**cDatePtr));
(*cDatePtr)->tm_year = date.year() - 1900;
(*cDatePtr)->tm_mon = date.month() - 1;
(*cDatePtr)->tm_mday = date.day();
return true;
}
QDateTime Isds::dateTimeFromStructTimeval(struct timeval *cDateTime)
{
QDateTime timeStamp;
if (cDateTime != NULL) {
/* TODO -- handle microseconds. */
#if (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0))
timeStamp.setSecsSinceEpoch(cDateTime->tv_sec);
#else /* < Qt-5.8 */
timeStamp.setTime_t(cDateTime->tv_sec);
#endif /* >= Qt-5.8 */
timeStamp = timeStamp.addMSecs(cDateTime->tv_usec / 1000);
}
return timeStamp;
}
bool Isds::toCDateTimeCopy(struct timeval **cDateTimePtr,
const QDateTime &dateTime)
{
if (Q_UNLIKELY(cDateTimePtr == Q_NULLPTR)) {
Q_ASSERT(0);
return false;
}
if (dateTime.isNull()) {
if (*cDateTimePtr != NULL) {
/* Delete allocated. */
std::free(*cDateTimePtr); *cDateTimePtr = NULL;
}
return true;
}
if (*cDateTimePtr == NULL) {
*cDateTimePtr =
(struct timeval *)std::malloc(sizeof(**cDateTimePtr));
if (Q_UNLIKELY(*cDateTimePtr == NULL)) {
Q_ASSERT(0);
return false;
}
}
std::memset(*cDateTimePtr, 0, sizeof(**cDateTimePtr));
/* TODO -- handle microseconds. */
#if (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0))
(*cDateTimePtr)->tv_sec = dateTime.toSecsSinceEpoch();
#else /* < Qt-5.8 */
(*cDateTimePtr)->tv_sec = dateTime.toTime_t();
#endif /* >= Qt-5.8 */
(*cDateTimePtr)->tv_usec = dateTime.time().msec() * 1000;
return true;
}
qint64 Isds::fromLongInt(const long int *cLongPtr)
{
if (cLongPtr == NULL) {
return -1;
}
return *cLongPtr;
}
bool Isds::toLongInt(long int **cLongPtr, qint64 i)
{
if (Q_UNLIKELY(cLongPtr == Q_NULLPTR)) {
Q_ASSERT(0);
return false;
}
if (i < 0) {
if (*cLongPtr != NULL) {
std::free(*cLongPtr); *cLongPtr = NULL;
}
return true;
}
if (*cLongPtr == NULL) {
*cLongPtr = (long int*)std::malloc(sizeof(**cLongPtr));
if (Q_UNLIKELY(*cLongPtr == NULL)) {
Q_ASSERT(0);
return false;
}
}
**cLongPtr = i;
return true;
}
enum Isds::Type::NilBool Isds::fromBool(const _Bool *cBoolPtr)
{
if (cBoolPtr == NULL) {
return Type::BOOL_NULL;
} else if (*cBoolPtr == false) {
return Type::BOOL_FALSE;
} else {
return Type::BOOL_TRUE;
}
}
bool Isds::toBool(_Bool **cBoolPtr, enum Type::NilBool nilBool)
{
if (Q_UNLIKELY(cBoolPtr == NULL)) {
Q_ASSERT(0);
return false;
}
if (nilBool == Type::BOOL_NULL) {
if (*cBoolPtr != NULL) {
std::free(*cBoolPtr); *cBoolPtr = NULL;
}
return true;
}
if (*cBoolPtr == NULL) {
*cBoolPtr = (_Bool *)std::malloc(sizeof(**cBoolPtr));
if (Q_UNLIKELY(*cBoolPtr == NULL)) {
Q_ASSERT(0);
return false;
}
}
**cBoolPtr = (nilBool == Type::BOOL_TRUE);
return true;
}
qint64 Isds::string2NonNegativeLong(const QString &str, bool *ok)
{
if (str.isEmpty()) {
if (ok != Q_NULLPTR) {
*ok = true;
}
return -1;
}
bool iOk = false;
qint64 num = str.toLongLong(&iOk);
if ((!iOk) || (num < 0)) {
if (ok != Q_NULLPTR) {
*ok = false;
}
return -1;
}
if (ok != Q_NULLPTR) {
*ok = true;
}
return num;
}
QString Isds::nonNegativeLong2String(qint64 num)
{
return (num >= 0) ? QString::number(num) : QString();
}
/*
* Copyright (C) 2014-2018 CZ.NIC
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*/
#pragma once
#include <cstdbool>
#include <QByteArray>
#include <QDate>
#include <QDateTime>
#include <QString>
#include "src/datovka_shared/isds/types.h"
namespace Isds {
/*!
* @brief Converts C data into QByteArray.
*
* @param[in] cData C data pointer.
* @param[in] cSize Size of data.
* @return Byte array object, null byte array if NULL pointer or zero size was supplied.
*/
QByteArray fromCData(const void *cData, size_t cSize);
/*!
* @brief Creates a C data copy from the supplied QBYteArray.
*
* @param[in,out] cDataPtr Pointer to C data.
* @param[in,out] cSize Size value to be set.
* @param[in] data Data object.
* @return True on success, false on failure.
*/
bool toCDataCopy(void **cDataPtr, size_t *cSize,
const QByteArray &data);
/*!
* @brief Converts a C-string into QString.
*
* @param[in] cStr C string.
* @return String object, null string if NULL pointer was supplied.
*/
QString fromCStr(const char *cStr);
/*!
* @brief Creates a C-string copy to the supplied QString.
*
* @param[in,out] cStrPtr Pointer to C string.
* @param[in] str String object.
* @return True on success, false in failure.
*/
bool toCStrCopy(char **cStrPtr, const QString &str);
/*!
* @brief Converts date from struct tm.
*
* @param[in] cDate Struct tm containing date.
* @return Date object, null date if NULL pointer was supplied.
*/
QDate dateFromStructTM(struct tm *cDate);
/*!
* @brief Creates a struct tm copy of the supplied QDate.
*
* @param[in,out] cDatePtr Address of pointer to struct tm.
* @param[in] date Date object.
* @return True on success, false in failure.
*/
bool toCDateCopy(struct tm **cDatePtr, const QDate &date);
/*!
* @brief Converts date and time from struct timeval.
*
* @param[in] cDateTime Struct timeval containing date and time.
* @return Datetime object, null datetime if NULL pointer was supplied.
*/
QDateTime dateTimeFromStructTimeval(struct timeval *cDateTime);
/*!
* @brief Creates a struct timeval copy of the supplied QDateTime.
*
* @param[in,out] cDateTimePtr Address of pointer to struct tm.
* @param[in] dateTime Date object.
* @return True on success, false in failure.
*/
bool toCDateTimeCopy(struct timeval **cDateTimePtr,
const QDateTime &dateTime);
/*!
* @brief Convert from long int.
*
* @param[in] cLongPtr Pointer to long.
* @return -1 if NULL pointer supplied as parameter.
*/
qint64 fromLongInt(const long int *cLongPtr);
/*!
* @brief Creates a long int from supplied number.
*
* @note Negative integers cause the pointer to be set to NULL.
*
* @param[in,out] cLongPtr Pointer to long int.
* @param[in] i Integerer.
* @return True on success, false in failure.
*/
bool toLongInt(long int **cLongPtr, qint64 i);
/*!
* @brief Converts internal pointer to bool.
*
* @param[in] cBoolPtr Pointer to bool.
* @return Nullable bool enumeration type.
*/
enum Type::NilBool fromBool(const _Bool *cBoolPtr);
/*!
* @brief Sets bool pointer according to supplied value.
*
* @param[in,out] cBoolPtr Bool pointer to be set.
* @param[in] nilBool Nullable bool value.
* @return True on success, false in failure.
*/
bool toBool(_Bool **cBoolPtr, enum Type::NilBool nilBool);
/*!
* @brief Converts string to non-negative integer.
*
* @param[in] str String containing non-negative number.
* @param[out] ok Set to true if string contains non-negative number.
* @return Non-negative number if string contains a non-negative number.
* -1 if string is empty or on error.
*/
qint64 string2NonNegativeLong(const QString &str, bool *ok);
/*!
* @brief Converts integer to string. Negative values result in null
* string.
*
* @param[in] num Number to be converted to string.
* @return String with decimal number representation.
* Null string if supplied value is negative.
*/
QString nonNegativeLong2String(qint64 num);
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment