source: libcaca/trunk/common.h @ 1360

Last change on this file since 1360 was 1360, checked in by Sam Hocevar, 13 years ago
  • Add a debug() function and a --enable-debug configure flag.
  • Property svn:keywords set to Id
File size: 2.6 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 1360 2006-11-12 13:11:01Z 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 DEBUG && !defined __KERNEL__
36#   include <stdio.h>
37#   include <stdarg.h>
38#   if defined(HAVE_ERRNO_H)
39#       include <errno.h>
40#   endif
41static inline void debug(const char *format, ...)
42{
43#   if defined(HAVE_ERRNO_H)
44    int saved_errno = errno;
45#   endif
46    va_list args;
47    va_start(args, format);
48    fprintf(stderr, "** libcaca debug ** ");
49    vfprintf(stderr, format, args);
50    fprintf(stderr, "\n");
51    va_end(args);
52#   if defined(HAVE_ERRNO_H)
53    errno = saved_errno;
54#   endif
55}
56#else
57#   define debug(format, ...) do {} while(0)
58#endif
59
60#if defined HAVE_HTONS
61#   if defined __KERNEL__
62        /* Nothing to do */
63#   elif defined HAVE_ARPA_INET_H
64#       include <arpa/inet.h>
65#   elif defined HAVE_NETINET_IN_H
66#       include <netinet/in.h>
67#   endif
68#   define hton16 htons
69#   define hton32 htonl
70#else
71#   if defined HAVE_ENDIAN_H
72#       include <endian.h>
73#   endif
74static inline uint16_t hton16(uint16_t x)
75{
76    /* This is compile-time optimised with at least -O1 or -Os */
77#if defined HAVE_ENDIAN_H
78    if(__BYTE_ORDER == __BIG_ENDIAN)
79#else
80    uint32_t const dummy = 0x12345678;
81    if(*(uint8_t const *)&dummy == 0x12)
82#endif
83        return x;
84    else
85        return (x >> 8) | (x << 8);
86}
87
88static inline uint32_t hton32(uint32_t x)
89{
90    /* This is compile-time optimised with at least -O1 or -Os */
91#if defined HAVE_ENDIAN_H
92    if(__BYTE_ORDER == __BIG_ENDIAN)
93#else
94    uint32_t const dummy = 0x12345678;
95    if(*(uint8_t const *)&dummy == 0x12)
96#endif
97        return x;
98    else
99        return (x >> 24) | ((x >> 8) & 0x0000ff00)
100                | ((x << 8) & 0x00ff0000) | (x << 24);
101}
102#endif
103
104#if defined __KERNEL__
105#   undef HAVE_ERRNO_H
106#endif
107
Note: See TracBrowser for help on using the repository browser.