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.
 
 
 
 
 
 

99 lines
2.5 KiB

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