source: libcaca/trunk/caca/caca_stubs.h @ 4093

Last change on this file since 4093 was 3904, checked in by Sam Hocevar, 10 years ago

Move debugging methods to caca_debug.h.

  • Property svn:keywords set to Id
File size: 2.0 KB
Line 
1/*
2 *  libcaca       Colour ASCII-Art library
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: caca_stubs.h 3904 2009-11-16 01:15:19Z sam $
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 __CACA_STUBS_H__
20#define __CACA_STUBS_H__
21
22/* errno handling */
23#if defined HAVE_ERRNO_H && !defined __KERNEL__
24#   include <errno.h>
25static inline void seterrno(int e) { errno = e; }
26static 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/* hton16() and hton32() */
33#if defined HAVE_HTONS && !defined __KERNEL__
34#   if defined HAVE_ARPA_INET_H
35#       include <arpa/inet.h>
36#   elif defined HAVE_NETINET_IN_H
37#       include <netinet/in.h>
38#   endif
39#   define hton16 htons
40#   define hton32 htonl
41#else
42#   if defined __KERNEL__
43        /* Nothing to do */
44#   elif defined HAVE_ENDIAN_H
45#       include <endian.h>
46#   endif
47static inline uint16_t hton16(uint16_t x)
48{
49    /* This is compile-time optimised with at least -O1 or -Os */
50#if defined HAVE_ENDIAN_H
51    if(__BYTE_ORDER == __BIG_ENDIAN)
52#else
53    uint32_t const dummy = 0x12345678;
54    if(*(uint8_t const *)&dummy == 0x12)
55#endif
56        return x;
57    else
58        return (x >> 8) | (x << 8);
59}
60
61static inline uint32_t hton32(uint32_t x)
62{
63    /* This is compile-time optimised with at least -O1 or -Os */
64#if defined HAVE_ENDIAN_H
65    if(__BYTE_ORDER == __BIG_ENDIAN)
66#else
67    uint32_t const dummy = 0x12345678;
68    if(*(uint8_t const *)&dummy == 0x12)
69#endif
70        return x;
71    else
72        return (x >> 24) | ((x >> 8) & 0x0000ff00)
73                | ((x << 8) & 0x00ff0000) | (x << 24);
74}
75#endif
76
77#endif /* __CACA_STUBS_H__ */
78
Note: See TracBrowser for help on using the repository browser.