source: libcaca/trunk/common.h @ 861

Last change on this file since 861 was 861, checked in by Sam Hocevar, 15 years ago
  • Look for htons/htonl in netinet/in.h in addition to arpa/inet.h
  • Implement htons/htonl in common.h just in case.
  • Property svn:keywords set to Id
File size: 1.7 KB
RevLine 
[858]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 861 2006-04-24 20:45:55Z 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)
21typedef signed char int8_t;
22typedef signed short int16_t;
23typedef signed long int int32_t;
[859]24
[858]25typedef unsigned char uint8_t;
26typedef unsigned short uint16_t;
27typedef unsigned long int uint32_t;
[859]28
29typedef long int intptr_t;
30typedef unsigned long int uintptr_t;
[858]31#endif
32
[861]33#if !defined(HAVE_HTONS) && !defined(HAVE_NETINET_IN_H)
34#   if defined(HAVE_ENDIAN_H)
35#       include <endian.h>
36#   endif
37static extern inline uint16_t htons(uint16_t x)
38{
39#if defined(HAVE_ENDIAN_H)
40    if(__BYTE_ORDER == __BIG_ENDIAN)
41#else
42    /* This is compile-time optimised with at least -O1 or -Os */
43    uint32_t const dummy = 0x12345678;
44    if(*(uint8_t const *)&dummy == 0x12)
45#endif
46        return x;
47    else
48        return (x >> 8) | (x << 8);
49}
50
51static extern inline uint32_t htonl(uint32_t x)
52{
53#if defined(HAVE_ENDIAN_H)
54    if(__BYTE_ORDER == __BIG_ENDIAN)
55#else
56    /* This is compile-time optimised with at least -O1 or -Os */
57    uint32_t const dummy = 0x12345678;
58    if(*(uint8_t const *)&dummy == 0x12)
59#endif
60        return x;
61    else
62        return (x >> 24) | ((x >> 8) & 0x0000ff00)
63                | ((x << 8) & 0x00ff0000) | (x << 24);
64}
65#endif
66
Note: See TracBrowser for help on using the repository browser.