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