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

110 行
2.7 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. /* C99 types */
  19. #if defined HAVE_INTTYPES_H && !defined __KERNEL__
  20. # include <inttypes.h>
  21. #else
  22. typedef signed char int8_t;
  23. typedef signed short int16_t;
  24. typedef signed long int int32_t;
  25. typedef unsigned char uint8_t;
  26. typedef unsigned short uint16_t;
  27. typedef unsigned long int uint32_t;
  28. typedef long int intptr_t;
  29. typedef unsigned long int uintptr_t;
  30. #endif
  31. /* errno handling */
  32. #if defined HAVE_ERRNO_H && !defined __KERNEL__
  33. # include <errno.h>
  34. static inline void seterrno(int e) { errno = e; }
  35. static inline int geterrno(void) { return errno; }
  36. #else
  37. # define seterrno(x) do { (void)(x); } while(0)
  38. # define geterrno(x) 0
  39. #endif
  40. /* debug messages */
  41. #if defined DEBUG && !defined __KERNEL__
  42. # include <stdio.h>
  43. # include <stdarg.h>
  44. static inline void debug(const char *format, ...)
  45. {
  46. int saved_errno = geterrno();
  47. va_list args;
  48. va_start(args, format);
  49. fprintf(stderr, "** libcaca debug ** ");
  50. vfprintf(stderr, format, args);
  51. fprintf(stderr, "\n");
  52. va_end(args);
  53. seterrno(saved_errno);
  54. }
  55. #else
  56. # define debug(format, ...) do {} while(0)
  57. #endif
  58. /* hton16() and hton32() */
  59. #if defined HAVE_HTONS
  60. # if defined __KERNEL__
  61. /* Nothing to do */
  62. # elif defined HAVE_ARPA_INET_H
  63. # include <arpa/inet.h>
  64. # elif defined HAVE_NETINET_IN_H
  65. # include <netinet/in.h>
  66. # endif
  67. # define hton16 htons
  68. # define hton32 htonl
  69. #else
  70. # if defined HAVE_ENDIAN_H
  71. # include <endian.h>
  72. # endif
  73. static inline uint16_t hton16(uint16_t x)
  74. {
  75. /* This is compile-time optimised with at least -O1 or -Os */
  76. #if defined HAVE_ENDIAN_H
  77. if(__BYTE_ORDER == __BIG_ENDIAN)
  78. #else
  79. uint32_t const dummy = 0x12345678;
  80. if(*(uint8_t const *)&dummy == 0x12)
  81. #endif
  82. return x;
  83. else
  84. return (x >> 8) | (x << 8);
  85. }
  86. static inline uint32_t hton32(uint32_t x)
  87. {
  88. /* This is compile-time optimised with at least -O1 or -Os */
  89. #if defined HAVE_ENDIAN_H
  90. if(__BYTE_ORDER == __BIG_ENDIAN)
  91. #else
  92. uint32_t const dummy = 0x12345678;
  93. if(*(uint8_t const *)&dummy == 0x12)
  94. #endif
  95. return x;
  96. else
  97. return (x >> 24) | ((x >> 8) & 0x0000ff00)
  98. | ((x << 8) & 0x00ff0000) | (x << 24);
  99. }
  100. #endif