source: libcaca/trunk/common.h @ 1304

Last change on this file since 1304 was 1304, checked in by Sam Hocevar, 13 years ago
  • Don't try to include arpa/inet.h in kernel mode.
  • 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 1304 2006-11-08 13:12:06Z 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 __KERNEL__
37        /* Nothing to do */
38#   elif defined HAVE_ARPA_INET_H
39#       include <arpa/inet.h>
40#   elif defined HAVE_NETINET_IN_H
41#       include <netinet/in.h>
42#   endif
43#   define hton16 htons
44#   define hton32 htonl
45#else
46#   if defined HAVE_ENDIAN_H
47#       include <endian.h>
48#   endif
49static inline uint16_t hton16(uint16_t x)
50{
51    /* This is compile-time optimised with at least -O1 or -Os */
52#if defined HAVE_ENDIAN_H
53    if(__BYTE_ORDER == __BIG_ENDIAN)
54#else
55    uint32_t const dummy = 0x12345678;
56    if(*(uint8_t const *)&dummy == 0x12)
57#endif
58        return x;
59    else
60        return (x >> 8) | (x << 8);
61}
62
63static inline uint32_t hton32(uint32_t x)
64{
65    /* This is compile-time optimised with at least -O1 or -Os */
66#if defined HAVE_ENDIAN_H
67    if(__BYTE_ORDER == __BIG_ENDIAN)
68#else
69    uint32_t const dummy = 0x12345678;
70    if(*(uint8_t const *)&dummy == 0x12)
71#endif
72        return x;
73    else
74        return (x >> 24) | ((x >> 8) & 0x0000ff00)
75                | ((x << 8) & 0x00ff0000) | (x << 24);
76}
77#endif
78
79#if defined __KERNEL__
80#   undef HAVE_ERRNO_H
81#endif
82
Note: See TracBrowser for help on using the repository browser.