home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3593 / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-08  |  3.1 KB  |  193 lines

  1. /*
  2.  * July 5, 1991
  3.  * Copyright 1991 Lance Norskog And Sundry Contributors
  4.  * This source code is freely redistributable and may be used for
  5.  * any purpose.  This copyright notice must be maintained. 
  6.  * Lance Norskog And Sundry Contributors are not responsible for 
  7.  * the consequences of using this software.
  8.  */
  9.  
  10. /*
  11.  * AUX miscellaneous stuff.
  12.  */
  13.  
  14. char *sizes[] = {
  15.     "NONSENSE!",
  16.     "bytes",
  17.     "shorts",
  18.     "longs",
  19.     "32-bit floats",
  20.     "64-bit floats",
  21.     "IEEE floats"
  22. };
  23.  
  24. char *styles[] = {
  25.     "NONSENSE!",
  26.     "unsigned",
  27.     "signed (2's complement)",
  28.     "u-law",
  29.     "a-law"
  30. };
  31.  
  32. #include "aux.h"
  33.  
  34. /* Utilities */
  35.  
  36. /* Read short, little-endian: little end first. VAX/386 style. */
  37. unsigned short
  38. rlshort(ft)
  39. ft_t ft;
  40. {
  41.     unsigned char uc, uc2;
  42.     uc  = getc(ft->fp);
  43.     uc2 = getc(ft->fp);
  44.     return (uc2 << 8) | uc;
  45. }
  46.  
  47. /* Read short, bigendian: big first. 68000/SPARC style. */
  48. unsigned short
  49. rbshort(ft)
  50. ft_t ft;
  51. {
  52.     unsigned char uc, uc2;
  53.     uc2 = getc(ft->fp);
  54.     uc  = getc(ft->fp);
  55.     return (uc2 << 8) | uc;
  56. }
  57.  
  58. /* Write short, little-endian: little end first. VAX/386 style. */
  59. unsigned short
  60. wlshort(ft, us)
  61. ft_t ft;
  62. unsigned short us;
  63. {
  64.     putc(us, ft->fp);
  65.     putc(us >> 8, ft->fp);
  66. }
  67.  
  68. /* Write short, big-endian: big end first. 68000/SPARC style. */
  69. unsigned short
  70. wbshort(ft, us)
  71. ft_t ft;
  72. unsigned short us;
  73. {
  74.     putc(us >> 8, ft->fp);
  75.     putc(us, ft->fp);
  76. }
  77.  
  78. /* Read long, little-endian: little end first. VAX/386 style. */
  79. unsigned long
  80. rllong(ft)
  81. ft_t ft;
  82. {
  83.     unsigned char uc, uc2, uc3, uc4;
  84.     uc  = getc(ft->fp);
  85.     uc2 = getc(ft->fp);
  86.     uc3 = getc(ft->fp);
  87.     uc4 = getc(ft->fp);
  88.     return (uc4 << 24) | (uc3 << 16) | (uc2 << 8) | uc;
  89. }
  90.  
  91. /* Read long, bigendian: big first. 68000/SPARC style. */
  92. unsigned long
  93. rblong(ft)
  94. ft_t ft;
  95. {
  96.     unsigned char uc, uc2, uc3, uc4;
  97.     uc4 = getc(ft->fp);
  98.     uc3 = getc(ft->fp);
  99.     uc2 = getc(ft->fp);
  100.     uc  = getc(ft->fp);
  101.     return (uc << 24) | (uc2 << 16) | (uc3 << 8) | uc4;
  102. }
  103.  
  104. /* Write long, little-endian: little end first. VAX/386 style. */
  105. unsigned long
  106. wllong(ft, ul)
  107. ft_t ft;
  108. unsigned long ul;
  109. {
  110.     putc(ul, ft->fp);
  111.     putc(ul >> 8, ft->fp);
  112.     putc(ul >> 16, ft->fp);
  113.     putc(ul >> 24, ft->fp);
  114. }
  115.  
  116. /* Write long, big-endian: big end first. 68000/SPARC style. */
  117. unsigned long
  118. wblong(ft, ul)
  119. ft_t ft;
  120. unsigned long ul;
  121. {
  122.     putc(ul >> 24, ft->fp);
  123.     putc(ul >> 16, ft->fp);
  124.     putc(ul >> 8, ft->fp);
  125.     putc(ul, ft->fp);
  126. }
  127.  
  128. /* Read and write words and longs in "machine format".  Swap if indicated. */
  129.  
  130. /* Read short. */
  131. unsigned short
  132. rshort(ft)
  133. ft_t ft;
  134. {
  135.     unsigned short us;
  136.  
  137.     fread(&us, 2, 1, ft->fp);
  138.     if (ft->swap)
  139.         us = swapw(us);
  140.     return us;
  141. }
  142.  
  143. /* Write short. */
  144. unsigned short
  145. wshort(ft, us)
  146. ft_t ft;
  147. unsigned short us;
  148. {
  149.     if (ft->swap)
  150.         us = swapw(us);
  151.     fwrite(&us, 2, 1, ft->fp);
  152. }
  153.  
  154. /* Read long. */
  155. unsigned long
  156. rlong(ft)
  157. ft_t ft;
  158. {
  159.     unsigned long ul;
  160.  
  161.     fread(&ul, 4, 1, ft->fp);
  162.     if (ft->swap)
  163.         ul = swapl(ul);
  164.     return ul;
  165. }
  166.  
  167. /* Write long. */
  168. unsigned long
  169. wlong(ft, ul)
  170. ft_t ft;
  171. unsigned long ul;
  172. {
  173.     if (ft->swap)
  174.         ul = swapl(ul);
  175.     fwrite(&ul, 4, 1, ft->fp);
  176. }
  177.  
  178. /* Byte swappers */
  179.  
  180. unsigned short
  181. swapw(us)
  182. unsigned short us;
  183. {
  184.     return ((us >> 8) | (us << 8)) & 0xffff;
  185. }
  186.  
  187. unsigned long
  188. swapl(ul)
  189. unsigned long ul;
  190. {
  191.     return (ul >> 24) | ((ul >> 16) & 0xff00) | ((ul << 16) & 0xff0000) | (ul << 24);
  192. }
  193.