source: libcaca/trunk/common.h @ 1416

Last change on this file since 1416 was 1416, checked in by Sam Hocevar, 13 years ago
  • Fix a compilation warning by pretending to use seterrno()'s argument even when it isn't.
  • Property svn:keywords set to Id
File size: 2.7 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 1416 2006-11-16 11:26:22Z 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/* C99 types */
21#if defined HAVE_INTTYPES_H && !defined __KERNEL__
22#   include <inttypes.h>
23#else
24typedef signed char int8_t;
25typedef signed short int16_t;
26typedef signed long int int32_t;
27
28typedef unsigned char uint8_t;
29typedef unsigned short uint16_t;
30typedef unsigned long int uint32_t;
31
32typedef long int intptr_t;
33typedef unsigned long int uintptr_t;
34#endif
35
36/* errno handling */
37#if defined HAVE_ERRNO_H && !defined __KERNEL__
38#   include <errno.h>
39static inline void seterrno(int e) { errno = e; }
40static inline int geterrno(void) { return errno; }
41#else
42#   define seterrno(x) do { (void)(x); } while(0)
43#   define geterrno(x) 0
44#endif
45
46/* debug messages */
47#if defined DEBUG && !defined __KERNEL__
48#   include <stdio.h>
49#   include <stdarg.h>
50static inline void debug(const char *format, ...)
51{
52    int saved_errno = geterrno();
53    va_list args;
54    va_start(args, format);
55    fprintf(stderr, "** libcaca debug ** ");
56    vfprintf(stderr, format, args);
57    fprintf(stderr, "\n");
58    va_end(args);
59    seterrno(saved_errno);
60}
61#else
62#   define debug(format, ...) do {} while(0)
63#endif
64
65/* hton16() and hton32() */
66#if defined HAVE_HTONS
67#   if defined __KERNEL__
68        /* Nothing to do */
69#   elif defined HAVE_ARPA_INET_H
70#       include <arpa/inet.h>
71#   elif defined HAVE_NETINET_IN_H
72#       include <netinet/in.h>
73#   endif
74#   define hton16 htons
75#   define hton32 htonl
76#else
77#   if defined HAVE_ENDIAN_H
78#       include <endian.h>
79#   endif
80static inline uint16_t hton16(uint16_t x)
81{
82    /* This is compile-time optimised with at least -O1 or -Os */
83#if defined HAVE_ENDIAN_H
84    if(__BYTE_ORDER == __BIG_ENDIAN)
85#else
86    uint32_t const dummy = 0x12345678;
87    if(*(uint8_t const *)&dummy == 0x12)
88#endif
89        return x;
90    else
91        return (x >> 8) | (x << 8);
92}
93
94static inline uint32_t hton32(uint32_t x)
95{
96    /* This is compile-time optimised with at least -O1 or -Os */
97#if defined HAVE_ENDIAN_H
98    if(__BYTE_ORDER == __BIG_ENDIAN)
99#else
100    uint32_t const dummy = 0x12345678;
101    if(*(uint8_t const *)&dummy == 0x12)
102#endif
103        return x;
104    else
105        return (x >> 24) | ((x >> 8) & 0x0000ff00)
106                | ((x << 8) & 0x00ff0000) | (x << 24);
107}
108#endif
109
Note: See TracBrowser for help on using the repository browser.