You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

caca_stubs.h 2.1 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2006-2012 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * This library is free software. It comes without any warranty, to
  7. * the extent permitted by applicable law. You can redistribute it
  8. * and/or modify it under the terms of the Do What the Fuck You Want
  9. * to Public License, Version 2, as published by Sam Hocevar. See
  10. * http://www.wtfpl.net/ for more details.
  11. */
  12. /*
  13. * This file contains replacements for commonly found object types and
  14. * function prototypes that are sometimes missing.
  15. */
  16. #ifndef __CACA_STUBS_H__
  17. #define __CACA_STUBS_H__
  18. /* errno handling */
  19. #if defined HAVE_ERRNO_H && !defined __KERNEL__
  20. # include <errno.h>
  21. static inline void seterrno(int e) { errno = e; }
  22. static inline int geterrno(void) { return errno; }
  23. #else
  24. # define seterrno(x) do { (void)(x); } while(0)
  25. # define geterrno(x) 0
  26. #endif
  27. /* hton16() and hton32() */
  28. #if defined HAVE_HTONS && !defined __KERNEL__
  29. # if defined HAVE_ARPA_INET_H
  30. # include <arpa/inet.h>
  31. # elif defined HAVE_NETINET_IN_H
  32. # include <netinet/in.h>
  33. # elif defined HAVE_WINSOCK2_H
  34. # include <winsock2.h>
  35. # endif
  36. # define hton16 htons
  37. # define hton32 htonl
  38. #else
  39. # if defined __KERNEL__
  40. /* Nothing to do */
  41. # elif defined HAVE_ENDIAN_H
  42. # include <endian.h>
  43. # endif
  44. static inline uint16_t hton16(uint16_t x)
  45. {
  46. /* This is compile-time optimised with at least -O1 or -Os */
  47. #if defined HAVE_ENDIAN_H
  48. if(__BYTE_ORDER == __BIG_ENDIAN)
  49. #else
  50. uint32_t const dummy = 0x12345678;
  51. if(*(uint8_t const *)&dummy == 0x12)
  52. #endif
  53. return x;
  54. else
  55. return (x >> 8) | (x << 8);
  56. }
  57. static inline uint32_t hton32(uint32_t x)
  58. {
  59. /* This is compile-time optimised with at least -O1 or -Os */
  60. #if defined HAVE_ENDIAN_H
  61. if(__BYTE_ORDER == __BIG_ENDIAN)
  62. #else
  63. uint32_t const dummy = 0x12345678;
  64. if(*(uint8_t const *)&dummy == 0x12)
  65. #endif
  66. return x;
  67. else
  68. return (x >> 24) | ((x >> 8) & 0x0000ff00)
  69. | ((x << 8) & 0x00ff0000) | (x << 24);
  70. }
  71. #endif
  72. #endif /* __CACA_STUBS_H__ */