Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

79 righe
2.0 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2006 Sam Hocevar <sam@hocevar.net>
  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 __CACA_STUBS_H__
  18. #define __CACA_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. /* hton16() and hton32() */
  29. #if defined HAVE_HTONS && !defined __KERNEL__
  30. # if defined HAVE_ARPA_INET_H
  31. # include <arpa/inet.h>
  32. # elif defined HAVE_NETINET_IN_H
  33. # include <netinet/in.h>
  34. # endif
  35. # define hton16 htons
  36. # define hton32 htonl
  37. #else
  38. # if defined __KERNEL__
  39. /* Nothing to do */
  40. # elif defined HAVE_ENDIAN_H
  41. # include <endian.h>
  42. # endif
  43. static inline uint16_t hton16(uint16_t x)
  44. {
  45. /* This is compile-time optimised with at least -O1 or -Os */
  46. #if defined HAVE_ENDIAN_H
  47. if(__BYTE_ORDER == __BIG_ENDIAN)
  48. #else
  49. uint32_t const dummy = 0x12345678;
  50. if(*(uint8_t const *)&dummy == 0x12)
  51. #endif
  52. return x;
  53. else
  54. return (x >> 8) | (x << 8);
  55. }
  56. static inline uint32_t hton32(uint32_t x)
  57. {
  58. /* This is compile-time optimised with at least -O1 or -Os */
  59. #if defined HAVE_ENDIAN_H
  60. if(__BYTE_ORDER == __BIG_ENDIAN)
  61. #else
  62. uint32_t const dummy = 0x12345678;
  63. if(*(uint8_t const *)&dummy == 0x12)
  64. #endif
  65. return x;
  66. else
  67. return (x >> 24) | ((x >> 8) & 0x0000ff00)
  68. | ((x << 8) & 0x00ff0000) | (x << 24);
  69. }
  70. #endif
  71. #endif /* __CACA_STUBS_H__ */