/*
 *  adf_util.h - utility functions
 *
 *  Copyright (C) 1997-2022 Laurent Clevy
 *                2023-2025 Tomasz Wolak
 *
 *  This file is part of ADFLib.
 *
 *  ADFLib 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  ADFLib 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 ADFLib; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifndef ADF_UTIL_H
#define ADF_UTIL_H

#include "adf_byteorder.h"
#include "adf_prefix.h"
#include "adf_types.h"

#ifndef BUILDING_WITH_CMAKE
#include "config.h"   // include config. header generated by autotools
#endif

#include <stdlib.h>   // for min(), max() on Windows/MSVC

struct DateTime {
    int year, mon, day, hour, min, sec;
};


/* defines max and min */
#ifndef max
#if defined(__clang__) || defined(__GNUC__)
#define max(a,b)             \
({                           \
    __typeof__ (a) _a = (a); \
    __typeof__ (b) _b = (b); \
    _a > _b ? _a : _b;       \
})
#else
#define max(a,b)        ((a)>(b) ? (a) : (b))
#endif
#endif

#ifndef min
#if defined(__clang__) || defined(__GNUC__)
#define min(a,b)             \
({                           \
    __typeof__ (a) _a = (a); \
    __typeof__ (b) _b = (b); \
    _a < _b ? _a : _b;       \
})
#else
#define min(a,b)        ((a)<(b) ? (a) : (b))
#endif
#endif


/* swap short and swap long macros for little endian machines */

static inline uint16_t swapUint16( const uint16_t n ) {
    return ( ( n << 8 ) |
             ( n >> 8 ) );
}

static inline uint32_t swapUint32( const uint32_t n ) {
    return ( ( ( (uint32_t) swapUint16( n & 0xffff ) ) << 16 ) |
             swapUint16( (uint16_t) ( n >> 16 ) ) );
}

static inline uint16_t swapUint16IfLittleEndianHost( const uint16_t n ) {
#ifdef LITT_ENDIAN
    return swapUint16( n );
#else
    return n;
#endif
}

static inline uint32_t swapUint32IfLittleEndianHost( const uint32_t n ) {
#ifdef LITT_ENDIAN
    return swapUint32( n );
#else
    return n;
#endif
}


static inline uint16_t swapUint16fromPtr( const uint8_t * const p ) {
    return (uint16_t) ( ( p[0] << 8 ) | p[1] );
}

static inline uint32_t swapUint32fromPtr( const uint8_t * const p ) {
    return (uint32_t) ( ( swapUint16fromPtr( p ) << 16 ) |
                        swapUint16fromPtr( p + 2 ) );
}

void swapUint32ToPtr( uint8_t * const  buf,
                      const uint32_t   val );

void swapUint16ToPtr( uint8_t * const  buf,
                      const uint16_t   val );

void swapUint32AtPtr( uint8_t * const  buf );
void swapUint16AtPtr( uint8_t * const  buf );


void adfDays2Date( int32_t      days,
                   int * const  yy,
                   int * const  mm,
                   int * const  dd );

bool adfIsLeap( const int y );

void adfTime2AmigaTime( struct DateTime  dt,
                        int32_t * const  day,
                        int32_t * const  min,
                        int32_t * const  ticks );

struct DateTime adfGiveCurrentTime(void);

#ifndef HAVE_STRNLEN
#pragma message "Using the custom strnlen()"
ADF_PREFIX size_t strnlen( const char *  str,
                           const size_t  maxlen );
#endif

#ifndef HAVE_STRNDUP
#pragma message "Using the custom strndup()"
ADF_PREFIX char * strndup( const char * const  s,
                           size_t              n );
#endif

#ifndef HAVE_STPNCPY
#pragma message "Using the custom stpncpy()"
ADF_PREFIX char * stpncpy( char * const         dst,
                           const char * const   src,
                           const size_t         sz );
#endif

#endif  /* ADF_UTIL_H */
