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

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

Large source code cleanup, getting rid of spaces, tabs, and svn keywords.

  • 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@hocevar.net>
4 *                All Rights Reserved
5 *
6 *  This library is free software. It comes without any warranty, to
7 *  the extent permitted by applicable law. You can redistribute it
8 *  and/or modify it under the terms of the Do What The Fuck You Want
9 *  To Public License, Version 2, as published by Sam Hocevar. See
10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12
13/*
14 *  This file contains replacements for commonly found object types and
15 *  function prototypes that are sometimes missing.
16 */
17
18#ifndef __CACA_STUBS_H__
19#define __CACA_STUBS_H__
20
21/* errno handling */
22#if defined HAVE_ERRNO_H && !defined __KERNEL__
23#   include <errno.h>
24static inline void seterrno(int e) { errno = e; }
25static inline int geterrno(void) { return errno; }
26#else
27#   define seterrno(x) do { (void)(x); } while(0)
28#   define geterrno(x) 0
29#endif
30
31/* hton16() and hton32() */
32#if defined HAVE_HTONS && !defined __KERNEL__
33#   if defined HAVE_ARPA_INET_H
34#       include <arpa/inet.h>
35#   elif defined HAVE_NETINET_IN_H
36#       include <netinet/in.h>
37#   endif
38#   define hton16 htons
39#   define hton32 htonl
40#else
41#   if defined __KERNEL__
42        /* Nothing to do */
43#   elif defined HAVE_ENDIAN_H
44#       include <endian.h>
45#   endif
46static inline uint16_t hton16(uint16_t x)
47{
48    /* This is compile-time optimised with at least -O1 or -Os */
49#if defined HAVE_ENDIAN_H
50    if(__BYTE_ORDER == __BIG_ENDIAN)
51#else
52    uint32_t const dummy = 0x12345678;
53    if(*(uint8_t const *)&dummy == 0x12)
54#endif
55        return x;
56    else
57        return (x >> 8) | (x << 8);
58}
59
60static inline uint32_t hton32(uint32_t x)
61{
62    /* This is compile-time optimised with at least -O1 or -Os */
63#if defined HAVE_ENDIAN_H
64    if(__BYTE_ORDER == __BIG_ENDIAN)
65#else
66    uint32_t const dummy = 0x12345678;
67    if(*(uint8_t const *)&dummy == 0x12)
68#endif
69        return x;
70    else
71        return (x >> 24) | ((x >> 8) & 0x0000ff00)
72                | ((x << 8) & 0x00ff0000) | (x << 24);
73}
74#endif
75
76#endif /* __CACA_STUBS_H__ */
77
Note: See TracBrowser for help on using the repository browser.