/*----------------------------------------------------------------------------*/
/*!
Copyright (c) 2018, 2019 by Christian Nowak a.k.a. Creestee a.k.a. chn
Contact: chnowak@web.de or creestee@creestee.com
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. This application may be used and redistributed in source code and binary
form free of charge for personal and for educational purposes.
4. Redistribution and usage of this application in source code or binary form
for commercial purposes requires explicit written permission from the
author.


THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the RadarVolumeExplorer project.
*/
/*----------------------------------------------------------------------------*/

#include <iostream>
#include "RveHDF5Attribute.h"

/*----------------------------------------------------------------------------*/
/*!
   Generic attribute constructor
   \param name name of the attribute
   \param type type of the attribute
   \param size size of the attribute in bytes
   \param contents pointer to the attribute data; the attribute will keep a copy
   \param parent the parent object; can be omitted
*/
/*----------------------------------------------------------------------------*/
RveHDF5Attribute::RveHDF5Attribute( QString name, ATTR_TYPE type, size_t size, int elementSize, void *contents, RveHDF5Object *parent ) :
   m_Type( type ), m_Size( size ), m_ElementSize( elementSize )
{
   m_Name = name;
   m_pParent = parent;

   if( contents && size > 0 )
   {
      m_pContents = malloc( size );
      memcpy( m_pContents, contents, size );
   } else
   {
      m_pContents = 0;
   }
}


/*----------------------------------------------------------------------------*/
/*!
   Integer attribute constructor
   \param name name of the integer attribute
   \param integer value of the attribute
   \param parent the parent object; can be omitted
*/
/*----------------------------------------------------------------------------*/
RveHDF5Attribute::RveHDF5Attribute( QString name, qint64 integer, RveHDF5Object *parent ) :
   m_Type( ATTR_INTEGER ), m_Size( sizeof( qint64 ) ), m_ElementSize( sizeof( qint64 ) )
{
   m_Name = name;
   m_pParent = parent;

   m_pContents = malloc( m_Size );
   memcpy( m_pContents, &integer, m_Size );
}


/*----------------------------------------------------------------------------*/
/*!
   Float attribute constructor
   \param name name of the attribute
   \param float value of the attribute
   \param parent the parent object; can be omitted
*/
/*----------------------------------------------------------------------------*/
RveHDF5Attribute::RveHDF5Attribute( QString name, double fl, RveHDF5Object *parent ) :
   m_Type( ATTR_FLOAT ), m_Size( sizeof( double ) ), m_ElementSize( sizeof( double ) )
{
   m_Name = name;
   m_pParent = parent;

   m_pContents = malloc( sizeof( double ) );
   memcpy( m_pContents, &fl, sizeof( double ) );
}


RveHDF5Attribute::RveHDF5Attribute( QString name, double *fl, int numItems, RveHDF5Object *parent ) :
   m_Type( ATTR_FLOAT ), m_Size( sizeof( double ) * numItems ), m_ElementSize( sizeof( double ) )
{
   m_Name = name;
   m_pParent = parent;

   m_pContents = malloc( m_Size );
   memcpy( m_pContents, fl, m_Size );
}


/*----------------------------------------------------------------------------*/
/*!
   String constructor
   \param name name of the attribute
   \param string value of the attribute
   \param parent the parent object; can be omitted
*/
/*----------------------------------------------------------------------------*/
RveHDF5Attribute::RveHDF5Attribute( QString name, QString string, RveHDF5Object *parent ) :
   m_Type( ATTR_STRING ), m_Size( string.size() ), m_ElementSize( string.size() )
{
   m_Name = name;
   m_pParent = parent;
   m_pContents = 0;
   setString( string );
}


/*----------------------------------------------------------------------------*/
/*!
   Destructor
*/
/*----------------------------------------------------------------------------*/
RveHDF5Attribute::~RveHDF5Attribute()
{
   if( m_pContents )
      free( m_pContents );
}


/*----------------------------------------------------------------------------*/
/*!
   Set a new string for the attribute
   \param str the string to be set as the attribute value
*/
/*----------------------------------------------------------------------------*/
void RveHDF5Attribute::setString( QString str )
{
   m_Type = ATTR_STRING;
   if( m_pContents )
   {
      free( m_pContents );
   }

   m_pContents = malloc( str.size() + 1 );
   memcpy( m_pContents, str.toLatin1(), str.size() );
   ((char *)m_pContents)[str.size()] = '\0';
   m_Size = str.size();
   m_ElementSize = m_Size;
}


/*----------------------------------------------------------------------------*/
/*!
   Retrieve the size of the attribute data in bytes
   \return size in bytes
*/
/*----------------------------------------------------------------------------*/
size_t RveHDF5Attribute::size()
{
   return( m_Size );
}


/*----------------------------------------------------------------------------*/
/*!
   \return The size of each element in bytes
*/
/*----------------------------------------------------------------------------*/
int RveHDF5Attribute::elementSize()
{
   return( m_ElementSize );
}


/*----------------------------------------------------------------------------*/
/*!
\return The number of elements
*/
/*----------------------------------------------------------------------------*/
int RveHDF5Attribute::numElements()
{
   return( m_Size / m_ElementSize );
}


/*----------------------------------------------------------------------------*/
/*!
   Retrieve the attribute data
   \param dest pointer to the memory destination where the data
   will be copied to
*/
/*----------------------------------------------------------------------------*/
void RveHDF5Attribute::contents( void *dest )
{
   if( m_Size > 0 && m_pContents )
      memcpy( dest, m_pContents, m_Size );
}


