選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

78 行
2.0 KiB

  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. # endif
  34. # define hton16 htons
  35. # define hton32 htonl
  36. #else
  37. # if defined __KERNEL__
  38. /* Nothing to do */
  39. # elif defined HAVE_ENDIAN_H
  40. # include <endian.h>
  41. # endif
  42. static inline uint16_t hton16(uint16_t x)
  43. {
  44. /* This is compile-time optimised with at least -O1 or -Os */
  45. #if defined HAVE_ENDIAN_H
  46. if(__BYTE_ORDER == __BIG_ENDIAN)
  47. #else
  48. uint32_t const dummy = 0x12345678;
  49. if(*(uint8_t const *)&dummy == 0x12)
  50. #endif
  51. return x;
  52. else
  53. return (x >> 8) | (x << 8);
  54. }
  55. static inline uint32_t hton32(uint32_t x)
  56. {
  57. /* This is compile-time optimised with at least -O1 or -Os */
  58. #if defined HAVE_ENDIAN_H
  59. if(__BYTE_ORDER == __BIG_ENDIAN)
  60. #else
  61. uint32_t const dummy = 0x12345678;
  62. if(*(uint8_t const *)&dummy == 0x12)
  63. #endif
  64. return x;
  65. else
  66. return (x >> 24) | ((x >> 8) & 0x0000ff00)
  67. | ((x << 8) & 0x00ff0000) | (x << 24);
  68. }
  69. #endif
  70. #endif /* __CACA_STUBS_H__ */