home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / fbm / src / flbyte.c < prev    next >
C/C++ Source or Header  |  1990-06-24  |  4KB  |  153 lines

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