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: stubs.h 2398 2008-06-15 12:28:14Z 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 | #ifndef __STUBS_H__ |
---|
21 | #define __STUBS_H__ |
---|
22 | |
---|
23 | /* errno handling */ |
---|
24 | #if defined HAVE_ERRNO_H && !defined __KERNEL__ |
---|
25 | # include <errno.h> |
---|
26 | static inline void seterrno(int e) { errno = e; } |
---|
27 | static inline int geterrno(void) { return errno; } |
---|
28 | #else |
---|
29 | # define seterrno(x) do { (void)(x); } while(0) |
---|
30 | # define geterrno(x) 0 |
---|
31 | #endif |
---|
32 | |
---|
33 | /* debug messages */ |
---|
34 | #if defined DEBUG && !defined __KERNEL__ |
---|
35 | # include <stdio.h> |
---|
36 | # include <stdarg.h> |
---|
37 | static inline void debug(const char *format, ...) |
---|
38 | { |
---|
39 | int saved_errno = geterrno(); |
---|
40 | va_list args; |
---|
41 | va_start(args, format); |
---|
42 | fprintf(stderr, "** libcaca debug ** "); |
---|
43 | vfprintf(stderr, format, args); |
---|
44 | fprintf(stderr, "\n"); |
---|
45 | va_end(args); |
---|
46 | seterrno(saved_errno); |
---|
47 | } |
---|
48 | #else |
---|
49 | # define debug(format, ...) do {} while(0) |
---|
50 | #endif |
---|
51 | |
---|
52 | /* hton16() and hton32() */ |
---|
53 | #if defined HAVE_HTONS && !defined __KERNEL__ |
---|
54 | # if defined HAVE_ARPA_INET_H |
---|
55 | # include <arpa/inet.h> |
---|
56 | # elif defined HAVE_NETINET_IN_H |
---|
57 | # include <netinet/in.h> |
---|
58 | # endif |
---|
59 | # define hton16 htons |
---|
60 | # define hton32 htonl |
---|
61 | #else |
---|
62 | # if defined __KERNEL__ |
---|
63 | /* Nothing to do */ |
---|
64 | # elif defined HAVE_ENDIAN_H |
---|
65 | # include <endian.h> |
---|
66 | # endif |
---|
67 | static inline uint16_t hton16(uint16_t x) |
---|
68 | { |
---|
69 | /* This is compile-time optimised with at least -O1 or -Os */ |
---|
70 | #if defined HAVE_ENDIAN_H |
---|
71 | if(__BYTE_ORDER == __BIG_ENDIAN) |
---|
72 | #else |
---|
73 | uint32_t const dummy = 0x12345678; |
---|
74 | if(*(uint8_t const *)&dummy == 0x12) |
---|
75 | #endif |
---|
76 | return x; |
---|
77 | else |
---|
78 | return (x >> 8) | (x << 8); |
---|
79 | } |
---|
80 | |
---|
81 | static inline uint32_t hton32(uint32_t x) |
---|
82 | { |
---|
83 | /* This is compile-time optimised with at least -O1 or -Os */ |
---|
84 | #if defined HAVE_ENDIAN_H |
---|
85 | if(__BYTE_ORDER == __BIG_ENDIAN) |
---|
86 | #else |
---|
87 | uint32_t const dummy = 0x12345678; |
---|
88 | if(*(uint8_t const *)&dummy == 0x12) |
---|
89 | #endif |
---|
90 | return x; |
---|
91 | else |
---|
92 | return (x >> 24) | ((x >> 8) & 0x0000ff00) |
---|
93 | | ((x << 8) & 0x00ff0000) | (x << 24); |
---|
94 | } |
---|
95 | #endif |
---|
96 | |
---|
97 | #endif /* __STUBS_H__ */ |
---|
98 | |
---|