view th_util.h @ 746:a557d1b2a356

Move TH_ATTR_PRINTF_FMT macro definition to th_types.h and improve the preprocessor checks. Apparently newer versions (not sure since which version exactly) seem to work fine with the attribute defined. Enabling for MinGW GCC >= 12 for now, though it may have worked earlier.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 21 Dec 2022 13:19:02 +0200
parents b415b70254fa
children db1a132c7754
line wrap: on
line source

/*
 * Generic utility-functions, macros and defaults
 * Programmed and designed by Matti 'ccr' Hamalainen
 * (C) Copyright 2002-2022 Tecnic Software productions (TNSP)
 *
 * Please read file 'COPYING' for information on license and distribution.
 */
/// @file
/// @brief Miscellaneous utility functions, memory allocation, error codes etc.
#ifndef TH_UTIL_H
#define TH_UTIL_H

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "th_types.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#ifndef HAVE_NO_ASSERT
#  include <assert.h>
#endif

#if defined(HAVE_STRING_H)
#  include <string.h>
#elif defined(HAVE_STRINGS_H)
#  include <strings.h>
#endif

#ifdef HAVE_MEMORY_H
#  include <memory.h>
#endif


#ifdef __cplusplus
extern "C" {
#endif


// Replacement for assert()
#ifdef HAVE_NO_ASSERT
#  ifdef NDEBUG
#    define assert(NEXPR) // stub
#  else
#    define assert(NEXPR) do { if (!(NEXPR)) { fprintf(stderr, "[%s:%d] assert(" # NEXPR ") failed!\n", __FILE__, __LINE__); abort(); } } while (0)
#  endif
#endif


/* Error codes
 */
enum
{
    // General error codes
    THERR_OK = 0,
    THERR_PROGRESS,     // Status OK, but operation in progress

    THERR_INTERNAL,
	
    THERR_FOPEN,
    THERR_FREAD,
    THERR_FWRITE,
    THERR_FSEEK,
    THERR_NOT_FOUND,    // Resource/data not found

    THERR_INVALID_DATA, // Some data was invalid
    THERR_MALLOC,       // Memory allocation failure
    THERR_ALREADY_INIT, // Resource has already been initialized
    THERR_INIT_FAIL,    // General initialization failure
    THERR_INVALID_ARGS,

    THERR_NULLPTR,      // NULL pointer specified in critical argument
    THERR_NOT_SUPPORTED,// Operation not supported
    THERR_OUT_OF_DATA,
    THERR_EXTRA_DATA,
    THERR_BOUNDS,

    THERR_TIMED_OUT,
    THERR_BUSY,
    THERR_IO_ERROR,

    // Network errors
    THERR_AUTH_FAILED,
};

#define TH_SYSTEM_ERRORS 100000


/* Log levels
 */
enum
{
    THLOG_NONE    = 0,
    THLOG_ERROR,
    THLOG_WARNING,
    THLOG_INFO,
    THLOG_DEBUG,
};


/* Global variables
 */
extern int  th_verbosity;
extern char *th_prog_name,
            *th_prog_desc,
            *th_prog_version,
            *th_prog_author,
            *th_prog_license;

/* Functions
 */
void    th_init(char *name, char *desc, char *version,
               char *author, char *license);
void    th_print_banner(FILE *outFile, const char *binName, const char *usage);
void    th_print_version(FILE *outFile);

int     th_term_width();
int     th_term_height();

int     th_get_error();
int     th_errno_to_error(int error);
const char *th_error_str(int error);

void    THERR(const char *fmt, ...)
        TH_ATTR_PRINTF_FMT(1, 2);
void    THMSG(int level, const char *fmt, ...)
        TH_ATTR_PRINTF_FMT(2, 3);
void    THPRINT(int level, const char *fmt, ...)
        TH_ATTR_PRINTF_FMT(2, 3);

void    THERR_V(const char *fmt, va_list ap);
void    THMSG_V(int level, const char *fmt, va_list ap);
void    THPRINT_V(int level, const char *fmt, va_list ap);

void    *th_malloc(size_t len);
void    *th_malloc0(size_t len);
void    *th_calloc(size_t n, size_t len);
void    *th_realloc(void *ptr, size_t len);
void    th_free(void *ptr);
void    th_free_r_real(void **ptr);

#define th_free_r(ptr) th_free_r_real((void **) ptr)


#define th_test_bit(pdata, nbit) \
    (pdata[nbit >> 3] & (1 << (nbit & 7)))

static inline void th_set_bit(uint8_t *pdata, const size_t nbit, const int value)
{
    if (value)
        pdata[nbit >> 3] |=  (1 << (nbit & 7));
    else
        pdata[nbit >> 3] &= ~(1 << (nbit & 7));
}


#ifdef __cplusplus
}
#endif
#endif // TH_UTIL_H