home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume19 / fbm / part03 / flbyte.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-08  |  4.0 KB  |  147 lines

  1. /*****************************************************************
  2.  * flbyte.c: FBM Library 0.94 (Beta test) 20-May-89  Michael Mauldin
  3.  *
  4.  * Copyright (C) 1989 by Michael Mauldin.  Permission is granted to
  5.  * use this file in whole or in part provided that you do not sell it
  6.  * for profit and that this copyright notice is retained unchanged.
  7.  *
  8.  * flbyte.c: 
  9.  *
  10.  * CONTENTS
  11.  *    get_long (rfile, order)
  12.  *    get_short (rfile, order)
  13.  *    put_long (long, wfile, order)
  14.  *    put_short (word, wfile, order)
  15.  *    machine_byte_order ()
  16.  *
  17.  *    order    BIG    msb first (Sun, IBM-RT, 68000)
  18.  *        LITTLE    lsb first (Vax, 6502)
  19.  *
  20.  * EDITLOG
  21.  *    LastEditDate = Sat May 20 19:11:04 1989 - Michael Mauldin
  22.  *    LastFileName = /usr2/mlm/src/misc/fbm/flbyte.c
  23.  *
  24.  * HISTORY
  25.  * 20-May-89  Michael Mauldin (mlm) at Carnegie Mellon University
  26.  *    Bug fix from Dave Cohrs <dave@cs.wisc.edu>
  27.  *
  28.  * 07-Mar-89  Michael Mauldin (mlm) at Carnegie Mellon University
  29.  *    Beta release (version 0.9) mlm@cs.cmu.edu
  30.  *
  31.  * 12-Nov-88  Michael Mauldin (mlm) at Carnegie-Mellon University
  32.  *    Created.
  33.  *****************************************************************/
  34.  
  35. # include <stdio.h>
  36. # include "fbm.h"
  37.  
  38. /****************************************************************
  39.  * get_long: get a long integer from a file
  40.  ****************************************************************/
  41.  
  42. #ifndef lint
  43. static char *fbmid =
  44.     "$FBM flbyte.c <0.9> 07-Mar-89  (C) 1989 by Michael Mauldin$";
  45. #endif
  46.  
  47. long
  48. get_long (file, order)
  49. FILE *file;
  50. int order;
  51. { register long word;
  52.  
  53.   if (order == BIG)
  54.   { word = fgetc (file) & 0x0ff;
  55.     word = word << 8 | (fgetc (file) & 0x0ff);
  56.     word = word << 8 | (fgetc (file) & 0x0ff);
  57.     word = word << 8 | (fgetc (file) & 0x0ff);
  58.   }
  59.   else
  60.   { word = fgetc (file) & 0x0ff;
  61.     word = word | ((fgetc (file) & 0x0ff) << 8);
  62.     word = word | ((fgetc (file) & 0x0ff) << 16);
  63.     word = word | ((fgetc (file) & 0x0ff) << 24);
  64.   }
  65.  
  66.   return (word);
  67. }
  68.  
  69. /****************************************************************
  70.  * put_long: Write a long integer to a file
  71.  ****************************************************************/
  72.  
  73. put_long (word, file, order)
  74. register long word;
  75. FILE *file;
  76. int order;
  77. {
  78.   if (order == BIG)
  79.   { fputc ((word >> 24) & 0x0ff, file);    /* Put out biggest byte */
  80.     fputc ((word >> 16) & 0x0ff, file);    /* Put out 2nd byte */
  81.     fputc ((word >> 8) & 0x0ff, file);    /* Put out 3rd byte */
  82.     fputc (word & 0x0ff, file);        /* Put out littlest byte */
  83.   }
  84.   else
  85.   { fputc (word & 0x0ff, file);        /* Put out littlest byte */
  86.     fputc ((word >> 8) & 0x0ff, file);    /* Put out 3rd byte */
  87.     fputc ((word >> 16) & 0x0ff, file);    /* Put out 2nd byte */
  88.     fputc ((word >> 24) & 0x0ff, file);    /* Put out biggest byte */
  89.   }
  90. }
  91.  
  92. /****************************************************************
  93.  * get_short: get a short integer from a file
  94.  ****************************************************************/
  95.  
  96. get_short (file, order)
  97. FILE *file;
  98. int order;
  99. { register int word;
  100.  
  101.   if (order == BIG)
  102.   { word = fgetc (file) & 0x0ff;
  103.     word = word << 8 | (fgetc (file) & 0x0ff);
  104.   }
  105.   else
  106.   { word = fgetc (file) & 0x0ff;
  107.     word = word | ((fgetc (file) & 0x0ff) << 8);
  108.   }
  109.  
  110.   return (word);
  111. }
  112.  
  113. /****************************************************************
  114.  * put_short: Write a short integer to a file
  115.  ****************************************************************/
  116.  
  117. put_short (word, file, order)
  118. register int word;
  119. FILE *file;
  120. int order;
  121. {
  122.   if (order == BIG)
  123.   { fputc ((word >> 8) & 0x0ff, file);    /* Put out 3rd byte */
  124.     fputc (word & 0x0ff, file);        /* Put out littlest byte */
  125.   }
  126.   else
  127.   { fputc (word & 0x0ff, file);        /* Put out littlest byte */
  128.     fputc ((word >> 8) & 0x0ff, file);    /* Put out 3rd byte */
  129.   }
  130. }
  131.  
  132. /****************************************************************
  133.  * machine_byte_order: Return BIG or LITTLE for the current machine
  134.  ****************************************************************/
  135.  
  136. machine_byte_order ()
  137. { short testshort;
  138.   char *teststr = (char*) &testshort;
  139.  
  140.   teststr[0] = '\1'; teststr[1] = '\0';
  141.  
  142.   if (testshort == 1)
  143.   { return (LITTLE); }
  144.   else
  145.   { return (BIG); }
  146. }
  147.