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 1362 2006-11-12 15:26:13Z 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 |
---|
24 | typedef signed char int8_t; |
---|
25 | typedef signed short int16_t; |
---|
26 | typedef signed long int int32_t; |
---|
27 | |
---|
28 | typedef unsigned char uint8_t; |
---|
29 | typedef unsigned short uint16_t; |
---|
30 | typedef unsigned long int uint32_t; |
---|
31 | |
---|
32 | typedef long int intptr_t; |
---|
33 | typedef unsigned long int uintptr_t; |
---|
34 | #endif |
---|
35 | |
---|
36 | /* errno handling */ |
---|
37 | #if defined HAVE_ERRNO_H && !defined __KERNEL__ |
---|
38 | # include <errno.h> |
---|
39 | static inline void seterrno(int e) { errno = e; } |
---|
40 | static inline int geterrno(void) { return errno; } |
---|
41 | #else |
---|
42 | # define seterrno(x) do {} 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> |
---|
50 | static 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 |
---|
80 | static 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 | |
---|
94 | static 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 | |
---|