source: libcaca/trunk/common.h @ 1303

Last change on this file since 1303 was 1303, checked in by Sam Hocevar, 13 years ago
  • Don't use htonl() directly, use common.h's hton32() wrapper.
  • Property svn:keywords set to Id
File size: 2.0 KB
Line 
1/*
2 *  libcucul      Canvas for ultrafast compositing of Unicode letters
3 *  libcaca       Colour ASCII-Art library
4 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
5 *                All Rights Reserved
6 *
7 *  $Id: common.h 1303 2006-11-08 13:05:42Z sam $
8 *
9 *  This library is free software; you can redistribute it and/or
10 *  modify it under the terms of the Do What The Fuck You Want To
11 *  Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 *  This file contains replacements for commonly found object types and
17 *  function prototypes that are sometimes missing.
18 */
19
20#if defined(HAVE_INTTYPES_H) && !defined(__KERNEL__)
21#   include <inttypes.h>
22#else
23typedef signed char int8_t;
24typedef signed short int16_t;
25typedef signed long int int32_t;
26
27typedef unsigned char uint8_t;
28typedef unsigned short uint16_t;
29typedef unsigned long int uint32_t;
30
31typedef long int intptr_t;
32typedef unsigned long int uintptr_t;
33#endif
34
35#if defined(HAVE_HTONS)
36#   if defined(HAVE_ARPA_INET_H)
37#       include <arpa/inet.h>
38#   elif defined(HAVE_NETINET_IN_H)
39#       include <netinet/in.h>
40#   endif
41#   define hton16 htons
42#   define hton32 htonl
43#else
44#   if 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#if defined(__KERNEL__)
78#undef HAVE_ERRNO_H
79#endif
Note: See TracBrowser for help on using the repository browser.