/*----------------------------------------------------------------------------*/
/*!
   Retrieve the object type
   \return since this is an attribute, TYPE_ATTRIBUTE will always be returned
*/
/*----------------------------------------------------------------------------*/
RveHDF5Object::OBJ_TYPE RveHDF5Attribute::type()
{
   return( RveHDF5Object::TYPE_ATTRIBUTE );
}


/*----------------------------------------------------------------------------*/
/*!
   Print information about this attribute
*/
/*----------------------------------------------------------------------------*/
void RveHDF5Attribute::print()
{
   std::cout << "(ATTRIBUTE) " << path().toStdString() << " ";

   switch( m_Type )
   {
      case ATTR_NONE:
         printf( "None" );
         break;
      case ATTR_INTEGER:
         printf( "Integer " );
         switch( m_Size )
         {
            case 1:
               printf( "%d", *(quint8 *)m_pContents );
               break;
            case 2:
               printf( "%d", *(quint16 *)m_pContents );
               break;
            case 4:
               printf( "%d", *(quint32 *)m_pContents );
               break;
            case 8:
               printf( "%d", (int) ( *(quint64 *)m_pContents) );
               break;
         }
         break;
      case ATTR_FLOAT:
         printf( "Float " );
         if( m_Size == sizeof( float ) )
            printf( "%f", *(float *)m_pContents );
         else
         if( m_Size == sizeof( double ) )
            printf( "%f", *(double *)m_pContents );
         break;
      case ATTR_STRING:
         printf( "String %s", (const char *)m_pContents );
         break;
      default:
         printf( "Unknown: %d", m_Type );
         break;
   }

   printf( "\n" );
}


/*----------------------------------------------------------------------------*/
/*!
   Retrieve the attribute's data type
   \return the attribute type, any value of the ATTR_TYPE enumeration
*/
/*----------------------------------------------------------------------------*/
RveHDF5Attribute::ATTR_TYPE RveHDF5Attribute::attrType()
{
   return( m_Type );
}


/*----------------------------------------------------------------------------*/
/*!
   If this is a string type attribute, retrieve the string
   \param str reference to the string to be filled with the attribute contents
   \return true on success
*/
/*----------------------------------------------------------------------------*/
bool RveHDF5Attribute::getString( QString &str )
{
   if( attrType() != ATTR_STRING )
      return( false );

   str = QString( (const char *)m_pContents );

   return( true );
}


/*----------------------------------------------------------------------------*/
/*!
   If this is an integer type attribute, retrieve the integer value
   \param integer reference to the integer to be filled with the attribute value
   \return true on success
*/
/*----------------------------------------------------------------------------*/
bool RveHDF5Attribute::getInteger( int &integer )
{
   if( attrType() != ATTR_INTEGER )
      return( false );

   switch( m_Size )
   {
      case 1:
         integer = *(quint8 *)m_pContents;
         break;
      case 2:
         integer = *(quint16 *)m_pContents;
         break;
      case 4:
         integer = *(quint32 *)m_pContents;
         break;
      case 8:
         integer = (int) (*(qint64 *)m_pContents);
         break;

      default:
         return( false );
   }

   return( true );
}


/*----------------------------------------------------------------------------*/
/*!
   If this is a float type attribute, retrieve the float value
   \param fl reference to the float to be filled with the attribute value
   \return true on success
*/
/*----------------------------------------------------------------------------*/
bool RveHDF5Attribute::getFloat( double &fl, int n )
{
   if( attrType() != ATTR_FLOAT )
      return false;

   int numElements = m_Size / m_ElementSize;
   if( n >= numElements )
      return( false );

   if( m_ElementSize == sizeof( float ) )
      fl = ((float *)m_pContents)[n];
   else
   if( m_ElementSize == sizeof( double ) )
      fl = ((double *)m_pContents)[n];

   return( true );
}


/*----------------------------------------------------------------------------*/
/*!
   Attach the attribute to an object within an hdf5 file
   \param f hdf5 object to which the attribute will be added
   \return an HDF5::Error object
*/
/*----------------------------------------------------------------------------*/
bool RveHDF5Attribute::save( hid_t f )
{
   hid_t t = H5T_NO_CLASS;

   switch( attrType() )
   {
      case ATTR_INTEGER:
      {
         t = H5T_NATIVE_LLONG;
         break;
      }

      case ATTR_FLOAT:
      {
         t = H5T_NATIVE_DOUBLE;
         break;
      }

      case ATTR_STRING:
      {
         t = H5T_C_S1;
         break;
      }

      default:
      {
         break;
      }
   }

   hid_t mtype = H5Tcopy( t );

   int s = size();
   int numItems = m_ElementSize == 0 ? 0 : ( m_Size / m_ElementSize );
   if( attrType() == ATTR_STRING )
   {
      s++;
      H5Tset_size( mtype, m_ElementSize + 1 );
   } else
   {
      H5Tset_size( mtype, m_ElementSize );
   }

   hid_t spc;

   if( numItems > 1 )
   {
      hsize_t szCurrent[1];
      szCurrent[0] = numItems;
      spc = H5Screate_simple( 1, szCurrent, 0 );
   } else
   {
      spc = H5Screate( H5S_SCALAR );
   }

   hid_t a = H5Acreate( f, name().toLatin1(), mtype, spc, H5P_DEFAULT );
   if( a >= 0 )
   {
      H5Awrite( a, mtype, m_pContents );
      H5Aclose( a );
   }

   H5Sclose( spc );
   H5Tclose( mtype );

   RveHDF5Object::save( f );

   return( true );
}
