home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / cdda / utils.h < prev   
Encoding:
C/C++ Source or Header  |  2008-11-07  |  2.0 KB  |  107 lines

  1. #include <stdlib.h>
  2. #include <endian.h>
  3. #include <stdio.h>
  4. #include <errno.h>
  5. #include <string.h>
  6.  
  7. extern long buffering_write(int outf, char *buffer, long num);
  8. extern int buffering_close(int fd);
  9.  
  10. /* I wonder how many alignment issues this is gonna trip in the
  11.    future...  it shouldn't trip any...  I guess we'll find out :) */
  12.  
  13. static inline int bigendianp(void){
  14.   int test=1;
  15.   char *hack=(char *)(&test);
  16.   if(hack[0])return(0);
  17.   return(1);
  18. }
  19.  
  20. static inline int32_t swap32(int32_t x){
  21.   return((((u_int32_t)x & 0x000000ffU) << 24) | 
  22.      (((u_int32_t)x & 0x0000ff00U) <<  8) | 
  23.      (((u_int32_t)x & 0x00ff0000U) >>  8) | 
  24.      (((u_int32_t)x & 0xff000000U) >> 24));
  25. }
  26.  
  27. static inline int16_t swap16(int16_t x){
  28.   return((((u_int16_t)x & 0x00ffU) <<  8) | 
  29.      (((u_int16_t)x & 0xff00U) >>  8));
  30. }
  31.  
  32. #if BYTE_ORDER == LITTLE_ENDIAN
  33.  
  34. static inline int32_t be32_to_cpu(int32_t x){
  35.   return(swap32(x));
  36. }
  37.  
  38. static inline int16_t be16_to_cpu(int16_t x){
  39.   return(swap16(x));
  40. }
  41.  
  42. static inline int32_t le32_to_cpu(int32_t x){
  43.   return(x);
  44. }
  45.  
  46. static inline int16_t le16_to_cpu(int16_t x){
  47.   return(x);
  48. }
  49.  
  50. #else
  51.  
  52. static inline int32_t be32_to_cpu(int32_t x){
  53.   return(x);
  54. }
  55.  
  56. static inline int16_t be16_to_cpu(int16_t x){
  57.   return(x);
  58. }
  59.  
  60. static inline int32_t le32_to_cpu(int32_t x){
  61.   return(swap32(x));
  62. }
  63.  
  64. static inline int16_t le16_to_cpu(int16_t x){
  65.   return(swap16(x));
  66. }
  67.  
  68.  
  69. #endif
  70.  
  71. static inline int32_t cpu_to_be32(int32_t x){
  72.   return(be32_to_cpu(x));
  73. }
  74.  
  75. static inline int32_t cpu_to_le32(int32_t x){
  76.   return(le32_to_cpu(x));
  77. }
  78.  
  79. static inline int16_t cpu_to_be16(int16_t x){
  80.   return(be16_to_cpu(x));
  81. }
  82.  
  83. static inline int16_t cpu_to_le16(int16_t x){
  84.   return(le16_to_cpu(x));
  85. }
  86.  
  87. static inline char *copystring(const char *s){
  88.   if(s){
  89.     char *ret=malloc((strlen(s)+1)*sizeof(char));
  90.     strcpy(ret,s);
  91.     return(ret);
  92.   }
  93.   return(NULL);
  94. }
  95.  
  96. static inline char *catstring(char *buff,const char *s){
  97.   if(s){
  98.     if(buff)
  99.       buff=realloc(buff,strlen(buff)+strlen(s)+1);
  100.     else
  101.       buff=calloc(strlen(s)+1,1);
  102.     strcat(buff,s);
  103.   }
  104.   return(buff);
  105. }
  106.  
  107.