1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
/*
* libcucul Canvas for ultrafast compositing of Unicode letters
* libcaca Colour ASCII-Art library
* Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
* All Rights Reserved
*
* $Id: common.h 1078 2006-11-16 11:26:22Z sam $
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Do What The Fuck You Want To
* Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/
/*
* This file contains replacements for commonly found object types and
* function prototypes that are sometimes missing.
*/
/* C99 types */
#if defined HAVE_INTTYPES_H && !defined __KERNEL__
# include <inttypes.h>
#else
typedef signed char int8_t;
typedef signed short int16_t;
typedef signed long int int32_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long int uint32_t;
typedef long int intptr_t;
typedef unsigned long int uintptr_t;
#endif
/* errno handling */
#if defined HAVE_ERRNO_H && !defined __KERNEL__
# include <errno.h>
static inline void seterrno(int e) { errno = e; }
static inline int geterrno(void) { return errno; }
#else
# define seterrno(x) do { (void)(x); } while(0)
# define geterrno(x) 0
#endif
/* debug messages */
#if defined DEBUG && !defined __KERNEL__
# include <stdio.h>
# include <stdarg.h>
static inline void debug(const char *format, ...)
{
int saved_errno = geterrno();
va_list args;
va_start(args, format);
fprintf(stderr, "** libcaca debug ** ");
vfprintf(stderr, format, args);
fprintf(stderr, "\n");
va_end(args);
seterrno(saved_errno);
}
#else
# define debug(format, ...) do {} while(0)
#endif
/* hton16() and hton32() */
#if defined HAVE_HTONS
# if defined __KERNEL__
/* Nothing to do */
# elif defined HAVE_ARPA_INET_H
# include <arpa/inet.h>
# elif defined HAVE_NETINET_IN_H
# include <netinet/in.h>
# endif
# define hton16 htons
# define hton32 htonl
#else
# if defined HAVE_ENDIAN_H
# include <endian.h>
# endif
static inline uint16_t hton16(uint16_t x)
{
/* This is compile-time optimised with at least -O1 or -Os */
#if defined HAVE_ENDIAN_H
if(__BYTE_ORDER == __BIG_ENDIAN)
#else
uint32_t const dummy = 0x12345678;
if(*(uint8_t const *)&dummy == 0x12)
#endif
return x;
else
return (x >> 8) | (x << 8);
}
static inline uint32_t hton32(uint32_t x)
{
/* This is compile-time optimised with at least -O1 or -Os */
#if defined HAVE_ENDIAN_H
if(__BYTE_ORDER == __BIG_ENDIAN)
#else
uint32_t const dummy = 0x12345678;
if(*(uint8_t const *)&dummy == 0x12)
#endif
return x;
else
return (x >> 24) | ((x >> 8) & 0x0000ff00)
| ((x << 8) & 0x00ff0000) | (x << 24);
}
#endif
|