No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

caca_stubs.h 2.4 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. /*
  14. * This file contains replacements for commonly found object types and
  15. * function prototypes that are sometimes missing.
  16. */
  17. #ifndef __STUBS_H__
  18. #define __STUBS_H__
  19. /* errno handling */
  20. #if defined HAVE_ERRNO_H && !defined __KERNEL__
  21. # include <errno.h>
  22. static inline void seterrno(int e) { errno = e; }
  23. static inline int geterrno(void) { return errno; }
  24. #else
  25. # define seterrno(x) do { (void)(x); } while(0)
  26. # define geterrno(x) 0
  27. #endif
  28. /* debug messages */
  29. #if defined DEBUG && !defined __KERNEL__
  30. # include <stdio.h>
  31. # include <stdarg.h>
  32. static inline void debug(const char *format, ...)
  33. {
  34. int saved_errno = geterrno();
  35. va_list args;
  36. va_start(args, format);
  37. fprintf(stderr, "** libcaca debug ** ");
  38. vfprintf(stderr, format, args);
  39. fprintf(stderr, "\n");
  40. va_end(args);
  41. seterrno(saved_errno);
  42. }
  43. #else
  44. # define debug(format, ...) do {} while(0)
  45. #endif
  46. /* hton16() and hton32() */
  47. #if defined HAVE_HTONS && !defined __KERNEL__
  48. # if defined HAVE_ARPA_INET_H
  49. # include <arpa/inet.h>
  50. # elif defined HAVE_NETINET_IN_H
  51. # include <netinet/in.h>
  52. # endif
  53. # define hton16 htons
  54. # define hton32 htonl
  55. #else
  56. # if defined __KERNEL__
  57. /* Nothing to do */
  58. # elif defined HAVE_ENDIAN_H
  59. # include <endian.h>
  60. # endif
  61. static inline uint16_t hton16(uint16_t x)
  62. {
  63. /* This is compile-time optimised with at least -O1 or -Os */
  64. #if defined HAVE_ENDIAN_H
  65. if(__BYTE_ORDER == __BIG_ENDIAN)
  66. #else
  67. uint32_t const dummy = 0x12345678;
  68. if(*(uint8_t const *)&dummy == 0x12)
  69. #endif
  70. return x;
  71. else
  72. return (x >> 8) | (x << 8);
  73. }
  74. static inline uint32_t hton32(uint32_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 >> 24) | ((x >> 8) & 0x0000ff00)
  86. | ((x << 8) & 0x00ff0000) | (x << 24);
  87. }
  88. #endif
  89. #endif /* __STUBS_H__ */